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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "graphics.h"
using namespace std;
//function to get player's name
string getname();
string getname()
{
string player;
cout << "What's your name? ";
getline(cin, player);
cout << endl;
return player;
}
//function to determine whether user wants to play
char getyorn();
char getyorn()
{
char yesno;
cin >> yesno;
while ((yesno!='y') && (yesno!='Y') && (yesno!='n') && (yesno!='N'))
{
cout << "Please enter a y or n or Y or N" << endl;
cin >> yesno;
}
if (yesno == 'y' || yesno == 'Y')
return 'y';
else
return 'n';
}
//function to get player's valid bet
int getbet(int cash);
int getbet(int cash)
{
int bet;
cout << "What do you want to bet (up to " << cash << ")? ";
cin >> bet;
while((bet > cash) || (bet <= 0))
{
if (bet > cash)
cout << "You can't bet more than you have!" << endl << endl;
else if (bet <= 0)
cout << "You can't bet a negative or zero!" << endl << endl;
cout << "What's your bet? ";
cin >> bet;
}
return bet;
}
//function that generates one random number
int getspin();
int getspin()
{
return rand() % 9 + 1;
}
//function that determines how the player won/lost and how much they won/lost
int figurepayoff(int bet, int w1, int w2, int w3);
int figurepayoff(int bet, int w1, int w2, int w3)
{
int payoff;
const int JACKPOT = 9;
const int BAR = 8;
if (w1 == JACKPOT && w2 == JACKPOT && w3 == JACKPOT)//3 jackpots
{
payoff = 20 * bet;
cout << "Jackpot!" << endl << "You won $" << payoff;
return payoff;
}
else if (w1 == BAR && w2 == BAR && w3 == BAR)//3 bars
{
payoff = 10 * bet;
cout << "Bar!" << endl << "You won $" << payoff;
return payoff;
}
else if (w1 == w2 && w1 == w3)//triple
{
payoff = 3 * bet;
cout << "Triple!" << endl << "You won $" << payoff;
return payoff;
}
else if (w1 == w2 || w1 == w3 || w2 == w3)//double
{
payoff = 2 * bet;
cout << "Double!" << endl << "You won $" << payoff;
return payoff;
}
else
{
cout << "You lost $" << bet;//lose
return -bet;
}
}
void drawwheels(int w1, int w2, int w3);
void drawwheels(int w1, int w2, int w3)
{
int ctr = 0;
string word1, word2, word3;
cout << "Pulling the handle . . ." << endl << endl;
while(ctr < 20)
{
w1 = getspin(); //3 random numbers between 1 and 9
w2 = getspin();
w3 = getspin();
if (w1 == 1) word1 = "Cherry ";
else if (w1 == 2) word1 = "Apple ";
else if (w1 == 3) word1 = "Lemon ";
else if (w1 == 4) word1 = "Melon ";
else if (w1 == 5) word1 = "Plum ";
else if (w1 == 6) word1 = "Grape ";
else if (w1 == 7) word1 = "Orange ";
else if (w1 == 8) word1 = "Bar ";
else word1 = "Jackpot ";
if (w2 == 1) word2 = "Cherry ";
else if (w2 == 2) word2 = "Apple ";
else if (w2 == 3) word2 = "Lemon ";
else if (w2 == 4) word2 = "Melon ";
else if (w2 == 5) word2 = "Plum ";
else if (w2 == 6) word2 = "Grape ";
else if (w2 == 7) word2 = "Orange ";
else if (w2 == 8) word2 = "Bar ";
else word2 = "Jackpot ";
if (w3 == 1) word3 = "Cherry ";
else if (w3 == 2) word3 = "Apple ";
else if (w3 == 3) word3 = "Lemon ";
else if (w3 == 4) word3 = "Melon ";
else if (w3 == 5) word3 = "Plum ";
else if (w3 == 6) word3 = "Grape ";
else if (w3 == 7) word3 = "Orange ";
else if (w3 == 8) word3 = "Bar ";
else word3 = "Jackpot ";
settextstyle(10, HORIZ_DIR, 1); // BOLD_FONT
bar(100, 220, 200, 260);
bar(270, 220, 370, 260);
bar(440, 220, 540, 260);
outtextxy(105, 230, const_cast<char*>(word1.c_str()));
outtextxy(275, 230, const_cast<char*>(word2.c_str()));
outtextxy(445, 230, const_cast<char*>(word3.c_str()));
ctr ++;
delay(100);
}
cout << word1 << word2 << word3 << endl;
}
int main()
{
//this part copied from tutorial
int gdriver = DETECT, gmode, errorcode; //request autodetection
initgraph(&gdriver, &gmode, ""); //initialize graphics mode
errorcode = graphresult(); //read result of initialization
if (errorcode != grOk) //an error occurred
{
cout << "Graphics error: " << grapherrormsg(errorcode) << endl;
return 1;
}
srand(time(NULL));
char yesno;
int w1, w2, w3, bet, payoff, cash = 200;
string showword1, showword2, showword3;
const int CHERRY = 1;
const int APPLE = 2;
const int LEMON = 3;
const int MELON = 4;
const int PLUM = 5;
const int GRAPE = 6;
const int ORANGE = 7;
const int BAR = 8;
const int JACKPOT = 9;
string player = getname();//introduction and getting player's name
cout << "You start off with 200 dollars" << endl;
cout << "Want to play with the one-armed bandit? ";
yesno = getyorn();//asking if the user wants to play
cout << endl;
//while user wants to keep playing and has money
while (yesno == 'y' && cash > 0)
{
bet = getbet(cash);//ask for the bet
drawwheels(w1, w2, w3);//pulling the handle
payoff = figurepayoff(bet, w1, w2, w3);//
cash = cash + payoff;//updating the player's cash total
cout << endl << endl << "You now have " << cash << " dollars";
if (cash > 0)//must have money to play again
{
cout << endl << "Wanna play again? ";
yesno = getyorn();
}
}
cout << endl << player << " Thanks for playing!" << endl;
cout << "You leave with " << cash << " dollars." << endl;
if(cash == 0)//lose all the money
{
cout << "Better luck next time!" << endl;
}
getch();
closegraph();
return 0;
}
|