【问题标题】:Java 2-D Arrays - Tic Tac Toe GameJava 2-D 数组 - 井字游戏
【发布时间】:2017-04-15 01:10:45
【问题描述】:

这款井字游戏是基于用户对计算机的。 本次作业,我要补缺的部分,就是补完main和moveAI方法(其他方法已经预设好了,我只需要调用它们);提供了 cmets,我需要在其中添加代码以使该井字游戏正常工作。我相信我的主要方法几乎完成了,除了 cmets 下的 2 条空行,我不确定它们是什么意思,而且我目前正坚持让我的 moveAI 方法工作。这是我应该生成随机位置的地方,在我将 X 放在某个地方后计算机将移动的地方。我不确定如何编写一些东西来检查以确保计算机移动的位置可用并且用户尚未占用。在此先感谢您的帮助!

import java.util.Random;
import java.util.Scanner;
public class TicTacToeGame
{
    public static void main ( String[] args )
    {
        Scanner in = new Scanner ( System.in );
        char[][] board = new char[ 3 ][ 3 ];
        int x, y = -1;
        char winner = 'N';
            // Initialize the board to spaces
          //  boolean noWinner = true;
        char player = 'X';
        for (int r = 0; x < board.length(); r++)
        {
            for (int c = 0; c < board.length(); c++)
            {
                board[r][c] = ' ';
            }
        }

        // Print the game board        
        printBoard(board);

        // Keep playing while the game isn't finished
        while (winner == 'N')
        {
            while (x < 0 && x >2 && y <0 && y > 2)
            {
                System.out.println("Enter the row and column, separated by spaces: ");
                x = in.nextInt();
                y = in.nextInt();
            }


            // Get the location from the user and validate it


            // Mark the position in the board according to the user's specified location


            // Print the board after the user plays
            printBoard(board);
            // Check to see if the game is finished. If it is, break out of the loop.

            // Have the AI make a move
            moveAI(board);
            // Print the board after the AI plays
            printBoard(board);

            // Check to see who the winner is
            winner =  checkWinner(board);
        }
        // If the winner is 'X' or 'O', print that, otherwise, it is a tie
        if (winner == 'X')
            System.out.println("X is the winner!");
        else if (winner == 'O')
            System.out.println("O is the winner!");
        else
            System.out.println("Tie");


    }
    /**
     * Makes a move for the AI, and marks the board with an 'O'.
     *
     * @param   board   The game board
     */
 public static void moveAI ( char[][] board )
    {
        int x,y = -1;
        Random r = new Random();
        x = r.nextInt(3);
        y = r.nextInt(3);
        boolean open = false;

        // Validate that the random location generated is valid.
        while (x < 0 && x >2 && y < 0 && y > 2)
        {
            x = r.nextInt(3);
            y = r.nextInt(3);
        }
        while (open == false)
        {
          if (board[r][c] != ' ')
            while (x < 0 && x >2 && y < 0 && y > 2)
            {
                x = r.nextInt(3);
                y = r.nextInt(3);
            }
        }
        }

            // Keep recalculating the location if the one generated is not
           // if (board[r][c] != ' ')
            //{


            // an empty space.
            System.out.print(" ");

            // Be sure to mark the position in the board with an 'O'
            board[][]=in.nextInt(3) + 'O';
        }

        /**
         * Prints out the tic-tac-toe board
         *
         * @param   board   The game board
         */
 public static void printBoard ( char[][] board )
        {
            // Box drawing unicode characters:

            char a = '\u250c';              // U+250C : top-left
            char b = '\u2510';              // U+2510 : top-right
            char c = '\u2514';              // U+2514 : bottom-left
            char d = '\u2518';              // U+2518 : bottom-right
            char e = '\u252c';              // U+252C : top-vertical-connector
            char f = '\u2534';              // U+2534 : bottom-vertical-connector
            char g = '\u251c';              // U+251C : left-horizontal-connector
            char h = '\u2524';              // U+2524 : right-horizontal-connector
            char i = '\u253c';              // U+253C : center plus sign connector
            char j = '\u2500';              // U+2500 : horizontal
            char k = '\u2502';              // U+2502 : vertical
            String l = j + "" + j + "" + j; // Three horizontals

            // Print out the game board

            System.out.printf ( "\n    0   1   2\n" +
                    "  %c%s%c%s%c%s%c\n" +
                    "0 %c %c %c %c %c %c %c\n" +
                    "  %c%s%c%s%c%s%c\n" +
                    "1 %c %c %c %c %c %c %c\n" +
                    "  %c%s%c%s%c%s%c\n" +
                    "2 %c %c %c %c %c %c %c\n" +
                    "  %c%s%c%s%c%s%c\n\n",
                    a, l, e, l, e, l, b,
                    k, board[0][0], k, board[0][1], k, board[0][2], k,
                    g, l, i, l, i, l, h,
                    k, board[1][0], k, board[1][1], k, board[1][2], k,
                    g, l, i, l, i, l, h,
                    k, board[2][0], k, board[2][1], k, board[2][2], k,
                    c, l, f, l, f, l, d );

        }

        /**
         * Checks the result of the game
         *
         * @param   board   The game board
         * @return          'X' if 'X' is the winner
         *                  'O' if 'O' is the winner
         *                  'T' if the game is a tie
         *                  'N' if the game isn't finished
         */
 public static char checkWinner( char[][] board )
        {
            if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' ||       // Check row 0
                    board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' ||   // Check row 1
                    board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' ||   // Check row 2
                    board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' ||   // Check col 0
                    board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' ||   // Check col 1
                    board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' ||   // Check col 2
                    board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' ||   // Check diag \
                    board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' ||   // Check diag /

                    board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' ||   // Check row 0
                    board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' ||   // Check row 1
                    board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' ||   // Check row 2
                    board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' ||   // Check col 0
                    board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' ||   // Check col 1
                    board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' ||   // Check col 2
                    board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' ||   // Check diag \
                    board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x')     // Check diag /


            {
                return 'X';
            }
            else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' ||  // Check row 0
                    board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' ||   // Check row 1
                    board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' ||   // Check row 2
                    board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' ||   // Check col 0
                    board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' ||   // Check col 1
                    board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' ||   // Check col 2
                    board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' ||   // Check diag \
                    board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' ||   // Check diag /

                    board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o' ||   // Check row 0
                    board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o' ||   // Check row 1
                    board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o' ||   // Check row 2
                    board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o' ||   // Check col 0
                    board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o' ||   // Check col 1
                    board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o' ||   // Check col 2
                    board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o' ||   // Check diag \
                    board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o')     // Check diag /

            {
                return 'O';
            }

            boolean finished = true;

            // If there is a blank space in the board, the game isn't finished yet
            for (int i = 0; i < board.length; i++)
                for (int j = 0; j < board[ i ].length; j++)
                    if (board[ i ][ j ] == ' ')
                        finished = false;

            // If the board is finished and 'X' or 'O' wasn't returned, then it is a tie
            // Otherwise, the game is not finished yet
            if ( finished )
                return 'T';
            else
                return 'N';
        }
    }

【问题讨论】:

  • 您似乎希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只会在发布者已经尝试自己解决问题时提供帮助。展示这项工作的一个好方法是包含您迄今为止编写的代码、示例输入(如果有的话)、预期输出以及您实际获得的输出(控制台输出、回溯等)。您提供的详细信息越多,您可能收到的答案就越多。检查FAQHow to Ask

标签: java arrays random


【解决方案1】:

// 从用户那里获取位置并验证它

看起来您已经在前面的几行中这样做了:

// you're validating the input here
while (x < 0 && x >2 && y <0 && y > 2)
{
    System.out.println("Enter the row and column, separated by spaces: ");
    // you're getting the location from the user here
    x = in.nextInt();
    y = in.nextInt();
}

您可能仍需要检查用户输入的位置是否已被填写。您需要在数组中查找。 moveAI 中有一条评论给你一个提示。你检查board[x][y] 是否是一个空格。

另外,我认为你有一个错误,因为你从未重置 xy 的值,所以下次轮到玩家时不会进入循环。 xy 仍然具有最后一轮的值,因此循环条件的计算结果为 false。我认为你可以通过使用do{...}while(...); 循环而不是while(...){...} 循环来解决这个问题。

//根据用户指定的位置在棋盘上标记位置

这只是意味着在board[x][y] 中添加一个'O'

// 检查游戏是否结束。如果是,则跳出循环。

看起来checkWinner 是这样做的。如果游戏结束则返回'T',如果仍在进行则返回'N'


对于 AI 代码,这里有一些我认为你应该做的伪代码:

1。生成随机 x, y 坐标 2. 检查 board[x][y] 是否已被使用(有一条评论说明如何使用) 3.如果用过,就回到1,否则在那个空格里放一个'X'并返回

此外,您不需要在 AI 代码中检查 while (x &lt; 0 &amp;&amp; x &gt;2 &amp;&amp; y &lt; 0 &amp;&amp; y &gt; 2) 的循环。 Random.nextInt(3) 已返回 0-2 范围内的数字。

【讨论】:

    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多