2016-11-12 30 views
0

基本上,項目描述要求用戶創建一個匹配遊戲,其中包括:配套的遊戲程序不提供控制檯輸出?

  1. 的二維播放表(4×4),16雙匹配的卡片,和麪的數量的運行計數下牌。在輸入x,y位置包含一個從表格中檢索特定卡片的方法。

  2. A gameBoard,以及繼續播放直到所有卡保持正面朝上的循環。
    該循環包括與用戶的接口以選擇兩張卡片(輸入x,y表格位置),檢查兩張卡片是否相等, 遞減卡片的計數和設置卡片的faceUp boolean

從根本上說,程序應該運行,直到所有的牌面朝上,遊戲贏了。我將我的課程分成四個獨立的課程。

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); 
    } 
} 

該代碼似乎運行良好,沒有錯誤或任何東西。然而,經過多次測試試驗後,顯而易見的問題是,它不會輸出任何東西給控制檯...我錯過了什麼嗎?幫助將不勝感激!

+0

看看你能不能用它修復[ SSCCE](http://sscce.org)或[mcve]。如果沒有,請使用調試器.. –

回答

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; 
    } 
} 

而且爲遊戲類,因爲Java數組索引從0開始,然後 這些線應該是

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