Thread: function trouble

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    function trouble

    Hi Guys,

    Trying to create a tictactoe game, and am working on a function for getting one of the two users playing to see what the outcome of the game is. So far I have got it as follows:

    Code:
    TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
    {  
      // Check for win in diagonals //
      if (board [0][0]!= '_' && board [0][0] == board [1][1] 
      && board [0][0] == board [2][2])
      return WIN;
      else if (board [0][2]!= '_' && board [0][2] == board [1][1] 
      && board [0][2] == board [2][0])
      return WIN;
      //** End Check for win in diagonals ** //
    
      // Check for win in rows  //
      if (board [0][0]!= '_' && board [0][0] == board [1][0] 
      && board [0][0] == board [2][0])
      return WIN;
        else if (board [0][1]!= '_' && board [0][1] == board [1][1] 
      && board [0][1] == board [2][2])
      return WIN; 
        else if (board [0][2]!= '_' && board [0][2] == board [1][2] 
      && board [0][2] == board [2][2])
      return WIN; 
      //** End Check for win in rows  **//  
      
      // Check for win in columns  //
      if (board [0][0]!= '_' && board [0][0] == board [0][1] 
      && board [0][0] == board [0][2])
      return WIN;
        else if (board [1][0]!= '_' && board [1][0] == board [1][1] 
      && board [1][0] == board [1][2])
      return WIN;
       else if (board [2][0]!= '_' && board [2][0] == board [2][1] 
      && board [2][0] == board [2][2])
      return WIN; 
      // ** End Check for win in columns ** //
      
      // Check for a completed game //
       return CONTINUE;     // game is not finished
      // ** End Check for completed game ** //
    
      
     // Check for a draw // 
    return DRAW; // game is a draw
      // ** End Check for draw ** //
    
    }
    Now as you can see I have tried to do is say get the winners for diagonals, columns and rows.

    What I need to do now is check for a drawn game, which I think I need to state something like, if move >9 (it’s a 3x3 board) then Return Draw. And also check for a completed game? But I’m stuck on how to do these two!

    Can someone please advise if I am going right so far and also how I’d go about checking for a completed game and a draw?

    Many Thanks

    Rebel

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Seems like your thoughts are on the right track. The algorithm that makes sense to me is this:
    1. Player moves
    2. Check for a win
    3. Check for a draw
    4. If no win or draw, next move

    In other words, if you've already checked for a win and a draw, you KNOW the answer to if the game is complete already.

    Oh, and as a stylistic matter, a common way of handling your return value is to set it a variable to the default (CONTINUE in your case) at the top of the function, then modify it as needed in your conditionals, then at the bottom of the function, just return the var you defined at the top. This makes it MUCH easier to understand and follow the function IMO.
    Last edited by Stuka; 12-15-2005 at 09:02 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    to check if the game is complete
    If any array value is still it's initial value then it's not complete.

    Code:
    int isComplete(array)
    {
       for(i=0; i < row; i++) {    
          for(j=0; j < col; j++) {
             if(array[i][j] == empty) {
                return 0;
             }
          }
       }
       return 1;
    }

    for a draw
    if game is complete and player one hasn't won and player two hasn't won then the game is a draw

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int isComplete(array)
    ->
    Code:
    int isComplete(int array[][size])
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Hi Guys,

    Thanks for your help - much appreciated!

    Think i'm getting somewhere now, but am a little stuck. Here's what I have done so far:

    Code:
    // TICTACTOE.H
    // A Program to run the Tic-Tac-Toe Program
    #include <iostream.h>
    #include <iomanip.h>
    class TicTacToe
    {
       private:
         enu Status {WIN, DRAW, CONTINUE };
         char board [3] [3];
       public:
         TicTacToe ();
         void makeMove ();
         void printBoard ();
         bool validMove (int r, int c);
         bool xoMove (int input)
         Status gameStatus ();
         void holdscreen ();
    };
    
    TicTacToe:: TicTacToe ()
    {        
    // Initialising TicTacToe //    
      for (int r = 0; r < r; r++) //looping rows?
      for (int c = 0; c < c; c++) //looping columns?
      validMove[r][c] = '_';
    
    // ** End TicTacToe ** //
    // ** IS THIS SETTING EVERY ELEMENT ON THE BOARD TO A SPACE? ** //
    }
    
    bool TicTacToe::validMove (int r, int c)
    {
    
    // Validate Move Start for X //
    cout << "Enter coordinates for X: ";
        cin >> r;
        while(true) {
        if ((r>=3) || (r<0))
        cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
    else break;}
    // ** End Validate Move Start for X ** //
     
    // Validate Move Start for O //
    cout << "Enter coordinates for O: ";
        cin >> c;
        while(true) {
        if ((c>=3) || (c<0))
        cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
    else break;}
    // ** End Validate Move Start for O ** //
    
    }
    
    TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
    {
      int a;
      
      // Check for win in diagonals //
      if (board [0][0]!= '_' && board [0][0] == board [1][1] 
      && board [0][0] == board [2][2])
      return WIN;
      else if (board [0][2]!= '_' && board [0][2] == board [1][1] 
      && board [0][2] == board [2][0])
      return WIN;
      //** End Check for win in diagonals ** //
    
      // Check for win in rows
      if (board [0][0]!= '_' && board [0][0] == board [1][0] 
      && board [0][0] == board [2][0])
      return WIN;
        else if (board [0][1]!= '_' && board [0][1] == board [1][1] 
      && board [0][1] == board [2][2])
      return WIN; 
        else if (board [0][2]!= '_' && board [0][2] == board [1][2] 
      && board [0][2] == board [2][2])
      return WIN; 
      //** End Check for win in rows  **//  
      
      // Check for win in columns
      if (board [0][0]!= '_' && board [0][0] == board [0][1] 
      && board [0][0] == board [0][2])
      return WIN;
        else if (board [1][0]!= '_' && board [1][0] == board [1][1] 
      && board [1][0] == board [1][2])
      return WIN;
       else if (board [2][0]!= '_' && board [2][0] == board [2][1] 
      && board [2][0] == board [2][2])
      return WIN; 
      // ** End Check for win in columns ** //
      
      // Check for a completed game
    {
       for(r=0; r < r; r++) {    
          for(c=0; c < c; C++) {
             if(array[r][c] == empty) {
                return 0;
             }
          }
       }
       return CONTINUE; 
    }  
      else
      return DRAW; // game is a draw
    }
    
    void TicTacToe::printBoard (void)
    {
         cout << "   0   1   2\n\n";
         
         for ( int r=0; r < 3; ++r ) {
             cout << r;
             
             for ( int c=0; c < 3; ++c ) {
                 cout << setw (3) << static_cast < char > ( board [r] [c] );
                 
                 if (c !=2 )
                    cout << " |";
             }
             
             if (r != 2)
                cout << "\n ______|______|______"
                     << "\n ______|______|______\n"
             }
         
         cout <<"\n\n";
         
    void  TicTacToe::makeMove(void)
    {
       printBoard();
       
       while ( true ) {
          if (xoMove ( 'X' ) )
             break;
       else if (xoMove ( 'O' ) )
             break;
       }
    }
    
    bool TicTacToe::xoMove (int symbol )
    {
       int x,y;
       
       do {
           cout << "Player" << static_cast < char > (symbol) << " enter move: ";
           cin >> x >> y;
           cout << '\n';
       } while ( !validMove ( x, y ) );
       
       board [x] [y] = symbol;
       printBoard();
       Status xoStatus = gamestatus();
       
       if ( xoStatus == WIN ) {
          cout << "Player " << static_cast < char > (symbol ) << " wins!\n";
          return true;
       }
       else if (xoStatus == DRAW ) {
            cout << "Game is a draw. \n";
            return true;
       }
       else //Continue
            return false;
    }
    Now i'm not sure on two bits;

    initialising the table

    Code:
    TicTacToe:: TicTacToe ()
    {        
    // Initialising TicTacToe //    
      for (int r = 0; r < r; r++) //looping rows?
      for (int c = 0; c < c; c++) //looping columns?
      validMove[r][c] = '_';
    
    // ** End TicTacToe ** //
    // ** IS THIS SETTING EVERY ELEMENT ON THE BOARD TO A SPACE? ** //
    }
    Would the above be setting every element on the board to a space?

    Checking the game status

    Code:
    TicTacToe:: Status TicTacToe:: gameStatus gameStatus(void)
    {
      int a;
      
      // Check for win in diagonals //
      if (board [0][0]!= '_' && board [0][0] == board [1][1] 
      && board [0][0] == board [2][2])
      return WIN;
      else if (board [0][2]!= '_' && board [0][2] == board [1][1] 
      && board [0][2] == board [2][0])
      return WIN;
      //** End Check for win in diagonals ** //
    
      // Check for win in rows
      if (board [0][0]!= '_' && board [0][0] == board [1][0] 
      && board [0][0] == board [2][0])
      return WIN;
        else if (board [0][1]!= '_' && board [0][1] == board [1][1] 
      && board [0][1] == board [2][2])
      return WIN; 
        else if (board [0][2]!= '_' && board [0][2] == board [1][2] 
      && board [0][2] == board [2][2])
      return WIN; 
      //** End Check for win in rows  **//  
      
      // Check for win in columns
      if (board [0][0]!= '_' && board [0][0] == board [0][1] 
      && board [0][0] == board [0][2])
      return WIN;
        else if (board [1][0]!= '_' && board [1][0] == board [1][1] 
      && board [1][0] == board [1][2])
      return WIN;
       else if (board [2][0]!= '_' && board [2][0] == board [2][1] 
      && board [2][0] == board [2][2])
      return WIN; 
      // ** End Check for win in columns ** //
      
      // Check for a completed game
    {
       for(r=0; r < r; r++) {    
          for(c=0; c < c; C++) {
             if(array[r][c] == empty) {
                return 0;
             }
          }
       }
       return CONTINUE; 
    }  
      else
      return DRAW; // game is a draw
    }
    As i've established by code for checking for a winner in rows and columns is right and i've been helped with the check for the completed game, so ELSE it would be a draw?

    Thanks

    Reb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM