如何确保井字游戏的玩家不会在 C# 中填充已填充的板



我正试图独自制作一款Tic-Tac-Toe游戏,我制作了包括谁赢了,如果有没有平局,问题是我没能找到一种不让任何玩家都会覆盖2D阵列中的特定位置
在主程序中,我使用了一个循环来填充每个玩家的棋盘,正如我所说我努力想办法不让玩家意外地填满一块已经填满的木板请帮忙,谢谢以下是控制台应用程序中OOP中的代码

class Player
{
public int player1 { get; set; }
public int player2 { get; set; }
public int GetPlayer1(Board board)
{
while (true)
{
Console.Write("Player 1,chose from the above numbers to Print X on the specific position: ");
player1 = int.Parse(Console.ReadLine());
if (player1 < 0 || player1 > 9)
{
Console.WriteLine("Try again and enter a valid number");
}

else
{
break;
}
}

return player1;
}
public int GetPlayer2(Board board)
{
while (true)
{
Console.Write("Player 2,chose from the above numbers to Print O on the specific position: ");
player2 = int.Parse(Console.ReadLine());
if (player2 < 0 || player2 > 9)
{
Console.WriteLine("Try again and enter a valid number");
}

else
{
break;
}
}
return player2;
}

class Board
{
public Player players = new Player();
public readonly char[,] gamee = new char[3, 3];

public char GetStateOfFirstPlayer(Board board)
{


players.GetPlayer1(board);

return players.player1 switch
{
1 => gamee[0, 0] = 'X',
2 => gamee[0, 1] = 'X',
3 => gamee[0, 2] = 'X',
4 => gamee[1, 0] = 'X',
5 => gamee[1, 1] = 'X',
6 => gamee[1, 2] = 'X',
7 => gamee[2, 0] = 'X',
8 => gamee[2, 1] = 'X',
9 => gamee[2, 2] = 'X',
_ => '0',
};


}
public char GetStateOfSecondPlayer(Board board)
{



players.GetPlayer2(board);
return players.player2 switch
{
1 => gamee[0, 0] = 'O',
2 => gamee[0, 1] = 'O',
3 => gamee[0, 2] = 'O',
4 => gamee[1, 0] = 'O',
5 => gamee[1, 1] = 'O',
6 => gamee[1, 2] = 'O',
7 => gamee[2, 0] = 'O',
8 => gamee[2, 1] = 'O',
9 => gamee[2, 2] = 'O',
_ => '0',
};

}
public void PrintBoard()
{
Console.WriteLine($" {gamee[0, 0]}   |{gamee[0, 1]}   |{gamee[0, 2]}");
Console.WriteLine($" {gamee[1, 0]}   |{gamee[1, 1]}   |{gamee[1, 2]}");
Console.WriteLine($" {gamee[2, 0]}   |{gamee[2, 1]}   |{gamee[2, 2]}");

}
static public void BoardNumbers()
{
Console.WriteLine($" (1)   |(2)   |(3)");
Console.WriteLine($" (4)   |(5)   |(6)");
Console.WriteLine($" (7)   |(8)   |(9)");
}


}

WinCcondition类{

static public bool IsFirstPlayerWon(Board board)
{
for (var x = 0; x < board.gamee.GetLength(0); x++)
{
for (var y = 0; y < board.gamee.GetLength(1); y++)
{
if (board.gamee[x, 0] == 'X' && board.gamee[x, 1] == 'X' && board.gamee[x, 2] == 'X') return true;
if (board.gamee[0, y] == 'X' && board.gamee[1, y] == 'X' && board.gamee[2, y] == 'X') return true;
if (board.gamee[0, 0] == 'X' && board.gamee[1, 1] == 'X' && board.gamee[2, 2] == 'X') return true;
if (board.gamee[0, 2] == 'X' && board.gamee[1, 1] == 'X' && board.gamee[2, 0] == 'X') return true;
}
}
return false;
}
static public bool IsSecondPlayerWon(Board board)
{
for (var x = 0; x < board.gamee.GetLength(0); x++)
{
for (var y = 0; y < board.gamee.GetLength(1); y++)
{
if (board.gamee[x, 0] == 'O' && board.gamee[x, 1] == 'O' && board.gamee[x, 2] == 'O') return true;
if (board.gamee[0, y] == 'O' && board.gamee[1, y] == 'O' && board.gamee[2, y] == 'O') return true;
if (board.gamee[0, 0] == 'O' && board.gamee[1, 1] == 'O' && board.gamee[2, 2] == 'O') return true;
if (board.gamee[0, 2] == 'O' && board.gamee[1, 1] == 'O' && board.gamee[2, 0] == 'O') return true;
}
}
return false;
}
static public bool IsDraw(Board board)
{
for (var x = 0; x < 3; x++)
{
for (var y = 0; y < 3; y++)
{
if (board.gamee[x, y] == '') return false;// default value of char
}
}
return true;
}
static void Main(string[] args)
{

Board board = new Board();
Board.BoardNumbers();
Player player = new Player();
for(var x=1;x<=6;x++)
{
if(WinCondition.IsDraw(board)==true)
{
Console.WriteLine("Draw");
}
board.GetStateOfFirstPlayer(board);
board.PrintBoard();
if(WinCondition.IsFirstPlayerWon(board)==true)
{
Console.WriteLine("First Player Won");
}
board.GetStateOfSecondPlayer(board);
board.PrintBoard();
if (WinCondition.IsSecondPlayerWon(board) == true)
{
Console.WriteLine("Second Player Won");
}
}

开始不错,编程很困难,但您已经走了很长的路。

我为您创建了一个Index类,它基本上允许根据玩家输入的数字计算游戏玩家数组的数组索引。如果玩家输入3,则x=2,y=0。因此,不再需要开关状态,更容易检查电路板在该位置是否空闲。

现在的新工作流程是这样的:

  1. 从玩家处获取号码
  2. 获取玩家输入的数字的数组索引
  3. 请检查此时板是否为空。如果没有,请转至步骤1
  4. 如果是空的,将玩家放在那里

这里有一个新的Index类,它有助于计算

class Index
{
public int x;
public int y;
public Index(int x, int y)
{
this.x = x;
this.y = y;
}
public static Index GetIndexFromNumber(int number)
{
// remove one from the number, required for the following calculation
int zeroBasedNumber = number - 1;
// Use modulo board width to get the remainder, this is the x value
int x = zeroBasedNumber % 3;
// Use division board height to get the row, this is the y value
int y = zeroBasedNumber / 3;
return new Index(x, y);
}
}

这是新的GetStateOfPlayer方法,它可以用于两个玩家

public void GetStateOfPlayer(Board board, char playerChar)
{
Index index;
do
{
players.GetPlayer1(board);
index = Index.GetIndexFromNumber(players.player1);
Console.WriteLine();
if (gamee[index.y, index.x] != '')
{
Console.WriteLine("You can't place your mark there");
}
// Repeat until the field is actually empty
} while (gamee[index.y, index.x] != '');
// Place it when it's clear
gamee[index.y, index.x] = playerChar;
}

现在的主要方法如下:

static void Main(string[] args)
{
Board board = new Board();
Board.BoardNumbers();
Player player = new Player();
for (var x = 1; x <= 6; x++)
{
if (IsDraw(board) == true)
{
Console.WriteLine("Draw");
}
board.GetStateOfPlayer(board, 'X');
board.PrintBoard();
if (IsFirstPlayerWon(board) == true)
{
Console.WriteLine("First Player Won");
}
board.GetStateOfPlayer(board, 'O');
board.PrintBoard();
if (IsSecondPlayerWon(board) == true)
{
Console.WriteLine("Second Player Won");
}
}
}

相关内容

最新更新