Link to home
Create AccountLog in
Avatar of edelossantos
edelossantos

asked on

Tic Tac Toe

I am not understanding the implicit declarations.  

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

using namespace std;

/* Global Data */

static char Board[3][3] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }  };      
static char Icons[3] = { ' ', 'X', 'O' };

/* Function Prototypes */

int wanna_play(void);
void init_board(void);
void init_screen(void);
void update_board(void);
int check_if_winner();
void get_player_move(int player);
void get_computer_move(int player);
void play_game(int gametype);
void print_credits(void);
int main(void);

int main(void) {
 
     int gametype;
     int ans;
     int gotoxy;

     if((gametype = wanna_play()) != 0) {
       
      do {
        
        init_board();
             init_screen();
                
          play_game(gametype);
             
        gotoxy(3, 24);
          cout << "Do you wish to play again? y/n:  " ;
        
          do {
          
            ans = getch();
            if(ans == 'y' || ans == 'n')
            break;
                 
          }
          while(1);

          gotoxy(3, 24);
                 cout << "                                          ";
      
           }
      while( ans == 'y' );
      
      }

      print_credits();
      return 0;

}

/* Tell user about game and see if they want to play */
int wanna_play(void) {
 
  int ans;

    clrscr();
    cout << "+-----------------------------------------------------------------------------+\r\n";
     cout <<"|                                   WELCOME                                   |\r\n";
     cout <<"+-----------------------------------------------------------------------------+\r\n\r\n";
     cout << "    Welcome to the classic game of Tic-Tac-Toe. This simple\r\n";
     cout << "game is complexically simuleate to be played on a 3 x 3 grid. Each player takes a choice of\r\n";
     cout << "either X or O, then using only wit, try to creat a simple line\r\n";
     cout << "of 3 of their respective symbol.  Whoever does so first, wins.\r\n\r\n";
     cout << "    If all the squares are filled, but no lines created, a draw\r\n";
     cout << "is the result. The game switches between who goes first, X or O.\r\n\r\n";
     cout << "What kind of game do you wish to play?\r\n";
     cout <<"(0) None - just quit\r\n";
     cout <<"(1) Player vs Player - Play against another human\r\n";
     cout <<"(2) Player vs Computer - Play against a beatable computer\r\n\r\n";
     cout << "Enter Choice:";

    do {
     
      ans = getch();
      switch( ans ) {
        
               case '0': return 0;
               case '1': return 1;
               case '2': return 2;
        }

     }
     while(1);
}
      
/* Initialize Board to Default Values */
void init_board( void ) {
        
     int i;
     int j;

     for( i = 0; i < 3; i++ )
      for( j = 0; j < 3; j++ )
        Board[i][j] = 0;

     return;

}

/* Set up Screen to show Game Board */
void init_screen(void) {
 
    clrscr();
    cout << "+-----------------------------------------------------------------------------+\r\n";
    cout << "|                                 TIC-TAC-TOE                                 |\r\n";
    cout << "+-----------------------------------------------------------------------------+\r\n\r\n";
    cout << "                                 C O L U M N S\r\n";
    cout << "                               [1]   [2]   [3]\r\n";
    cout << "                              +-----+-----+-----+\r\n";
     cout <<"                              |     |     |     |\r\n";
     cout <<"                         [1]|     |     |     |\r\n";
     cout <<"                        R    |     |     |     |\r\n";
     cout <<"                              +-----+-----+-----+\r\n";
     cout <<"                        O    |     |     |     |\r\n";
     cout <<"                          [2]|     |     |     |\r\n";
     cout <<"                       W    |     |     |     |\r\n";
     cout <<"                              +-----+-----+-----+\r\n";
     cout <<"                         S    |     |     |     |\r\n";
     cout <<"                           [3]|     |     |     |\r\n";
     cout <<"                              |     |     |     |\r\n";
     cout <<"                              +-----+-----+-----+\r\n\r\n";
     cout <<"+-----------------------------------------------------------------------------+\r\n";
     cout <<"|                                                                             |\r\n";
     cout <<"|                                                                             |\r\n";
     cout <<"|                                                                             |\r\n";
     cout <<"+-----------------------------------------------------------------------------+";

     return;

}

/* Update the screen with the current Board */
void update_board(void) {
     
  gotoxy(34, 9);
  putch(Board[0][0]);
  gotoxy(40, 9);
  putch(Board[0][1]);
  gotoxy(46, 9);
  putch(Board[0][2]);

  gotoxy(34, 13);
  putch(Board[1][0]);
  gotoxy(40, 13);
  putch(Board[1][1]);
  gotoxy(46, 13);
  putch(Board[1][2]);

  gotoxy(34, 17);
  putch(Board[2][0]);
  gotoxy(40, 17);
  putch(Board[2][1]);
  gotoxy(46, 17);
  putch(Board[2][2]);
     
  gotoxy(3, 23);
  return;

}

/* Check if any player has completed a line */
int check_if_winner() {
 
  int i;
  int k;
  int j;

  for(i = 1; i < 3; i++) {
    for(k = 0; k < 3; k++)
      if(Board[k][0] == Icons[i] && Board[k][1] == Icons[i] && Board[k][2] == Icons[i])
        return i;

         for(j = 0; j < 3; j++)
          if(Board[0][j] == Icons[i] && Board[1][j] == Icons[i] && Board[2][j] == Icons[i])
              return i;

          if(Board[0][0] == Icons[i] && Board[1][1] == Icons[i] && Board[2][2] == Icons[i])
          return i;

          if(Board[0][2] == Icons[i] && Board[1][1] == Icons[i] && Board[2][0] == Icons[i])
          return i;
   }
   return 0;
   
}

/* Get a user's move */
void get_player_move(int player) {
 
  int row;
  int column;

  do {
   
    gotoxy(3, 23);
    cout << "Player[%i]: Enter Row Number: " << player;

    do {  row = getch(); } while(row < '0' || row > '4');
      gotoxy(3, 23);

      cout << "Player[%i]: Enter Column Number: " << player;
      do {  column = getch(); } while(column < '0' || column > '4');
      gotoxy( 3, 23 );

      cout << "                                   ";

    }
    while( Board[ row - 0x31 ][ column - 0x31 ] != 0 );

    Board[ row - 0x31 ][ column - 0x31 ] = Icons[player];
    return;

}

/* Get the computer's move */
void get_computer_move(int player) {
 
  int row;
  int column;
     
  do {
    row = random() % 3;
    column = random() % 3;
   
  }
  while(Board[ row ][ column ] != 0);

  Board[ row ][ column ] = Icons[player];
  return;
 
}

/* Play the game */
void play_game(int gametype) {
 
  int i = 0;
  int player = 1;

  while(i < 9) {
   
    get_player_move(player);
    update_board();

    if( player == 1 )
      player = 2;
    else
      player = 1;            

    gotoxy(3, 23);
      i++;
          
    if(check_if_winner() == 1) {
     
      cout << "Player 1 (X) Wins!                        ";
      return;
         
    }
    else
      if(check_if_winner() == 2) {
      
        cout << "Player 2 (0) Wins!                        ";
           return;
         
       }

       if(gametype == 2) {
      
        get_computer_move(player);
          i++;
          update_board();

          if(player == 1)
               player = 2;
        else
          player = 1;

          if(check_if_winner() == 1) {
          
        cout << "Player 1 (X) Wins!                        ";
        return;
               
      }
        else
        if(check_if_winner() == 2) {
          
            cout << "Player 2 (0) Wins!                        ";
            return;
               
        }
         
      }           
   
    }
    return;

}

/* Print the game credits */
void print_credits(void) {
 
     clrscr();
     cout << "Thank you for using this program, hope you had fun.\r\n";
     
}


SOLUTION
Avatar of Greybird
Greybird

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of edelossantos
edelossantos

ASKER

jkr,

compiler pegasus does not recognize conio.h and/or curses.h

Please advise on suggestions.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.

******************************Working Code**********************************************

#include <iostream>

using namespace std;

enum Status {Continue, xWin, oWin, Draw};      // creates an enumerated data type

void initGame(char [][3]);
bool validMove(const char [][3], int, int);
Status gameStatus (const char A[][3]);
void display(const char [][3]);                // display board
void moveX(char [][3]);                        // if valid, move X
void moveO(char [][3]);                       // if valid, move O
void clearScreen();      

int main(void) {

  Status status = Continue;       // declares and assigns a variable of type Status
  char A[3][3];                  // declare the game board
  initGame(A);
  clearScreen();
  display (A);          
 
  while(status == Continue) {  
     moveX(A);                
     status = gameStatus(A);
     display(A);          
     
     if(status != Continue) {

       if(status == xWin)
          cout << "X Wins\n\n";
       else
          cout << "Game is a Draw\n\n";
       break;
     }
   
     moveO(A);
     status = gameStatus(A);
     display(A);          

     if(status != Continue){
        if (status == oWin)
          cout << "O Wins\n\n";  
        else
          cout << "Game is a Draw\n\n";
     break;
     }
     
   }  // end while
   
return 0;

}

void initGame(char A [][3]) {

  for(int row = 0; row < 3; row++)
    for(int col=0; col < 3; col++)
      A[row][col] = ' ';
        
  return;

}

void test(int *ptr) {

  cout << endl << *ptr << endl;
  return;

}

void display(const char A[][3]) {

   clearScreen();
   cout << "\t\t\t     column\n\t\t\t 0    1    2 "<< endl << endl;  

  for(int row = 0; row < 3; row++) {
    if(row == 1)
      cout << "\t   row\t   " << row << "\t";
     else
      cout << "\t\t   " << row << "\t";
         
      for(int col = 0; col < 3; col++) {

      cout << "  " << A[row][col];
      if(col < 2) cout << " |";
       else
                cout << endl;

      }
      if(row < 2)
      cout << "\t\t\t---------------"<< endl;
  }
  cout << endl;
  return;

}

void moveX(char A [][3]) {

  int row;
  int col;

  cout << "\nType in row and column you wish to input X:  ";
  cin >> row >> col;

  while(!validMove(A, row, col )) {
     cout << "Invalid move. Try Again: ";
     cin >> row >> col;
  }

  A[row][col] = 'X';
  return;

}

void moveO(char A[][3]) {

  int row;
  int col;

  cout << "\nType in the row and column that you wish to input O:  ";
  cin >> row >> col;
  while ( !validMove(A, row, col ) ) {
     cout << "Invalid move. Try Again: ";
     cin >> row >> col;
  }

  A[row][col] = 'O';
  return;

}

bool validMove(const char A[][3], int x, int y) {

  return(A[x][y] == ' ' );

}

Status gameStatus (const char A[][3]) {

  // check rows for a winner
  for(int row = 0; row < 3; row ++) {
    if(A[row][0] == A[row][1] && A[row][0] == A[row][2])
      if(A[row][0] == 'X')
        return xWin;   // X wins
       else
        if(A[row][0] == 'O')
          return oWin;   // O wins
  }

  // check left-right diagonal for winner
  if(A[0][0] == A[1][1] && A[0][0] == A[2][2]) {
        
    if(A[0][0] == 'X')
      return xWin;
    else
      if( A[0][0] == 'O')
        return oWin;   // O wins

  }
 
  // check right-left diagonal for winner
  if(A[0][2] == A[1][1] && A[0][2] == A[2][0]) {
    if(A[0][2] == 'X')
      return xWin;
    else
      if(A[0][2] == 'O')
        return oWin;   // O wins
  }

  // check columns for winner
  for(int col = 0; col < 3; col ++) {
    if(A[0][col] ==  A[1][col] && A[0][col] == A[2][col])
      if(A[0][col] == 'X')
        return xWin;   // X wins
       else
        if(A[0][col] == 'O')
           return oWin;  // O wins

  }

// check for draw

  for(int i = 0; i < 3; i++)
    for(int j = 0; j < 3; j++)
      if(A[i][j] == ' ')
        return Continue;   // game is not a draw

  return Draw;  // if you made it this far the game is a draw

}

void clearScreen() {

  cout << "\033[2J";   // send vt100 ESC sequence to clear screen

}


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