0

I'm new to writing code and I'm starting with a TicTacToe game to introduce myself. So far I have this:

namespace TicTacToe
{
    class Program
    {
        static void Main(string[] args)
        {

            string square1 = " ";
            string square2 = " ";
            string square3 = " ";
            string square4 = " ";
            string square5 = " ";
            string square6 = " ";
            string square7 = " ";
            string square8 = " ";
            string square9 = " ";

            bool playerIsX = true;
            string symbol = "X";

            while (true)
            {
                Console.WriteLine(" " + square1 + " | " + square2 + " | " + square3);
                Console.WriteLine("-----------");
                Console.WriteLine(" " + square4 + " | " + square5 + " | " + square6);
                Console.WriteLine("-----------");
                Console.WriteLine(" " + square7 + " | " + square8 + " | " + square9);

                if (playerIsX == true)
                {
                    Console.Write("X enter square: ");
                }
                else
                {
                    Console.Write("O enter square: ");
                }
                string square = Console.ReadLine();

                if (square == "1")
                {
                    square1 = symbol;
                }
                else if (square == "2")
                {
                    square2 = symbol;
                }
                else if (square == "3")
                {
                    square3 = symbol;
                }
                else if (square == "4")
                {
                    square4 = symbol;
                }
                else if (square == "5")
                {
                    square5 = symbol;
                }
                else if (square == "6")
                {
                    square6 = symbol;
                }
                else if (square == "7")
                {
                    square7 = symbol;
                }
                else if (square == "8")
                {
                    square8 = symbol;
                }
                else if (square == "9")
                {
                    square9 = symbol;
                }

                if (square1 == square2 & square2 == square3)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                else if (square1 == square4 & square4 == square7)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                if (square4 == square5 & square5 == square6)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                if (square7 == square8 & square8 == square9)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                if (square2 == square5 & square5 == square8)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                if (square3 == square6 & square6 == square9)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                if (square1 == square5 & square5 == square9)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }
                if (square3 == square5 & square5 == square7)
                {
                    Console.Clear();
                    Console.WriteLine("You Win!");
                    Console.ReadLine();
                    break;
                }

                Console.Clear();
                playerIsX = !playerIsX;
                if (playerIsX == true)
                {
                    symbol = "X";
                }
                else
                {
                    symbol = "O";
                }
            }

        }
    }
}

I know it's beefy but I'm only trying to get a basic understanding so I can move on from there. Currently for the winning I compare three values, but since they're all spaces, at the start of the game multiple possibilities already trigger the win. Is there a way to exclude a value, say space? Thanks.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • You are comparing strings with `==` but you should be using the `string.Equals()` method if you want to compare the contents of the strings. https://learn.microsoft.com/en-us/dotnet/api/system.string.equals?view=netcore-3.1 – Jacob Huckins Jun 15 '20 at 16:52
  • 10
    @JacobHuckins What? This is not Java. – GSerg Jun 15 '20 at 16:52
  • 3
    *Is there a way to exclude a value, say space* - `if (square1 != " " && square1 == square2 && square2 == square3)`? – GSerg Jun 15 '20 at 16:53
  • 3
    @JacobHuckins `String.Equals()` and `operator==` for `System.String` perform value comparison. If you want to see if two strings are the same object, you use `ReferenceEquals()`. – Marius Bancila Jun 15 '20 at 16:57
  • 2
    @JacobHuckins Please don't spread [misinformation](https://stackoverflow.com/a/1659107/11683). – GSerg Jun 15 '20 at 16:57
  • @JacobHuckins: https://stackoverflow.com/questions/1659097/why-would-you-use-string-equals-over – Xaqron Jun 15 '20 at 16:59
  • 1
    for tic tac toe, I guess the best way to write your code is using a matrix. Check the values for lines and columns + main diagonal and antidiagonal to see if it's a win. – Dragos Stoica Jun 15 '20 at 17:04
  • Does this answer your question? [Tic Tac Toe, Help/Determine Winner](https://stackoverflow.com/questions/21369135/tic-tac-toe-help-determine-winner) – GSerg Jun 15 '20 at 17:10
  • 2
    @GSerg @MariusBancila @Xaqron My bad, came from Java background and didn't read the top answer to the link I posted saying it was a common mistake Java people make. Now i'm kind of annoyed I've been typing out `.Equals()` every time I needed to compare strings. – Jacob Huckins Jun 15 '20 at 17:11

2 Answers2

1

In your specific situation, you have a common field in each if statement - a field is always evaluated twice as such:

if (square4 == square5 & square5 == square6)
{
    Console.Clear();
    Console.WriteLine("You Win!");
    Console.ReadLine();
    break;
}

square5 is evaluated against both square4, and square6, so we can definitely argue that if square5 is not equal to " ", this statement will not pass.

From there, you could easily just implement a quick check:

if (square4 == square5 & square5 == square6 && square5 != " ")
{
    Console.Clear();
    Console.WriteLine("You Win!");
    Console.ReadLine();
    break;
}

I would highly suggest you look into developing a more readable, and elegant piece of code for what you're doing though, good luck on your learning journey!

awoodhead
  • 87
  • 8
0

The quickest (not best) way to do it would be to check for a space in your comparison. That means this if (square1 == square2 & square2 == square3) would turn into if (square1 == square2 & square2 == square3 & square3 != " ")

schwechel
  • 305
  • 3
  • 10