-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationMain.java
More file actions
182 lines (151 loc) · 7.09 KB
/
ApplicationMain.java
File metadata and controls
182 lines (151 loc) · 7.09 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
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
import java.util.Arrays;
import java.util.Scanner;
public class ApplicationMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SimplifiedOkeyGame game = new SimplifiedOkeyGame();
System.out.print("Please enter your name: ");
String playerName = sc.next();
game.setPlayerName(0, playerName);
game.setPlayerName(1, "John");
game.setPlayerName(2, "Jane");
game.setPlayerName(3, "Ted");
game.createTiles();
game.shuffleTiles();
game.distributeTilesToPlayers();
// developer mode is used for seeing the computer players hands, to be used for debugging
System.out.print("Play in developer's mode with other player's tiles visible? (Y/N): ");
char devMode = sc.next().charAt(0);
boolean devModeOn = devMode == 'Y';
boolean firstTurn = true;
boolean gameContinues = true;
int playerChoice = -1;
while(gameContinues) {
int currentPlayer = game.getCurrentPlayerIndex();
System.out.println(game.getCurrentPlayerName() + "'s turn.");
if(currentPlayer == 0) {
// this is the human player's turn
game.displayCurrentPlayersTiles();
game.displayDiscardInformation();
System.out.println("What will you do?");
if(!firstTurn) {
// after the first turn, player may pick from tile stack or last player's discard
System.out.println("1. Pick From Tiles");
System.out.println("2. Pick From Discard");
}
else{
// on first turn the starting player does not pick up new tile
System.out.println("1. Discard Tile");
}
System.out.print("Your choice: ");
/* ADDITION: making sure that a valid choice is entered by the user
* Bora Balcı - done.
*/
boolean continueAsking = true;
while (continueAsking) // Asking the user for a choice until a valid one is entered.
{
playerChoice = sc.nextInt();
if (playerChoice < 1 || playerChoice > 2)
{
System.out.print("Invalid choice. Choice should be either 1 or 2: ");
}
else
{
continueAsking = false;
}
}
// after the first turn we can pick up
if(!firstTurn) {
if(playerChoice == 1) {
System.out.println("You picked up: " + game.getTopTile());
firstTurn = false;
}
else if(playerChoice == 2) {
System.out.println("You picked up: " + game.getLastDiscardedTile());
}
// display the hand after picking up new tile
game.displayCurrentPlayersTiles();
}
else{
// after first turn it is no longer the first turn
firstTurn = false;
}
gameContinues = !game.didGameFinish() && game.hasMoreTileInStack();
if(gameContinues) {
// if game continues we need to discard a tile using the given index by the player
System.out.println("Which tile you will discard?");
System.out.print("Discard the tile in index: ");
/* TODO: make sure the given index is correct, should be 0 <= index <= 14
* Bora Balcı - done.
*/
continueAsking = true;
while (continueAsking) // Asking the user for an index until a valid one is entered.
{
playerChoice = sc.nextInt();
if (playerChoice < 0 || playerChoice > 14)
{
System.out.print("Invalid index. Index should be between 0 and 14: ");
}
else
{
continueAsking = false;
}
}
game.discardTile(playerChoice);
game.passTurnToNextPlayer();
}
else{
if(game.didGameFinish()) {
// if we finish the hand we win
System.out.println("Congratulations, you win!");
}
else{
/* TODO: the game ended with no more tiles in the stack
* determine the winner based on longest chain lengths of the players
* use getPlayerWithHighestLongestChain method of game for this task
* Bora Balcı - done.
*/
System.out.print("The game ends. Winner(s): ");
for (Player player : game.getPlayerWithHighestLongestChain())
{
System.out.print(player.getName() + " ");
}
}
}
}
else{
// this is the computer player's turn
if(devModeOn) {
game.displayCurrentPlayersTiles();
}
// computer picks a tile from tile stack or other player's discard
game.pickTileForComputer();
gameContinues = !game.didGameFinish() && game.hasMoreTileInStack();
if(gameContinues) {
// if game did not end computer should discard
game.discardTileForComputer();
game.passTurnToNextPlayer();
}
else{
if(game.didGameFinish()) {
// current computer character wins
System.out.println(game.getCurrentPlayerName() + " wins.");
}
else{
/* TODO: the game ended with no more tiles in the stack
* determine the winner based on longest chain lengths of the players
* use getPlayerWithHighestLongestChain method of game for this task
* Bora Balcı - done.
*/
System.out.println("The game ends. Winner(s): ");
for (Player player : game.getPlayerWithHighestLongestChain())
{
System.out.print(player.getName() + " ");
}
}
}
}
}
sc.close();
}
}