-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLobby.java
More file actions
205 lines (187 loc) · 6.1 KB
/
Copy pathLobby.java
File metadata and controls
205 lines (187 loc) · 6.1 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* Main method for the game. The selection of the game, number of players, and hosting the game are portrayed here.
*
* @author Bryce J. Fisher, Carlos Arredondo
*/
public class Lobby {
private static final int LOBBY_CHOOSE_MODE = 0;
private static final int LOBBY_HOST_MODE = 1;
private static final int LOBBY_JOIN_MODE = 2;
// private static final int NETWORK_PLAY_MODE = 3;
// private static final int NETWORK_PAUSE_MODE = 4;
private static final int NETWORK_SHUTDOWN_MODE = 5;
private static final int NETWORK_SINGLE_PLAYER_MODE = 6;
private static final int GAME_EGYPTIAN_RATCREW = 1;
// private static final int GAME_POKER = 2;
// private static final int GAME_HEARTS = 3;
private static int mode = LOBBY_CHOOSE_MODE;
protected static int game = LOBBY_CHOOSE_MODE;
private static int numberOfTotalPlayers = 0;
private static int numberOfUserPlayers = 0;
private static int numberOfComputerAIs = 0;
@SuppressWarnings("unused")
public static void main(final String[] args) {
final Scanner input = new Scanner(System.in);
// prompt user to host or join game
while (true) {
System.out.print("(H)ost, (J)oin, (E)xit ? ");
final String in = input.nextLine().trim();
if (in.length() > 1 || in.length() == 0) {
System.out
.println("Invalid Input: should be only one character.");
continue;
}
final char c = in.toLowerCase().charAt(0);
if (c == 'h') {
mode = LOBBY_HOST_MODE;
break;
} else if (c == 'j') {
mode = LOBBY_JOIN_MODE;
break;
} else if (c == 'e') {
mode = NETWORK_SHUTDOWN_MODE;
break;
} else {
System.out.println("Invalid Input: should be h,j, or e.");
continue;
}
}
if (mode == LOBBY_HOST_MODE) {
// prompt user to select game
while (true) {
System.out
.print("Select a game: (E)gyptian Ratscrew, (P)oker, (H)earts ");
final String in = input.nextLine().trim();
if (in.length() > 1 || in.length() == 0) {
System.out
.println("Invalid Input: should be only one character.");
continue;
}
final char c = in.toLowerCase().charAt(0);
if (c == 'e') {
game = GAME_EGYPTIAN_RATCREW;
break;
} else if (c == 'p') {
System.out.println("Invalid Game: Poker is not ready yet.");
continue;
} else if (c == 'h') {
System.out
.println("Invalid Game: Hearts is not ready yet.");
continue;
} else {
System.out.println("Invalid Input: should be h,j, or e.");
continue;
}
}
// prompt user to select max number of players
while (true) {
System.out.print("Select number of players (2 - 9) ");
final String in = input.nextLine().trim();
if (in.length() > 1 || in.length() == 0) {
System.out
.println("Invalid Input: should be only one character.");
continue;
}
try {
numberOfTotalPlayers = Integer.parseInt(in);
} catch (final NumberFormatException e) {
System.out.println("Invalid Input: should be a number.");
continue;
}
if (numberOfTotalPlayers < 2 || numberOfTotalPlayers > 9) {
System.out.println("Invalid Input: should within 2 - 9.");
continue;
}
break;
}
// prompt user to select number of computer players
final int numberOfComputerPlayerBounds = numberOfTotalPlayers - 1;
if (numberOfComputerPlayerBounds != 1) {
final String numberOfComputerPlayerBoundsString = 1 + " - "
+ numberOfComputerPlayerBounds;
while (true) {
System.out.print("Select number of computer players ("
+ numberOfComputerPlayerBoundsString + ") ");
final String in = input.nextLine().trim();
if (in.length() > 1 || in.length() == 0) {
System.out
.println("Invalid Input: should be only one character.");
continue;
}
try {
numberOfComputerAIs = Integer.parseInt(in);
} catch (final NumberFormatException e) {
System.out
.println("Invalid Input: should be a number.");
continue;
}
if (numberOfComputerAIs < 1
|| numberOfComputerAIs > numberOfComputerPlayerBounds) {
System.out.println("Invalid Input: should within "
+ numberOfComputerPlayerBoundsString + ".");
continue;
}
break;
}
if (numberOfComputerPlayerBounds == numberOfComputerAIs) {
mode = NETWORK_SINGLE_PLAYER_MODE;
}
} else {
// if there is only one other player, prompt the user for
// computer opponent or human
while (true) {
System.out
.print("Would you like a computer opponent? (Y/N) ");
final String in = input.nextLine().trim();
if (in.length() > 1 || in.length() == 0) {
System.out
.println("Invalid Input: should be only one character.");
continue;
}
final char c = in.toLowerCase().charAt(0);
if (c == 'y') {
mode = NETWORK_SINGLE_PLAYER_MODE;
break;
} else if (c == 'n') {
break;
} else {
System.out.println("Invalid Input: should be y or n.");
continue;
}
}
}
numberOfUserPlayers = numberOfTotalPlayers - numberOfComputerAIs;
}
// decide what to do
if (mode == NETWORK_SINGLE_PLAYER_MODE) {
// pass npcs to game class with a blank network
System.out.println("Starting single player game with "
+ numberOfComputerAIs + " npc(s)");
new Game(numberOfTotalPlayers);
} else if (mode == LOBBY_HOST_MODE) {
// display network address and start network threads
System.out.println("Waiting on " + numberOfUserPlayers
+ " players....");
// create a host and join it
final Network hostingNetwork = new Network(numberOfUserPlayers);
// Network clientNetwork = new Network(hostingNetwork.getAddress());
} else if (mode == LOBBY_JOIN_MODE) {
// Get network address to join
System.out.println("Enter a network address...");
// new Network( inet address );
try {
final Network clientNetwork = new Network(
InetAddress.getLocalHost());
} catch (final UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("Exiting...");
}
input.close();
}
}