I’m going to post the hole code so you can test yourself.
I don’t get why the victory condition for player 2 is not triggered in the if statement.
I know that is cause the if statement, maybe I wrote it wrong but is like 2 hourse that I’m on it and I don’t get what’s wrong with the statement.
Works fine for player 1.
private static void ticTacToe()
{
string[,] board = new string[3, 3];
for (int i = 0; i < board.GetLength(0); i++)
for (int j = 0; j < board.GetLength(1); j++)
board[i, j] = "-";
string player1 = "X";
string player2 = "O";
string currentPlayer;
int round = 1;
int aX = 0;
int aY = 0;
bool flag = false;
bool victory = false;
while (flag == false || victory == false)
{
if (round == 1)
{
currentPlayer = player1;
round++;
}
else
{
currentPlayer = player2;
round--;
}
bool valid = false;
while (valid == false)
{
bool validCord = false;
while (validCord == false)
{
Console.Write("Where to put(X Co)?: ");
aX = int.Parse(Console.ReadLine());
Console.Write("Where to put(Y Co)?: ");
aY = int.Parse(Console.ReadLine());
if(0 <= aX && aX <= 2 && 0 <= aY && aY <= 2)
{
validCord = true;
}
}
if (board[aY, aX] == "-")
{
valid = true;
board[aY, aX] = currentPlayer;
}
}
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
Console.Write(board[i, j]);
if (board[i, j] != "-")
{
flag = true;
}
else
{
flag = false;
}
}
Console.WriteLine();
}
//|||
if ((board[0, 0] == "X" && board[1, 0] == "X" && board[2, 0] == "X") || (board[0, 0] == "Y" && board[1, 0] == "Y" && board[2, 0] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
else if ((board[0, 1] == "X" && board[1, 1] == "X" && board[2, 1] == "X") || (board[0, 1] == "Y" && board[1, 1] == "Y" && board[2, 1] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
else if ((board[0, 2] == "X" && board[1, 2] == "X" && board[2, 2] == "X") || (board[0, 2] == "Y" && board[1, 2] == "Y" && board[2, 2] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
//---
else if ((board[0, 0] == "X" && board[0, 1] == "X" && board[0, 2] == "X") || (board[0, 0] == "Y" && board[0, 1] == "Y" && board[0, 2] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
else if ((board[1, 0] == "X" && board[1, 1] == "X" && board[1, 2] == "X") || (board[1, 0] == "Y" && board[1, 1] == "Y" && board[1, 2] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
else if ((board[2, 0] == "X" && board[2, 1] == "X" && board[2, 2] == "X") || (board[2, 0] == "Y" && board[2, 1] == "Y" && board[2, 2] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
//X
else if ((board[0, 0] == "X" && board[1, 1] == "X" && board[2, 2] == "X") || (board[0, 0] == "Y" && board[1, 1] == "Y" && board[2, 2] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
else if ((board[0, 2] == "X" && board[1, 1] == "X" && board[2, 0] == "X") || (board[0, 2] == "Y" && board[1, 1] == "Y" && board[2, 0] == "Y"))
{
victory = true;
Console.Write(victory + " Victory for player " + currentPlayer);
}
}
Console.ReadLine();
}
>Solution :
At the top of your code, you have these definitions for player1
and player2
:
string player1 = "X";
string player2 = "O";
However, all of your if
statements look like this:
if ((board[0, 0] == "X" && board[1, 0] == "X" && board[2, 0] == "X") || (board[0, 0] == "Y" && board[1, 0] == "Y" && board[2, 0] == "Y"))
You’re checking for X
and Y
instead of X
and O
, hence why the win condition is never triggered for player2
.