-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplicationMain.java
More file actions
157 lines (129 loc) · 6.14 KB
/
ApplicationMain.java
File metadata and controls
157 lines (129 loc) · 6.14 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
import java.util.Scanner;
public class ApplicationMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
OkeyGame game = new OkeyGame();
System.out.println("WELCOME THE OKEY GAME");
System.out.println("---------------------");
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;
boolean sortColorAfterPicking = false;
boolean sortValueAfterPicking = false;
System.out.println("---------------------");
System.out.println("Game is starting!");
while(gameContinues) {
int currentPlayer = game.getCurrentPlayerIndex();
System.out.println("\n" + 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?");
System.out.println("1. Sort By Color First");
System.out.println("2. Sort By Value First");
if(!firstTurn) {
// after the first turn, player may pick from tile stack or last player's discard
System.out.println("3. Pick From Tiles");
System.out.println("4. Pick From Discard");
}
else{
// on first turn the starting player does not pick up new tile
System.out.println("3. Discard Tile");
}
System.out.print("Your choice: ");
playerChoice = sc.nextInt();
// sorting does not pass turn, so it is in a loop until user choses some other value
while(playerChoice == 1 || playerChoice == 2) {
if(playerChoice == 1) {
game.currentPlayerSortTilesColorFirst();
// will also sort after picking new tile
sortColorAfterPicking = true;
sortValueAfterPicking = false;
}
else{
game.currentPlayerSortTilesValueFirst();
// will also sort after picking new tile
sortColorAfterPicking = false;
sortValueAfterPicking = true;
}
game.displayCurrentPlayersTiles();
System.out.print("Your choice: ");
playerChoice = sc.nextInt();
}
// after the first turn we can pick up
if(!firstTurn) {
if(playerChoice == 3) {
System.out.println("You picked up: " + game.getTopTile());
firstTurn = false;
}
else if(playerChoice == 4) {
System.out.println("You picked up: " + game.getLastDiscardedTile());
}
// sort after picking up new tile
if(sortColorAfterPicking) {
game.currentPlayerSortTilesColorFirst();
}
else if(sortValueAfterPicking) {
game.currentPlayerSortTilesValueFirst();
}
// 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();
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: ");
playerChoice = sc.nextInt();
// TODO: make sure the given index is correct, should be 0 <= index <= 14
while(playerChoice < 0 || playerChoice > 14){
System.out.println("This index is not MEANINGFUL, please enter another index which should be at the interval of 0 <= index <= 14");
playerChoice = sc.nextInt();
}
game.discardTile(playerChoice);
game.passTurnToNextPlayer();
}
else{
// if we finish the hand we win
System.out.println("Congratulations, you win!");
}
}
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();
if(gameContinues) {
// if game did not end computer should discard
game.discardTileForComputer();
game.passTurnToNextPlayer();
}
else{
// current computer character wins
System.out.println(game.getCurrentPlayerName() + " wins.");
}
}
}
}
}