How to avoid that? Simplified code:
static void Main()
{
int[] board = { 1 };
CheckMove(board);
//board = 2
}
static void CheckMove(int[] board)
{
board[0] = 2;
}
full code (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;
}
}
Copy an Array in C 1 Create a new array with a similar data type and size. 2 Use a loop to iterate through the original array. 3 Copy the ith element of the original array to the ith element of the new array. More ...
The function copy () is the user defined function which copies the elements of one array into another array. 2) The main () function calls the copy () by passing a,b arrays and n as arguments to the function copy ().
Thus, the different ways to do so in C programming are as follows: Read the array size and store it into the variable n. 2) Read the entered elements using scanf () function and store the elements into the array a [] using for loop fir (i=0;i<n;i++).Then all entered elements will be stored into the array a [].
In C++ you can also use memcpy if your array members are POD (that is, essentially types which you could also have used unchanged in C), but in general, memcpy will not be allowed. As others mentioned, the function to use is std::copy.
As far as I can see, you have problems with 2D array Sym[,]
in the
static int CheckMove(Sym[,] board, int a, int b, bool cpuTurn)
method; since board
is 2D array, typical solutions like board.ToArray()
don't work (they don't even compile). Try Clone()
the board
instance:
// 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
...
}
Since Sym
is a enum
(i.e. a value type), shallow copy is enough
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With