Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TicTacToe if statement not working for player 2

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading