Tic Tac Toe Player vs Computer

This is a discussion on Tic Tac Toe Player vs Computer within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm currently creating a Tic Tac Toe program for a player to vs a computer. I already did one ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Tic Tac Toe Player vs Computer

    Hi, I'm currently creating a Tic Tac Toe program for a player to vs a computer. I already did one for a player vs another player and now I have no idea where to insert the rand function and I don't know how to start doing so with my current code. Can someone help please?

    I didn't do any classes yet, I like to do those after I get the code up and running.

    Here is my current code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
      char cSquare1( '1' );
      char cSquare2( '2' );
      char cSquare3( '3' );
      char cSquare4( '4' );
      char cSquare5( '5' );
      char cSquare6( '6' );
      char cSquare7( '7' );
      char cSquare8( '8' );
      char cSquare9( '9' );
      int iPlayerTurn( 1 );
      bool bGameOver( true );
    
      //Main game loop
      do{
        //Print board
        cout << cSquare1 << " | " << cSquare2 << " | " << cSquare3 << endl;
        cout << "--+---+--" << endl;
        cout << cSquare4 << " | " << cSquare5 << " | " << cSquare6 << endl;
        cout << "--+---+--" << endl;
        cout << cSquare7 << " | " << cSquare8 << " | " << cSquare9 << endl;
    
        //Set player marker: Player 1 uses X and Player 2 uses 0
    
        char cPlayerMark;
        if( iPlayerTurn == 1 ){
          cPlayerMark = 'X';
        } else{
          cPlayerMark = '0';
        }
    
        //Prompt the player for a move
        cout << "Player " << iPlayerTurn << "'s move: " << endl;
        bool bValidMove;
        //Loop until we get a valid move
        do{
          char cNextMove;
          cin >> cNextMove;
          bValidMove = true;
    
          //Check for a valid move
          if( cNextMove == '1' && cSquare1 == '1' ){
            cSquare1 = cPlayerMark;
          } else if( cNextMove == '2' && cSquare2 == '2' ){
            cSquare2 = cPlayerMark;
          } else if( cNextMove == '3' && cSquare3 == '3' ){
            cSquare3 = cPlayerMark;
          } else if( cNextMove == '4' && cSquare4 == '4' ){
            cSquare4 = cPlayerMark;
          } else if( cNextMove == '5' && cSquare5 == '5' ){
            cSquare5 = cPlayerMark;
          } else if( cNextMove == '6' && cSquare6 == '6' ){
            cSquare6 = cPlayerMark;
          } else if( cNextMove == '7' && cSquare7 == '7' ){
            cSquare7 = cPlayerMark;
          } else if( cNextMove == '8' && cSquare8 == '8' ){
            cSquare8 = cPlayerMark;
          } else if( cNextMove == '9' && cSquare9 == '9' ){
            cSquare9 = cPlayerMark;
          } else{
            cout << "Invalid Move. Try again." << endl;
            bValidMove = false;
          }
        } while( !bValidMove );
    
        bGameOver = false;
        bool bWinGame = true;
        //Check for end of game conditions
        if( cSquare1 != '1' ){
          if( cSquare2 == cSquare1 && cSquare3 == cSquare1 ){
            bGameOver = true;
          }
          if( cSquare4 == cSquare1 && cSquare7 == cSquare1 ){
            bGameOver = true;
          }
        }
    
        if( cSquare5 != '5' ){
          if( cSquare1 == cSquare5 && cSquare9 == cSquare5 ){
            bGameOver = true;
          }
          if( cSquare2 == cSquare5 && cSquare8 == cSquare5 ){
            bGameOver = true;
          }
          if( cSquare4 == cSquare5 && cSquare6 == cSquare5 ){
            bGameOver = true;
          }
          if( cSquare3 == cSquare5 && cSquare7 == cSquare5 ){
            bGameOver = true;
          }
        }
        if( cSquare9 != '9' ){
          if( cSquare3 == cSquare9 && cSquare6 == cSquare9 ){
            bGameOver = true;
          }
          if( cSquare7 == cSquare9 && cSquare8 == cSquare9 ){
            bGameOver = true;
          }
        }
        //Need to check the board full (no-win condition)
        if( cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
            cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
            cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver ){
          bGameOver = true;
          bWinGame = false;
        }
    
        if( bGameOver ){
          if( bWinGame ){
            cout << "\nPlayer " << iPlayerTurn << " wins!" << endl;
          }
          //Print ending board
          cout << cSquare1 << " | " << cSquare2 << " | " << cSquare3 << endl;
          cout << "--+---+--" << endl;
          cout << cSquare4 << " | " << cSquare5 << " | " <<cSquare6 << endl;
          cout << "--+---+--" << endl;
          cout << cSquare7 << " | " << cSquare8 << " | " << cSquare9 << endl;
    
          cout << "Game Over!" << endl;
          cout << "Play again (y/n)?" << endl;
          char cPlayAgain;
          cin >> cPlayAgain;
    
          if( cPlayAgain == 'y' ){
            bGameOver = false;
            // Clear the board
            cSquare1 = '1';
            cSquare2 = '2';
            cSquare3 = '3';
            cSquare4 = '4';
            cSquare5 = '5';
            cSquare6 = '6';
            cSquare7 = '7';
            cSquare8 = '8';
            cSquare9 = '9';
          }
          iPlayerTurn = 1;
        } else{
          // Alternate player turns
          if( iPlayerTurn == 1 ){
            iPlayerTurn = 2;
          } else{
            iPlayerTurn = 1;
          }
        }
      } while( !bGameOver );
    
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by chakolate
    I already did one for a player vs another player and now I have no idea where to insert the rand function and I don't know how to start doing so with my current code.
    When it is the computer's turn, instead of prompting the player for the move, generate the move using rand().

    By the way, did you not learn how to use arrays?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Hahah, I did, but someone told me it'd be a lot easier to code without arrays. I started the entire thing with arrays and then he somehow convinced me not to. So I changed it. I admit, it was a lot quicker to do though.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by chakolate
    Hahah, I did, but someone told me it'd be a lot easier to code without arrays. I started the entire thing with arrays and then he somehow convinced me not to. So I changed it. I admit, it was a lot quicker to do though.
    Did you ask that someone if he/she was proficient in the use of arrays? I'm guessing that either that someone is also a beginner, or did not tell you the truth. For example, this tedious block of code:
    Code:
          //Check for a valid move
          if( cNextMove == '1' && cSquare1 == '1' ){
            cSquare1 = cPlayerMark;
          } else if( cNextMove == '2' && cSquare2 == '2' ){
            cSquare2 = cPlayerMark;
          } else if( cNextMove == '3' && cSquare3 == '3' ){
            cSquare3 = cPlayerMark;
          } else if( cNextMove == '4' && cSquare4 == '4' ){
            cSquare4 = cPlayerMark;
          } else if( cNextMove == '5' && cSquare5 == '5' ){
            cSquare5 = cPlayerMark;
          } else if( cNextMove == '6' && cSquare6 == '6' ){
            cSquare6 = cPlayerMark;
          } else if( cNextMove == '7' && cSquare7 == '7' ){
            cSquare7 = cPlayerMark;
          } else if( cNextMove == '8' && cSquare8 == '8' ){
            cSquare8 = cPlayerMark;
          } else if( cNextMove == '9' && cSquare9 == '9' ){
            cSquare9 = cPlayerMark;
          } else{
            cout << "Invalid Move. Try again." << endl;
            bValidMove = false;
          }
    could have been easily written as:
    Code:
    if( cNextMove >= '1' && cNextMove <= '9' && cSquare[cNextMove - '1'] == cNextMove ) {
      cSquare[cNextMove - '1'] = cPlayerMark;
    } else {
      cout << "Invalid Move. Try again." << endl;
      bValidMove = false;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    what's your email?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Why would you need that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by chakolate View Post
    I didn't do any classes yet, I like to do those after I get the code up and running.
    That makes sense. I like to dig the basement after I build the house. It's also more fun to put the parachute on after I jump out of the plane.

    What a stupid concept -- write the code to a working state, then rewrite the whole thing to shoehorn in the class? If you want to be a programmer, change your thinking.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Computer Science vs Computer Engineering degree
    By PCG33K in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-03-2007, 07:13 AM
  2. Is it a watch? Is it an MP3 player? Is it a video player?
    By twomers in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-15-2006, 10:54 PM
  3. Replies: 23
    Last Post: 01-31-2003, 02:13 AM
  4. wave player or mp3 player using C
    By lliero in forum C Programming
    Replies: 5
    Last Post: 02-27-2002, 10:33 AM
  5. computer science or computer engineering??
    By pkananen in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-26-2002, 11:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21