0

目前,我有一个用 java 编写的简单井字游戏程序的工作代码。正如您将在下面看到的那样,唯一的问题是显示我的板时正在打印空字符(\u0000)而不是空白区域。

我的教授告诉我们编写这个程序的方式是检测空空格并用 X 或 O 填充它们,我这样做了。

现在,我希望能够将空字符从显示为 00 更改为只是一个空格,因为否则格式不正确。

我已经尝试简单地擦除 '\u0000' 字符并将其替换为 ' ' 字符,但是我的板根本没有出现。任何帮助表示赞赏!

import java.util.Scanner; 

  public class TicTacToe
  {
  public static void main(String[] args)
  {

     char[][] board = new char[3][3];

     while (true) {

        makeCompMove(board, 'X');

        displayBoard(board);
        if(isWon('X', board)) {
           System.out.println("\n\nComputer won!");
           System.exit(1);
        }

        else if (isDraw(board)) {
           System.out.println("\n\nDraw Game! No winner");
           System.exit(2);
        }

        makeAMove(board, 'O');
        displayBoard(board);

        if (isWon('O', board)) {
           System.out.println("\n\nPlayer won!");
           System.exit(3);
        }
        else if (isDraw(board)) {
           System.out.println("\n\nDraw Game! No winner");
           System.exit(4);
        }
     }
  }

  public static void displayBoard(char[][] board) 
  {

     for(int k = 0; k < 3; k++)
     {
        for(int i = 0; i < 28; i++)
        {
           System.out.print("-");
        }
        System.out.println();

        for(int j = 0; j < 3; j++)
        {
           System.out.print("|" + "  " + board[k][j] + "  ");
        }
        System.out.println("|");

     }
        for(int i = 0; i < 28; i++)
     {
        System.out.print("-");
     }

  }

  public static void makeAMove(char[][] board, char o)
  {
     Scanner input = new Scanner(System.in);
     while(true)
     {
        System.out.print("\n\nYour turn. Enter a row and col(0,1 or 2): ");
        int row = input.nextInt();
        int col = input.nextInt();
        if(row > 2 || row < 0 || col > 2 || col < 0)
        {
           System.out.println("Incorrect Input. Try Again!");
           continue;
        }
        if(board[row][col] == '\u0000')
        {
                System.out.print("\n You (O) have made your move...\n\n");
           board[row][col] = 'O';
                break;
        }
        else
           System.out.println("Incorrect Input. Try Again!");
     }
  }

    public static void makeCompMove(char[][] board, char x)
  {
      System.out.println();
      System.out.println();
    System.out.print("Computer (X) has made his move...\n");
     while(true)
     {
        int row = (int)(Math.random()*3);
        int col = (int)(Math.random()*3);

            if(board[row][col] == '\u0000')
        {
           board[row][col] = x;
           break;
        }
     }
        System.out.println();
    }

 public static boolean isDraw(char[][] board)
  {
     for(int row = 0; row < 3; row++)
     {
        for(int col = 0; col < 3; col++)
        {
           if(board[row][col] == '\u0000')
           {
              return false;
           }
        }
     }
     return true;
  }

  public static boolean isWon(char x, char[][] board)
  {
    // Check Rows
        for (int i = 0; i < 3; i++)
            if (x == board[i][0] && x == board[i][1] && x == board[i][2]) 
                return true;

    // Check Columns
        for (int j = 0; j < 3; j++)
            if (x == board[0][j] && x == board[1][j] && x == board[2][j]) 
                return true;

    // Check first diagonal
        if (x == board[0][0] && x == board[1][1] && x == board[2][2]) 
            return true;

    // Check second diagonal
        if (x == board[0][2] && x == board[1][1] && x == board[2][0]) 
            return true;

    return false;
    }
}
4

2 回答 2

1

无需更改您在显示前检查的任何代码

在 displayBoard 中这样使用

 for(int j = 0; j < 3; j++)
    {
        if(board[k][j]=='\u0000')
    System.out.print("|" + "     ");
    else
    System.out.print("|" + "  " + board[k][j] + "  ");

    }
于 2013-04-01T05:02:16.437 回答
0

二维数组中的元素在数组初始化时设置为空字符。如果要将它们全部转换为空格,则遍历它们并用空格替换字符。

for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
        board[i][j] = ' ';
    }
}

如果该位置从未使用过,那么它将有一个空格而不是其中的空字符。在使用阵列之前执行此操作。

于 2013-04-01T04:56:06.380 回答