怎么避免呢?简化代码:
static void Main()
{
int[] board = { 1 };
CheckMove(board);
//board = 2
}
static void CheckMove(int[] board)
{
board[0] = 2;
}
Run Code Online (Sandbox Code Playgroud)
完整代码(WIP):
enum Sym
{
E, X, O
}
class Program
{
static void Main(string[] args)
{
Sym[,] board = new Sym[3,3];
int x, y;
while (End(board) == 2)
{
Display(board);
Console.WriteLine("Make your move - column: ");
x = Convert.ToInt32(Console.ReadLine())-1;
Console.WriteLine("Make your move - row: ");
y = Convert.ToInt32(Console.ReadLine())-1;
board[y, x] = Sym.X;
int[,] chances = new int[3, 3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
chances[i, j] = CheckMove(board, i, j, true);
}
}
Display(board);
Console.WriteLine("GG");
Console.WriteLine("____________________________________________________________");
Console.ReadLine();
}
static void Display(Sym[,] board) //Displays whole board
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine($"{board[i,0]} {board[i,1]} {board[i,2]}");
}
}
static int End(Sym[,] board) //Chcecks if the game shall end (-1 player won) (0 tie) (1 cpu won) (2 game in progress)
{
bool Full = true;
for (int i = 0; i < 3; i++)
{ //This part is currently broken
if (board[i, 0] == Sym.E || board[i, 1] == Sym.E || board[i, 2] == Sym.E) Full = false;
if (board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2] && board[i, 0] == Sym.X) return -1;
if (board[0, i] == board[1, i] && board[1, i] == board[2, i] && board[0, i] == Sym.X) return -1;
if (board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2] && board[i, 0] == Sym.O) return 1;
if (board[0, i] == board[1, i] && board[1, i] == board[2, i] && board[0, i] == Sym.O) return 1;
}
if (board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2] && board[0, 0] == Sym.X) return -1;
if (board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2] && board[0, 0] == Sym.O) return 1;
if (board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2] && board[2, 0] == Sym.X) return -1;
if (board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2] && board[2, 0] == Sym.O) return 1;
if (Full == true) return 0;
return 2;
}
static int CheckMove(Sym[,] board, int a, int b, bool cpuTurn) //Check how good subjected move is
{
if (board[a, b] == Sym.E)
if (cpuTurn == true) board[a, b] = Sym.O;
else board[a, b] = Sym.X;
else return 0;
if (End(board) != 2) return End(board);
int Value = 0;
for (int m = 0; m < 3; m++)
for(int n = 0; n < 3; n++)
{
Value += CheckMove(board, m, n, !cpuTurn);
}
return Value;
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,您在2D数组Sym[,]
方面遇到了问题
static int CheckMove(Sym[,] board, int a, int b, bool cpuTurn)
Run Code Online (Sandbox Code Playgroud)
方法; 由于board
是二维数组,典型的解决方案board.ToArray()
不起作用(它们甚至无法编译)。尝试一下Clone()
实例board
:
// Let's rename board into value...
static int CheckMove(Sym[,] value, int a, int b, bool cpuTurn) {
// ... in order to preserve all the other code:
// we are now working with the copy of the passed board
Sym[,] board = value.Clone() as Sym[,];
// Your code from CheckMove here
...
}
Run Code Online (Sandbox Code Playgroud)
既然Sym
是a enum
(即值类型),浅拷贝就足够了
归档时间: |
|
查看次数: |
111 次 |
最近记录: |