-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameConsole.java
More file actions
46 lines (36 loc) · 1.46 KB
/
GameConsole.java
File metadata and controls
46 lines (36 loc) · 1.46 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
package com.dj.game;
import java.util.Scanner;
public class GameConsole<T extends Game<? extends Player>> {
private final T game;
private static final Scanner scanner = new Scanner(System.in);
public GameConsole(T game) {
this.game = game;
}
public int addPlayer() {
System.out.println("Enter you playing name: ");
String name = scanner.nextLine();
System.out.printf("Welcome to %s, %s!%n".formatted(game.getGameName(), name));
return game.addPlayer(name);
}
public void playGame(int playerIndex) {
boolean done = false;
while (!done) {
var gameActions = game.getGameActions(playerIndex);
System.out.println("Select from one of the following actions: ");
for (Character c : gameActions.keySet()) {
String prompt = gameActions.get(c).prompt();
System.out.println("\t" + prompt + "(" + c + ")");
}
System.out.println("Enter Next Action: ");
char nextMove = scanner.nextLine().toUpperCase().charAt(0);
GameAction gameAction = gameActions.get(nextMove);
if (gameAction != null) {
System.out.println("---------------------------------");
done = game.executeGameAction(playerIndex, gameAction);
if (!done) {
System.out.println("-------------------------------");
}
}
}
}
}