Also for your information, CheckForVictory( int[,]) returns an int based on who has won. So, if the computer wins it returns a 2 and returns a 1 for a user win. It returns a 0 if nobody has won yet. The computer is represented as the int 2 in ComputerMove and the user is represented as 1. Here is the recursion method.
private Tuple<int, int> ComputerMove(int[,] board , int player)
{
int[,] temp = board;
Tuple<int, int> t = null;
if (CheckForVictory(temp) == 1 || CheckForVictory(temp) == 2)
{
return t;
}
if (player == 1)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if (temp[i, j] == 0)
{
temp[i, j] = 1;
if (CheckForVictory(temp) == 1)
{
temp = _board;
}
t = ComputerMove(temp, 2);
}
}
}
return t;
}
else if (player == 2)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (temp[i, j] == 0) //board position is empty
{
temp[i, j] = 2;
t = ComputerMove(temp, 1);
if (t == null)
{
t = new Tuple<int, int>(i, j);
return t;
}
}
}
}
return t;
}
else
{
Tuple<int, int> nulled = null;
return nulled;
}
}
Also, here is my button click to make a move
private void uxSubmit_Click(object sender, EventArgs e)
{
Tuple<int, int> x = null;
if (CheckForVictory(_board) == 1)
{
MessageBox.Show("You Win");
}
else if(CheckForVictory(_board) == 0)
{
x = ComputerMove(_board, 2);
if (x == null)
{
int[,] temp = _board;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (temp[i, j] == 0)
{
_board[i, j] = 2;
}
}
}
if (CheckForVictory(_board) == 2)
{
MessageBox.Show("You Lose");
}
}
else
{
_board[x.Item1, x.Item2] = 2;
if (CheckForVictory(_board) == 2)
{
MessageBox.Show("You Lose");
}
}
}
}
}

New Topic/Question
Reply



MultiQuote




|