7

这是一个井字游戏发生器。仅计算机与计算机,与通常的播放器与计算机略有不同。我的大部分代码都是为此编写的,但我遇到的问题是有时当我生成游戏时,整个棋盘都填满了,会有一行 X 和一行 O,它会作为平局出现. 有时会生成两行X或两行O,并且游戏不会在第一行连续3个之后停止……有什么见解吗?谢谢你。

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        private Random rn = new Random();

        const int SIZE = 9;
        char[] cell = new char[SIZE];
        char firstPlayer = ' ', secondPlayer = ' ';

        private void button1_Click(object sender, EventArgs e)
        {
            //Clear the labels and starting values

            for (int i = 0; i < SIZE; i++)
            {
                cell[i] = ' ';
            }
            label10.Text = "";

            //Pick X or O to go first
            switch (rn.Next(2))
            {
                case 0: firstPlayer = 'O'; secondPlayer = 'X'; break;
                case 1: firstPlayer = 'X'; secondPlayer = 'O'; break;
            }

            //Get five non-repeating numbers from 0 to 8
            int[] positions = new int[5];
            positions[0] = rn.Next(9);
            for (int i = 1; i < 5; i++)
            {
                int temp = rn.Next(9);
                for (int j = 0; j < i; j++)
                {
                    if (temp == positions[j])
                    {
                        i--;
                        break;
                    }
                    else
                    {
                        positions[i] = temp;
                    }
                }
            }

            //Set each position found to have first players letter
            for (int i = 0; i < 5; i++)
            {
                cell[positions[i]] = firstPlayer;
            }

            for (int i = 0; i < SIZE; i++)
            {
                if (cell[i] != firstPlayer)
                {
                    cell[i] = secondPlayer;
                }
            }
            //Place cell values into the labels
            label1.Text = cell[0].ToString();
            label2.Text = cell[1].ToString();
            label3.Text = cell[2].ToString();
            label4.Text = cell[3].ToString();
            label5.Text = cell[4].ToString();
            label6.Text = cell[5].ToString();
            label7.Text = cell[6].ToString();
            label8.Text = cell[7].ToString();
            label9.Text = cell[8].ToString();

            //Check for a winner
            switch(checkWinner())
            {
                case 'T' : label10.Text = "It's a tie!"; break;
                case 'O' : label10.Text = "O Wins!"; break;
                case 'X' : label10.Text = "X Wins!"; break;
                default: label10.Text = "This will never appear"; break;
            }
        }

        private char checkWinner()
        {
            //return either 'T' for tie, 'O' for O wins, and 'X' for X wins
            char winner = ' ';
            int winning_line = 0;
            //check for a row win
            if(cell[0].Equals(cell[1]) && cell[0].Equals(cell[2]))
            {
                winning_line++;
                winner = cell[0];
            }
            if (cell[3].Equals(cell[4]) && cell[3].Equals(cell[5]))
            {
                winning_line++;
                winner = cell[3];
            }
            if (cell[6].Equals(cell[7]) && cell[6].Equals(cell[8]))
            {
                winning_line++;
                winner = cell[6];
            }

            //check for column wins
            if (cell[0].Equals(cell[3]) && cell[0].Equals(cell[6]))
            {
                winning_line++;
                winner = cell[0];
            }
            if (cell[1].Equals(cell[4]) && cell[1].Equals(cell[7]))
            {
                winning_line++;
                winner = cell[1];
            }
            if (cell[2].Equals(cell[5]) && cell[2].Equals(cell[8]))
            {
                winning_line++;
                winner = cell[2];
            }
            //check for diagonal winner
            if (cell[0].Equals(cell[4]) && cell[0].Equals(cell[8]))
            {
                winning_line++;
                winner = cell[0];
            }
            if (cell[2].Equals(cell[4]) && cell[2].Equals(cell[8]))
            {
                winning_line++;
                winner = cell[2];
            }

            if (winning_line == 0 || winning_line > 1)
            winner = 'T';

            return winner;
        }

        public int i { get; set; }
    }
}
4

4 回答 4

4
if (winning_line == 0 || winning_line > 1)

如果有两条线,它将 r 端口平局。如果你想在划线时停下来,你需要在每一步之后检查赢家,而不是在整个棋盘都填满之后。

于 2013-03-07T09:22:30.040 回答
2

第二个对角线获胜者检查应该是 6 而不是 8。

您当前正在检查:
XOO
OXO
OOX

和:
OOX
OXO
OOX

显然最后一个 x 应该在左边。

正如其他人所发布的那样。制作两条线不应导致平局。一个玩家甚至可以单独制作两条线,导致平局。
更改函数以在找到获胜行时立即返回结果,并在每次移动后检查它。

于 2013-03-07T09:20:47.163 回答
1

这有效:您需要摆脱...

如果(获胜线 == 0 || 获胜线 > 1)

用这三段代码替换那行代码:

    if (winnerX == " X ")
    {
        theWinner = winnerX;
    }
    if (winnerO == " O ")
    {
        theWinner = winnerO;
    }
    if(winnerX == " X " && winnerO == " O ")
    {
        winnerT = " T ";
        theWinner = winnerT;
    }

所以我所做的就是改变一些事情。我没有使用“winning_line++;” 一点代码。相反,我为每个 if 语句检查做了类似的事情。

    if (cell[2, 0].Equals(cell[1, 1]) && cell[2, 0].Equals(cell[0, 2]))
        {
            if (cell[2, 0] == 0)
            {
                winnerX = " X ";
            }

            else if (cell[2, 0] == 1)
            {
                winnerO = " O ";
            }
        }

所以我有 4 个字符串,我正在使用一个来跟踪 X 是否有获胜线,对于 O 也是如此。然后我有一个获胜者字符串来跟踪平局。它仅用于代替您的旧领带检查 if 语句所在的位置。

如果您决定使用字符串而不是整数,您还需要更改您的 switch 语句,即

    switch (checkWinner())
        {
            case " X ": 
                textBox1.Text = "X Wins!";
                break;
            case " O ":
                textBox1.Text = "O Wins!";
                break;
            case " T ": 
                textBox1.Text = "It's a tie!";
                break;
        }
于 2013-10-21T21:24:37.727 回答
0

嗨,您应该有一个功能,给定一个网格和一个新棋子,告诉您游戏是否结束。然后如果游戏结束了,你知道赢家是最后一个棋子。

我认为 Marthyi 的开源项目 MikMak 有一个完美的实现,见

bool IsFinished(Grid currentState, Pawn newPawn)

在:

https://github.com/Marthyi/MikMak/blob/master/MikMakSolution/Morpion/MorpionManager.cs

祝你好运!

于 2013-03-07T09:32:20.687 回答