我想在 C# 中检查 3x3 2D 矩阵上井字游戏的胜利。我有以下结构:
public struct GridCoordinates {
public int row;
public int column;
}
为了检查棋盘的所有行、列和对角线,我可以执行以下操作。我可以创建一个预先计算好的 List<List<GridCoordinates>>
其中内部列表是 3 个坐标的集合,每个坐标代表一行、列或对角线。但是当我想到那个 List<List<GridCoordinates>>
的初始化需要多长时间将使用 new
s,我开始觉得在C#中应该有更好的方法。请建议如何优雅地填充该预先计算的坐标。
我的 BG 来自 C++,在那里我可以做这样的事情:
#include <iostream>
#include <vector> using namespace std;
struct GridCoordinates {
int i;
int j;
};
vector<vector<GridCoordinates>> vec{
{{0,0}, {0,1}, {0, 2}}
};
int main() { std::cout << vec[0][2].j; }
优雅吧?
最佳答案
您可以像这样将 (X,Y) 元组的二维数组声明为类中的一个字段:
static readonly (int X, int Y)[,] winningLines =
{
{(0, 0), (0, 1), (0, 2)},
{(0, 0), (1, 1), (2, 2)},
{(0, 0), (1, 0), (2, 0)},
{(0, 1), (1, 1), (2, 1)},
{(0, 2), (1, 2), (2, 2)},
{(1, 0), (1, 1), (1, 2)},
{(2, 0), (1, 2), (2, 2)},
{(0, 2), (1, 1), (2, 0)}
};
这使得声明数组变得相当简洁。但是,访问它仍然相当冗长,例如:
using System;
namespace Demo
{
class Program
{
static void Main()
{
Console.WriteLine(isWinFor('X')); // false
Console.WriteLine(isWinFor('O')); // false
board[0, 0] = 'X';
board[1, 1] = 'X';
board[2, 2] = 'X';
Console.WriteLine(isWinFor('X')); // true
Console.WriteLine(isWinFor('O')); // false
}
static bool isWinFor(char player)
{
for (int line = 0; line < winningLines.GetUpperBound(0); ++line)
{
bool won = true;
for (int coord = 0; coord < 3; ++coord)
{
var p = winningLines[line, coord];
if (board[p.X, p.Y] != player)
won = false;
}
if (won)
return true;
}
return false;
}
static readonly char[,] board = new char[3,3];
static readonly (int X, int Y)[,] winningLines =
{
{(0, 0), (0, 1), (0, 2)},
{(0, 0), (1, 1), (2, 2)},
{(0, 0), (1, 0), (2, 0)},
{(0, 1), (1, 1), (2, 1)},
{(0, 2), (1, 2), (2, 2)},
{(1, 0), (1, 1), (1, 2)},
{(2, 0), (1, 2), (2, 2)},
{(0, 2), (1, 1), (2, 0)}
};
}
}
(注意:元组支持需要 c#7.0 或更高版本。)
关于c# - C# 中 List<List<Coordinate>> 的优雅初始化方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679556/