Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Game Loop help

Author
Message
Hashmark
12
Years of Service
User Offline
Joined: 6th Jul 2011
Location:
Posted: 28th Jul 2011 04:02
Hello again all i am in need of some help here. I know the code i have is rough, and most likely not the most efficient way to write this but I am still learning and making all kinds of mistakes using switches.

I am using a lot of "if's" anyways the program works fine as i have it with 2 missing things i'd like to add.

the first being I would like for the game to reset and ask for another round of input instead of just closing.

secondly i want this to be a best of 3 or 5 RPS game and when


here is the long (probably messy to a seasoned programmer) code to what I have.



once I get this working with some veteran help I will add in the mouse clicks and change the atoi to image clicks.

thank you in advance for your time, and Thank you for helping a newbie.

Thank you for helping!
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 28th Jul 2011 21:03
I have looked through the program and have several suggestions for you. I will post them in a few hours from now.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 29th Jul 2011 00:12 Edited at: 29th Jul 2011 00:15
As promised, here are my suggestions about the program. The way you tried to implement the exit condition is almost good, but you put this block inside the main loop (LoopGDK):


It's not in a very good place there. What you need is to increment a counter only at the moment when somebody wins. The proper place to handle the counters will be in the winLose() function. Delete this block from the main loop.

The counter variables are not initialized. Always initialize your variables because in C++ they are not guaranteed to be zero!
Instead, they may contain a random garbage value and cause strange bugs. When you declare a variable, give it an initial value:


To continue playing through several rounds, all you need to do is move your game playing functions inside the main loop:



These are at present before the loop. If you put them inside, they will be repeatedly called until you set stillPlaying to false.

There should be dbSync and wait commands after the final announcements, otherwise the program exits without displaying the final result:



In the cpuPlay() function, you generate random numbers with dbRnd(2), but this will give numbers between 0-2 and you need numbers between 1-3. Add one to it:


In the GamePlay() function, you get the player input:


Dark GDK functions which return a pointer to a string (char*) tend to cause memory leaks because the temporary string does not get deleted. It is better to use such function this way:


The winLose() function tries to determine who won by comparing the choices:


Unfortunately it's not that simple. Consider what happens when the player chose Scissors (3) and the CPU chose rock (1). The program would say that the player wins, but the CPU should win. The problem is that the comparison is "circular", not linear. (The "if" structure is not completely correct anyway, because I think an "else" is missing from the second block.) I modified the comparisons in this function, and also included incrementing the counters:


(Notice also that I put the wait command in the beginning, because if there is something that you execute in every branch in an "if" command, then it is not conditional any more.)

After these changes, I think the program works well. Since I don't have your images, I tested it by printing text on the screen instead, but I did run it several times.

A final remark: the program currently does not have a protection against the user entering an invalid value (a number not between 1-3 or a text that is not a number at all). You might try to validate the user input, this is a bigger task so I don't have a ready-made suggestion, but it would also be a good programming exercise.

Login to post a reply

Server time is: 2024-05-24 02:21:31
Your offset time is: 2024-05-24 02:21:31