Computer Science Canada Dice Game Help Please! |
Author: | a22asin [ 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
|
Author: | Zren [ 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. |
Author: | a22asin [ 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. |
Author: | Zren [ Tue Sep 16, 2014 12:38 am ] | ||
Post subject: | RE:Dice Game Help Please! | ||
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? |
Author: | a22asin [ Tue Sep 16, 2014 7:57 am ] | ||
Post subject: | Re: RE:Dice Game Help Please! | ||
Zren @ Tue Sep 16, 2014 12:38 am wrote:
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). |
Author: | DemonWasp [ Tue Sep 16, 2014 9:33 am ] | ||||
Post subject: | RE:Dice Game Help Please! | ||||
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;
Your program initializes whoTurn = 1. If player == 'b', then what happens? |
Author: | a22asin [ Tue Sep 16, 2014 11:04 am ] | ||||
Post subject: | Re: RE:Dice Game Help Please! | ||||
DemonWasp @ Tue Sep 16, 2014 9:33 am wrote:
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;
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. |
Author: | benjemin23 [ 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:
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;
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? |
: |