dimanche 31 décembre 2017

Change values in an array suing C++

I want to create the game of Peg Solitaire in C++. To do that I have created an 2D array which prints the values 0s and 1s (1s representing marbles on the board) I have created functions called setupBoard() and displayBoard() as:

void SolitaireGame::setupBoard() {

{
    a = 8;
    b = 8;
    board = new int*[a];
    for (int i = 1; i < a; i++)
        board[i] = new int[b];

    {
        for (int y = 1; y < a; y++)
        {
            for (int x = 1; x < b; x++)
            {
                if ((y == 1 || y == 2 ||y == 6 || y == 7)
                        && (x == 1 || x == 2 || x == 6|| x == 7)) 
                        {                               
                            board[x][y] = 2;
                        }
                else if ((y == 4) && (x == 4))
                {
                    board[x][y] = 0;
                }
                else
                    board[x][y] = 1;
            }
        }
    }
}

}

void SolitaireGame::displayBoard()
{
    cout << "The initial state of your board is: " <<endl << endl;
    for (int y = 1; y < a; y++)
    {
        for (int x = 1; x < b; x++)
        {
            if (board[x][y] == 2)
                cout << " \t";
            else  if (board[x][y] == 1)
                cout << board[x][y] << "\t";
            else if (board[x][y] == 0)
                cout << board[x][y] << "\t";
            else 
                cout << "idf_row_num"<<"\t";
        }
        cout << "\n\n";
    }
}

Now I want to write a function which accepts the values from the user, i.e. the row and column number of the board to access a particular marble and move it to a different valid position on the board. After accepting the values from the user and checking the condition for a valid move, how do I print the board again?

Aucun commentaire:

Enregistrer un commentaire