Jump to content
  • Advertisement
Sign in to follow this  
shadowmark

game loop (C++)

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Ive created this program where it asks you what kind of dice you would like to roll. their a two options a 6 sided dice or a 20 sided dice. Once it rolls it then asks if you want to roll again. (y/n). I have this statement here: if (again == 'y') cout << "You want to roll again.\n"; break; and my question is when they select to roll again how do i get it to do the whole program again starting with which dice you would like to roll?

Share this post


Link to post
Share on other sites
Advertisement
You would need to place the program in a loop. Something like this

do
{


Program here


Roll again?

}
while(again == 'y');

Share this post


Link to post
Share on other sites
These are the two ways that I can think of.
int main(){
beginning:
// Some code here.
if(again == 'y'){
goto beginning;
}
return 0;
}

// Or

int main(){
do{
// Some code here.
}
while(again == 'y');
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by shadowmark
what do you mean by "Program here"

do you mean like the name that i saved the program as?


i guess he means the program source code. the part where the "dice is rolled".

i would advice not to use GoTo´s.


greets DeviceLost

Share this post


Link to post
Share on other sites
I still don't see what you mean. here's my code.

// Which Die Roll

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;

int main()
{
cout << "Dice choices\n\n";
cout << "1 - d6\n";
cout << "2 - d20\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked a d6.\n";
{
srand(time(0));

int randomRoll = rand();

int die = (randomRoll % 6) + 1;
cout << "You rolled a " << die << endl;
}

break;
case 2:
cout << "you picked a d20.\n";
{
srand(time(0));

int randomRoll = rand();

int d20 = (randomRoll % 20) + 1;
cout << "You rolled " << d20 << endl;
}

break;
default:
cout << "You made an illegal choice.\n";
}

char again = 'y';
while (again == 'y')
{
cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;

while (again == 'y')
cout << "You want to roll again.\n ";
break;

}
if (again != 'y')
cout << "\nOkay, bye.";

getch();
return 0;
}

sry it takes up so much space, i don't kno how to put it in one of those scrolling menus

Share this post


Link to post
Share on other sites
first, using code tags is described here:
http://www.gamedev.net/community/forums/faq.asp

you could solve the problem like this:




// Which Die Roll

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;


void Roll6SidedDice()
{
srand(time(0));

int randomRoll = rand();

int die = (randomRoll % 6) + 1;
cout << "You rolled a " << die << endl;

}


void Roll20SidedDice()
{

srand(time(0));

int randomRoll = rand();

int d20 = (randomRoll % 20) + 1;
cout << "You rolled " << d20 << endl;
}

int main()
{
cout << "Dice choices\n\n";
cout << "1 - d6\n";
cout << "2 - d20\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked a d6.\n";
Roll6SidedDice();

}

break;
case 2:
{
cout << "you picked a d20.\n";
Roll20SidedDice();
}

break;
default:
cout << "You made an illegal choice.\n";
}

char again;
bool roll_again =true;


while(roll_again)
{

cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;

if(again == 'y')
{
Roll6SidedDice(); //....

}
else
{
cout << "\nOkay, bye.";
roll_again = false;
}

}
getch();
return 0;
}





i havent tested the code but i guess you to solve the problem easily by now


greets DeviceLost

Share this post


Link to post
Share on other sites
Sorry my first post was a bit unclear. Try this



// Which Die Roll

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;

int main()
{

// Start Loop Here
do
{
cout << "Dice choices\n\n";
cout << "1 - d6\n";
cout << "2 - d20\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked a d6.\n";
{
srand(time(0));

int randomRoll = rand();

int die = (randomRoll % 6) + 1;
cout << "You rolled a " << die << endl;
}

break;
case 2:
cout << "you picked a d20.\n";
{
srand(time(0));

int randomRoll = rand();

int d20 = (randomRoll % 20) + 1;
cout << "You rolled " << d20 << endl;
}

break;
default:
cout << "You made an illegal choice.\n";
}


// Ask if you want to roll again
char again;

cout << "\nGood roll, do you want to roll again? (y/n): ";
cin >> again;
}
while (again == 'y'); // End the loop here.
// Loop will continue as long as again is equal to 'y'.


// Exited loop so we quit.
cout << "\nOkay, bye.";
return 0;
}



Share this post


Link to post
Share on other sites
rycons code is even better as the choice between 6 sided or 20 sided is also repeated. use his version!



greets DeviceLost

Share this post


Link to post
Share on other sites
I think you should effectively learn to use GoTos. Not only do they make your program neater, and easier to read, but they also help organize your code in a logical fashion.

</joke>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!