棋盘游戏的 int 数组
Posted
技术标签:
【中文标题】棋盘游戏的 int 数组【英文标题】:Int array for board game 【发布时间】:2021-12-07 16:33:09 【问题描述】: int[,] board =
0, 0, 2, 0, 0, 0, 0, 0,
0, 0, 0, 1, 2, 2, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 2, 1, 0, 0,
0, 0, 1, 1, 2, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
;
这是我的数组代码。我想将所有 0 显示为“”,将所有 1 显示为“X”,将所有 2 显示为“O”。我这样做是为了制作一个棋盘游戏,并希望输出看起来像这样。 Array output wanted
我尝试过创建一个名为“DisplayBoard”的方法,这就是我目前所拥有的。
static void DisplayBoard(int[,] board)
Write(" ");
for (int i = 0; i < BOARD_SIZE; ++i)
Write(" 0 ", i);
Console.WriteLine();
Console.WriteLine(" ---------------------------------");
for (int row = 0; row < BOARD_SIZE; row++)
Write(" 0 |",row);
for (int col = 0; col < BOARD_SIZE; ++col)
Console.Write(String.Format(" 0 |", board[row, col]));
Console.WriteLine();
Console.WriteLine(" ---------------------------------");
WriteLine();
【问题讨论】:
那么问题出在哪里? 【参考方案1】:你可以像这样定义一个局部函数:
static string getMark(int n) => n switch
1 => "X",
2 => "O",
_ => " "
;
并替换你的这一行
Console.Write(String.Format(" 0 |", board[row, col]));
与
Console.Write($" getMark(board[row, col]) |");
【讨论】:
【参考方案2】:让我们概括问题并将任何board
转换为string
:
private static string BoardToString(int[,] board)
if (null == board || board.GetLength(0) <= 0 || board.GetLength(1) <= 0)
return "";
StringBuilder sb = new StringBuilder();
string marks = " XO";
string line = " " + new string('-', board.GetLength(1) * 4 - 1);
sb.Append(line);
for (int r = 0; r < board.GetLength(0); ++r)
sb.AppendLine();
for (int c = 0; c < board.GetLength(1); ++c)
sb.Append(" | ");
sb.Append(marks[board[r, c]]);
sb.AppendLine(" | ");
sb.Append(line);
return sb.ToString();
那你就可以放
static void DisplayBoard(int[,] board)
Console.Write(BoardToString(board));
演示:
int[,] board =
0, 0, 2, 0, 0, 0, 0, 0,
0, 0, 0, 1, 2, 2, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 2, 1, 0, 0,
0, 0, 1, 1, 2, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
;
DisplayBoard(board);
结果:
-------------------------------
| | | O | | | | | |
-------------------------------
| | | | X | O | O | | |
-------------------------------
| | | | | X | | | |
-------------------------------
| | | | X | O | X | | |
-------------------------------
| | | X | X | O | | | |
-------------------------------
| | | | | O | | | |
-------------------------------
| | | | | O | | | |
-------------------------------
| | | | | X | | | |
-------------------------------
【讨论】:
【参考方案3】:图块将成为您程序中非常重要的对象。所以你不妨给它一个适当的类。如果你给它一个类,你就有一个自然的地方来放置将它转换为各种格式的代码。在您的情况下,您需要能够将其从整数转换为字符串或字符。
class Tile
protected readonly int _value;
public Tile(int integerValue)
if (integerValue < 0 || integerValue >= _map.Length) throw new ArgumentException("Tile value invalid");
_value = integerValue;
public override string ToString() => ((char)this).ToString();
static char[] _map = new char[] ' ', 'X', 'O' ;
public static implicit operator char(Tile input) => _map[input._value];
public static implicit operator Tile(int input) => new Tile(input);
一旦完成,剩下的逻辑就更轻了:
var board = new Tile[,]
0, 0, 2, 0, 0, 0, 0, 0,
0, 0, 0, 1, 2, 2, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 2, 1, 0, 0,
0, 0, 1, 1, 2, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0,
;
for (var row = 0; row < board.GetUpperBound(0); row++)
for (var column = 0; column < board.GetUpperBound(1); column++)
Console.Write("|0", board[row, column]);
Console.WriteLine("|");
输出:
| | |O| | | | |
| | | |X|O|O| |
| | | | |X| | |
| | | |X|O|X| |
| | |X|X|O| | |
| | | | |O| | |
| | | | |O| | |
【讨论】:
以上是关于棋盘游戏的 int 数组的主要内容,如果未能解决你的问题,请参考以下文章