6 Replies - 293 Views - Last Post: 05 May 2014 - 07:04 PM Rate Topic: -----

#1 jafern32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-April 14

Tic-Tac-Toe C# If statement problem

Posted 30 April 2014 - 04:14 PM

Hello everyone, I am new to this website and i was hoping someone can shed some light for me on this program. I have tried countless ways to fix the issue i am having with this program. For the most part, the program works. The problem i am having is when "player 1" chooses "X" and if "player 2" tries to use the same box to put "O", it does not give the output error "Console.WriteLine("Warning: This box has already been used.");". Also, after completeing a game on whoever wins and you want to play again, the board does not reset. Your help is greatly appreciated.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string A = "1";
            string B = "2";
            string C = "3";
            string D = "4";
            string E = "5";
            string F = "6";
            string G = "7";
            string H = "8";
            string I = "9";
            string turn = "player1";
            string won = "false";
            string playAgain = "false";
            string player1, player2, selection;

            Console.WriteLine("Welcome to Tic-Tac-Toe");
            Console.WriteLine("");
            Console.WriteLine("Press any key to continue.");
            Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Please enter the name of the first player.");
            player1 = Console.ReadLine();
            Console.WriteLine("Please enter the name of the second player.");
            player2 = Console.ReadLine();



            Start: // goto statement reference http://msdn.microsoft.com/en-us/library/b34dt9cd.aspx
            Console.Clear();

            
            if (turn == "player1")
            {
                turn = "player2";
                Console.Clear();
                Console.WriteLine("Player: " + player1 + "'s turn!");
                Console.WriteLine("");


                //Drawing the board
                //1|2|3
                //4|5|6
                //7|8|9
                Console.WriteLine(A + "|" + B + "|" + C);
                Console.WriteLine(D + "|" + E + "|" + F);
                Console.WriteLine(G + "|" + H + "|" + I);
                Console.WriteLine("******************");
                Console.WriteLine("Please make a selection");
                selection = Console.ReadLine();

                if (selection == "1")
                {
                    if (A == "1")
                    {
                        A = "X";
                    }
                    else A = "O"; A = "X";
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }

                if (selection == "2")
                {
                    if (B == "2")
                    {
                        B = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "3")
                {
                    if (C == "3")
                    {
                        C = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "4")
                {
                    if (D == "4")
                    {
                        D = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "5")
                {
                    if (E == "5")
                    {
                        E = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "6")
                {
                    if (F == "6")
                    {
                        F = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "7")
                {
                    if (G == "7")
                    {
                        G = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "8")
                {
                    if (H == "8")
                    {
                        H = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "9")
                {
                    if (I == "9")
                    {
                        I = "X";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }

                if (A == B & B == C) // X|X|X
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (D == E & E == F) // X|X|X
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (G == H & H == I) // X|X|X
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                        
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (A == D & D == G)    // "X"  STRAIGHT UP & DOWN
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (B == E & E == H) // "X"  STRAIGHT UP & DOWN
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (C == F & F == I) // "X"  STRAIGHT UP & DOWN
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (G == E & E == C) // "X" DIAGONAL
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (A == E & E == I) // "X" DIAGONAL
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player1 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";

                    }
                    else
                    {
                        playAgain = "false";
                    }
                }



            }

            if (turn == "player2")
            {
                turn = "player1"; 
                Console.Clear();
                Console.WriteLine("Player: " + player2 + "'s turn!");
                Console.WriteLine("");
                Console.WriteLine(A + "|" + B + "|" + C);
                Console.WriteLine(D + "|" + E + "|" + F);
                Console.WriteLine(G + "|" + H + "|" + I);
                Console.WriteLine("******************");
                Console.WriteLine("Please make a selection");
                selection = Console.ReadLine();

                if (selection == "1")
                {
                    if (A == "1")
                    {
                        A = "O";
                    }
                    else 
                    {
                        selection = "1";
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }

                if (selection == "2")
                {
                    if (B == "2")
                    {
                        B = "O";
                    }
                    else
                    {
                        
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "3")
                {
                    if (C == "3")
                    {
                        C = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "4")
                {
                    if (D == "4")
                    {
                        D = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "5")
                {
                    if (E == "5")
                    {
                        E = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "6")
                {
                    if (F == "6")
                    {
                        F = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "7")
                {
                    if (G == "7")
                    {
                        G = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "8")
                {
                    if (H == "8")
                    {
                        H = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }
                if (selection == "9")
                {
                    if (I == "9")
                    {
                        I = "O";
                    }
                    else
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }
                }

                if (A == B & B == C) // O|O|O
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (D == E & E == F) // O|O|O
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (G == H & H == I) // O|O|O
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (A == D & D == G) // "O"  STRAIGHT UP & DOWN
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (B == E & E == H) // "O"  STRAIGHT UP & DOWN
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (C == F & F == I) // "O"  STRAIGHT UP & DOWN
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (G == E & E == C) // "O" DIAGONAL
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                    }
                    else
                    {
                        playAgain = "false";
                    }
                }
                if (A == E & E == I) // "O" DIAGONAL
                {
                    won = "true";
                    Console.WriteLine("Congratulations! " + player2 + " won!");
                    Console.WriteLine("");
                    Console.WriteLine("Do you want to play again? Y / N");
                    selection = Console.ReadLine();
                    if (selection == "Y")
                    {
                        playAgain = "true";
                        

                    }
                    else
                    {
                        playAgain = "false";
                    }
                   
                }


            }
            if (won == "false")
            {
             
                goto Start; // reference http://msdn.microsoft.com/en-us/library/b34dt9cd.aspx

            }
            if (playAgain == "true")
            {
                
                A = "1";
                B = "2";
                C = "3";
                D = "4";
                E = "5";
                F = "6";
                G = "7";
                H = "8";
                I = "9";
                goto Start; // reference http://msdn.microsoft.com/en-us/library/b34dt9cd.aspx
            }
            else
            {
                Console.WriteLine("Thank you for playing!");
            }
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe C# If statement problem

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: Tic-Tac-Toe C# If statement problem

Posted 30 April 2014 - 06:59 PM

First of all, do not use goto's. It's leads to writing spaghetti code. Use a more appropriate looping structure using the keywords while or do-while.

Second of all, your reference to goto's points to the C++ documentation. This is C#, remember?

Next, why are you using the string's won and playAgain to contain just the values "true" and "false"? You should actually use the a boolean variable. (eg. bool)
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: Tic-Tac-Toe C# If statement problem

Posted 30 April 2014 - 07:10 PM

Next: You have a lot of repetitive code. That is usually a code smell that indicates that you should be using functions.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: Tic-Tac-Toe C# If statement problem

Posted 30 April 2014 - 07:19 PM

View Postjafern32, on 30 April 2014 - 07:14 PM, said:

The problem i am having is when "player 1" chooses "X" and if "player 2" tries to use the same box to put "O", it does not give the output error "Console.WriteLine("Warning: This box has already been used.");".

There are two issues.

The first one is that for the case of player 1, and box 1, your code is incorrect for line 63-70. Your code that looks like:
if (A == "1")
{
    A = "X";
}
else A = "O"; A = "X";
{
    Console.WriteLine("Warning: This box has already been used.");
}



is actually read by the compiler to be
if (A == "1")
{
    A = "X";
}
else
{
    A = "O";
}

A = "X";

{
    Console.WriteLine("Warning: This box has already been used.");
}



The second issue is actually the reason why you are not seeing the warning. The problem is that you are actually printing the warning, and then your goto jumps back to Start where the next statement is to clear the screen and redraw the board.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: Tic-Tac-Toe C# If statement problem

Posted 30 April 2014 - 07:32 PM

View Postjafern32, on 30 April 2014 - 07:14 PM, said:

Also, after completeing a game on whoever wins and you want to play again, the board does not reset.

Actually, it does reset if you enter a "Y". Remember that your code is case-sensitive when it checks to reset.
Was This Post Helpful? 0
  • +
  • -

#6 jafern32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-April 14

Re: Tic-Tac-Toe C# If statement problem

Posted 05 May 2014 - 12:14 PM

Being as this is my first class in C#, so understanding on what you are telling me is wrong is hard to follow. I do apologize. But what i have do is deleted the coding that was incorrect and corrected it like this.
if (selection == "1")
                {
                    if (A == "1")
                    {
                        A = "X";
                    }
                    else 
                    {
                        Console.WriteLine("Warning: This box has already been used.");
                    }

I am using if statements because i haven't been introduced to bool and i am not too familiar with do-while and while. I am no longer using goto's as well. Thank you for pointing out the wrong information i am inputting. I am still not getting "Warning: This box has already been used." when i try to see if it works. How could i fix this?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,643
  • Joined: 05-May 12

Re: Tic-Tac-Toe C# If statement problem

Posted 05 May 2014 - 07:04 PM

Show us your new code.

Think about it. You are printing your warning to the console and then immediately clearing the screen. How can you conceivably expect to see the warning stay on the screen if you were clearing it? You should either pause for a few seconds after showing the warning, or ask the user to press a key after you show the warning.

View Postjafern32, on 05 May 2014 - 03:14 PM, said:

I am using if statements because i haven't been introduced to bool

Huh? When you write an if statement you are already using booleans:
if (boolean expression)
{
    // statements to run when boolean expression is true
}
else
{
    // statements to run when boolean expression is false
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1