创建,使用和比较按钮集合中的元素

Nik*_*ric 1 c# collections button

我正在做一个Tic-tac-toe项目,我遇到了一些困难.我只是对收藏品松懈,所以你可以看到问题.

事情是,我创建了9个按钮,单击时可以更改背景图像,并禁用它们自己.我已经设法让它为2名玩家运行,但我的愿望是创建某种AI.

我需要一个按钮集合,以便我可以比较它们的属性,并避免通常的逐个比较和大量和众多的If语句.

我真正想要的是能够测试同一行或列中的按钮是否具有相同的值(背景图像).到目前为止我使用的是一个描述符号值的字符串数组,但它完全脱离了按钮,并且输入所有代码需要花费大量时间.

如果不能按照我想象的方式完成,请告诉我们.我对建议持开放态度,并将非常感激.

哦,如果您需要任何代码或更多细节,请告诉我.

Dan*_*mov 5

您需要分隔数据(数组)和演示文稿(按钮).

更新

我发布了一个编译和运行的示例控制台项目.
它的模型与表示分开,因此您可以TicTacToe.cs为其编写GUI.

在此输入图像描述

你不需要字符串; 布尔都适用于两个州.
实际上,有一个第三个状态,它是空的,所以你可以使用一个可以为空的布尔值.

因此X将对应于trueO to false和empty space to null.
我创建了一个封装可以为空的布尔方阵列的类:

class TicTacToe {
    const int Length = 3;
    private bool? [][] _data;
    private bool? _winner;

    public TicTacToe ()
    {
        _data = Enumerable
            .Range (0, Length)
            .Select (_ => new bool? [Length])
            .ToArray ();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我将行,列和对角线表示为向量:

public bool? GetCell (int row, int column)
{
    return _data [row][column];
}

public IEnumerable<bool?> GetRow (int index)
{
    return _data [index];
}

IEnumerable<int> GetIndices ()
{
   return Enumerable.Range (0, Length);
}

public IEnumerable<bool?> GetColumn (int index)
{
    return GetIndices ()
        .Select (GetRow)
        .Select (row => row.ElementAt (index));
}

public IEnumerable<bool?> GetDiagonal (bool ltr)
{
    return GetIndices ()
        .Select (i => Tuple.Create (i, ltr ? i : Length - 1 - i))
        .Select (pos => GetCell (pos.Item1, pos.Item2));
}

public IEnumerable<IEnumerable<bool?>> GetRows ()
{
    return GetIndices ()
        .Select (GetRow);
}

public IEnumerable<IEnumerable<bool?>> GetColumns ()
{
    return GetIndices ()
        .Select (GetColumn);
}

public IEnumerable<IEnumerable<bool?>> GetDiagonals ()
{
    return new [] { true, false }
        .Select (GetDiagonal);
}

public IEnumerable<IEnumerable<bool?>> GetVectors ()
{
    return GetDiagonals ()
        .Concat (GetRows ())
        .Concat (GetColumns ());
}
Run Code Online (Sandbox Code Playgroud)

然后我会编写一个带矢量的函数,并说它是否是一个胜利者:

static bool? FindWinner (IEnumerable<bool?> vector)
{
    try {
        return vector
            .Distinct ()
            .Single ();
    } catch (InvalidOperationException) {
        return null;
    }
}

static bool? FindWinner (IEnumerable<IEnumerable<bool?>> vectors)
{
    return vectors
        .Select (FindWinner)
        .FirstOrDefault (winner => winner.HasValue);
}

public bool? FindWinner ()
{
    return FindWinner (GetVectors ());
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以打电话GetWinner找出是否有人赢了.
然后我会写一个方法来移动:

public bool MakeMove (int row, int column, bool move)
{
    if (_winner.HasValue)
        throw new InvalidOperationException ("The game is already won.");

    if (_data [row][column].HasValue)
        throw new InvalidOperationException ("This cell is already taken.");

    _data [row][column] = move;
    _winner = FindWinner ();

    return move == _winner;
}

public bool? Winner {
    get { return _winner; }
}
Run Code Online (Sandbox Code Playgroud)

这一切都在TicTacToe课堂上.
您的GUI应该创建它并调用其方法.

单击按钮时,您可以执行以下操作:

private TicTacToe _game = new TicTacToe ();
private Button [][] _buttons = new Button [][3];

const bool HumanPlayer = true;
const bool AIPlayer = false;

public void HandleButtonClick (object sender, EventArgs e)
{
    // Assuming you put a Tuple with row and column in button's Tag property
    var position = (Tuple<int, int>) ((Button) sender).Tag;
    var row = position.Item1;
    var column = position.Item2;

    // Sanity check
    Debug.Asset (sender == _buttons [row][column]);

    bool won = _game.MakeMove (row, column, HumanPlayer);

    if (won) {
        MessageBox.Show ("You won.");
    }

    RefreshButtons ();
}

void RefreshButtons ()
{
    for (var i = 0; i < 3; i++) {
        for (var j = 0; j < 3; j++) {
            var btn = _buttons [i][j];
            var cell = _game.GetCell (i, j);

            btn.Enabled = !cell.HasValue;
            btn.Text = cell.HasValue
                ? (cell.Value ? "X" : "O")
                : string.Empty;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你的AI也应该调用MakeMove和呼吁做基于信息的计算GetRow,GetColumnGetDiagonal.

我没有检查代码,它只是一个草图.(但是控制台项目运行得很好.)