Essentially, when a button is pressed the randomMove() method is called, this is mean’t to find a empty square in TicTacToe and then assign an “O” to it, the problem is that it then doesn’t break out of the loop as at least two squares turn to “O”.
I’m not sure where I’ve gone wrong but I don’t doubt it’s a simple solution.
private void randomMove()
{
for (int i = 0; i < 3; i++)
{
for (int a = 0; a < 3; a++)
{
if (Board[a,i] == "")
{
Board[a,i] = "O";
Temp = i + a;
compMove(Temp);
break;
}
}
}
}
private void compMove(int Temp)
{
switch (Temp)
{
case 0:
btn1.Text = "O";
break;
case 1:
btn2.Text = "O";
break;
case 2:
btn3.Text = "O";
break;
case 3:
btn4.Text = "O";
break;
case 4:
btn5.Text = "O";
break;
case 5:
btn6.Text = "O";
break;
case 6:
btn7.Text = "O";
break;
case 7:
btn8.Text = "O";
break;
case 8:
btn9.Text = "O";
break;
}
hasWon();
}
Well, if I look on the code provided and, if I correctly understand what you want, the only thing you need here is a
return, like this:And also it’s better to change
compMovemethod to something like this, imo (pseudocode)