如何确保井字游戏的玩家不会在 C# 中填满已经填满的棋盘
How to make sure that players of tic tac toe game don't fill an already filled board in C#
我正在尝试自己制作井字游戏,我制作了所有内容,包括
谁赢了,如果有平局,问题是我没能找到不让的方法
任何玩家覆盖二维数组中的特定位置
在主程序中,我使用了一个循环来填充每个玩家的棋盘,正如我所说
我努力寻找一种方法来避免让玩家不小心填满已经填满的棋盘
请帮忙,谢谢
这是控制台应用程序中 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)");
}
}
class 胜利条件
{
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] == '[=11=]') 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");
}
}
好的开始,编程很难,但你已经走了很远。
我为您创建了一个索引 class,它基本上允许根据玩家输入的数字计算游戏数组的数组索引。如果玩家输入 3,则得到 x = 2,y=0。因此不再需要switch语句,也更容易检查该位置的电路板是否空闲。
现在新的工作流程是这样的:
- 从玩家处获取号码
- 获取玩家输入数字的数组索引
- 检查此时板是否为空。如果不是,转到步骤 1
- 如果是空的,将播放器放在那里
这是一个有助于计算的新索引 class
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] != '[=11=]')
{
Console.WriteLine("You can't place your mark there");
}
// Repeat until the field is actually empty
} while (gamee[index.y, index.x] != '[=11=]');
// 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");
}
}
}
我正在尝试自己制作井字游戏,我制作了所有内容,包括
谁赢了,如果有平局,问题是我没能找到不让的方法
任何玩家覆盖二维数组中的特定位置
在主程序中,我使用了一个循环来填充每个玩家的棋盘,正如我所说
我努力寻找一种方法来避免让玩家不小心填满已经填满的棋盘
请帮忙,谢谢
这是控制台应用程序中 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)");
}
}
class 胜利条件 {
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] == '[=11=]') 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");
}
}
好的开始,编程很难,但你已经走了很远。
我为您创建了一个索引 class,它基本上允许根据玩家输入的数字计算游戏数组的数组索引。如果玩家输入 3,则得到 x = 2,y=0。因此不再需要switch语句,也更容易检查该位置的电路板是否空闲。
现在新的工作流程是这样的:
- 从玩家处获取号码
- 获取玩家输入数字的数组索引
- 检查此时板是否为空。如果不是,转到步骤 1
- 如果是空的,将播放器放在那里
这是一个有助于计算的新索引 class
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] != '[=11=]')
{
Console.WriteLine("You can't place your mark there");
}
// Repeat until the field is actually empty
} while (gamee[index.y, index.x] != '[=11=]');
// 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");
}
}
}