-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActualChips.cpp
151 lines (133 loc) · 3.74 KB
/
ActualChips.cpp
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
using std::string;
using std::ofstream;
using std::cout;
using std::cin;
using std::ios;
using std::endl;
const float MAX_TURN = .5;
const int MAX_CHIPS = 100;
struct player
{
string name;
int numWins;
};
void getUserNames(string players[]);
string FindPlayerName(string names[], bool playerTurn);
int askMove(bool player1Turn, int chipsInPile, string names[]);
int main()
{
player player1;
player player2;
bool player1Turn = true;
bool gameOver = false;
int moveCounter = 0;
int chipsInPile = 0;
int chipsTaken = 0;
char userChoice;
string playerNames[2];
//Output file variables
ofstream outFile;
outFile.open("Winners.txt", ios::app);
//seed the random number generator
srand(time(0));
//ask the players for their names, then store in an array
getUserNames(playerNames);
player1.name = playerNames[0];
player2.name = playerNames[1];
player1.numWins = 0;
player2.numWins = 0;
//start the game with a random number of chips in the pile
do
{
chipsInPile = (rand() % MAX_CHIPS) + 1;
cout << "This round will start with " << chipsInPile << " chips in the pile\n";
gameOver = false;
moveCounter = 0;
while (gameOver == false)
{
chipsTaken = askMove(player1Turn, chipsInPile, playerNames);
chipsInPile = chipsInPile - chipsTaken;
cout << "There are " << chipsInPile << " left in the pile\n";
player1Turn = !player1Turn;
moveCounter++;
if (chipsInPile == 0)
{
gameOver = true;
cout << FindPlayerName(playerNames, player1Turn) << ", congratulations you won" << endl;
outFile << FindPlayerName(playerNames, player1Turn) << " won in " << moveCounter << " moves" << endl;
}
}
if (FindPlayerName(playerNames, player1Turn) == playerNames[0])
{
player1.numWins = player1.numWins + 1;
}
else if (FindPlayerName(playerNames, player1Turn) == playerNames[1])
{
player2.numWins = player2.numWins + 1;
}
cout << "\nDo you wish to play again? (Y/N)" << endl;
cin >> userChoice;
userChoice = toupper(userChoice);
if (userChoice == 'N')
{
cout << player1.name << " had " << player1.numWins << " total wins this round" << endl;
cout << player2.name << " had " << player2.numWins << " total wins this round" << endl;
}
}while ( userChoice == 'Y');
outFile.close();
}
void getUserNames(string players[])
{
cout << "Player 1, please enter your name: ";
cin >> players[0];
cout << "\nThanks and good luck!" << endl;
cout << "Player 2, please enter your name \n";
cout << "(If you wish to play against the computer, enter AI): ";
cin >> players[1];
cout << "\nThanks and good luck! \n";
}
string FindPlayerName(string names[], bool playerTurn)
{
if (playerTurn == true)
return names[0];
else
return names[1];
}
int askMove(bool player1Turn, int chipsInPile, string names[])
{
int chipsTaken;
int maxPerTurn = MAX_TURN * chipsInPile;
do
{
cout << FindPlayerName(names, player1Turn) << " How many chips would you like?\n";
cout << "You can take up to ";
if (( maxPerTurn ) == 0)
{
cout << " 1\n";
}
else
{
cout << maxPerTurn << endl;
}
if (FindPlayerName(names, player1Turn) == "AI")
{
if (maxPerTurn == 0)
{
chipsTaken = 1;
}
else
{
chipsTaken = (rand() % maxPerTurn) + 1;
}
}
else
{
cin >> chipsTaken;
}
}while ((chipsTaken > maxPerTurn ) && (chipsInPile > 1));
return chipsTaken;
}