views:

147

answers:

3

SigTerm said this: "Smells like a bad code. squareOne..squareSix should be probably array. Instead of char constants, enums or ints should be probably used... " How do i do that with this code for the board also if someone could suggest the code I would have to use to get users to enter the row and column for the X and O for the game.

void showboard(char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
                char &squareEight, char &squareNine)
{

    cout << squareOne << "|" << squareTwo << "|" << squareThree << endl
            << "-+-+-"<< endl
            << squareFour << "|" << squareFive << "|" << squareSix << endl
            << "-+-+-"<< endl
            << squareSeven << "|" << squareEight << "|" << squareNine << endl;

The problem I am having is taking this code and turning it to the a function. Listed below is the code of what I am trying to make into a function. Followed after it the "checkboard" function is me trying to make the code above it into a function

  bGameOver     = false;
                bool bWinGame   = true;
                // Check for end of game conditions
                if (squareOne != '1') {
                    if (squareTwo == squareOne && squareThree == squareOne) {
                        bGameOver = true;
                    }
                    if (squareFour == squareOne && squareSeven == squareOne) {
                        bGameOver = true;
                    }
                }
                if (squareFive != '5') 
                {
                    if (squareOne == squareFive && squareNine == squareFive) 
                    {
                        bGameOver = true;
                    }
                    if (squareTwo == squareFive && squareEight == squareFive) 
                    {
                        bGameOver = true;
                    }
                    if (squareFour == squareFive && squareSix == squareFive) 
                    {
                        bGameOver = true;
                    }
                    if (squareThree == squareFive && squareSeven == squareFive) 
                    {
                        bGameOver = true;
                    }
                }
                if (squareNine != '9') 
                {
                    if (squareThree == squareNine && squareSix == squareNine) 
                    {
                        bGameOver = true;
                    }
                    if (squareSeven == squareNine && squareEight == squareNine) 
                    {
                        bGameOver = true;
                    }
                }
                /* Need to check the board full (no-win condition*/)
                if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
                    squareFour != '4' && squareFive != '5' && squareSix != '6' &&
                    squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
                {
                    bGameOver = true;
                    bWinGame = false;
                }

                system("cls");

                if (bGameOver) 
                {
                    if (bWinGame) 
                    {
                        cout << "Player " << currentPlayer << " wins!" << endl;
                    }
                    // Print ending board
                    cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
                    cout << "-+-+-"<< endl;
                    cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
                    cout << "-+-+-"<< endl;
                    cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;

                    cout << "Game Over!" << endl;
                    cout << "Play again (y/n)?" << endl;
                    char cPlayAgain;
                    cin >> cPlayAgain;

                    if (cPlayAgain == 'y') 
                    {
                        bGameOver = false;
                        /* Clear the board*/
                        squareOne = '1';
                        squareTwo = '2';
                        squareThree = '3';
                        squareFour = '4';
                        squareFive = '5';
                        squareSix = '6';
                        squareSeven = '7';
                        squareEight = '8';
                        squareNine = '9';
                    }
                    currentPlayer[100] = 1;
                } 

            } while (!bGameOver);



void checkboard(char** pCurrentPlayer, char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
                    char &squareEight, char &squareNine, char &playerMarker, bool bGameOver)
    {

            bGameOver       = false;
            bool bWinGame   = true;
            // Check for end of game conditions
            if (squareOne != '1') {
                if (squareTwo == squareOne && squareThree == squareOne) {
                    bGameOver = true;
                }
                if (squareFour == squareOne && squareSeven == squareOne) {
                    bGameOver = true;
                }
            }
            if (squareFive != '5') 
            {
                if (squareOne == squareFive && squareNine == squareFive) 
                {
                    bGameOver = true;
                }
                if (squareTwo == squareFive && squareEight == squareFive) 
                {
                    bGameOver = true;
                }
                if (squareFour == squareFive && squareSix == squareFive) 
                {
                    bGameOver = true;
                }
                if (squareThree == squareFive && squareSeven == squareFive) 
                {
                    bGameOver = true;
                }
            }
            if (squareNine != '9') 
            {
                if (squareThree == squareNine && squareSix == squareNine) 
                {
                    bGameOver = true;
                }
                if (squareSeven == squareNine && squareEight == squareNine) 
                {
                    bGameOver = true;
                }
            }
            // Need to check the board full (no-win condition)
            if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
                squareFour != '4' && squareFive != '5' && squareSix != '6' &&
                squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
            {
                bGameOver = true;
                bWinGame = false;
            }

            system("cls");

            if (bGameOver) 
            {
                if (bWinGame) 
                {
                    cout << "Player " << pCurrentPlayer << " wins!" << endl;
                }
                // Print ending board
                cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
                cout << "-+-+-"<< endl;
                cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
                cout << "-+-+-"<< endl;
                cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;

                cout << "Game Over!" << endl;
                cout << "Play again (y/n)?" << endl;
                char cPlayAgain;
                cin >> cPlayAgain;

                if (cPlayAgain == 'y') 
                {
                    bGameOver = false;
                    // Clear the board
                    squareOne = '1';
                    squareTwo = '2';
                    squareThree = '3';
                    squareFour = '4';
                    squareFive = '5';
                    squareSix = '6';
                    squareSeven = '7';
                    squareEight = '8';
                    squareNine = '9';
                }
                pCurrentPlayer = "1";
            } 

    } 
+1  A: 

In PlayerMove() you are passing in pCurrentPlayer as a char **, which is a pointer to a pointer. Change that to a char * then it should display the contents correctly within the function. You're not changing the value of the pointer in that function so it doesn't need the extra indirection.

Remove the address of operator (&) from the corresponding function call arguments as well.

Amardeep
Thank You this worked
Renge
it solved one part of the problem I was having now I am on to the next
Renge
A: 

This line:

currentPlayer[100] = 1;

Is very problematic.

currentPlayer is supposed to point to either playerOne or playerTwo.

I have no idea what you're trying to do with that line, but it is clearly wrong.

abelenky
Yea it is but I don't knows how to fix that. when it goes to the function it become a problem outside the function when it is in main it isn't.
Renge
To fix it, you should only do a few things with currentPlayer: within main, set "currentPlayer = playerOne;" (or playerTwo). Within a sub-function, you should set "*pCurrentPlayer = playerOne;" (or playerTwo).
abelenky
I did that but the program stops responding after I enter the first location
Renge
A: 

Hey, it just so happens that not long ago, I wrote a c++ tic tac toe 'game' for a blog post.

http://alongbutsimplenameformyblog.blogspot.com/2010/08/tic-tac-toe.html

Here, I tried my best to write code compact, yet readable, and kept performance in mind. I got the program to be 32 lines of code. The only thing I'm not doing, is letting the game be 'playable'.

If you look over my code, you'll be able to see a way to organize your game into a multi-dimensional array. The way you read it (assuming you're un-aware, sorry if I'm telling you things you know) is:

arrayName[rowNumber][columnNumber];

So, in order to change the upper right box of tic tac toe to 'x' you would say:

arrayName[0][2] = 'x';

Keeping in mind, when you use arrays, that it starts at element 0, not element 1. That's something I forgot when starting off, at the very least.

So, hopefully reading my code, you'll be able to see how to check for a win condition fairly easily, in a function. If you have questions about how my code works, or anything further with how to implement this, feel free. I'll be back periodically. Also, as the school year starts in fall, I should be blogging more about stuff like this, when I'm helping first and second year students in my program. So feel free to bookmark or subscribe.

#include < iostream >

const char grid[3][3] = {'x', ' ', 'o', 'o','x', 'o', 'x', 'x', 'o'};

inline const bool checkLine(char x, char y, char z) {
    return (x == y && y == z && x != ' ');
}

inline const bool checkWin(){
    bool a = 0, b = 0;
    for(unsigned char i = 0; i < 3; ++i)
       if(checkLine(grid[i][0], grid[i][1], grid[i][2]) || checkLine(grid[0][i], grid[1][i], grid[2][i]))
           return true;
    for(unsigned char i = 0; i < 2; ++i)
        if(checkLine(grid[i == 0 ? 0 : 2][0], grid[1][1], grid[i == 0 ? 2 : 0][2]))
            return true;
    return false;
}

int main(){
    for (unsigned char i = 0; i < 3; ++i) {
        for (unsigned char j = 0; j < 3; ++j)
            std::cout << grid[i][j];     
        std::cout << "\n";
    }

    if (checkWin()) 
        std::cout << "WINNER\n";

    return 0;
}
Xoorath