JAVA游戏“ BattleShip

时间:2018-11-18 23:57:16

标签: java eclipse

此游戏使用2d数组(7x7),并使用两个数组列表来存储飞船(长度为2,3,4)和用户猜测。用户首先键入每艘船的行和列以及方向(向下或向右延伸)。然后将空白板打印到控制台,用户将猜测行和列。如果那个地方有一艘船“命中!”打印到控制台,否则“未命中!”打印。每次猜测之后,将打印新的棋盘状态,其中每个命中为“ X”,每个未命中为“ M”。还将每个猜测添加到猜测数组列表中。一旦用户猜到了飞船的每个元素在哪里,游戏就结束了,并且打印了猜谜历史。我的代码似乎可以正常工作,但是我无法结束游戏。它永远不会脱离主循环的while循环。这是我的代码,我在做错什么或需要更改吗?

================================================ =========================== 我的代码:

class Ship {
int x;
int y;
//String orient;

public Ship (int x, int y) {
    super();
    this.x = x;
    this.y = y;
    //this.orient = orient;
}

}

class Guess {
    int row;
    int col;

    public Guess (int row, int col) {
        super();
        this.row = row;
        this.col = col;
    }

}


public class CS125_Project5
{
//create the 2d arrays for the realBoard where the user will add the ships
// and the guessBoard which will update when user makes guesses
/////////what i had at first but kept getting nullpointerexception
//static String realBoard[][];
//static String  guessBoard[][];


//with these i get the thing printed but it says null for all values
static String gameBoard[][] = new String[7][7];
static String  guessBoard[][] = new String[7][7];

//creating the arrayList for both ships and guesses
static ArrayList<Ship> ships = new ArrayList<>();
static ArrayList<Guess> guesses = new ArrayList<>();


////////////////////////////////////////////////////////
////// Constructor
////////////////////////////////////////////////////////

public CS125_Project5() {

    //initializing the arrays to have a fixed size of 7x7
    //realBoard = new String[7][7];
    //guessBoard = new String[7][7];
    ships = new ArrayList<>();
    guesses = new ArrayList<>();


    //adding a '-' to each element of the 2d arrays for both boards
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            gameBoard[i][j] = "-"; 
            guessBoard[i][j] = "-";
        }
    }
}


////////////////////////////////////////////////////////
/////// Method to Print the game maker board
////////////////////////////////////////////////////////

public static void printGameMakerBoard(){
    System.out.print("r\\c\t");

    //printing the column numbers
    for(int i=0;i<7;i++){
        System.out.print(i+"\t");
    }

    //printing the row numbers
    System.out.println();

    //printing the gameMaker board to show user what the board looks like
    for(int i=0;i<7;i++){
        System.out.print(i+"\t");
        for(int j=0;j<7;j++){
            System.out.print(gameBoard[i][j] +"\t");
        }
        System.out.println();
    }


}

public static void updateBoard(int row, int col, String orient, int length) {



    //if statement to determine orientation of ship
    if (orient.contains("r")) {
        int updateCol;
        for (int j = 0; j < length; j++) {
            updateCol = j + col;
            gameBoard[row][updateCol] = "S";
        } 

    } else {
        int updateRow;
        for (int j = 0; j < length; j++) {
            updateRow = row + j;
            gameBoard[updateRow][col] = "S";
        }

    }

    printBoard(gameBoard);


}

public static void printBoard(String[][] board) {
    System.out.print("r\\c"+"\t");

    //printing the column numbers
    for(int i=0;i<7;i++){
        System.out.print(i+"\t");
    }
    System.out.println();

    //printing the row numbers

    // printing the guess board 
    for(int i=0;i<7;i++){
        System.out.print(i+"\t");
        for(int j=0;j<7;j++){
            System.out.print(board[i][j] +"\t");
        }
        System.out.println();
    }

}

private static void addShip(int x, int y, int length, String orient) {

    gameBoard[x][y] = "S";

    if (length == 2 || length == 3 || length == 4) {
        if (orient.contains("d")) {
            for (int i = 0; i < length; i++) { 
                Ship ship = new Ship(x, y+i);
                ships.add(ship);
            }
        } else {   //orient.contains("s") 
            for (int j = 0; j < length; j++) {
                Ship ship = new Ship(x+j, y);
                ships.add(ship);
            }
        }
    }
    //      Ship ship = new Ship(x, y);
    //      ships.add(ship);
}

public void guess(int x, int y) {
    //adding user guesses to ArrayList
    Guess g = new Guess(x , y);
    guesses.add(g);

    //check to see if hit or miss, if hit replace user guess with 'X'
    // else if its a miss replace guess with 'M'
    if (gameBoard[x][y] == "S") {
        guessBoard[x][y] = "X";
        System.out.println("Hit!");

        //now remove the ship from the list
        for(int i = 0; i < ships.size(); i++) {
            Ship ship = ships.get(i);
            if (ship.x== x && ship.y == y){
                ships.remove(i);
                break;
            }

        }
    } else {
        guessBoard[x][y] = "M";
        System.out.println("Miss!");

    }
}

public static boolean gameOver() {
    if(ships.size() == 0) {
        return true;
    } return false;

}

public static void printGuesses() {
    System.out.println("Guess || Row Col");
    System.out.println("=====================");
    for(int i = 0; i < guesses.size(); i++) {
        Guess g = guesses.get(i);
        System.out.println(" " + i + "    || " + g.row + "  " + g.col);
    }
}




////////////////////////////////////////////////////////
////// Main 
////////////////////////////////////////////////////////

public static void main(String[] args)
{
    // Your program should always output your name and the project number.
    // DO NOT DELETE OR COMMENT OUT. Replace with relevant info.
    System.out.println("Mason Sarna");
    System.out.println("Project 5");
    System.out.println("");
    // Your code should go below this line
    System.out.println("------------Welcome to BattleShip------------");
    //printGameMakerBoard();

    CS125_Project5 userShip = new CS125_Project5();
    //CS125_Project5.printGameMakerBoard();
    printBoard(guessBoard);
    // create scanner
    Scanner sc = new Scanner(System.in);

    //initializing variables
    int row = 0;
    int col = 0;
    String orient = "";
    int length = 0;
    /////////////////////
    // Getting user input for row, col, and orientation of 3 different ships, updates/prints the board after each ship input
    for (int i = 2; i < 5; i++) {
        System.out.println("Please enter coordinates for ship of length "+ i);
        System.out.println("Starting Row (0-6):");
        row = sc.nextInt();
        System.out.println("Starting column (0-6):");
        col = sc.nextInt();
        System.out.println("From the starting point, extend down or right? (d/r):");
        orient = sc.next().toLowerCase();
        length = i;  
        //CS125_Project5.updateBoard(row, col, orient, length);
        updateBoard(row,col,orient,length);
        CS125_Project5.addShip(row,col, i, orient);
    }

    System.out.println();
    System.out.println("------------Final Game Maker Board------------");
    CS125_Project5.printGameMakerBoard();
    System.out.println();
    System.out.println();



    System.out.println("------------GAME STARTING NOW------------");
    printBoard(guessBoard);



    while(!gameOver()) { 
        System.out.println("Enter guess in row/col:");
        int r = sc.nextInt();
        int c = sc.nextInt();
        if (guesses.contains(r) && guesses.contains(c)) {
            System.out.println("r\\c = " + r + "\\" + c + " has already been guessed");
        } else {
            userShip.guess(r, c);
            CS125_Project5.printBoard(guessBoard);

        }
        System.out.println("---------------------------------------------------");

    } 



    System.out.println("------------Game Over------------");
    CS125_Project5.printGuesses();


}

0 个答案:

没有答案