Advertisement

C# Tic-Tac-Toe Console Application

Started by October 10, 2013 08:07 PM
8 comments, last by jHaskell 11 years, 4 months ago

Hello, I have been working on an assignment for a programming class where I am to make a Tic-Tac-Toe Game with a C# Console Application for two human players.. I have it set up so that you place an X or O by typing in the number of the space. I've been working on it for a couple days and am now stuck on a part on how to alternate between X's and O's. Right now I have it just so that it only enters an X and was wondering how I would go about alternating between the for each turn. Let me show you what I have so far. I had to create a separate class since I kept getting this error where it would tell me I needed an object reference since the variables couldn't be static because they needed to be able to change, so I'll show that first:


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

namespace TicTacToe
{
    class Class1
    {
        //create array for tic tac toe game
        public string[,] tictactoeArray = new string[3, 3] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };

        public void Display()
        {
            Console.WriteLine("\n{0} {1} {2}\n", tictactoeArray[0, 0], tictactoeArray[0, 1], tictactoeArray[0, 2]);
            Console.WriteLine("{0} {1} {2}\n", tictactoeArray[1, 0], tictactoeArray[1, 1], tictactoeArray[1, 2]);
            Console.WriteLine("{0} {1} {2}\n\n", tictactoeArray[2, 0], tictactoeArray[2, 1], tictactoeArray[2, 2]);
        }
    }
}

And here is the main code


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

namespace TicTacToe
{
    class Program
    {
            
            //Note: I created another class for this assignment since I had problems with an error related to the array not being static and
            //if I made it static I don't think it can change.

        static void Main(string[] args)
        {

            //introduction
            Console.WriteLine("Welcome to Tic Tac Toe, X goes first.");
            Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
            //creating object necessary to prevent error since variables nonstatic
            Class1 Obj1;
            Obj1 = new Class1();
            //create tic tac toe board
            Obj1.Display();


            
            //be able to receive input
            string input = Console.ReadLine();
            //loop for constantly checking input
            do
            {
                //when user enters a number, change number to X or O
                if (input == "1")
                {
                    Obj1.tictactoeArray[0, 0] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "2")
                {
                    Obj1.tictactoeArray[0, 1] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "3")
                {
                    Obj1.tictactoeArray[0, 2] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "4")
                {
                    Obj1.tictactoeArray[1, 0] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "5")
                {
                    Obj1.tictactoeArray[1, 1] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "6")
                {
                    Obj1.tictactoeArray[1, 2] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }

                if (input == "7")
                {
                    Obj1.tictactoeArray[2, 0] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "8")
                {
                    Obj1.tictactoeArray[2, 1] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "9")
                {
                    Obj1.tictactoeArray[2, 2] = "X";
                    Obj1.Display();
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                
                //If an invalid input is put in, tell user to put in valid input
            if (input != "q" && input != "quit" && input != "1" && input != "2" && input != "3" && input != "4" && input != "5" && input != "6" && input != "7" && input != "8" && input != "9")
            {
                Console.WriteLine("Please enter an unoccupied number, or press q or quit to exit");
                Obj1.Display();
            }
               
                input = Console.ReadLine();
            } while (input != "q" || input != "quit");
            
            
            //if user enters quit or q, exit application
            if (input == "quit" || input == "q")
            {
                Environment.Exit(0);
            }
            


            
          
            //prevents application from closing when starts
            Console.ReadLine();
        }


        //Methods area
        //create a boolean to tell whether board is completed
        bool BoardComplete = false;
    }
}

Hopefully someone can give me some guidance on what I should do next.

You need a

bool isXToMove

which starts off true. Then have a

char symbolToFillInSpace = isXToMove ? "X" : "O";

replace occurences of "X" with symbolToFillInSpace and at the end of your do while loop you need to do

isXToMove = !IsXToMove;

If you don't know about the ternary operator ?: you can replace

char symbolToFillInSpace = isXToMove ? "X" : "O";

with

char symbolToFillInSpace;

if(isXToMove) symbolToFillInSpace = "X";

else symbolToFillInSpace = "O";

Does that help?

You could also work out the array index directly from the number typed (you need division and the modulus operator % as well to do that), which would shorten your code considerably and scales up for bigger boards (although coordinates as a pair may be more useful anyway then).

EDIT: Just realised this is a homework question, tut tut! Do your own homework! We're not supposed to answer homework questions here ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

Thank you for your help with the boolean suggestion. But I'm not sure why I need to use char instead of string, so I didn't put that in yet. When I implement the boolean to my program in the way I thought would be most logical, the program first shows the X and then immediately shows the board with the O in the same place before I have had a chance to enter a new number. I've attached a photo of what happens. I'm sure I'm probably just forgetting a small thing but do you know what that is? I'll post my updated code:


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

namespace TicTacToe
{
    class Program
    {
            
            //Note: I created another class for this assignment since I had problems with an error related to the array not being static and
            //if I made it static I don't think it can change.

        static void Main(string[] args)
        {

            //introduction
            Console.WriteLine("Welcome to Tic Tac Toe, X goes first.");
            Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
            //creating object necessary to prevent error since variables nonstatic
            Class1 Obj1;
            Obj1 = new Class1();
            //create tic tac toe board
            Obj1.Display();

            //create boolean for who's turn it is
            bool isXToMove = true;
            
            //be able to receive input
            string input = Console.ReadLine();
            //loop for constantly checking input
            do
            {
                //when user enters a number, change number to X
                if (input == "1" && isXToMove == true)
                {
                    Obj1.tictactoeArray[0, 0] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "2" && isXToMove == true)
                {
                    Obj1.tictactoeArray[0, 1] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "3" && isXToMove == true)
                {
                    Obj1.tictactoeArray[0, 2] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "4" && isXToMove == true)
                {
                    Obj1.tictactoeArray[1, 0] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "5" && isXToMove == true)
                {
                    Obj1.tictactoeArray[1, 1] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "6" && isXToMove == true)
                {
                    Obj1.tictactoeArray[1, 2] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }

                if (input == "7" && isXToMove == true)
                {
                    Obj1.tictactoeArray[2, 0] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "8" && isXToMove == true)
                {
                    Obj1.tictactoeArray[2, 1] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "9" && isXToMove == true)
                {
                    Obj1.tictactoeArray[2, 2] = "X";
                    Obj1.Display();
                    isXToMove = false;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }

                //when user enters a number, change number to O
                if (input == "1" && isXToMove == false)
                {
                    Obj1.tictactoeArray[0, 0] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "2" && isXToMove == false)
                {
                    Obj1.tictactoeArray[0, 1] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "3" && isXToMove == false)
                {
                    Obj1.tictactoeArray[0, 2] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "4" && isXToMove == false)
                {
                    Obj1.tictactoeArray[1, 0] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "5" && isXToMove == false)
                {
                    Obj1.tictactoeArray[1, 1] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "6" && isXToMove == false)
                {
                    Obj1.tictactoeArray[1, 2] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }

                if (input == "7" && isXToMove == false)
                {
                    Obj1.tictactoeArray[2, 0] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "8" && isXToMove == false)
                {
                    Obj1.tictactoeArray[2, 1] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                if (input == "9" && isXToMove == false)
                {
                    Obj1.tictactoeArray[2, 2] = "O";
                    Obj1.Display();
                    isXToMove = true;
                    Console.WriteLine("Pick a number to select a space.  To exit, type q or quit.");
                }
                
                //If an invalid input is put in, tell user to put in valid input
            if (input != "q" && input != "quit" && input != "1" && input != "2" && input != "3" && input != "4" && input != "5" && input != "6" && input != "7" && input != "8" && input != "9")
            {
                Console.WriteLine("Please enter an unoccupied number, or press q or quit to exit");
                Obj1.Display();
            } 
                
                //if user enters quit or q, exit application
            if (input == "quit" || input == "q")
            {
                Environment.Exit(0);
            }
               
                input = Console.ReadLine();
            } while (input != "q" || input != "quit");
            
            
           
            


            
          
            //prevents application from closing when starts
            Console.ReadLine();
        }


        //Methods area
        //create a boolean to tell whether board is completed
        bool BoardComplete = false;
    }
}

post-638351-138144054004.png

Yeah, I meant string instead of char. I just put that in to make you do your own homework! :)

Don't repeat the logic twice in your loop.

Anyway I have given you the information you need to make it work. And I'm outta here, since it is homework!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

What do you mean "don't repeat the logic twice in the loop"? I did one set for when it's X's and one set for when it's O's. And I'm not asking you to do my whole assignment, I just need help on this one part since we didn't really talk about changing booleans within loops in class.

What do you mean "don't repeat the logic twice in the loop"? I did one set for when it's X's and one set for when it's O's.

That's what I mean by "don't repeat the logic twice in the loop".

isXToMove = !isXToMove;

is the same as

if(isXToMove) isXToMove = false;

else isXToMove = true;

which is the same as

if(isXToMove == true) isXToMove = false;

else isXToMove = true;

i.e. if isXToMove is true, make it false, otherwise, it was false so make it true (it flips the true/false state of the boolean).

I've also left out another key detail which is to do with error logic, I'll let you work that out yourself.

And that's the last thing I'm saying! If a moderator sees this thread it may get closed because of the homework rule anyway!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

I think it's fine to help him because he's stuck on an issue. He's not asking us to do his homework for him. :) IMO.

Beginner in Game Development?  Read here. And read here.

 

Well I've given enough information to work it out, I reckon... including a suggestion to get rid of the 9 (now 18) very similar cases (involves working out whether input is a number, and if it is (and is >= 1 and <= 9), then do a calculation to work out the correct array indices, involving / and %). It would also simplify the error correction code, which looks nasty as is. Depends if they know how to parse an integer from a string in C# (I'd have to look it up myself).

I don't know how much knowledge the OP has anyway, I can't go suggesting using the ! operator to reverse bool values and using ?: if they haven't been taught them now can I ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I did this once in Delphi. I used a varible called TURN which was an integer.

If TURN = 1 then Put "X" at Location

If TURN = 2 then Put "Y" at Location

Of course the code was a little more involvwd, But I hope this gives you another idea.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

What do you mean "don't repeat the logic twice in the loop"? I did one set for when it's X's and one set for when it's O's. And I'm not asking you to do my whole assignment, I just need help on this one part since we didn't really talk about changing booleans within loops in class.

General rule of thumb, repetitive code is a bad design. As has already been shown, there's a better way to determine whether to put an X or an O in the selected cell, but there's also a better way to convert the selection made into the appropriate indices into your two dimensional array as well, with a little application of some basic math. Your code can be reduced to a fraction of it's current size and still get the job done.

This topic is closed to new replies.

Advertisement