jeudi 5 décembre 2019

Fill a column without overwriting what's already inside C++

I'm trying to build a 2 player game in which the program rolls the dice and a player has to choose what column to play in, out of 12 columns.

My problem is when the dice rolled (for example: 5) and Player 1 chooses their column. It works fine until Player 2 chooses the same column and overwrites what player 1 had in that column, instead of playing in the available space above it.

Here's my code

void Stage(Users play[])
    {
        int k = 0;
        int dice = 0;


        char space = '-';
        int gridrow = 12;               // max space for rows
        int gridcol = 12;               // max space for column
        int col_num = 1;                // used to print incremented numbers starting at 1 and ending at 12 - column labels not to be trifled with!
        int col_space = 0;              // user will select the column desired
        int col_in = 0;
        //int z = 0;                      // variable for printing out the column labels .. see above
        int counter = 0;
        int x = 0;
        int y = 0;

        // initialized board
        char board[gridrow] [gridcol];
        for (x = 0; x < gridrow; x++)
        {
            for (y = 0; y < gridcol; y++)
            {
                board[x][y] = space;
            }
        }

        do
        {
            //---------------------------- Print --------------------------------
            // this prints the board with all the changes made by the player... this is OK
            string border(48, '-');
            string sub(15, '^');
            system("cls");

            cout << border << endl
                 << "|                    Title                     | \n"
                 << border << endl
                 << "  1   2   3   4   5   6   7   8   9  10  11  12  \n"
                 << border << endl;



            for (x = 0; x < gridrow; x++) // actual grid being printed x is row, y is col... just like a normal x and y axis
            {
                for (y = 0; y < gridcol; y++)
                {
                    cout << "  " << board[x][y] << " ";
                }
                cout << endl;
            }

            int gridcount;
            gridcount = count(*board, *board+12, space); // I want this to count available spaces
            int count1 = count(*board, *board+144, play[0].token);
            int count2 = count(*board, *board+144, play[1].token);

            cout << endl << sub << "\t\t\t " << sub << endl;
            cout << "Player: " << play[0].name << "\t\t\t" << " Player: " << play[1].name << endl;
            cout << "Score: " << count1 << " -> " << play[0].token << "\t\t\t" << " Score: " << count2 << " -> " << play[1].token << endl << endl;


            if (counter % 2 == 0) { //Player 1 turn
                cout << "Dice is rolling..\n";
                Sleep(100);
                dice = DiceRoll();
                cout << "Dice stopped rolling at: " << dice << endl;
                cout << play[0].name <<" Enter column to play in: ";
                cin >> col_in;

                if ((col_in < 1) || (col_in > 12))
                {
                    cin.get();
                    cout << "Turn lost! \n";
                    Sleep(50);
                } else
                {
                    col_space = col_in - 1;
                }


                for (k = 0; k <= arrsize; k++)
                {

                    for (x = gridcol; x > gridcol-(gridcount-(dice+1)); x--)
                    {// this loop allows player pieces to start at the bottom of the board rather than at the top
                        {
                            if (board[x][col_space] == space)
                            {

                                board[x][col_space] = play[0].token;
                                Sleep(50);
                            }
                        }
                    }
                }


                }else{ //Player 2 turn
                cout << "Dice is rolling..\n";
                Sleep(50);
                dice = DiceRoll();
                cout << "Dice stopped rolling at: " << dice << endl;
                cout << play[1].name <<" Enter column to play in: ";
                cin >> col_in;

                if ((col_in < 1) || (col_in > 12))
                {
                    cin.get();
                    cout << "Turn lost! \n";
                    Sleep(50);
                } else
                {
                    col_space = col_in - 1;
                }

                for (k = 0; k <= arrsize; k++)
                {
                    for (x = gridcol; x > gridcol - (dice +1); x--)
                    {// this loop allows player pieces to start at the bottom of the board rather than at the top
                            {
                                if (board[x][col_space] == space && board[x][col_space] != play[0].token)
                                {
                                    board[x][col_space] = play[1].token;
                                    Sleep(50);
                                }
                            }x--;
                    }
                }

                }counter++;
            }while(gridrow != 11 && gridcol != 11); //while (!endgame)



        }

Anyone have any ideas on correcting this?

Aucun commentaire:

Enregistrer un commentaire