匹配的游戏程序没有提供控制台的输出?

时间:2016-11-12 17:05:40

标签: java while-loop println

基本上,项目描述要求用户创建一个匹配的游戏,包括:

  1. 二维游戏桌(4x4),16对匹配卡,以及面朝下卡数的运行计数。包括从输入x,y位置的表中检索特定卡片的方法。

  2. 一个gameBoard,循环继续播放,直到所有牌面朝上 循环包括一个界面,用户可以选择两张牌(输入x,y表位置),检查两张牌是否相等, 递减faceDown张卡的数量,并设置卡片的faceUp boolean

  3. 从某种程度上说,程序应该运行直到所有牌面朝上,并赢得游戏。我将我的课程分成了以下四个单独的课程。

    1

    public class Card {
        private final int cardValue;
        private boolean faceUp;
    
        public Card(int value) {
            cardValue = value;
            faceUp = false;
        }
    
        public int getValue() {
            return cardValue;
        }
    
        public boolean isFaceUp() {
            return faceUp;
        }
    
        public void setFaceUp(boolean input) {
            faceUp = input;
        }
    
        public static void printBoard(Card[][] cards) {
            System.out.println("\t\t1\t2\t3\t4");
            System.out.println("\t____________");
    
            for(int i = 0; i < cards.length; i++) {
                System.out.println((i + 1) + "\t|\t");
    
                for(int j = 0; j < cards[0].length; j++)
                    if(cards[i][j].isFaceUp()) {
                        System.out.print(cards[i][j].getValue() + "\t"); }
                    else
                        System.out.println("*\t");
    
            }
            System.out.println();
        }
    
    }
    

    2

    public class CreateBoard {
    
    public static Card[][] createBoard() {
        Card[][] board = new Card[4][4];
    
        for(int i = 1; i <= 8; i++) {
            for(int j = 1; j <= 2; j++) {
    
                boolean boardLocation = false;
    
                while(!boardLocation) {
                    int row = (int)(Math.random() * 4); 
                    int column = (int)(Math.random() * 4);
    
                    if(board[row] == null && board[column] == null) {
                        boardLocation = true;
                        board[row][column] = new Card(i);
                        }
                    }
                }
            }
                    return board;
        }
    }
    

    3

    public class Game {
        public static boolean wonGame(Card[][] board) {
            for(int i = 0; i < board.length; i++) {
                for(int j = 0; j < board[0].length; j++) {
                    if(!board[i][j].isFaceUp())
                        return false;
                }
            }
                        return true;
        }
    
    }
    

    最后,主要课程:

    public class GameDriver {
        public static void main(String[] args) {
            Card[][] board = CreateBoard.createBoard();
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Starting Game...");
    
            while(!Game.wonGame(board)) {
                Card.printBoard(board);
    
                System.out.println("Enter X-Coordinate #1 (1-4): ");
                int column1 = keyboard.nextInt();
    
                System.out.println("Enter Y-Coordinate #1 (1-4): ");
                int row1 = keyboard.nextInt();
    
                System.out.println("Enter X-Coordinate #2 (1-4): ");
                int column2 = keyboard.nextInt();
    
                System.out.println("Enter Y-Coordinate #2 (1-4): ");
                int row2 = keyboard.nextInt();
    
                Card card1 = board[row1][column1];
                Card card2 = board[row2][column2];
    
                if(card1.getValue() == card2.getValue() && !(row1 == row2 && column1 == column2)) 
                {
                    card1.setFaceUp(true);
                    card2.setFaceUp(true);
                }
    
                else if(row1 == row2 && column1 == column2)
                {
                    System.out.println("Points selected are the same, try again");
                }
    
                else
                {
                    System.out.println(card1.getValue() + " and "  + card2.getValue() + " do not match");
    
                }
            }
                Card.printBoard(board);
        }
    }
    

    代码似乎运行良好,没有错误或任何东西。然而,经过多次测试试验后,明显的问题是它没有输出任何东西到控制台...我错过了什么?帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

创建董事会应

public class CreateBoard {

public static Card[][] createBoard() {
    Card[][] board = new Card[4][4];

    for(int i = 1; i <= 8; i++) {
        for(int j = 1; j <= 2; j++) {

            boolean boardLocation = false;

            while(!boardLocation) {
                int row = (int)(Math.random() * 4); 
                int column = (int)(Math.random() * 4);

                if(board[row][column] == null) {
                    boardLocation = true;
                    board[row][column] = new Card(i);
                    }
                }
            }
        }
                return board;
    }
}

对于Game类,因为java数组索引从0开始 这些行应该是

Card card1 = board[row1-1][column1-1];
Card card2 = board[row2-1][column2-1];