【发布时间】:2016-11-01 14:13:38
【问题描述】:
这是井字游戏的一部分。 checkmate 方法返回一个字符串,该字符串将被另一个函数(此处未显示)用于进行井字棋移动。
这是棋盘,玩家在最上面一排排了两块棋子:
public static char[] board = { '+', '+', 'E',
'A', 'S', 'D',
'Z', 'X', 'C' };
这是决定下一步行动的代码:
private static string checkmate()
{
List<char[]> dalist = new List<char[]>
{
new char[3] { board[3], board[4], board[5] },
new char[3] { board[0], board[4], board[8] },
new char[3] { board[2], board[4], board[6] },
new char[3] { board[1], board[4], board[7] },
new char[3] { board[0], board[1], board[2] },
new char[3] { board[6], board[7], board[8] },
new char[3] { board[0], board[3], board[6] },
new char[3] { board[2], board[5], board[8] }
};
foreach (var item in dalist)
{
if (item.Where(x => x == '+').Count() == 2)
return new string(item.Where(x => x != '+').ToArray());
else if (item.Where(x => x == '-').Count() == 2)
return new string(item.Where(x => x != '-').ToArray());
else if (item.Where(x => x == '+').Count() == 1)
return item.Where(x => x != '+').First().ToString();
else
{
Random random = new Random();
int rList = random.Next(0, 3);
int rPosition = random.Next(0, 2);
return dalist.ElementAt(rList).GetValue(rPosition).ToString();
}
}
return "AA";
}
该方法将单个字母作为字符串返回,该字符串对应于棋盘上的移动。
该方法通过创建 tic-tack-toe 中 8 种可能获胜模式的列表来分析棋盘,并根据棋盘上已有的内容对其进行测试
在 foreach 循环中按 ifelse 条件顺序工作的逻辑四个组件。 对于当前,该方法应返回“E”并在第一个 if 语句处中断循环。相反,程序会遍历整个循环并返回最后的 else 语句? 为什么不识别第一个 if 条件的匹配?
循环应该遍历 8 个列表项中的每一个,直到找到匹配项。
1) checkmate 如果有可用的棋子,它将返回获胜的棋子
2) 如果没有将死棋,则此步将检查对手是否有将死棋并阻挡路径
3)link two如果没有将死棋子获胜或阻止,这将放置第二个棋子以链接到获胜路径
4) 将首字母随机放置在前两轮的四个角之一或中间
【问题讨论】: