Thread: 3-D Tic-Tac-Toe

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    3-D Tic-Tac-Toe

    Last week I posted a topic for help correcting an error in a Tic-Tac-Toe code, and my assignment for this week is to create a 3-D Tic-Tac-Toe game in C++ that will play against the user. The AI doesn't have to actually use strategy; it can randomly select a square. "3-D" means that there are three TTT boards on the screen, and in addition to winning in the normal way, you can win "down". I.e. if there is an 'X' in the top left square of each of the three boards, that is another point for 'X'. My program must use OOP concepts and use inherited classes from my class for the board I designed in my previous assignment. I understand that no one here knows what my original class looked like, but my problem isn't the programming, but the concept. What could my base class include (conceptually) and what do I gain in inheriting its traits?

    Thanks for the help, and I'll post the code from my last assignment if it becomes necessary.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    With 3x3x3 tic-tac-toe, if the starting player plays right they will always win; not just draw but win. E.g. I could win every time if I wanted, and you couldn't stop me.

    An actual 3D tic-tac-toe board is 4x4x4, which doesn't suffer from that problem.

    I don't know what else useful I could add without seeing code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My program must use OOP concepts and use inherited classes from my class for the board I designed in my previous assignment. I understand that no one here knows what my original class looked like, but my problem isn't the programming, but the concept.
    Last time you had a single TicTacToe class (here) which IMO essentially means that most variables in the program (members) were global to all functions (methods) in the program. That is, the class looks like it's too big and has too many tasks, and therefore I don't see how you could reuse it by inheriting from it. (Particularly the space array is a hindrance, because now it wouldn't make sense to have a 3 x 3 char array.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    I rewrote the program entirely since the last time I posted. I didn't remember whether I had since posted it or not, but here it is, for convenience. @1st reply: I don't have any expertise in algorithms or game strategy, but my assignment showed a 3x3x3, so that's what I have to make.

    2nd reply: Here is the new code (with a smaller class). I see that I could instantiate it three times for three boards (with a small rewrite to remove the 'turn' var), but I can't figure out how to usefully implement an inherited class...

    Code:
    #include <iostream>
    
    class TicTacToe {
          public:
                 char space[3][3]; // [row][col]
                 char turn; // X or O
                 char winner; //X O C or empty space
                 
                 TicTacToe() { //constructor
                         int iTurn;
                         iTurn = (rand() % 2) + 1;
                         turn = (iTurn == 1) ? 'X' : 'O';
                         winner = ' ';
                         
                         int i,j;
                         for (i=0; i<=2; i++) {
                             for (j=0; j<=2; j++) {
                                 space[i][j] = ' ';
                             }
                         }
                 } //end constructor
                 
                 void drawBoard(void);
                 void checkWin(void);
                 int userMove(void);
                 int AIMove(void);
                 int makeMove(int, char);
                 
    } ; //end class TicTacToe
    
    void TicTacToe::drawBoard(void) {
         using namespace std;
               
         system("cls");
         cout << "\n\n\t\t\t|\t|\t\n" ;
         cout << "\t\t    " << space[0][0] << "\t|   " << space[0][1] << "\t|   " << space[0][2] << endl;
         cout << "\t\t\t|\t|\n" ;
         cout << "\t\t--------|-------|--------\n" ;
         cout << "\t\t\t|\t|\n" ;
         cout << "\t\t    " << space[1][0] << "\t|   " << space[1][1] << "\t|   " << space[1][2] << endl;
         cout << "\t\t\t|\t|\n" ;
         cout << "\t\t--------|-------|--------\n" ;
         cout << "\t\t\t|\t|\n" ;
         cout << "\t\t    " << space[2][0] << "\t|   " << space[2][1] << "\t|   " << space[2][2] << endl;
         cout << "\t\t\t|\t|\n" ;
    } //end def drawBoard
    
    void TicTacToe::checkWin(void) {
        char returnValue = ' ';
        
        //test for X or O win
        //test rows
        if ( (space[0][0] == space[0][1]) && (space[0][0] == space[0][2]) )
             returnValue = space[0][0];
        else if ( (space[1][0] == space[1][1]) && (space[1][0] == space[1][2]) )
             returnValue = space[1][0];
        else if ( (space[2][0] == space[2][1]) && (space[2][0] == space[2][2]) )
             returnValue = space[2][0];
        
        //test columns
        else if ( (space[0][0] == space[1][0]) && (space[0][0] == space[2][0]) )
             returnValue = space[0][0];
        else if ( (space[0][1] == space[1][1]) && (space[0][1] == space[2][1]) )
             returnValue = space[0][1];
        else if ( (space[0][2] == space[1][2]) && (space[0][2] == space[2][2]) )
             returnValue = space[0][2];
        
        //test diagonals
        else if ( (space[0][0] == space[1][1]) && (space[0][0] == space[2][2]) )
             returnValue = space[0][0];
        else if ( (space[0][2] == space[1][1]) && (space[0][2] == space[2][0]) )
             returnValue = space[0][2];
             
    
        if ( returnValue == ' ' ) { //then test for cat
           int i, j, catCheck=0;
           for (i=0; i<=2; i++) {
               for (j=0; j<=2; j++) {
                   if ( space[i][j] == ' ' )
                        catCheck++;
               }
           }
           if ( catCheck == 0 )
              returnValue = 'C';
        }
        
        winner = returnValue;
    } //end def checkWin
    
    int TicTacToe::userMove(void) {
        using namespace std;
        int userChoice=0;
        
        while ( userChoice < 1 || userChoice > 9 ) {
              cout << "\n\nEnter a square [1-9]: ";
              cin >> userChoice;
        } //end while
        return userChoice;
    } //end def userMove
    
    int TicTacToe::AIMove(void) {
        int returnValue = (rand() % 9) + 1;
        return returnValue;
    } //end def AIMove
    
    int TicTacToe::makeMove(int intSpace, char player) {
        int returnValue;
         switch (intSpace) {
                case 1:
                     if ( space[0][0] == ' ' ) {
                        space[0][0] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 2:
                     if ( space[0][1] == ' ' ) {
                        space[0][1] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 3:
                     if ( space[0][2] == ' ' ) {
                        space[0][2] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 4:
                     if ( space[1][0] == ' ' ) {
                        space[1][0] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 5:
                     if ( space[1][1] == ' ' ) {
                        space[1][1] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 6:
                     if ( space[1][2] == ' ' ) {
                        space[1][2] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 7:
                     if ( space[2][0] == ' ' ) {
                        space[2][0] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 8:
                     if ( space[2][1] == ' ' ) {
                        space[2][1] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
                case 9:
                     if ( space[2][2] == ' ' ) {
                        space[2][2] = player;
                        returnValue = 1;
                     }
                     else
                          returnValue = 0;
                     break;
         } //end switch
         return returnValue;
    } //end def makeMove
    
    main() {
           using namespace std;
           
           srand(time(NULL));
           TicTacToe Game;
           
           int intSpace;
           
           while ( Game.winner == ' ' ) {
    
                 Game.drawBoard();
    
                 if ( Game.turn == 'X' ) {
                      doOver1:
                      intSpace = Game.AIMove();
                      if ( Game.makeMove(intSpace, 'X') == 0 )
                         goto doOver1;
                      Game.turn = 'O';
                      }
                 
                 else if ( Game.turn == 'O' ) {
                      doOver2:
                      intSpace = Game.userMove();
                      if ( Game.makeMove(intSpace, 'O') == 0 )
                         goto doOver2;
                      Game.turn = 'X';
                      }
                      
                 Game.checkWin();
                      
           } //end while
           
           Game.drawBoard();
           
           if ( Game.winner != 'C' )
              cout << "\n\n" << Game.winner << " wins!" << endl;
           else
               cout << "\n\n Cat wins!" << endl;
    
           system("pause");
           
    } //end def main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe check winner
    By dhardin in forum C++ Programming
    Replies: 15
    Last Post: 12-20-2009, 07:57 PM
  2. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM