看到(在一个2人的)Tic Tac Toe赢得比赛的最佳方式是什么?现在我正在使用类似于以下内容的东西:
if (btnOne.Text == "X" && btnTwo.Text == "X" && btnThree.Text == "X")
{
MessageBox.Show("X has won!", "X won!");
return;
}
else
// I'm not going to write the rest but it's really just a bunch
// if statements.
那么如何摆脱多重if?
答案 0 :(得分:11)
一些事情:
rowSum == 3 || columnSum == 3 || diagnolSum == 3
..?
答案 1 :(得分:3)
如果将按钮存储在多维数组中,可以编写一些扩展方法来获取行,列和对角线。
public static class MultiDimensionalArrayExtensions
{
public static IEnumerable<T> Row<T>(this T[,] array, int row)
{
var columnLower = array.GetLowerBound(1);
var columnUpper = array.GetUpperBound(1);
for (int i = columnLower; i <= columnUpper; i++)
{
yield return array[row, i];
}
}
public static IEnumerable<T> Column<T>(this T[,] array, int column)
{
var rowLower = array.GetLowerBound(0);
var rowUpper = array.GetUpperBound(0);
for (int i = rowLower; i <= rowUpper; i++)
{
yield return array[i, column];
}
}
public static IEnumerable<T> Diagonal<T>(this T[,] array,
DiagonalDirection direction)
{
var rowLower = array.GetLowerBound(0);
var rowUpper = array.GetUpperBound(0);
var columnLower = array.GetLowerBound(1);
var columnUpper = array.GetUpperBound(1);
for (int row = rowLower, column = columnLower;
row <= rowUpper && column <= columnUpper;
row++, column++)
{
int realColumn = column;
if (direction == DiagonalDirection.DownLeft)
realColumn = columnUpper - columnLower - column;
yield return array[row, realColumn];
}
}
public enum DiagonalDirection
{
DownRight,
DownLeft
}
}
如果您使用包含3行和3列的TableLayoutPanel
,则可以轻松地以可编程方式创建按钮并将其存储到Button[3, 3]
数组中。
Button[,] gameButtons = new Button[3, 3];
for (int row = 0; column <= 3; row++)
for (int column = 0; column <= 3; column++)
{
Button button = new Button();
// button...
gameLayoutPanel.Items.Add(button);
gameButtons[row, column] = button;
}
并检查获胜者:
string player = "X";
Func<Button, bool> playerWin = b => b.Value == player;
gameButtons.Row(0).All(playerWin) ||
// ...
gameButtons.Column(0).All(playerWin) ||
// ...
gameButtons.Diagonal(DiagonalDirection.DownRight).All(playerWin) ||
// ...
答案 2 :(得分:2)
另一个简单的方法是将winnable位置保存为数组中的数据并使用循环来检查所有可能的获胜条件而不是多个ifs语句
// winnable positions
var winnables = new[] {
"012",
"345",
"678",
"036",
"147",
"258",
"048",
"246"
};
// extracted from btnOne Two Three....
var gameState = new[] { "X", "O", "X", "whatever" };
string winner = null;
// check each winnable positions
foreach (var position in winnables) {
var pos1 = int.Parse(position[0].ToString());
var pos2 = int.Parse(position[1].ToString());
var pos3 = int.Parse(position[2].ToString());
if (gameState[pos1] == gameState[pos2] &&
gameState[pos2] == gameState[pos3])
winner = gameState[pos1];
}
// do we have a winner?
if (!string.IsNullOrEmpty(winner))
/* we've got a winner */
基本上,不要使用btnOne btnTwo btnThree,使用适当的按钮数组或以更易于访问的格式保存游戏状态的数组,并且计算起来会更容易。
答案 3 :(得分:0)
我倾向于做这样的事情:
bool x_wins =
Enumerable
.Range(0, 3)
.SelectMany(i => new Func<int, string>[] { x => array[i, x], x => array[x, i] })
.Concat(new Func<int, string>[] { x => array[x, x], x => array[2 - x, x], })
.Where(f => String.Concat(Enumerable.Range(0, 3).Select(x => f(x))) == "XXX")
.Any();