dimanche 21 avril 2019

c++ can't figure out why for loop isn't showing different parts of array

So I'm finishing up a c++ program, where I needed 2 virtual dice to roll, and then display what each dice rolled and which one was higher. I'm so close to finishing and I know I'm missing something stupid, but when I display the rolls, they are all the same. Not sure if the problem is in the for loop or the section that actually generates the numbers.

                    for (int i = 0; i < rounds; i++)
                    {

                            //fills array with randomly rolled numbers
                            score[i][0] = player1->roll();
                            score[i][1] = player2->roll();

                            cout << "Player 1 dice is " << player1->getType();
                            cout << " with " << player1->getSides() << " sides." << endl;

                            cout << "Player 2 dice is " << player2->getType();
                            cout << " with " << player2->getSides() << " sides." << endl;


                            cout << "Player 1 rolls " << score[i][0] << endl;
                            cout << "Player 2 rolls " << score[i][1] << endl;
                            //tests to see who won each round and adds
                            if (score[i][0] > score[i][1])
                                    p1Score++;
                            else if (score[i][0] < score[i][1])
                                    p2Score++;

                            cout << "Player 1 has won " << p1Score << " times" << endl;
                            cout << "Player 2 has won " << p2Score << " times" << endl;
                            cout << "-----------------------------------------" << endl;
                    }

                    //tests to see who won the game overall
                    if (p1Score > p2Score)
                            cout << "Player 1 wins." << endl;
                    else if (p1Score < p2Score)
                            cout << "Player 2 wins." << endl;
                    else
                            cout << "The players have tied." << endl;
                    delete[] score;

            }

};

endif

OUTPUT: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Welcome. What would you like to do? 1. Play game 2. Exit game 1 How many rounds will be played? 4 What type of die for player 1? Please enter 1 for regular and 2 for loaded. 2 What type of die for player 2? Please enter 1 for regular and 2 for loaded. 1 What size die for player 1? 6 What size die for player 2? 7 Player 1 dice is loaded with 6 sides. Player 2 dice is regular with 7 sides. Player 1 rolls 0 Player 2 rolls 3 Player 1 has won 0 times

Player 2 has won 1 times

Player 1 dice is loaded with 6 sides. Player 2 dice is regular with 7 sides. Player 1 rolls 0 Player 2 rolls 3 Player 1 has won 0 times

Player 2 has won 2 times

Player 1 dice is loaded with 6 sides. Player 2 dice is regular with 7 sides. Player 1 rolls 0 Player 2 rolls 3 Player 1 has won 0 times

Player 2 has won 3 times

Player 1 dice is loaded with 6 sides. Player 2 dice is regular with 7 sides. Player 1 rolls 0 Player 2 rolls 3 Player 1 has won 0 times

Player 2 has won 4 times

Player 2 wins.

and here is my roll function:

            //function to generate a random number
            virtual int roll()
            {
                    srand((unsigned)time(NULL));
                    return (rand()%sides) + 1;
            }

sorry if this is formatted terribly, first post here. any help appreciated, thanks




Aucun commentaire:

Enregistrer un commentaire