Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Dice Game Help Please!
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
a22asin




PostPosted: Mon Sep 15, 2014 6:54 pm   Post subject: Dice Game Help Please!

Hi, so i finally started learning c++ in my 2nd year uni. I got my first assigment where i have to create a dice game for 2 players or a player and a bot where each player takes turn rolling the dice. The game continues till one user reaches a score of 100 or more. If the player roles a 1, the player doesnt get any points. Furthermore, when one player finally reaches 100 or more, the other player gets one last time to role and try to catch up.

My program works for the most part, but my problem is that when the player chooses to play against a bot, the program seems to skip the ask user to continue and just keeps playing the game till a winner occurs. The part the program seems to skip is separated

code:


/*
Press Your Luck is a dice game where the players take turn rolling the die till one of the player reaches 100 points. 
*/

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
       
        int score1=0;
        int score2=0;
        int tempscore=0;
        int dice=0;
        int whoTurn =1;
        char cont = 'y';
        char player;
        bool lastTry = false;
        bool lastGame = false;
        srand(time(NULL));
       
        cout << "Do you want to play against a player (p) or a bot (b): ";
        cin >> player;
       
        do {
                //Runs through the current player's turn
                while (cont == 'y'){
                        cout << "Player " << whoTurn << ": Your Turn" << endl;
                        dice = rand()%6 + 1;
                        cout << "Dice :" <<  dice << endl;
                       
                        //checks the results from the die and does the according actions
                        if (dice != 1 ){
                                tempscore = tempscore + dice;



                                if (player == 'p'){ 
                                        cout << "Your score is currently: " << tempscore << endl << "Would you like to continue (y/n)?: "<< endl;
                                        cin >> cont;
                                }else if (player == 'b' && whoTurn == 2){
                                        if((rand()%2 + 1) == 1){
                                                cout << "Bot is continuing..." << endl;
                                                cont = 'y';
                                        }else if ((rand()%2 + 1) == 2){
                                                cout << "Bot is ending turn..." << endl;
                                                cont = 'n';
                                        }
                                }




                        }else if (dice == 1){
                                tempscore = 0;
                                cout << "Sorry, your score is 0 and you must end your turn." << endl;
                                cont = 'n';
                        }
                }       

                //checks whose turn it is and applies the score to the correct player      
                if (whoTurn == 1){
                        score1 = score1 +  tempscore;
                        tempscore = 0;
                        whoTurn = 2;
        }else if (whoTurn == 2){
                        score2 = score2 + tempscore;
                        tempscore = 0;
            whoTurn = 1;
                }
               
                //outputs the current score          
                cout <<endl<< "SCORE [Player: " << score1 << " Player2: " << score2 <<"]"<< endl <<endl;
                cont = 'y';

                //checks for a winner and gives the other  player the last turn if a winner is found
                if (score1 >= 100 || score2 >= 100){
                        if (lastTry == true){
                                lastGame = true;
                        }
                        if (lastTry == false){
                                lastTry = true;
                                if (score1 >= 100){
                                        cout << "Player1 is winning! Player2, you have one last try to catch up!" << endl;
                                }else if (score2 >= 100){
                                        cout << "Player2 is winning! Player1, you have one last try to catch up!" << endl;
                                }
                        }
                }
        }while (lastGame == false);
       
        //Outputs the winner of the game
        if (score1 > score2){
                cout << endl << "Congrats Player1, you won the game!" << endl << endl;
        }else if (score1 < score2){
                cout << endl << "Congrats Player2, you won the game!" << endl << endl;
        }else if (score1 == score2){
                cout << endl << "Tie, Congrats Player1 and Player2, you have tied the game!" << endl << endl;
        }       
}


Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Sep 15, 2014 8:07 pm   Post subject: RE:Dice Game Help

The exit condition is still checked when you use continue.

http://msdn.microsoft.com/en-us/library/6e3dc2z3.aspx

Quote:

In a do or while loop, the next iteration starts by reevaluating the controlling expression of the do or while statement.


Continue is designed to just skip the rest of the code in the current iteration.

If you are only checking the exit conditions after player 2 rolls, then only ____ the ____ _____ after player _ rolls.
a22asin




PostPosted: Mon Sep 15, 2014 8:24 pm   Post subject: Re: RE:Dice Game Help

Zren @ Mon Sep 15, 2014 8:07 pm wrote:
The exit condition is still checked when you use continue.

http://msdn.microsoft.com/en-us/library/6e3dc2z3.aspx

Quote:

In a do or while loop, the next iteration starts by reevaluating the controlling expression of the do or while statement.


Continue is designed to just skip the rest of the code in the current iteration.

If you are only checking the exit conditions after player 2 rolls, then only ____ the ____ _____ after player _ rolls.


Ok, but thats not my problem now, thanks though. i edited my post with the new problem im encountering instead of making a new one.
Zren




PostPosted: Tue Sep 16, 2014 12:38 am   Post subject: RE:Dice Game Help Please!

c++:

bool lastGame = false;

cout << "Do you want to play against a player (p) or a bot (b): ";
cin >> player;

do {
    while (cont == 'y') {
   
    }
} while (lastGame == false);


Do you see the bug now? At what point will you enter in inner loop? What is the value of cont after first entering the outer loop?
a22asin




PostPosted: Tue Sep 16, 2014 7:57 am   Post subject: Re: RE:Dice Game Help Please!

Zren @ Tue Sep 16, 2014 12:38 am wrote:
c++:

bool lastGame = false;

cout << "Do you want to play against a player (p) or a bot (b): ";
cin >> player;

do {
    while (cont == 'y') {
   
    }
} while (lastGame == false);


Do you see the bug now? At what point will you enter in inner loop? What is the value of cont after first entering the outer loop?


To be honest, i dont see the bug. cont is intilized with 'y', since the outer loop is a do while, the program will run atleast once before checking conditions. The value of cont after the while loop is the problem. It seems that the program always finds the "else if (player == 'b' && whoTurn == 2){" to be true even when it shouldnt be. It doesnt make sense to me.

Also, the problem seems to only occur when the player selects to play against a bot (b).
DemonWasp




PostPosted: Tue Sep 16, 2014 9:33 am   Post subject: RE:Dice Game Help Please!

code:

if((rand()%2 + 1) == 1){
    cout << "Bot is continuing..." << endl;
    cont = 'y';
}else if ((rand()%2 + 1) == 2){
    cout << "Bot is ending turn..." << endl;
    cont = 'n';
}


If the first expression is false, then the second expression will be evaluated, which will call rand() again. That's probably not what you meant to do.

But that's not the real bug. Look at this;

code:

if (player == 'p'){ 
    // ...snip...
}else if (player == 'b' && whoTurn == 2){
    // ...snip...
}


Your program initializes whoTurn = 1. If player == 'b', then what happens?
a22asin




PostPosted: Tue Sep 16, 2014 11:04 am   Post subject: Re: RE:Dice Game Help Please!

DemonWasp @ Tue Sep 16, 2014 9:33 am wrote:
code:

if((rand()%2 + 1) == 1){
    cout << "Bot is continuing..." << endl;
    cont = 'y';
}else if ((rand()%2 + 1) == 2){
    cout << "Bot is ending turn..." << endl;
    cont = 'n';
}


If the first expression is false, then the second expression will be evaluated, which will call rand() again. That's probably not what you meant to do.

But that's not the real bug. Look at this;

code:

if (player == 'p'){ 
    // ...snip...
}else if (player == 'b' && whoTurn == 2){
    // ...snip...
}


Your program initializes whoTurn = 1. If player == 'b', then what happens?


the rand() in the if statment is a way to make the AI choose yes or no. Uhm, i kinda see whats the problem; my 1st if only considers if 'player == p' and the else if only considers if player == b AND whoTurn == 2. So technically my if statment doesnt consider player1's. Damn this is confusing lol. Thanks as i got it to work.
benjemin23




PostPosted: Fri Jan 03, 2020 2:14 am   Post subject: Re: Dice Game Help Please!

Hello there,

a22asin wrote:

DemonWasp @ Tue Sep 16, 2014 9:33 am wrote:
code:

if((rand()%2 + 1) == 1){
    cout << "Bot is continuing..." << endl;
    cont = 'y';
}else if ((rand()%2 + 1) == 2){
    cout << "Bot is ending turn..." << endl;
    cont = 'n';
}


If the first expression is false, then the second expression will be evaluated, which will call rand() again. That's probably not what you meant to do.

But that's not the real bug. Look at this;

code:

if (player == 'p'){ 
    // ...snip...
}else if (player == 'b' && whoTurn == 2){
    // ...snip...
}


Your program initializes whoTurn = 1. If player == 'b', then what happens?


the rand() in the if statment is a way to make the AI choose yes or no. Uhm, i kinda see whats the problem; my 1st if only considers if 'player == p' and the else if only considers if player == b AND whoTurn == 2. So technically my if statment doesnt consider player1's. Damn this is confusing lol. Thanks as i got it to work.


Well done and i try to use this code but code running problem so what can i do?
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: