views:

59

answers:

2

Is there a way to store the x and y entered by the user into a location for the array.

I have this:

if (x == 1 && y == 1)
        {
            board[0][0] = playerMarker;
        } 

I want to make it so the x and y are stored into a variable that matches the name of a spot in an array.

But I want to try and make it more like this:

if (currentMove == '1' && squareOne == '1')
                squareOne = playerMarker;

This is a code I have seen above. The code bellow is my own.

void playerMove(char* CurrentPlayer, char (&board)[3][3], char &playerMarker)
    {
        bool bValidMove;
        int x,y;
    // Prompt the player for a move
    cout << "Player " << CurrentPlayer << "'s move:" << endl;



    // Loop until we get a valid move
    do 
    {
        cout << "Please enter the row number for the place you wish to mark: " << endl;
        cin >> x;
        cout << "Please enter the column number for the place you wish to mark" << endl;
        cin >> y;
        bValidMove = true;

        // Check for a valid move
        if (x == 1 && y == 1)
        {
            board[0][0] = playerMarker;
        } 
        else if (x == 1 && y == 2) 
        {
            board[0][1] = playerMarker;
        } 
        else if (x == 1 && y == 3) 
        {
            board[0][2] = playerMarker;
        }
        else if (x == 2 && y == 1) 
        {
            board[1][0] = playerMarker;
        }
        else if (x == 2 && y == 2)
        {
            board[1][1] = playerMarker;
        }
        else if (x == 2 && y == 3)
        {
            board[1][2] = playerMarker;
        }
        else if (x == 3 && y == 1)
        {
            board[2][0] = playerMarker;
        }
        else if (x == 3 && y == 2)
        {
            board[2][1] = playerMarker;
        }
        else if (x == 3 && y == 3) 
        {
            board[2][2] = playerMarker;
        }
        else 
        {
            cout << "Invalid Move. Try again." << endl;
            bValidMove = false;
        }

    } 
    while (!bValidMove);

}

This is the code I am trying to make my own into.

// Ask current player for a move
        cout << currentPlayer << "'s move: ";
        bool validMove;
        // Loop until a valid move is chosen
        do
        {
            char currentMove;
            cin >> currentMove;
            validMove = true;

            // Check for a valid move
            if (currentMove == '1' && squareOne == '1')
                squareOne = playerMarker;
            else if (currentMove == '2' && squareTwo == '2')
                squareTwo = playerMarker;
            else if (currentMove == '3' && squareThree == '3')
                squareThree = playerMarker;
            else if (currentMove == '4' && squareFour == '4')
                squareFour = playerMarker;
            else if (currentMove == '5' && squareFive == '5')
                squareFive = playerMarker;
            else if (currentMove == '6' && squareSix == '6')
                squareSix = playerMarker;
            else if (currentMove == '7' && squareSeven == '7')
                squareSeven = playerMarker;
            else if (currentMove == '8' && squareEight == '8')
                squareEight = playerMarker;
            else if (currentMove == '9' && squareNine == '9')
                squareNine = playerMarker;
            else
            {
                cout << "Invalid Move. Try again: ";
                validMove = false;
            }
        } while ( !validMove );
A: 

You could do

char& squareOne = board[0][0];
char& squareTwo = board[0][1];
char& squareThree = board[0][2];
char& squareFour = board[1][0];
...

The ampersand(&) means that squareFour is a reference to board[1][0] (they share the same underlying memory, so changing one is changing both).

Note that instead of 9 'if's, you can compute the row/column and set board[row][column] = playerMarker (row & column may be reversed for you)

Tom Sirgedas
+1  A: 

ok this is just a guess at what you want, but here it goes:

do 
{
    cout << "Please enter the row number for the place you wish to mark: " << endl;
    cin >> x;
    cout << "Please enter the column number for the place you wish to mark" << endl;
    cin >> y;
    bValidMove = true;

    int xx = x-'1';
    int yy = y-'1';

    if ( xx < 0 || xx > 2 || yy < 0 || yy > 2 )
    {
            cout << "Invalid Move (outside board). Try again: ";
            validMove = false;
    } else if ( board[xx][yy] == 0 ) {
            board[xx][yy] = playerMarker;
    } else {
            cout << "Invalid Move (position already taken). Try again: ";
            validMove = false;
    }

etc etc

This assumes the board is a 3x3 array, containing 0 when the position is not taken by a player, and takes row 1/2/3 and col 1/2/3 as input.

Hope this helps...

mvds
Please using the code at the top in the first one I showed change it to match yours. I'm trying to use your code but i just keep getting the error messages when i try debugging it.
Renge
@Renge: could you accept some of the 9 answers people have given to your questions? Furthermore, if you want someone to help, please 1) update your question to the new situation, 2) show what goes wrong (warnings/errors) and 3) be *very* clear in telling what you actually want. "I get error messages" is just too vague!
mvds