-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
137 lines (119 loc) · 4.56 KB
/
Game.java
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
package rockPaperScissors;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Game class manages rounds, round statisitcs, and flow of control in general
*/
public class Game {
private int wins = 0;
private int losses = 0;
private int ties = 0;
private int rounds = 0;
private ArrayList<String> winLossHistory = new ArrayList<String>();
private MoveThrower humanThrower = new MoveThrower();
private MoveThrower computerThrower = new MoveThrower();
/**
* Overloaded Constructor calls the playRound method a many times (number
* specified by 'rounds') When the rounds are complete, the constructor
* prints out statistics.
*
* @param rounds
* @param humanSimType
* @param smartAI
* @param quiet
*/
public Game(int rounds, int humanSimType, boolean smartAI, boolean quiet) {
for (int i = 0; i < rounds; i++) {
playRound(i, humanSimType, smartAI, quiet);
this.rounds++;
}
Talker.printResults(this.wins, this.ties, this.losses, this.rounds);
}
/**
* Plays a single round of the game
*/
private void playRound(int round, int humanSimType, boolean smartAI, boolean quiet) {
String result = "";
String reason = "";
// Select the AI to move either randomly or intelligently
Move computerMove = computerThrower.computerMove(smartAI, humanThrower.moveSequence);
computerThrower.moveSequence.add(computerMove);
computerThrower.updateMoveDistribution();
// Select the move based on the type of human to simulate update
Move humanMove = humanThrower.simulatedHumanMove(humanSimType, round);
humanThrower.moveSequence.add(humanMove);
humanThrower.updateMoveDistribution();
// Updates the fields based on results of the round
String interaction = " " + Move.interaction(humanMove, computerMove) + " ";
if (humanMove.winsAgainst(computerMove)) {
this.wins++;
result = "Win";
reason = humanMove.name + interaction + computerMove.name;
winLossHistory.add("W");
} else {
if (humanMove.initial.equals(computerMove.initial)) {
this.ties++;
result = "Tie";
reason = computerMove.name + interaction + humanMove.name;
winLossHistory.add("T");
} else {
this.losses++;
result = "Loss";
reason = computerMove.name + interaction + humanMove.name;
winLossHistory.add("L");
}
}
if(!quiet) {
Talker.printRoundResult(humanMove, computerMove, result, reason);
}
}
/**
* Creates a manual instance of the game
*/
public Game() {
Talker.printWelcomeText();
// While playRound method returns true, continues playing more rounds.
while (playRound()) {
rounds++;
}
Talker.printResults(this.wins, this.ties, this.losses, this.rounds);
Talker.printEndgameInfo();
}
/**
* Plays a round and prompts user whether to play another
*/
private boolean playRound() {
String humanMoveString = Talker.promptInput();
if (humanMoveString.charAt(0) == 'Z' || humanMoveString.charAt(0) == 'z')
return false;
String result = "";
String reason = "";
Move humanMove = new Move(humanMoveString);
humanThrower.moveSequence.add(humanMove);
humanThrower.updateMoveDistribution();
Move computerMove = MoveThrower.randomMove();
computerThrower.moveSequence.add(computerMove);
computerThrower.updateMoveDistribution();
String interaction = " " + Move.interaction(humanMove, computerMove) + " ";
if (humanMove.winsAgainst(computerMove)) {
this.wins++;
result = "Win";
reason = humanMove.name + interaction + computerMove.name;
winLossHistory.add("W");
} else {
if (humanMove.initial.equals(computerMove.initial)) {
this.ties++;
result = "Tie";
reason = computerMove.name + interaction + humanMove.name;
winLossHistory.add("T");
} else {
this.losses++;
result = "Loss";
reason = computerMove.name + interaction + humanMove.name;
winLossHistory.add("L");
}
}
Talker.printRoundResult(humanMove, computerMove, result, reason);
return true;
}
}