显示C#tic tac toe的X和Os

时间:2017-11-04 21:43:58

标签: c# multidimensional-array

我正在尝试为课程编写一个简单的tic tac toe程序。我知道网站上有其他帖子回应类似的课程工作,但我找不到我需要的帮助。我为我的网格设置了一个二维数组。我有9个标签来保持随机生成的0或1代表X(1)或O(0)。我的问题是我不确定如何显示X或O而不是数字。另外,我有几行设置来检查X或O是否赢了,但是,我不知道如何显示它是否是平局。我不是在寻找直接的答案,而是指导如何实现这一目标。截至目前,我的表格将显示随机的1和0,如果符合条件将说明X或O是否赢了,但即使X或O没有获胜也会这样做。除了单击调用函数的按钮以生成数字并更新表单之外,此游戏没有人为因素。我的代码如下:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void playAgain_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            const int rows = 3;
            const int columns = 3;
            int[,] board = new int[rows, columns];

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < columns; col++)
                {
                    board[row, col] = rand.Next(2);
                }
            }

            square1.Text = board[0, 0].ToString();
            square2.Text = board[0, 1].ToString();
            square3.Text = board[0, 2].ToString();
            square4.Text = board[1, 0].ToString();
            square5.Text = board[1, 1].ToString();
            square6.Text = board[1, 2].ToString();
            square7.Text = board[2, 0].ToString();
            square8.Text = board[2, 1].ToString();
            square9.Text = board[2, 2].ToString();

            //O win check
            if (board[0,0] == 0 && (board[0,0] == board[0,1]) && (board[0,1] == board[0,2]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[1, 0] == 0 && (board[1, 0] == board[1, 1]) && (board[1, 1] == board[1, 2]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[2, 0] == 0 && (board[2, 0] == board[2, 1]) && (board[2, 1] == board[2, 2]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[0, 0] == 0 && (board[0, 0] == board[1, 0]) && (board[1, 0] == board[2, 0]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[0, 1] == 0 && (board[0, 1] == board[1, 1]) && (board[1, 1] == board[2, 1]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[0, 2] == 0 && (board[0, 2] == board[1, 2]) && (board[1, 2] == board[2, 2]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[0, 0] == 0 && (board[0, 0] == board[1, 1]) && (board[1, 1] == board[2, 2]))
            {
                winnerBox.Text = "O wins!";
            }
            else if (board[0, 2] == 0 && (board[0, 2] == board[1, 1]) && (board[0, 1] == board[2, 0]))
            {
                winnerBox.Text = "O wins!";
            }


            //X win check
            if (board[0, 0] == 1 && (board[0, 0] == board[0, 1]) && (board[0, 1] == board[0, 2]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[1, 0] == 1 && (board[1, 0] == board[1, 1]) && (board[1, 1] == board[1, 2]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[2, 0] == 1 && (board[2, 0] == board[2, 1]) && (board[2, 1] == board[2, 2]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[0, 0] == 1 && (board[0, 0] == board[1, 0]) && (board[1, 0] == board[2, 0]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[0, 1] == 1 && (board[0, 1] == board[1, 1]) && (board[1, 1] == board[2, 1]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[0, 2] == 1 && (board[0, 2] == board[1, 2]) && (board[1, 2] == board[2, 2]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[0, 0] == 1 && (board[0, 0] == board[1, 1]) && (board[1, 1] == board[2, 2]))
            {
                winnerBox.Text = "X wins!";
            }
            else if (board[0, 2] == 1 && (board[0, 2] == board[1, 1]) && (board[0, 1] == board[2, 0]))
            {
                winnerBox.Text = "X wins!";
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

private Random rand = new Random();

private void playAgain_Click(object sender, EventArgs e)
{
    TextBox[][] squares = new TextBox[][]
    {
        new TextBox[] { square1, square2, square3 },
        new TextBox[] { square4, square5, square6 },
        new TextBox[] { square7, square8, square9 },
    };

    const int rows = 3;
    const int columns = 3;
    char[,] board = new char[rows, columns];

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < columns; col++)
        {
            board[row, col] = rand.Next(2) == 0 ? 'O' : 'X';
            squares[row][col].Text = board[row, col].ToString();
        }
    }

    var scoring =
        Enumerable
            .Range(0, 3)
            .SelectMany(n => new[]
            {
                Enumerable.Range(0, 3).Select(x => board[n, x]),
                Enumerable.Range(0, 3).Select(x => board[x, n]),
            })
            .Concat(new[]
            {
                Enumerable.Range(0, 3).Select(x => board[x, x]),
                Enumerable.Range(0, 3).Select(x => board[x, 2 - x]),
            })
            .Select(xs => String.Join("", xs))
            .ToLookup(x => x);

    int xwins = scoring["XXX"].Count();
    int owins = scoring["OOO"].Count();

    if (xwins == 1 & owins == 0)
    {
        winnerBox.Text = "X wins!";
    }
    else if (xwins == 0 && owins == 1)
    {
        winnerBox.Text = "O wins!";
    }
    else if (xwins == 0 && owins == 0)
    {
        winnerBox.Text = "Draw";
    }
    else
    {
        winnerBox.Text = "Invalid Game";
    }
}

答案 1 :(得分:0)

以下是您想要的提示。

将纸板项目分配给标签文本时,将其转换为X或O:

square1.Text = board[0, 0] == 0 ? "O" : "X";

//In the check if O wins record the winning text in a string instead of winnerBox.Text. 

string oWinsText == string.Empty;

if (board[0,0] == 0 && (board[0,0] == board[0,1]) && (board[0,1] == board[0,2])) { 
   oWinsText = "O wins!"; }

//Then do the same in the check if X wins:
string xWinsText == string.Empty;

if (board[0,0] == 0 && (board[0,0] == board[0,1]) && (board[0,1] == board[0,2])) { 
   xWinsText = "X wins!"; }

//Check if it's a draw:
bool isDraw = (oWinsText.Length > 0 && xWinsText.Length > 0);

//Finally show the result
if (isDraw)
  winnerBox.Text = "Draw";
else
  winnerBox.Text = oWinsText + xWinsText;