Skip to content

Console Game App - Tic Tac Toe

F D

Asked Mar 15, 2011, 1:40 PM EDT1 answer
Share
LinkedInX
Hey Guys,

I'm trying to do some OOP programming and I am not quite sure if I'm going in the right direction.

I was writing TicTacToe program (Console App) and wanted to know your opinion. I'm not quite done yet, but would definitely

appreciate some feedback. 

First -any ideas as to how I can make this code better or efficient. Please point me in the right direction!

Second - Do I have the right idea as far as OOP.
 Class name - Game
   Member Vairables - X & Y coordinates, two dimensional array, and 2 constants (player1 = 'X' , player2 = 'O') 
  Methods - Player1_Move, Player2_Move, DisplayMoveOnGrid, CheckForWinner 

 Main Program - One object of type Game is created.

Third - How would I go about creating a computer move. A move that is randomoly generated. What should I be thinking?

The entire source code is attached. 

As always your time and input is greatly appreciated. 

Thanks.

Fabe.
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Here is my code  

    namespace Tic_Tac_Toe
    {
      class Game
      {
      private const int xCordinate = 3;
      private const int yCordinate = 3;
      private const char player1 = 'X';
      private const char player2 = 'O';
      char[,] grid = new char[xCordinate, yCordinate];
     

    //Method to instruct player 1 to make move
      public void Player1_Move()
      {
      Console.WriteLine("Player 1 enter X,Y coordinates for your move.");
      Console.Write("\n  Enter X coordinate: ");
      int xcord = Convert.ToInt32(Console.ReadLine());
      Console.Write("\n  Enter Y coordinate: ");
      int ycord = Convert.ToInt32(Console.ReadLine());
     
    xcord--;
    ycord--;
     
    if ((grid[xcord, ycord] != 'X') && (grid[xcord, ycord] != 'O'))
      {
      grid[xcord, ycord] = player1;
      DisplayMoveOnGrid();
      }
      else
      {
      Console.WriteLine("\n* * * * * Invalid move, try again.* * * * * \n\n");
      Player1_Move();
      } 
     
      }
     

    //Method to instruct player 2 to make move
      public void Player2_Move()
      {
      Console.WriteLine("\nPlayer 2 enter X,Y coordinates for your move.");
      Console.Write("\n  Enter X coordinate: ");
      int xcord = Convert.ToInt32(Console.ReadLine());
      Console.Write("\n  Enter Y coordinate: ");
      int ycord = Convert.ToInt32(Console.ReadLine());
     
    xcord--;
    ycord--;
     
    if ((grid[xcord, ycord] != 'X') && (grid[xcord, ycord] != 'O'))
      {
      grid[xcord, ycord] = player2;
      DisplayMoveOnGrid();
      }
      else
      {
      if ((grid[xcord, ycord] == 'X') && (grid[xcord, ycord] == 'O'))
      {
      Console.WriteLine("\n* * * * * Invalid move, try again.* * * * * \n\n");
      Player2_Move();
      }
      } 
      }
      
     
    //Method to display moves on the tic tac toe grid.
      public void DisplayMoveOnGrid()
      {
      Console.Write("\n");
      for (int d = 0; d < 3; d++)
      {
      Console.WriteLine("\t {0} | {1} | {2} ", grid[d, 0], grid[d, 1], grid[d, 2]);
      if (d != 2)
      Console.WriteLine("\t---|---|---");
      }
      Console.Write("\n");
      }
      
     
    //Method to determine winner.
      public void CheckForWinner()
      {
      int checkrows;
      int checkcolumns;
      
      
      //Check winners in the rows.
      for (checkrows = 0; checkrows < 3; checkrows++)
      {
      if ((grid[checkrows, 0] == player1) && (grid[checkrows, 1] == player1) && (grid[checkrows, 2] == player1))
      {
      Console.Clear();
      Console.WriteLine("***PLAYER 1 YOU ARE THE WINNER***\n");
      DisplayMoveOnGrid();
      }
      else
      {
      if ((grid[checkrows, 0] == player2) && (grid[checkrows, 1] == player2) && (grid[checkrows, 2] == player2))
      {
      Console.Clear();
      Console.WriteLine("***PLAYER 2 YOU ARE THE WINNER***\n"); 
      DisplayMoveOnGrid();
      }
      }
      }
     
      //Check winner in the columns.
      for (checkcolumns = 0; checkcolumns < 3; checkcolumns++)
      {
      if ((grid[0, checkcolumns] == player2) && (grid[1, checkcolumns] == player2) && (grid[2, checkcolumns] == player2))
      {
      Console.Clear();
      Console.WriteLine("\n***PLAYER 2 YOU ARE THE WINNER***\n");
      DisplayMoveOnGrid();
      }
      else
      {
      if ((grid[0, checkcolumns] == player1) && (grid[1, checkcolumns] == player1) && (grid[2, checkcolumns] == player1))
      {
      Console.Clear();
      Console.WriteLine("\n***PLAYER 1 YOU ARE THE WINNER***\n");
      DisplayMoveOnGrid();
      }
      }
      }
     
      //Check winner diagonally for player1.
      for (checkrows = 0; checkrows < 3; checkrows++)
      {
      for (checkcolumns = 0; checkcolumns < 3; checkcolumns++)
      {
      if ((grid[0, 0] == player1) && (grid[1, 1] == player1) && (grid[2, 2] == player1))
      {
      Console.Clear();
      Console.WriteLine("\n***PLAYER 1 YOU ARE THE WINNER***\n");
     
      DisplayMoveOnGrid(); 
      }
      else
      {
      if ((grid[2, 0] == player1) && (grid[1, 1] == player1) && (grid[0, 2] == player1))
      {
      Console.Clear();
      Console.WriteLine("\n***PLAYER 1 YOU ARE THE WINNER***\n");
      DisplayMoveOnGrid();
      }
      }
      }
     
      }
     
      //Check winner diagonally for player2.
      for (checkrows = 0; checkrows < 3; checkrows++)
      {
      for (checkcolumns = 0; checkcolumns < 3; checkcolumns++)
      {
      if ((grid[0, 0] == player2) && (grid[1, 1] == player2) && (grid[2, 2] == player2))
      {
      Console.Clear();
      Console.WriteLine("\n***PLAYER 2 YOU ARE THE WINNER***\n");
      DisplayMoveOnGrid();
      }
      else
      {
      if ((grid[2, 0] == player2) && (grid[1, 1] == player2) && (grid[0, 2] == player2))
      {
      Console.Clear();
      Console.WriteLine("\n***PLAYER 2 YOU ARE THE WINNER***\n");
      DisplayMoveOnGrid();
      }
      }
      }
      }
      } 
      }
      




      class Program
      {
      static void Main(string[] args)
      {
      Console.WriteLine("\n  Welcome to Tic Tac Toe!\n");
      for (int i = 0; i < 3; i++)
      {
      Console.WriteLine("\t  |  |  ");
      if (i != 2)
      Console.WriteLine("\t---|---|---");
      }
      Console.Write("\n  Press Enter to start. \n");
      Console.ReadLine(); 
      Console.Clear();
      Game tictactoe = new Game();
      bool gameover = false;
     
      do
      { 
      tictactoe.Player1_Move();
      tictactoe.CheckForWinner();
      tictactoe.Player2_Move();
      tictactoe.CheckForWinner();
     
      }while (gameover != true);
      Console.WriteLine("Game over.");
      }
      }
    0