1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <iostream>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include "colours.h"
using namespace std;
//Main
int main(void)
{
int money; //Money user has been assigned
int bet; //Amount waggered by the user
char user_choice; //Rock Paper Scissors option inputted by the user
char comp_choice; //random choice made by computer
int i; // Loop counter
char playagain; //Does the user want to play again?
// Seed the random number generator use the timer
srand((unsigned)time(NULL));
for (i = 0; i<rand(); i++)
rand();
//Assign the user a random amount of money to start
money = rand() % 1000 + 1;
text_colour(f_green, b_black);
cout << "You have $" << money << " to start..." << endl;
//Get the user's choice
text_colour(f_gray, b_black);
cout << "Do you choose (R)ock, (P)aper, or (S)cissors? ";
user_choice = toupper(_getche());
cout << endl;
//Validate user's choice
while (user_choice != 'R' && user_choice != 'P' && user_choice != 'S')
{
text_colour(f_red, b_black);
cout << "INVALID CHOICE ";
cout << "Do you choose (R)ock, (P)aper, or (S)cissors? ";
text_colour(f_gray, b_black);
user_choice = toupper(_getche());
cout << endl;
}//while
//Get the user's bet
cout << "How much do you want to bet? $";
cin >> bet;
//Validate user's bet
while (bet < 1 || bet > money)
{
text_colour(f_red, b_black);
cout << "INVALID BET! ";
cout << "How much do you want to bet? $";
text_colour(f_gray, b_black);
cin >> bet;
}//while
//Computer makes random choice
comp_choice = rand() % 3 + 1; // 1 = Rock, 2 = Paper and 3 = Scissors
//Does the user win, lose, or is it a tie? Print results
switch (user_choice)
{
//If rock was chosen
case 'R': //Find comp choice
switch (comp_choice)
{
case 1: text_colour(f_dcyan, b_black);
cout << " Both you and the computer picked Rock, so it is a tie."; break;
case 2: text_colour(f_dred, b_black);
cout << " You picked Rock, and the computer picked Paper. Paper covers rock, you lose." << endl;
cout << "You lost your $" << bet << ", and now have $";
money -= bet;
cout << money << endl; break;
case 3: text_colour(f_green, b_black);
cout << " You picked Rock, and the computer picked Scissors. Rock smashes Scissors, you win!" << endl;
cout << " You gained your $" << bet << ", and now have $";
money += bet;
cout << money << endl; break;
}//switch
//If paper was chosen
case 'P': //Find comp choice
switch (comp_choice)
{
case 1: text_colour(f_green, b_black);
cout << " You picked Paper, and the computer picked Rock. Paper covers rock, you win!" << endl;
cout << " You gained your $" << bet << ", and now have $";
money += bet;
cout << money << endl; break;
case 2: text_colour(f_dcyan, b_black);
cout << " Both you and the computer picked Paper, so it is a tie."; break;
case 3: text_colour(f_dred, b_black);
cout << " You picked Paper, and the computer picked Scissors. Scissors cut Paper, you lose." << endl;
cout << "You lost your $" << bet << ", and now have $";
money -= bet;
cout << money << endl; break;
}//switch
//If scissors were chosen
case 'S': //Find comp choice
switch (comp_choice)
{
case 1: text_colour(f_dred, b_black);
cout << " You picked Scissors, and the computer picked Rock. Rock smashes scissors, you lose." << endl;
cout << "You lost your $" << bet << ", and now have $";
money -= bet;
cout << money << endl; break;
case 2: text_colour(f_green, b_black);
cout << " You picked Scissors, and the computer picked Paper. Scissors cut paper, you win!" << endl;
cout << " You gained your $" << bet << ", and now have $";
money += bet;
cout << money << endl; break;
case 3: text_colour(f_dcyan, b_black);
cout << " Both you and the computer picked Scissors, so it is a tie."; break;
}//switch
}//user_choice switch
//Check to see if user can or wants to play again
//If they have no money, kick them out
if (money == 0)
{
text_colour(f_red, b_black);
cout << "You are out of money, come back later." << endl;
//Set playagain to No
playagain = 'N';
}//if
else
{//Ask them if they want to play again
text_colour(f_blue, b_black);
cout << "Do you want to play again? (Y/N) ";
text_colour(f_gray, b_black);
playagain = toupper(_getche());
cout << endl;
//Validate the response
while (playagain != 'Y' && playagain != 'N')
{
text_colour(f_red, b_black);
cout << "ILLEGAL CHOICE!";
cout << "Do you want to play again? (Y/N) ";
text_colour(f_gray, b_black);
playagain = toupper(_getche());
cout << endl;
}//while
}//else
while (playagain == 'Y'); //Loop until they say no
//Check to see if they leave with money
if (money > 0)
{
text_colour(f_dgreen, b_black);
cout << "Congrats, you made it out with $" << money << ", I'm really proud, honestly." << endl;
}//if
//return colour to normal
text_colour(f_gray, b_black);
return 0;
} //Main
|