Repository navigation
Expand file tree
/
Copy pathClientGame.java
More file actions
126 lines (110 loc) · 3.75 KB
/
Copy pathClientGame.java
File metadata and controls
126 lines (110 loc) · 3.75 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
package networking.client;
import logic.utils.Card;
import logic.utils.players.*;
import networking.protocol.Command;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class ClientGame {
private static boolean isHuman = true;
private static ClientPlayer player;
private static final CountDownLatch isInit = new CountDownLatch(1);
private static final Scanner scanner = new Scanner(System.in);
// Informative data
public static Card lastCardPlayed;
public static int pileSize;
public static void main(String[] args) throws InterruptedException {
if (args.length != 1) throw new RuntimeException("missing argument for player type.");
if (args[0].equals("-ai")) isHuman = false;
ServerHandler handler = new ServerHandler();
new Thread(handler).start();
isInit.await();
if (!(player instanceof HumanPlayer)) return;
HumanPlayer humanPlayer = (HumanPlayer) player;
String input;
while ((input = scanner.nextLine()) != null) {
if (input.startsWith("!"))
handler.sendCommand(
Command.CHAT,
player.getName(),
input.replaceFirst("!", "")
);
humanPlayer.handleInput(input, handler);
}
}
/**
* Gets user input from the console. ONly useful to initialize
*
* @param prompt The prompt message displayed to the user.
* @return The user input as a String.
*/
public static String getUserInput(String prompt) {
System.out.print(prompt + "\n>> ");
return scanner.next();
}
/**
* This method is used to indicate that initialization of the game has been completed.
*/
public static void doneInit() {
isInit.countDown();
}
/**
* Sets the last card played
*
* @param rawCard The type of the last card played.
*/
public static void setLastCardPlayed(String rawCard) {
if (rawCard.isEmpty()) {
lastCardPlayed = null;
return;
}
lastCardPlayed = Card.valueOf(rawCard);
}
/**
* Sets the size of the pile.
*
* @param num The size of the pile as a String.
*/
public static void setPileSize(String num) {
pileSize = Integer.parseInt(num);
}
/**
* Sets the name of the player in the game.
*
* @param name the name of the player
*/
public static void setName(String name) {
if (isHuman)
try {
player = new HumanPlayer(name);
} catch (IOException ignored) {}
else player = new ComputerPlayer(name);
}
/**
* Retrieves the ClientPlayer object representing the player in the game.
*
* @return The ClientPlayer object representing the player.
*/
public static ClientPlayer getPlayer() {
return player;
}
/**
* Prints a message to the console, if the player is not an instance of ComputerPlayer.
*
* @param message The message to be printed.
*/
public static void print(String message) {
if (player instanceof ComputerPlayer) return;
System.out.println(message);
}
/**
* Displays the current state of the game.
* Prints a message to the console with information such as the size of the pile and the last card played.
* Only prints the message if the player is not an instance of ComputerPlayer.
*/
public static void displayGame() {
if (player instanceof ComputerPlayer) return;
print(((HumanPlayer) player).getHandDisplay());
print("The top card: " + (lastCardPlayed != null ? lastCardPlayed.name():"None") + " Pile size: " + pileSize);
}
}