Jump to content

C# Very basic Tic-Tac-Toe

Hey guys! I'm new to the LTT forums and I'm new to C#, Watched 1 Bob Tabor course aprox ~7 hours long about a week and a half ago. I just wanted to see what you guys thought of my code, give me pointers etc. Like the title suggests its a simple Tic-Tac-Toe game that took around an hour or so to program.

Spoiler

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

namespace Tic_Tac_toe
{
    class PlayerStuff
    {

        public static string OponentChar { get; set; }
        public static string PlayerChar { get; set; }
    }
    class Program : PlayerStuff
    {
        public static void EndScreen() //After you win or lose the game
        {

            Console.Write("Would you like to play again? Y/N: ");
                string input = Console.ReadLine();
            if (input == "y" ||input == "Y" ||input == "yes" ||input == "Yes")
            {
                Console.WriteLine("Okay!");
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                GameBoard();
            }
            else if (input == "N" || input == "n" || input == "No" || input == "no")
            {
                Console.WriteLine("Okay see you next time!");
                Thread.Sleep(2000);
                Environment.Exit(0);
            }
        }
        public static void GameBoard()
        {
            string[] GameBoardArr = new string[9];
            GameBoardArr[0] = "0";
            GameBoardArr[1] = "1";
            GameBoardArr[2] = "2";
            GameBoardArr[3] = "3";
            GameBoardArr[4] = "4";
            GameBoardArr[5] = "5";
            GameBoardArr[6] = "6";
            GameBoardArr[7] = "7";
            GameBoardArr[8] = "8";
            string WinorLose = "";
            Random Numbergen = new Random();
            int enemy = Numbergen.Next(0, 9);
            int AvalSpace = 9;
            while(AvalSpace > 0)
            {
                Console.Clear();
                Console.WriteLine("           |               |           ");
                Console.WriteLine("     {0}     |       {1}       |     {2}     ", GameBoardArr[0], GameBoardArr[1], GameBoardArr[2]);
                Console.WriteLine("___________|_______________|___________");
                Console.WriteLine("           |               |           ");
                Console.WriteLine("     {0}     |       {1}       |     {2}     ", GameBoardArr[3], GameBoardArr[4], GameBoardArr[5]);
                Console.WriteLine("___________|_______________|___________");
                Console.WriteLine("           |               |           ");
                Console.WriteLine("     {0}     |       {1}       |     {2}     ", GameBoardArr[6], GameBoardArr[7], GameBoardArr[8]);
                Console.WriteLine("           |               |           ");
                Console.WriteLine("                                       ");
                
                Console.Write(" ENTER A COORDINATE TO PLACE AN {0}: ", PlayerChar);
                try
                {
                    int input = Convert.ToInt32(Console.ReadLine());
                    
                       
                    GameBoardArr[input] = PlayerChar;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message + " \nThere was an Error please try again...");
                    Console.Write("Press any Key to continue...");
                    Console.ReadKey();
                    
                }
      
                if (GameBoardArr[enemy] == OponentChar || GameBoardArr[enemy] == PlayerChar) //Checks to see if number Generated is availible
                {
                    do //If its not
                    {
                      
                        enemy = Numbergen.Next(0, 9);
                        
                        if (GameBoardArr[enemy] != PlayerChar)
                        {
                            GameBoardArr[enemy] = OponentChar;
                            break;

                        }
                        
                    }while(GameBoardArr[enemy] == OponentChar || GameBoardArr[enemy] == PlayerChar);
                  

                }
                else
                {
                    GameBoardArr[enemy] = OponentChar;
                }
                Thread.Sleep(500);
               if(     GameBoardArr[0] == PlayerChar && GameBoardArr[1] == PlayerChar && GameBoardArr[2] == PlayerChar //Checks to see if 3 are in a row top level
                    || GameBoardArr[0] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[8] == PlayerChar //Checks to see if 3 are in a row Diagonally from top left to bottom right 
                    || GameBoardArr[3] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[5] == PlayerChar //Checks to see if 3 are in a row middle level
                    || GameBoardArr[6] == PlayerChar && GameBoardArr[7] == PlayerChar && GameBoardArr[8] == PlayerChar //Checks to see if 3 are in a row bottom level
                    || GameBoardArr[6] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[2] == PlayerChar //Checks to see if 3 are in a row Diagonally from bottom left to top right
                    || GameBoardArr[1] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[7] == PlayerChar //Checks to see if 3 are in a row from top middle to bottom middle
                    || GameBoardArr[0] == PlayerChar && GameBoardArr[3] == PlayerChar && GameBoardArr[6] == PlayerChar //Checks to see if 3 are in a row from top left to bottom left
                    || GameBoardArr[2] == PlayerChar && GameBoardArr[5] == PlayerChar && GameBoardArr[8] == PlayerChar)//Checks to see if 3 are in a row from top right to bottom right
                {
                    WinorLose = "Won";
                    break;
                }
               else if (GameBoardArr[0] == OponentChar && GameBoardArr[1] == OponentChar && GameBoardArr[2] == OponentChar //Checks to see if 3 are in a row top level
                    || GameBoardArr[0] == OponentChar && GameBoardArr[4] == OponentChar && GameBoardArr[8] == OponentChar //Checks to see if 3 are in a row Diagonally from top left to bottom right 
                    || GameBoardArr[3] == OponentChar && GameBoardArr[4] == OponentChar && GameBoardArr[5] == OponentChar //Checks to see if 3 are in a row middle level
                    || GameBoardArr[6] == OponentChar && GameBoardArr[7] == OponentChar && GameBoardArr[8] == OponentChar //Checks to see if 3 are in a row bottom level
                    || GameBoardArr[6] == OponentChar && GameBoardArr[4] == OponentChar && GameBoardArr[2] == OponentChar //Checks to see if 3 are in a row Diagonally from bottom left to top right
                    || GameBoardArr[1] == OponentChar && GameBoardArr[4] == OponentChar && GameBoardArr[7] == OponentChar //Checks to see if 3 are in a row from top middle to bottom middle
                    || GameBoardArr[0] == OponentChar && GameBoardArr[3] == OponentChar && GameBoardArr[6] == OponentChar //Checks to see if 3 are in a row from top left to bottom left
                    || GameBoardArr[2] == OponentChar && GameBoardArr[5] == OponentChar && GameBoardArr[8] == OponentChar)//Checks to see if 3 are in a row from top right to bottom right)
                {
                    WinorLose = "Lost";
                    break;
                }
               else
                    AvalSpace--;

            }
            if (AvalSpace == 0)
            {
                Console.WriteLine("CATS GAME!");
                EndScreen();
            }
            else
                Console.Clear();
                Console.WriteLine("You {0}!", WinorLose);
            EndScreen();
           

        }
        public static void Init()
        {
            Console.WriteLine("+---TIC---TAC---TOE---+");
            Console.Write("+-By Thomas D. 'Zira'-+");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.Write("Would you like to be X or O: ");
        }
        static void Main(string[] args)
        {
            /*TODO 
             * make a game board 
             * make AI (Random num gen)
             * give each place on the game board a coord.
             * Choice between X or 0
             * 
             * Make it happen
             */
            
            string x = "x";
            string O = "o";
            int[] thingy = new int[8]; //This is the game board array *edit after the fact - No it is not lol
            Init();
            string input = Console.ReadLine();
            if (input == x)
            {
                PlayerChar = "X";
                OponentChar = "O";
                Console.WriteLine("Lets get to the game!");
                Console.Write("Press any key to comtinue...");
                Console.ReadKey();
                GameBoard();
            }
            else if (input == O)
            {
                PlayerChar = "O";
                OponentChar = "X";

                Console.WriteLine("Lets get to the game!");
                Console.Write("Press any key to comtinue...");
                Console.ReadKey();
                GameBoard();
            }
            else
            {
                Console.WriteLine("{0} is not a valid command", input);
                Console.ReadKey();
                Console.Clear();
                Init();
            }

            Console.ReadLine();
        }
    }
}
 

 Also if you have any experience with XNA/Monogame or Unity3D Please tell me where I should go to learn more about either (I'd like to get into game programming).

Link to comment
Share on other sites

Link to post
Share on other sites

I would recommend you using loops, when you initialize array and when you are checking for patterns (horizontal and vertical match) diagonal are little bit trickier but also easy. Drawing a board could be the hardest one, but I guess the funniest. 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks both of you!

Link to comment
Share on other sites

Link to post
Share on other sites

First and foremost, never, EVER compare strings using ==

To compare strings, such as the input, the best and shortest way of doing it is:

if(input.ToLower().Equals("y"))

Also, instead of doing

  if(     GameBoardArr[0] == PlayerChar && GameBoardArr[1] == PlayerChar && GameBoardArr[2] == PlayerChar //Checks to see if 3 are in a row top level
                    || GameBoardArr[0] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[8] == PlayerChar //Checks to see if 3 are in a row Diagonally from top left to bottom right 
                    || GameBoardArr[3] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[5] == PlayerChar //Checks to see if 3 are in a row middle level
                    || GameBoardArr[6] == PlayerChar && GameBoardArr[7] == PlayerChar && GameBoardArr[8] == PlayerChar //Checks to see if 3 are in a row bottom level
                    || GameBoardArr[6] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[2] == PlayerChar //Checks to see if 3 are in a row Diagonally from bottom left to top right
                    || GameBoardArr[1] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[7] == PlayerChar //Checks to see if 3 are in a row from top middle to bottom middle
                    || GameBoardArr[0] == PlayerChar && GameBoardArr[3] == PlayerChar && GameBoardArr[6] == PlayerChar //Checks to see if 3 are in a row from top left to bottom left
                    || GameBoardArr[2] == PlayerChar && GameBoardArr[5] == PlayerChar && GameBoardArr[8] == PlayerChar)//Checks to see if 3 are in a row from top right to bottom right 

try this:

          if(CheckWin(0 ,1,2 ) || Checkwin(0, 4, 8) || CheckWin(3, 4, 5) ... )

where CheckWin is:


private bool CheckWin(int first, int second, int third) {

    return GameBoardArr[first].ToLower().Equals(PlayerChar) && GameBoardArr[second].ToLower().Equals(PlayerChar) && GameBoardArr[third].ToLower().Equals(PlayerChar);

}

Not written in an IDE, sorry if there are any inconsistencies!

Just some minor suggestions!

Considering you're just starting out, your code is pretty good. The most dangerous part is comparing strings with ==. == compares references, not values.

----Let's break it apart!!-----

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, ManuAndrei said:

First and foremost, never, EVER compare strings using ==

To compare strings, such as the input, the best and shortest way of doing it is:


if(input.ToLower().Equals("y"))

Also, instead of doing


  if(     GameBoardArr[0] == PlayerChar && GameBoardArr[1] == PlayerChar && GameBoardArr[2] == PlayerChar //Checks to see if 3 are in a row top level
                    || GameBoardArr[0] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[8] == PlayerChar //Checks to see if 3 are in a row Diagonally from top left to bottom right 
                    || GameBoardArr[3] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[5] == PlayerChar //Checks to see if 3 are in a row middle level
                    || GameBoardArr[6] == PlayerChar && GameBoardArr[7] == PlayerChar && GameBoardArr[8] == PlayerChar //Checks to see if 3 are in a row bottom level
                    || GameBoardArr[6] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[2] == PlayerChar //Checks to see if 3 are in a row Diagonally from bottom left to top right
                    || GameBoardArr[1] == PlayerChar && GameBoardArr[4] == PlayerChar && GameBoardArr[7] == PlayerChar //Checks to see if 3 are in a row from top middle to bottom middle
                    || GameBoardArr[0] == PlayerChar && GameBoardArr[3] == PlayerChar && GameBoardArr[6] == PlayerChar //Checks to see if 3 are in a row from top left to bottom left
                    || GameBoardArr[2] == PlayerChar && GameBoardArr[5] == PlayerChar && GameBoardArr[8] == PlayerChar)//Checks to see if 3 are in a row from top right to bottom right 

try this:


          if(CheckWin(0 ,1,2 ) || Checkwin(0, 4, 8) || CheckWin(3, 4, 5) ... )

where CheckWin is:


private bool CheckWin(int first, int second, int third) {

    return GameBoardArr[first].ToLower().Equals(PlayerChar) && GameBoardArr[second].ToLower().Equals(PlayerChar) && GameBoardArr[third].ToLower().Equals(PlayerChar);

}

Not written in an IDE, sorry if there are any inconsistencies!

Just some minor suggestions!

Considering you're just starting out, your code is pretty good. The most dangerous part is comparing strings with ==. == compares references, not values.

Thanks a ton! I didn't realize I could just do if (input.ToLower().Equals("y")) instead of doing == "y" and == "Y". That was really helpful thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×