forked from n-utku-n/Simple-Okey-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationMain.java
More file actions
155 lines (132 loc) · 6.2 KB
/
Copy pathApplicationMain.java
File metadata and controls
155 lines (132 loc) · 6.2 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
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.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();
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");
}
// Below lines are updated by Utku Kabukçu to improve game logic
System.out.print("Your choice: ");
while (!sc.hasNextInt()) {
System.out.println("Invalid input! Please enter a number.");
sc.next(); // Discard invalid input
}
playerChoice = sc.nextInt();
// If it's the first turn, only allow "1"
if (firstTurn) {
while (playerChoice != 1) {
System.out.println("Invalid choice! On the first turn, you can only discard a tile.");
System.out.print("Your choice: ");
while (!sc.hasNextInt()) {
System.out.println("Invalid input! Please enter a number.");
sc.next(); // Discard invalid input
}
playerChoice = sc.nextInt();
}
}
// Otherwise, allow both "1" (Pick from Tiles) and "2" (Pick from Discard)
else {
while (playerChoice < 1 || playerChoice > 2) {
System.out.println("Invalid choice! Please enter 1 or 2.");
System.out.print("Your choice: ");
while (!sc.hasNextInt()) {
System.out.println("Invalid input! Please enter a number.");
sc.next(); // Discard invalid input
}
playerChoice = sc.nextInt();
}
}
// 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();
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?");
// Done by Ali: make sure the given index is correct, should be 0 <= index <= 14
boolean correctIndex;
do{
System.out.print("Discard the tile in index: ");
playerChoice = sc.nextInt();
if (playerChoice < 0 || playerChoice > 14){
correctIndex = false ;
System.out.println("This is not a valid index. Try again!");
}
else {
correctIndex = true ;
}
} while (!correctIndex) ;
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.");
}
}
}
}
}