2017-04-21 66 views
0

我的代码只打印空白空格,而不是每次提示输入行号和列号,并查看空格是否为空。我要生成电脑玩家的随机数,但如果我这样做是不知道正确的带计算机端随机数发生器的2D TicTacToe

import java.util.*; 
public class TicTacToe 
{ 
public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    int row, column; 
    char player = 'X'; 
    char[][] board = new char[3][3]; 
    for(int i=0;i<3;i++){ 
    for(int j=0;j<3;++j){ 
     board[i][j] = ' '; 
    } 
    } 
    while(!checkWin(board,'O')==true) 
    { 
     computerPlay(board); 
     displayBoard(board); 
     if(checkWin(board,'X')) 
     { 
      System.out.println("Computer Wins"); 
      System.exit(0); 
     } 
     if(checkTie(board)) 
     { 
      System.out.println("Tie game"); 
      System.exit(0); 
     } 
     playerPlay(board); 
     displayBoard(board); 
     if(checkWin(board,'O')) 
     { 
      System.out.println("Player Wins"); 
      System.exit(0); 
     } 
     if(checkTie(board)) 
     { 
      System.out.println("Tie game"); 
      System.exit(0); 
     } 
    } 
    } 

    // Prompt the user for row & column index. Continue asking 
    // until an empty cell is selected. set the cell to 'O' 
    public static void playerPlay(char[][] board) 
    { 
    int row, column; 
    Scanner in = new Scanner(System.in); 
    while(checkTie(board)!= true) 
    { 

    System.out.println("Enter a row and column (0, 1, or 2); for player 
:"); 
    row = in.nextInt(); 
    column = in.nextInt(); 
    } 
} 
// Check by row, column, and diagonals 
public static boolean checkWin(char[][] board,char ch) 
{ 
    if ( board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // 
    1st row 
     board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // 
    2nd row 
     board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // 
    3rd row 
     board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // 
    1st col. 
     board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // 
    2nd col. 
     board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // 
    3rd col. 
     board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // 
    Diagonal   \ 
     board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O') // 
    Diagonal /
    { 
    return true; 
    } 
    else { 

    return false; 
    } 
} 

// check for tie. If there no empty cells, then it is a tie 
public static boolean checkTie(char[][] board) 
{ 
    for (int i = 0; i< board.length; i++) { 
    for (int j = 0; j < board[0].length; j++) { 
     if (board[i][j] != 'O' && board[i][j] != 'X') { 
      return true; 
     } 
    } 
    } 
    return false; 
} 
// Display the board 
public static void displayBoard(char[][] board) 
{ 
    System.out.println(board[0][0] + " | " + board[0][1] + " | " + board[0][2] 
+ "\n---------"); 
    System.out.println(board[1][0] + " | " + board[1][1] + " | " + board[1][2] 
+ "\n---------"); 
    System.out.println(board[2][0] + " | " + board[2][1] + " | " + board[2][2] 
+ "\n"); 
} 
// Continue generating random values for row and col until an 
// empty cell selected. Set the cell to 'X' 
public static void computerPlay(char[][]board) 
{ 
    while(checkTie(board)!= true) 
    { 
    for (int i=0; i<board.length; i++){ 
     for(int j = 0; j<board.length; j++){ 
      board[i][j] = (char)(Math.random()*10); 

     } 

    } 

    } 

} 


} 

回答

0

checkTie函数返回true如有板值为空(严格地说,不是“X”或“O”)。如果checkTie的返回值为false,则computerPlay函数仅会继续执行。其中一个(但不是两个)的逻辑应该颠倒。我建议更改isTied函数的名称以避免混淆,并反转该函数中的逻辑。这是我的建议(它也将while替换为if,因为它应该只运行一次):

public static boolean isTied(char[][] board) 
{ 
    for (int i = 0; i< board.length; i++) { 
    for (int j = 0; j < board[0].length; j++) { 
     if (board[i][j] != 'O' && board[i][j] != 'X') { 
      return false; 
     } 
    } 
    } 
    return true; 
} 
... 
public static void computerPlay(char[][]board) 
{ 
    if(!isTied(board)) 
    { 
    for (int i=0; i<board.length; i++){ 
     for(int j = 0; j<board.length; j++){ 
      board[i][j] = (char)(Math.random()*10); 
     } 
    } 
    } 
}