-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigTwoUI.java
More file actions
176 lines (160 loc) · 4.31 KB
/
BigTwoUI.java
File metadata and controls
176 lines (160 loc) · 4.31 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
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* This class is used for modeling a user interface for the Big Two card game.
*
* @author Kenneth Wong
*/
public class BigTwoUI {
private final static int MAX_CARD_NUM = 13; // max. no. of cards each player holds
private BigTwo game = null; // a BigTwo object
private ArrayList<CardGamePlayer> playerList; // the list of players
private ArrayList<Hand> handsOnTable; // the list of hands played on the
private Scanner scanner; // the scanner for reading user in put
private int activePlayer = -1; // the index of the active player
private boolean[] selected = new boolean[MAX_CARD_NUM]; // selected cards
/**
* Creates and returns an instance of the BigTwoUI class.
*
* @param game a BigTwo object associated with this UI
*/
public BigTwoUI(BigTwo game) {
this.game = game;
playerList = game.getPlayerList();
handsOnTable = game.getHandsOnTable();
scanner = new Scanner(System.in);
}
/**
* Sets the index of the active player.
*
* @param activePlayer the index of the active player (i.e., the player who can
* make a move)
*/
public void setActivePlayer(int activePlayer) {
if (activePlayer < 0 || activePlayer >= playerList.size()) {
this.activePlayer = -1;
} else {
this.activePlayer = activePlayer;
}
}
/**
* Redraws the UI.
*/
public void repaint() {
for (int i = 0; i < playerList.size(); i++) {
CardGamePlayer player = playerList.get(i);
String name = player.getName();
if (activePlayer == i) {
System.out.println("<" + name + ">");
System.out.print("==> ");
player.getCardsInHand().print(true, true);
} else if (activePlayer == -1) {
System.out.println("<" + name + ">");
System.out.print(" ");
player.getCardsInHand().print(true, true);
} else {
System.out.println("<" + name + ">");
System.out.print(" ");
player.getCardsInHand().print(false, true);
}
}
System.out.println("<Table>");
Hand lastHandOnTable = (handsOnTable.isEmpty()) ? null : handsOnTable.get(handsOnTable.size() - 1);
if (lastHandOnTable != null) {
System.out
.print(" <" + lastHandOnTable.getPlayer().getName() + "> {" + lastHandOnTable.getType() + "} ");
lastHandOnTable.print(true, false);
} else {
System.out.println(" [Empty]");
}
}
/**
* Prints the specified string to the UI.
*
* @param msg the string to be printed to the UI
*/
public void printMsg(String msg) {
System.out.print(msg);
}
/**
* Clears the message area of the UI.
*/
public void clearMsgArea() {
// not used in non-graphical UI
}
/**
* Resets the UI.
*/
public void reset() {
// not used in non-graphical UI
}
/**
* Enables user interactions.
*/
public void enable() {
// not used in non-graphical UI
}
/**
* Disables user interactions.
*/
public void disable() {
// not used in non-graphical UI
}
/**
* Prompts active player to select cards and make his/her move.
*/
public void promptActivePlayer() {
printMsg(playerList.get(activePlayer).getName() + "'s turn: ");
int[] cardIdx = getSelected();
resetSelected();
game.makeMove(activePlayer, cardIdx);
}
/**
* Returns an array of indices of the cards selected through the UI.
*
* @return an array of indices of the cards selected, or null if no valid cards
* have been selected
*/
private int[] getSelected() {
CardGamePlayer player = playerList.get(activePlayer);
String input = scanner.nextLine();
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()) {
try {
int idx = Integer.parseInt(st.nextToken());
if (idx >= 0 && idx < MAX_CARD_NUM && idx < player.getCardsInHand().size()) {
selected[idx] = true;
}
} catch (Exception e) {
// e.printStackTrace();
}
}
int[] cardIdx = null;
int count = 0;
for (int j = 0; j < selected.length; j++) {
if (selected[j]) {
count++;
}
}
if (count != 0) {
cardIdx = new int[count];
count = 0;
for (int j = 0; j < selected.length; j++) {
if (selected[j]) {
cardIdx[count] = j;
count++;
}
}
}
return cardIdx;
}
/**
* Resets the list of selected cards to an empty list.
*/
private void resetSelected() {
for (int j = 0; j < selected.length; j++) {
selected[j] = false;
}
}
}