forked from ChicoState/PigsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (97 loc) · 3.91 KB
/
main.cpp
File metadata and controls
116 lines (97 loc) · 3.91 KB
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
/****************************************************************\
* The Pigs Game *
* Brought to you by: *
* Christian Cleveland *
* Natalie Astorga *
* Crystal Vang *
* Michelle Rodriguez *
* Matthew Daras *
\****************************************************************/
/*The Pigs Game
This is a command-line implementation of the Pigs Game.
The rules of the game as explaned on Wikipedia are:
Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player decides to "hold":
If the player rolls a 1, they score nothing and it becomes the next player's turn. If the player rolls
any other number, it is added to their turn total and the player's turn continues. If a player chooses to
"hold", their turn total is added to their score, and it becomes the next player's turn. The first player to
score 100 or more points wins.
*/
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "Dice.h"
#include "Player.h"
using namespace std;
//When this command line application is launched it asks the user how many human and Computer
//players wish to play the game. Once thoose numbers are determined it then asks the user to enter
//names for each of the human players. The computer players are automatically given names. Once
//the naming process is complete the game begins. During the naming process a new player class
//is instantiated for each player and the address of that player class is stored into a vector. WIth
//all the players in the vector we are able to cycle through the vector and present each player a
//chance to play the game until a winner is determined. Upon a winner being declared the
//application ends but prior to exit the memory is freed.
int main()
{
//Find out how many human players there are.
int numPlayers;
cout << "How many human players?" << endl;
cin >> numPlayers;
//Find out how many computer players there are.
int numCpu;
cout << "How many cpu players?" << endl;
cin >> numCpu;
vector<Player*> players; // creating vector for players
string playerName;
string cpuName;
//This allows the user to name each of the human players ; once named
//they are added to our players vector.
for(int k = 0; k < numPlayers; k++)
{
cout << "Please enter player " << k << "'s name? " << endl;
cin >> playerName;
players.push_back(new Player(playerName, false));
}
//Each computer player is added to the players vector with a auto
//created name that is based on the current computer player number.
for(int j = 1; j <= numCpu; j++)
{
stringstream ss;
ss << "Cpu" << j;
players.push_back(new Player (ss.str(), true));
}
//Create the random dice that will provide a random roll.
Dice *dice = new Dice;
//default the starting state as a "rolling" action.
string state = "roll";
//Inform the users the game has begun.
cout << "<<<<<<<<<<<<<<<<<<<< Start Game! >>>>>>>>>>>>>>>>>>>>" << endl;
//While there is no winner we cycle though all players allowing each of them to complete their turn
//untill a winner is declared.
for( int i = 0; state != "win"; i = (i+1)%(numPlayers+numCpu))
{
//must always start as a "rolling" state for each player
state = "roll";
//While the current player is rolling we do not move onto the next player.
while( state == "roll")
{
state = players[i]->decision(dice->roll());
}
if (state != "win")
{
cout << endl << "<<<<<<<<<<<<<<< Next Player >>>>>>>>>>>>>>>" << endl;
}
else
{
cout << "<<<<<<<<<<<<<<<<<<<< End Game! >>>>>>>>>>>>>>>>>>>>" << endl;
}
}
//Delete all the created player classes to free memory upon exit.
for(unsigned int i = 0; i < players.size(); ++i)
{
delete players[i];
}
//Delete the dice class to free memory on exit.
delete dice;
return 0;
}