Tic-Tac-Toe without a class
Write a modular program that allows two players to play a game of tic–tac-toe. Use a two-dimensional char array with 3 rows and 3 columns as the game board. Each element of the array should be initialized with an asterisk(*). The program should display the intial board configuration and then start a loop that does the following.

· Allow player 1 to select a location on the boardfor an X by entering a row and column number. Then redisplay the board with an X replacing * in the chosenlocation.

· If there is no winner yet and the board is not yet full, allow player 2 to select to location on the board for an 0 byentering a row a column number. Then redisplay the board with an O replacing the * in the chosen laction

The loop should continue until a player has won or a tie has occurred.

· Player 1 wins when there are three Xs in a row, a column, or a diagonal on the game board

· Player 2 wins when there are three Os in a row, ac olumn, or a diagonal on the game board.

· A tie occurs when all of the location on the board are full, but there is no winner.

************************************************************************************************

The "class-y" TicTacToe :)
The program should display an appropriate message indicating whowon, or reporting that a tie occurred.

The program needs to use a class TicTacToe. The main function needs to have a loop that drives the action of the game invoking methods on an instance the class TicTacToe.

The TicTacToe class should have a private data member for the game board (a two-dimensional array) and a private data member that keeps track of the current player - among any other private data members the class may require. Alter the state of the game board using public member functions.

I mostly need help with the TicTacToe class program. If someone could get back to me sooner that later, I would appreciate it.

Thanks in advance!

MY TICTACTOE CODE SO FAR (without a class):

#include <iostream>
#include <iomanip>
using namespace std;

const int SIZE = 3;
char a[3][3] = {
    {'*','*','*'},
    {'*','*','*'},
    {'*','*','*'}
                };              
char b = '0';

void draw();
bool getinputP1();
bool getinputP2();
bool checkPlayerOne();
bool checkPlayerTwo();


void draw()
{
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            cout << setw(5) << a[i][j];
        }
        cout << endl;
    }
}

bool getinputP1()
{
    cout << "Enter 1-9, respectively: ";
    cin >> b;
    switch(b)
    {
        case '1' : a[0][0] = 'X'; break;
        case '2' : a[0][1] = 'X'; break;
        case '3' : a[0][2] = 'X'; break;
        case '4' : a[1][0] = 'X'; break;
        case '5' : a[1][1] = 'X'; break;
        case '6' : a[1][2] = 'X'; break;
        case '7' : a[2][0] = 'X'; break;
        case '8' : a[2][1] = 'X'; break;
        case '9' : a[2][2] = 'X'; break;
        default: cout << "You have to play by the rules!" << endl;
    }
    return 0;
}

bool getinputP2()
{
    cout << "Enter 1-9, respectively: ";
    cin >> b;
    switch(b)
    {
        case '1' : a[0][0] = 'O'; break;
        case '2' : a[0][1] = 'O'; break;
        case '3' : a[0][2] = 'O'; break;
        case '4' : a[1][0] = 'O'; break;
        case '5' : a[1][1] = 'O'; break;
        case '6' : a[1][2] = 'O'; break;
        case '7' : a[2][0] = 'O'; break;
        case '8' : a[2][1] = 'O'; break;
        case '9' : a[2][2] = 'O'; break;
        default: cout << "You have to play by the rules!" << endl;
    }
    return 0;
}

    bool checkPlayerOne()
    {
        if(a[0][0] == 'X' && a[0][1] == 'X' && a[0][2] == 'X') return 1;
        if(a[1][0] == 'X' && a[1][1] == 'X' && a[1][2] == 'X') return 1;
        if(a[2][0] == 'X' && a[2][1] == 'X' && a[2][2] == 'X') return 1;
        if(a[0][0] == 'X' && a[1][0] == 'X' && a[2][0] == 'X') return 1;
        if(a[0][1] == 'X' && a[1][1] == 'X' && a[2][1] == 'X') return 1;
        if(a[0][2] == 'X' && a[1][2] == 'X' && a[2][2] == 'X') return 1;
        if(a[0][0] == 'X' && a[1][1] == 'X' && a[2][2] == 'X') return 1;
        if(a[2][0] == 'X' && a[1][1] == 'X' && a[0][2] == 'X') return 1;
    }

    bool checkPlayerTwo()
    {
        if(a[0][0] == 'O' && a[0][1] == 'O' && a[0][2] == 'O') return 1;
        if(a[1][0] == 'O' && a[1][1] == 'O' && a[1][2] == 'O') return 1;
        if(a[2][0] == 'O' && a[2][1] == 'O' && a[2][2] == 'O') return 1;
        if(a[0][0] == 'O' && a[1][0] == 'O' && a[2][0] == 'O') return 1;
        if(a[0][1] == 'O' && a[1][1] == 'O' && a[2][1] == 'O') return 1;
        if(a[0][2] == 'O' && a[1][2] == 'O' && a[2][2] == 'O') return 1;
        if(a[0][0] == 'O' && a[1][1] == 'O' && a[2][2] == 'O') return 1;
        if(a[2][0] == 'O' && a[1][1] == 'O' && a[0][2] == 'O') return 1;
    }

int main()
{
    cout << " -= Tic Tac Toe =-" << endl;
    cout << " -----------------" << endl;
    draw();
    cout << "\nPlayer one" << endl;
    getinputP1();
    draw();
    cout << "\nPlayer two" << endl;
    getinputP2();
    draw();

    for (;;)
    {
        cout << "\nPlayer one" << endl;
        getinputP1();
        if(checkPlayerOne() == 1)
        {
            draw();
            cout << "Player one wins!" << endl;
            break;
        }
        draw();
        cout << "\nPlayer two" << endl;
        getinputP2();
        if (checkPlayerTwo() == 1)
        {
            draw();
            cout << "Player two wins!" << endl;
            break;
        }
        else 
        {
            draw();
            cout << "Draw!" << endl;
            break;
        }
        system("pause");
    }
    return 0;
}

Edited 4 Years Ago by jay.zyzz: forgot some stuff

I know basics but I am going to try what I know. How would you relate the following (let's say it's correct:) in the main? I understand you can call your class/functions and whatnot, but can someone explain how would I go about this problem?

class TicTacToe

{
    public:
        int Pick_Player();
        int Pick_Row();  
        int Pick_Column(); 
        int Check_Board();
        void Choice_of_Row(int); 
        void Choice_of_Column(int); 
        void draw(); 
        bool Check_Move(int,int);
    private:
        int row;
        int column;
        int player;
        int board[3][3];
        char draw[3][3];
};

Well. Thanks for the help, unhelpful peeps :)

Here, I did what I could for those like me out there. I will delete this thread so make a use of it for now.

// library files
#include <iostream>
#include <string>

//standard namespace
using namespace std;

class TicTacToe
{
   private:
   char board[3][3]; //two-dimensional arrays

   public:
   TicTacToe() {}

   //Organizing our board for the game
   void SetBoard()
   {
      int n = 1;
      int i = 0;
      int j = 0;

      for ( i = 0; i < 3; i++ )
      {
         for ( j = 0; j < 3; j++ )
         {
            board[i][j] = '0' + n; //Cast the n to a character
            n++;
         }
      }
   } //End SetBoard

   //here we attempt to print (output) our board
   void PrintBoard()
   {
      int i = 0;
      int j = 0;

      for ( i = 0; i < 3; i++ )
      {
         for ( j = 0; j < 3; j++ )
         {
            if ( j < 2 )
            {
               cout << board[i][j] << "  |  ";
            }
               else
               {
                  cout << board[i][j] << endl;
               }
         }
      }
   } //End Print Table

   //Control Player moves
   void PlayerTurn(char num, char Player)
   {
      int i = 0;
      int j = 0;

      bool WrongMove = true; //If true, the player has made a wrong move
      for( i = 0; i < 3; i++ )
      {
         for( j = 0; j < 3; j++ )
         { //Mark square with X or O if WrongMove is not true
            if(board[i][j] == num) 
            { 
               board[i][j] = Player; //Assigns the space with the X or O, 
               WrongMove = false;    //depending on the board. 
            }
         }
      }
      if(WrongMove == true) { cout << "You can only mark the open sets!\n"; }
   } //End Player Move


//checking for the winner, if any, finish the game. If not, cotinue or a draw
//in the next function (CheckDraw)
   bool CheckResult(char Player, bool GameOver)
   {
      for(int i = 0; i < 3; i++) //Increment i to check rows
      {
         if(board[i][0] == board[i][1] && board[i][1] == 
         board[i][2]) GameOver = true;
      }
      for(int i = 0; i < 3; i++) //Increment i to check column
      {
         if(board[0][i] == board[1][i] && board[1][i] == 
         board[2][i]) GameOver = true;
      }
      //Checking the Diagonals
      if(board[0][0] == board[1][1] && board[1][1] == board[2][2]) 
      {
         GameOver = true;
      }
      if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
      {
         GameOver = true;
      }
      if(GameOver == true) 
      {
         cout << "Player " << Player << " wins!\n\n";
         cout << "-----------------------" << endl;
         cout << "|   CONGRATULATIONS " << Player << " |\n";
         cout << "-----------------------" << endl << endl;
      }
         return GameOver;
   } //End Check Winner
//Check the board to see if the match has come to a draw
   bool CheckDraw(bool GameOver)
   {
      int n = 1;
      int i = 0;
      int j = 0;
      int counter = 0;

      for( i = 0; i < 3; i++ )
      {
         for( j = 0; j < 3; j++ )
         {
            //Check to see if the board if full
            if(board[i][j] == '0' + n) 
            { 
               counter++; 
            }
            n++;
         }
      }
      if( counter < 1 ) 
      {
         cout << "No one won :(, keep playing :)!\n\n"; 
         GameOver = true;
      }
      return GameOver;
   }
};

int main()
{
    bool done     = false, GameOver      = false;
    char Player   = 'O', num;

    cout << "-=WELCOME TO=-\n";
    cout << "-=Tic-Tac-Toe=-\n";
    cout << "-------------"<< endl;

    TicTacToe myGame;
    myGame.SetBoard();
    do
    {
        if( Player == 'X' ) 
        {
           Player = 'O';
        }
           else
           {
              Player = 'X';
           } 
        myGame.PrintBoard();
        cout << "\nPlayer \"" << Player << "\", it's your turn: ";
        cin >> num; 
        cout << "\n";

        myGame.PlayerTurn(num, Player);
        GameOver = myGame.CheckResult(Player, GameOver);
        GameOver = myGame.CheckDraw(GameOver);
        //if game over, starting over
        if(GameOver == true) 
        {   myGame.SetBoard(); 
            GameOver = false; 
        }
    } while(!done);

    system("pause");
    return 0;
}

Peace!

This article has been dead for over six months. Start a new discussion instead.