有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

eclipse JAVA游戏“战舰”的单人版

这个游戏使用一个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) 个答案