【问题标题】:Tic Tac Toe, Help/Determine Winner井字游戏,帮助/确定获胜者
【发布时间】:2014-01-26 21:00:56
【问题描述】:

我正在创建一个包含 3 x 3 矩形整数数组的井字游戏类,由 2 个人类玩家玩。 “1”用于第一个玩家的移动,“2”用于第二个玩家的移动。目前,我被卡住了,不知道如何确定/检查游戏是否赢了,或者在每一步完成后是否平局。

不幸的是,我一直在检查每个玩家移动后是否赢得或平局,并希望有人能提供帮助。

到目前为止,这是我的代码: 主类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TicTacToe game = new TicTacToe();
            game.PrintBoard();
            game.Play();
        }
    }
}

井字游戏类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    class TicTacToe
    {
        private const int BOARDSIZE = 3; //size of the board
        private int[,] board = new int [BOARDSIZE,BOARDSIZE]; // board representation
        string player1row, player1column, player2row, player2column;
        enum DONE {win1, win2, win3, win4, win5, win6, win7,win8,
                   win9, win10, win11, win12, win13, win14, win15, win16};


        //Default Constructor
        public TicTacToe(){
            board = new int [3,3] { {0,0,0},{0,0,0},{0,0,0} };
        }

        int win1 = 1;  

        public void PrintBoard(){
            Console.WriteLine("-----------------------\n"+
                              "|       |       |       |\n"+
                              "|   {0}   |   {1}   |   {2}   |\n" +
                              "|_______|_______|_______|\n"+
                              "|       |       |       |\n"+
                              "|   {3}   |   {4}   |   {5}   |\n" +
                              "|_______|_______|_______|\n"+
                              "|       |       |       |\n"+
                              "|   {6}   |   {7}   |   {8}   |\n" +
                              "|_______|_______|_______|\n",
                              board[0,0],board[0,1],board[0,2],
                              board[1,0],board[1,1],board[1,2],
                              board[2,0],board[2,1],board[2,2]);
        }// end PrintBoard method

        public void Play(){
            while (true)
            {
                Console.WriteLine("Player 1's turn.");
                Console.Write("Player 1: Enter row ( 0 <= row < 3 ): "); //prompt user
                player1row = Console.ReadLine(); //get string from user           
                Console.Write("Player 1: Enter column ( 0 <= row < 3 ): "); //prompt user 
                player1column = Console.ReadLine(); //get string from user

                //Convert string to ints
                int p1r = Convert.ToInt32(player1row);
                int p1c = Convert.ToInt32(player1column);
                // assign marker to desired position
                board[p1r, p1c] = 1;

                PrintBoard(); // Update board
                checkWinner();

                Console.WriteLine("\nPlayer 2's turn.");
                Console.Write("Player 2: Enter row ( 0 <= row < 3 ): ");
                player2row = Console.ReadLine(); //get string from user     
                Console.Write("Player 2: Enter column ( 0 <= row < 3 ): ");
                player2column = Console.ReadLine(); //get string from user

                //Convert string to ints
                int p2r = Convert.ToInt32(player2row);
                int p2c = Convert.ToInt32(player2column);
                // assign marker to desired position
                board[p2r, p2c] = 2;

                PrintBoard(); // Update board
                checkWinner();

            }
        }//end Play method

        private bool checkWinner()
        {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)

                    //I WANT TO CHECK FOR A WIN OR DRAW HERE BUT DONT KNOW HOW TO






    }}// end class

我想使用 2 个 for 循环检查获胜者,第一个检查行,然后另一个检查列,然后添加 2 个单独的 if 语句来检查对角线,但我真的不知道如何 & 一直暂时卡住了。

【问题讨论】:

    标签: c# console


    【解决方案1】:

    我不会为你编写代码,但这里是你可以使用的逻辑:

    1) 对于每一列,检查所有行是否相同,如果是则宣布获胜者。如果没有,请转到第 2 步。

    2) 对于每一行,检查所有列值是否相同,如果是则宣布获胜者。如果没有,请转到第 3 步。

    3) 现在检查对角线,这里只有两种可能性 [0,0] [1,1] [2,2] 和 [0,2] [1,1] [2,0] 如果它们是同样声明获胜者,如果不检查数组中的所有值是否都被填充,如果是则声明抽奖如果没有让用户输入值。

    【讨论】:

    • 看,这是我有点卡住的地方。我不知道在检查它时该放什么。我询问用户的行和列,将他们的输入转换为字符串并使用'board[p1r, p1c] =1'或'board[p2r, p2c] = 2'将它们放在板上。我不知道我会用什么来检查行和列。我想我有点搞糊涂了。
    【解决方案2】:

    作为Iiya has said,您将不得不通过所有获胜案例。有很多方法可以做到这一点,但代码中的要点是

    private bool checkWinner(int player)
    {
        // check rows
        if (board[0, 0] == player && board[0, 1] == player && board[0, 2] == player) { return true; }
        if (board[1, 0] == player && board[1, 1] == player && board[1, 2] == player) { return true; }
        if (board[2, 0] == player && board[2, 1] == player && board[2, 2] == player) { return true; }
    
        // check columns
        if (board[0, 0] == player && board[1, 0] == player && board[2, 0] == player) { return true; }
        if (board[0, 1] == player && board[1, 1] == player && board[2, 1] == player) { return true; }
        if (board[0, 2] == player && board[1, 2] == player && board[2, 2] == player) { return true; }
    
        // check diags
        if (board[0, 0] == player && board[1, 1] == player && board[2, 2] == player) { return true; }
        if (board[0, 2] == player && board[1, 1] == player && board[2, 0] == player) { return true; }
    
        return false;
    }
    

    您可以根据自己的喜好进行优化(使用 for 循环或矩阵)。注意checkWinner() 函数需要player 输入。

    【讨论】:

    • 这就是我刚刚想到的,但现在“玩家”输入让我感到困惑,因为我是在 2 个人类玩家而不是 1 个玩家与计算机之间玩。
    • 或者当有 2 个人类玩家在玩时,我在哪里/如何声明玩家?
    • 这个方法不关心是人还是电脑玩,它只是检查给定的数字是否以中奖格式存在。在播放功能中只需将checkWinner() 行更改为if (checkWinner(1)) Console.WriteLine("\nPlayer 1 has won"); else if (checkWinner(2)) Console.WriteLine("\nPlayer 2 has won");
    • 在井字游戏中,如果所有单元格都被占用但没有赢家,则决定为平局。所以当所有的board 单元格都有一个值,而checkWinner() 对两个玩家来说都是假的,那就是平局
    • 阅读我的 #3 为这个用户 3238423,如果所有检查都失败(意味着没有赢家)并且所有数组值都已满,则宣布平局。
    【解决方案3】:

    我将尝试回答如何检测平局,因为所有其他人都只关注检查是否有人赢了。

    只有在棋盘完全完成时才能进行游戏,并且没有其他获胜组合。因此,在您检查了获胜者并没有找到获胜者之后,不难发现。

    • 检查获胜者,如果未找到,继续检测平局。
    • 扫描整个电路板阵列,无论顺序如何。
    • 如果找到空位,则不是平局(可以进一步移动),中断循环并继续播放。
    • 到达循环结束后,宣布平局(所有空格均已填满,没有获胜组合)。

    【讨论】:

    • 检查平局是它自己的方法还是保留在 checkWinner() 方法中?
    • 任何一种方式都可以。它必须在 CheckWinner 完成后完成,所以我要做的是创建自己的方法,但从 CheckWinner 内部调用它。更多的是 OOP 设计实践,而不是任何技术原因或限制,然后只是个人喜好。
    • 我尝试了类似的方法,但它不起作用。 private bool checkDraw(int player) { for (int i = 0; i
    • 你应该检查board[i,j]==empty然后返回false(如果有空格,还不能平局)。当循环结束时,返回true
    • 我想出了一个不同的方法来检查是否有平局。我在for循环中制作了整个游戏。每个玩家移动后,计数器加 1。如果 9 次移动后没有赢家,因为棋盘上有 9 个位置,我宣布平局。这行得通,现在我需要弄清楚在宣布获胜者后如何结束游戏,因为目前,即使有获胜者,游戏仍会继续并提示下一个玩家采取行动。关于如何做到这一点的任何指示?
    【解决方案4】:

    一种简单的方法是尝试每个方格(对于 i = 0;i

    【讨论】:

    • 我不太确定我是否掌握了您的方法,因为我将其编写为控制台应用程序。
    • 我试图描述一种算法方法,因此 UI 模式应该无关紧要。我现在没时间了,但如果你明天还没有弄清楚,我会详细说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多