c# – Tic Tac Toe – 检测胜利,失败或平局

c# – Tic Tac Toe – 检测胜利,失败或平局,第1张

概述这是一个tic tac toe发电机.仅限计算机与计算机,与通常的播放器与计算机略有不同.我为此编写了大部分代码,但我遇到的问题有时是在我生成游戏时,整个电路板都会填满,并且会出现一行X和一行O并且会出现一个关系.有时会生成两行X或两行O,并且游戏不会在第一行连续3行后停止……任何见解?谢谢. namespace TicTacToe{ public partial class Form1 这是一个tic tac toe发电机.仅限计算机与计算机,与通常的播放器与计算机略有不同.我为此编写了大部分代码,但我遇到的问题有时是在我生成游戏时,整个电路板都会填满,并且会出现一行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; }    }}
解决方法
if (winning_line == 0 || winning_line > 1)

如果有两条线,它将会打开平局.如果你想在一条线路上停下来,你需要在每次移动后检查一个胜利者,而不是在整个棋盘被填满之后.

总结

以上是内存溢出为你收集整理的c# – Tic Tac Toe – 检测胜利,失败或平局全部内容,希望文章能够帮你解决c# – Tic Tac Toe – 检测胜利,失败或平局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1251527.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存