开发者

Storing locations in Array as a variable to be changed in a if statement

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 =开发者_Go百科= '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 );


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...


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜