我正在尝试创建一个井字游戏。我设法创建一个空板并要求用户输入,但我的符号没有显示在板上。
private class Player
{
public bool IsSquareTaken { get => GetsSquareAndVerifies(); }
public char Symbol { get; set; }
public int Row { get; set; }
public int Column { get; set; }
// creates a player and gives them a symbol
public Player(char symbol)
{
Symbol = symbol;
}
public Player()
{
}
public bool GetsSquareAndVerifies()
{
TicTacToeGame game = new TicTacToeGame();
while (true)
{
Console.WriteLine($"It is {Symbol}'s turn.\nWhich square would you like to play in? (1-9)");
if ((!int.TryParse(Console.ReadLine(), out int userInput) || userInput < 1 || userInput > 9))
{
Console.WriteLine("Please enter a valid number!");
return true;
}
Row = (userInput - 1) / 3;
Column = (userInput - 1) % 3;
if (game.Board[Row, Column] != '\0')
{
Console.WriteLine("Halt! That square is taken.");
return true;
} else if (game.Board[Row, Column] == '\0')
{
game.Board[Row, Column] = Symbol;
return false;
}
}
}
}
}
private class TicTacToeGame()
{
private char[,] board = new char[3, 3];
Player player = new('X');
Player player2 = new('O');
public char[,] Board { get => board; set => board = value; }
public void InitializeBoard()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Board[i, j] = '\0';
}
}
for (int r = 0; r < 3; r++)
{
Console.WriteLine(" {0} | {1} | {2} ", Board[r, 0], Board[r, 1], Board[r, 2]);
if (r < 2)
{
Console.WriteLine("----+----+-----");
}
}
}
public void DisplayPlacedSymbol()
{
while (true)
{
if (player.IsSquareTaken == false)
{
for (int r = 0; r < 3; r++)
{
Console.WriteLine(" {0} | {1} | {2} ", board[r, 0], board[r, 1], board[r, 2]);
if (r < 2)
{
Console.WriteLine("----+----+-----");
}
}
Console.WriteLine($"Symbol {player.Symbol} is placed at {player.Row}, {player.Column}");
}
if (player2.IsSquareTaken == false)
{
for (int r = 0; r < 3; r++)
{
Console.WriteLine(" {0} | {1} | {2} ", board[r, 0], board[r, 1], board[r, 2]);
if (r < 2)
{
Console.WriteLine("----+----+-----");
}
}
Console.WriteLine($"Symbol {player.Symbol} is placed at {player.Row}, {player.Column}");
}
}
}
}
internal partial class Program
{
static void Main(string[] args)
{
TicTacToeGame game = new TicTacToeGame();
game.InitializeBoard();
game.DisplayPlacedSymbol();
}
}
我期待符号出现,然后我可以修复任何古怪的格式。以前,我能够毫无问题地显示符号,但每次我做出新动作后它们都会消失。之后,我通过执行以下代码对代码的功能进行了一些更改:
Row = (userInput - 1) / 3;
Column = (userInput - 1) % 3;
在此之前,情况比较混乱,有很多 if else 语句。我曾经也有一个单独的 UserSymbol 字段,我打印了它而不是板,但我认为它让我检查当前方块是否为空......我真的很困惑下一步该做什么。我想在讨论胜利、平局和失败代码之前解决这个问题。
在你的
Player
,
public bool GetsSquareAndVerifies()
{
TicTacToeGame game = new TicTacToeGame();
// ...
// verify and puts circle in game
}
这意味着每当玩家输入时,您首先会生成一个全新的
TicTacToeGame
,然后将输入放入该新实例中。但是,那不是你的 game
中的 Main()
。因此,您正在绘制的游戏始终是空的,因为您从未将 O
或 X
放入其中,它们位于您在 Player
中生成的其他棋盘中,并且从未被绘制。
你可以重新考虑一下类结构。例如,您当前正在检查面板状态并更改
TicTacToeGame
中的 Player
。然而,Player
是一个Player,它不应该直接改变TicTacToeGame
,而是告诉TicTacToeGame
它做了什么,然后让TicTacToeGame
做出反应。
有一些单独的线,
Player currentPlayer = player;
while (true) {
if (!game.Verify(player.GetInput(game, out string playerSign))
{
ShowErrorCellOccupied();
continue;
}
game.PlaceSign(playerSign);
currentPlayer = currentPlayer == player ? player2 : player;
}
所以
Verify
和PlaceSign
在TicTacToeGame
上处理,而Player
只需要处理Input
。
玩得开心!