One quick question...

This is a discussion on One quick question... within the C++ Programming forums, part of the General Programming Boards category; I run this tic tac toe problem and it runs great through 1 iteration and then when the next iteration ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    One quick question...

    I run this tic tac toe problem and it runs great through 1 iteration and then when the next iteration runs the same board appears. I thought my initBoard function was good, but I guess its lacking something or its in the wrong place? If someone could help me that would be great.

    Here is my code...

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char [][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;                                                                   //Variable to hold response for new game
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};           //Initiailizing tictactoe board
        char answer = 'n' ;                                                         //Initializing winner
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
                                                                 //Initializing playing board
        do
        {
             initBoard(B, ROWS);
             displayBoard(B, ROWS);
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    //************************************************************
    //This function allows players to play 1 game of tic tac toe *
    //************************************************************
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
        
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B);               
                  answer = winner(B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);
                  player2 (rowO, colO, B);              
                  answer = winner(B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS);
             }
             
    }
    
    //**********************************
    //This function displays the board *
    //**********************************
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    //***************************************
    // This function initializes the board  *
    //***************************************
    
    void initBoard ( char array[][COLS], int rows )
    {
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
    }
    
    //*****************************************
    //This function records moves for player1 *
    //*****************************************
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    //*****************************************
    //This function records moves for player2 *
    //*****************************************
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    //*************************************************
    //This function determines the winner of the game *
    //*************************************************
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2] && B[0][2] != '*')
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2] && B[1][2] != '*')
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2] && B[2][2] != '*')
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0] && B[2][0] != '*')
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1] && B[2][1] != '*')
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2] && B[2][2] != '*')
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2] && B[2][2] != '*')
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0] && B[2][0] != '*')
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
            cout << "player " << answer << " has won\n" ;
         return answer;
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Its cause your initBoard function doesnt work

    You cant change a variable by simply passing it to a function, try this:

    Code:
    ...
    initBoard (char &array[][COLS],  int rows);
    ...
    That should do the trick (so long as you put it in the prototype and the in the function definition)

    =)

    Good luck!
    Last edited by Junior89; 04-12-2007 at 08:30 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Error message trying to pass array by reference...

    I get some error messages when I try and pass it that way.

    line: 13 declaration of `array' as array of references
    In function `int main()':
    line: 30 invalid conversion from `char (*)[3]' to `int'
    line: 13 too many arguments to function `void initBoard(int)'
    line: 30 at this point in file
    line: 30 At global scope:
    line: 91 declaration of `refArray' as array of references

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char &array[][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;                                                                   //Variable to hold response for new game
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};           //Initiailizing tictactoe board
        char answer = 'n' ;                                                         //Initializing winner
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
                                                                 //Initializing playing board
        do
        {
             initBoard(B, ROWS);
             displayBoard(B, ROWS);
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    //************************************************************
    //This function allows players to play 1 game of tic tac toe *
    //************************************************************
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
        
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B);               
                  answer = winner(B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);
                  player2 (rowO, colO, B);              
                  answer = winner(B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS);
             }
             
    }
    
    //**********************************
    //This function displays the board *
    //**********************************
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    //***************************************
    // This function initializes the board  *
    //***************************************
    
    void initBoard ( char &refArray[][COLS], int rows )
    {
        char refArray[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
    }
    I only put up part of the program that involves the initBoard function and I have tried putting an & just about everywhere.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > initBoard (char &array[][COLS], int rows);
    No, arrays are passed as a pointer anyway.

    initBoard just needs the two nested loops to assign each member of the array a value - namely '*' (just like the one to print it)

    There is no such thing as array assignment in C or C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Maybe I did it wrong...

    K so I made my initBoard just like my display board because my display board displays a 3x3 array of *'s when I start. The problem I have is when it asks to play another game and I say yes the board does not clear when I do the initBoard function. Do I need to do something to my loop?

    Here is my entire program:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char array[][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;                                                                   //Variable to hold response for new game
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};           //Initiailizing tictactoe board
        char answer = 'n' ;                                                         //Initializing winner
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
                                                                 //Initializing playing board
        do
        {
             initBoard(B, ROWS);
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    //************************************************************
    //This function allows players to play 1 game of tic tac toe *
    //************************************************************
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
        
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B);               
                  answer = winner(B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);
                  player2 (rowO, colO, B);              
                  answer = winner(B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS);
             }
             
    }
    
    //**********************************
    //This function displays the board *
    //**********************************
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    //***************************************
    // This function initializes the board  *
    //***************************************
    
    void initBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
      
    }
    
    //*****************************************
    //This function records moves for player1 *
    //*****************************************
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    //*****************************************
    //This function records moves for player2 *
    //*****************************************
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    //*************************************************
    //This function determines the winner of the game *
    //*************************************************
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2] && B[0][2] != '*')
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2] && B[1][2] != '*')
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2] && B[2][2] != '*')
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0] && B[2][0] != '*')
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1] && B[2][1] != '*')
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2] && B[2][2] != '*')
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2] && B[2][2] != '*')
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0] && B[2][0] != '*')
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
            cout << "player " << answer << " has won\n" ;
         return answer;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    When I said use the same loop as display board, I didn't think you'd be that dim to just copy the code character for character!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Sorry about that...

    Yea sorry I feel like a retard, I got it tho.

    Code:
    void initBoard ( char array[][COLS], int rows )
    {
         for (int a = 0; a < ROWS;  a++)
         {
             for (int b = 0; b < COLS; b++)
             { 
                 array [a][b] = '*';
             }
             cout << endl;
         }
      
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Isn't that what I said to do in the other thread? Please ask for specific clarification if you don't understand something instead of repeating the question in a new thread.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Yes...

    Yea thats what you told me to do and I asked why the function I tried didn't work and no one responded. So I thought my question would be answered faster in a new thread and it was.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Sorry, I actually typed out an answer to your question and ended up not posting it for some reason, so my post above was in reference to an answer that doesn't exist.

  11. #11
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Sorry, my bad, ha, it works for vectors though haha I dunno if i'll go back to using arrays again after using vectors, i think they're much better =)
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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