-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayerList.java
More file actions
79 lines (54 loc) · 2.05 KB
/
PlayerList.java
File metadata and controls
79 lines (54 loc) · 2.05 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
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class PlayerList {
private Queue<Player> players;
private Player currentPlayer;
public PlayerList(){
this.players = new LinkedList<>();
}
public void addPlayer(Player player){
players.add(player);
}
public void startTurn(QuestionList questions, Scanner scanner){
if(players.isEmpty()){
System.out.println("No players in the game.");
return;
}
currentPlayer = players.poll();
System.out.println("It's " + currentPlayer.getPlayerId() + "'s turn.");
System.out.println("Current Score: " + currentPlayer.getScore());
System.out.println("Enter category:");
String category = scanner.nextLine();
System.out.println("Enter value:");
int value = scanner.nextInt();
scanner.nextLine(); // Consume newline
ChooseQuestionAction chooseAction = new ChooseQuestionAction(currentPlayer, category, value, questions);
currentPlayer.setAction(chooseAction);
currentPlayer.doAction();
Question selectedQuestion = chooseAction.getQuestion();
if(selectedQuestion == null || selectedQuestion.isAnswered()) {
players.add(currentPlayer); // Add the player back to the end of the queue
return;
}
System.out.println("Question: " + selectedQuestion.getContent());
System.out.println("Enter your answer:");
String answer = scanner.nextLine();
AnswerQuestionAction answerAction = new AnswerQuestionAction(currentPlayer, selectedQuestion, answer);
currentPlayer.setAction(answerAction);
currentPlayer.doAction();
}
public void endTurn(){
if(currentPlayer != null){
players.add(currentPlayer);
currentPlayer = null;
}
}
public Queue<Player> getPlayers() {
return players;
}
public Player getCurrentPlayer() {
return currentPlayer;
}
}