forked from ChicoState/PigsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
113 lines (104 loc) · 3.15 KB
/
Player.cpp
File metadata and controls
113 lines (104 loc) · 3.15 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
#include <string>
#include <iostream>
#include "Player.h"
using namespace std;
//Constructor for the Player Class
//requires a name and a boolean that is set to true if the
//player is a cpu.
Player::Player(string new_name, bool new_cpu)
{
total = 0;
current = 0;
cpu = new_cpu;
name = new_name;
}
//This function provides the means to communicate with
//the users by informing them of their current totals, their
//overall totals and the values of their rolls. It also requests
//what actions the players wish to make when it is their turn.
//It also is responsible for controlling the computer . Finnally
//it determines if a certain player has one. The results of
//these actions is reported to the main application which
//monitors the string outputs of this function in order to
//determine if it is the next players turn or if the game is over.
string Player::decision(int amount)
{
string decision;
//Inform the user of their current die roll.
cout << endl;
cout << name << ":" << endl;
cout << "A "<<amount<< " was rolled" << endl;
if(amount == 1) //Determine if the player scored nothing this round.
{
decision = "reset";
update(decision, amount);
cout << "You lost your turn, total = " << total << endl;
}
else if((amount + total + current) >= 100) //Determine if there is a winner.
{
decision = "win";
update(decision, amount);
cout << "WIN with " << total << endl;
}
else if(cpu == false) //If it the current player is not a computer we must ask what they want to do.
{
cout << "Turn total = " << current+amount << endl;
cout << "Game total = " << total+current+amount << endl;
cout << "What would you like to do? [roll, hold] "<< endl;
cin >> decision;
while (decision != "hold" && decision != "roll")
{
cout << "The options are hold or roll, please try again." << endl;
cin >> decision;
}
update(decision, amount);
}
else //This is a computer so we automatically determine what they do.
{
if (amount == 1)
{
decision = "reset";
}
else
{
if (current <= 20)
{
decision = "roll";
}
else
{
decision = "hold";
}
}
//We have to update the current total and game total for this player.
update(decision, amount);
//Inform the user of the results of this round for them.
cout << decision << endl;
cout << "Current total = "<<current << endl;
cout << "Game total = "<<total+current << endl;
}
return decision;
}
//During the process of a player doing his or her turn values
//must be updated such as the current roll total and the total
//a player has scored through a game. The values that are
//updated are based off the decision string, and the current
//roll amount which are the inputs of this function. The output
//of this function tells what the current total for the player is.
void Player::update(std::string decision, int amount)
{
if (decision == "reset")
{
current = 0;
}
else if (decision == "roll")
{
current += amount;
}
else if ((decision == "hold") || (decision == "win"))
{
current += amount;
total += current;
current = 0;
}
}