I recently finished a tutorial for the creation of a CRAPS game. However, the finished game was hard to follow because the text in the console got longer and longer, and no matter how well formatted, it just didn't look good.
I decided to try to clear the screen at the end of the main loop, so that it would have a clear screen for the next pass. (Sorry if any of this is hard to follow, I'm not really sure about some of the terminology) Obviously, for this to work I needed to pause the game at the end of the loop (before clearing the screen), otherwise the player would never see the outcome of his actions.
I did a bit of googling, and the simplest answer (although not necessarily the best, from what I've read) was to use cin.get() to pause.
Unfortunately, this has the irritating side effect of letting the player type random junk into the console at the end of each loop. Normally this would just be an aesthetic issue, but apparently passing more than the single enter key seems to use the extra entries to fill the next few "cin >> variable_name" calls.
What would be the best way to freeze the screen until the user presses [enter]? If possible, I'd like to be able to detect the key they press, so I can exit on [esc]...
Any references or built in functions for me to look into would be great,
Thank you for your time,
Timothy S.
[EDIT]: A little searching revealed the line "cin.ignore(numeric_limits<streamsize>::max(), '\n');" which fixed the problem related to having extra characters typed before hitting enter during pauses, but I'd still like to know if there's a better way to pause, especially one that would let me retrieve the key that was pressed to un-pause. My apologies for not searching more before posting, I should have been able to find this the first time... Unfortunately, adding this appears to make the program require that enter be pressed twice at some pauses...
main.cpp
Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "header.h"
using namespace std;
int main()
{
unsigned long MoneyEarned;
unsigned long Money;
short DiceValue;
short Bet;
short BetMoney;
/* Show Info Screen and Init Game */
ShowIntroScreen();
cout << "Press [ENTER] to begin...";
Pause();
ClearScreen();
Money = 1000;
/* While the player has more than 100 dollars... */
do
{
ShowInformation(Money);
Bet=GetBet();
BetMoney=GetAmount();
DiceValue=DoDiceThrow();
MoneyEarned=DoMoneyCalc(DiceValue, Bet, BetMoney);
Money-=BetMoney;
cout << "The bet: $" << BetMoney << " on ";
switch(Bet)
{
case 1:
cout << "6 and 8.";
break;
case 2:
cout << "4 and 10.";
break;
case 3:
cout << "2 and 12.";
break;
}
cout << endl << "The dice: " << DiceValue << endl << endl;
if(MoneyEarned == 0)
{
cout << "You lost $" << BetMoney << "." << endl << endl;
}
else
{
cout << "You won $" << MoneyEarned-BetMoney << ". ";
cout << "($" << MoneyEarned << " Won - $" << BetMoney << " Bet)" << endl << endl;
Money+=MoneyEarned;
}
cout << "Press [ENTER] to continue...";
Pause();
Pause();
ClearScreen();
}
while(Money>100);
cout << "You are down to your last $100, which you save for the ride home." << endl;
return 0;
}
header.cpp (header.h contains only the function definitions)
Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "header.h"
using namespace std;
/* FUNCTION DEFINITIONS */
void Pause(void)
{
cin.get();
}
void ClearScreen(void)
{
system("cls");
}
void ShowIntroScreen(void)
{
cout << " Welcome to Craps 1.0" << endl << endl;
cout << "Rules: " << endl;
cout << "You start with $1000 to bet." << endl;
cout << "There are three possible bets: " << endl;
cout << "2 and 12 (Win Ratio 5:1)" << endl;
cout << "4 and 10 (Win Ratio 2.5:1)" << endl;
cout << "6 and 8 (Win Ratio 1.5:1)" << endl << endl;
cout << "You can bet between $10 and $100 each round." << endl << endl;
}
void ShowInformation(unsigned long Money)
{
cout << "CRAPS 1.0" << endl << endl;
cout << "Money: $" << Money << endl << endl;
}
short GetBet(void)
{
unsigned short BetType;
cout << "Type of Bet (1 - 6/8, 2 - 4/10, 3 - 2/12): ";
cin >> BetType;
if ((BetType==1) || (BetType==2) || (BetType==3))
{
return BetType;
}
else
{
return 1;
}
}
short DoDiceThrow(void)
{
short DiceValue;
srand(time(NULL));
DiceValue = (rand() % 6) + 1;
DiceValue += (rand() % 6) + 1;
return DiceValue;
}
unsigned short DoMoneyCalc(short Dice, short Bet, short BetMoney)
{
unsigned long MoneyEarned;
switch(Bet)
{
case 1:
if ((Dice==6) || (Dice==8))
{
MoneyEarned=BetMoney*1.5;
}
else
{
MoneyEarned=0;
}
break; break;
case 2:
if ((Dice==4) || (Dice==10))
{
MoneyEarned=BetMoney*2.5;
}
else
{
MoneyEarned=0;
}
break; break;
case 3:
if ((Dice==2) || (Dice==12))
{
MoneyEarned=BetMoney*5;
}
else
{
MoneyEarned=0;
}
break;
/*default:
MoneyEarned=0;
cout << "LOST!";
break;*/
}
return MoneyEarned;
}
unsigned long GetAmount(void)
{
unsigned short BetAmount;
cout << "Bet Amount ($10-$100): ";
cin >> BetAmount;
if(BetAmount < 10)
{
BetAmount=10;
}
if(BetAmount > 100)
{
BetAmount=100;
}
return BetAmount;
}