Thread: Tic Tac Toe Problem. Need HELP!!!!

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    Tic Tac Toe Problem. Need HELP!!!!

    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    void playerTurn(int *playerPickRow, int *playerPickCol);
    void compTurn(int *compPickRow, int *compPickCol);
    void playGame();
    void playerWin(char *array, int value1, int value2);
    int main()
    {
        cout << "Welcome to TicTacToe v.1" << endl;
        cout << "Programmed by: Ryan Nielson\n\n" << endl;
        playGame();
        system("PAUSE");
        return 0;
    }
    
    void playGame()
    {
        bool playerWin = true;
        int playerPickCol, playerPickRow;
        int compPickCol, compPickRow;
        int i, j;
        char array[3][3] = {{'.', '.', '.'}, 
                            {'.', '.', '.'},
                            {'.', '.', '.'}};
        
        do {
            playerTurn(&playerPickRow, &playerPickCol);
            while (array[playerPickRow][playerPickCol] == 'O')
            {
                    cout << "Do not place your piece on the opponents. Please try again." << endl;
                    playerTurn(&playerPickRow, &playerPickCol);
            }
            array[playerPickRow][playerPickCol] = 'X';
            playerWin(array, 3, 3);
            compTurn(&compPickRow, &compPickCol);
            while ((array[compPickRow][compPickCol] == 'X') || (array[compPickRow][compPickCol] == 'O') )
            {
                    compTurn(&compPickRow, &compPickCol);
            }
            array[compPickRow][compPickCol] = 'O';
            cout << endl;
            for (i = 0; i < 3; i++) {                
                    for (j = 0; j < 3; j++) {
                                    cout << array[i][j];
                    }
                    cout << endl;
            }
            cout << endl;
        }while (playerWin != false);
    }
         
    void playerTurn(int *playerPickRow, int *playerPickCol)
    {
        cout << "Please pick a column: ";
        cin >> *playerPickCol;
        cout << "Please pick a Row: ";
        cin >> *playerPickRow;
        *playerPickRow -= 1;
        *playerPickCol -= 1;
    }
    void compTurn(int *compPickRow, int *compPickCol)
    {
    							// Get Dice Value
    	*compPickRow = (rand () % 3);
    	*compPickCol = (rand () % 3);
    }
    void playerWin(char *array, int value1, int value2)
    {
            if ((array[0][0] == 'X') && (array[0][1] == 'X') && (array[0][2] == 'X') )
            {
                    cout << "X wins";
            }
            if ((array[1][0] == 'X') && (array[1][1] == 'X') && (array[1][2] == 'X') )
            {
                    cout << "X wins";
            }
            if ((array[2][0] == 'X') && (array[2][1] == 'X') && (array[2][2] == 'X') )
            {
                    cout << "X wins";
            }
            if ((array[0][0] == 'X') && (array[1][0] == 'X') && (array[2][0] == 'X') )
            {
                    cout << "X wins";
            }
            if ((array[0][0] == 'X') && (array[1][1] == 'X') && (array[2][1] == 'X') )
            {
                    cout << "X wins";
            }
            if ((array[0][2] == 'X') && (array[1][2] == 'X') && (array[2][2] == 'X') )
            {
                    cout << "X wins";
            }
    }

    Can anyone tell me whats wrong with it.
    I know its in the playerWin function. I think im pointing to the array wrong.
    Can anyone tell me?
    Learning C++
    Programmer in training

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Change this:
    Code:
    void playerWin(char *array, int value1, int value2)
    // To this
    void playerWin(char **array, int value1, int value2)
    You have declared array and you use it as a 2D array but you said to the compiler that it is a 1D array in the original code. (I hope this made sence LOL).

  3. #3
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    playerWin(array, 3, 3);


    THere seems to be a problem there
    Learning C++
    Programmer in training

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void playerWin(char **array, int value1, int value2)
    No, this is not how you pass a 2D array.

    Code:
    void playerWin(char array[][3], int value1, int value2)
    You have to specify the sizes of all the minor dimensions.

    For simplicity (because it's dead easy to remember), you can just paste the declaration of the array you want to pass
    Code:
    void playerWin(char array[3][3], int value1, int value2)
    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.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    >No, this is not how you pass a 2D array.

    Ooooops, guess I should have tested it first.

  6. #6
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    I'm having a problem with this line in my code now

    playerWin(array, 3, 3);

    The error says : "playerWin" cannot be used as a.

    thats all it says.

    Ive tried playerWin(array[3][3], 3, 3); but still nothing
    any ideas?
    Learning C++
    Programmer in training

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    bool playerWin = true;
    This isn't a function

    void playerWin(char array[][3], int value1, int value2);
    This is.

    Unfortunately for you, scope rules win, so you get the one with the nearest scope.
    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.

  8. #8
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    yea I noticed the playerWin bool.
    I forgot I made that
    I know what the problem is now though.
    Thx alot guys
    Learning C++
    Programmer in training

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 10:43 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  4. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  5. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM