-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElevensBoard.java
153 lines (140 loc) · 5.04 KB
/
ElevensBoard.java
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
import java.util.List;
import java.util.ArrayList;
/**
* The ElevensBoard class represents the board in a game of Elevens.
*/
public class ElevensBoard extends Board {
/**
* The size (number of cards) on the board.
*/
private static final int BOARD_SIZE = 9;
/**
* The ranks of the cards for this game to be sent to the deck.
*/
private static final String[] RANKS =
{"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};
/**
* The suits of the cards for this game to be sent to the deck.
*/
private static final String[] SUITS =
{"spades", "hearts", "diamonds", "clubs"};
/**
* The values of the cards for this game to be sent to the deck.
*/
private static final int[] POINT_VALUES =
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0};
/**
* Creates a new <code>ElevensBoard</code> instance.
*/
public ElevensBoard() {
super(BOARD_SIZE, RANKS, SUITS, POINT_VALUES);
}
/**
* Determines if the selected cards form a valid group for removal.
* In Elevens, the legal groups are (1) a pair of non-face cards
* whose values add to 11, and (2) a group of three cards consisting of
* a jack, a queen, and a king in some order.
* @param selectedCards the list of the indices of the selected cards.
* @return true if the selected cards form a valid group for removal;
* false otherwise.
*/
@Override
public boolean isLegal(List<Integer> selectedCards) {
/* *** TO BE MODIFIED IN ACTIVITY 11 *** */
if (selectedCards.size() == 2) {
return containsPairSum11(selectedCards);
} else if (selectedCards.size() == 3) {
return containsJQK(selectedCards);
} else {
return false;
}
}
/**
* Determine if there are any legal plays left on the board.
* In Elevens, there is a legal play if the board contains
* (1) a pair of non-face cards whose values add to 11, or (2) a group
* of three cards consisting of a jack, a queen, and a king in some order.
* @return true if there is a legal play left on the board;
* false otherwise.
*/
@Override
public boolean anotherPlayIsPossible() {
/* *** TO BE MODIFIED IN ACTIVITY 11 *** */
List<Integer> cIndexes = cardIndexes();
return containsPairSum11(cIndexes) || containsJQK(cIndexes);
}
/**
* Look for an 11-pair in the selected cards.
* @param selectedCards selects a subset of this board. It is list
* of indexes into this board that are searched
* to find an 11-pair.
* @return a list of the indexes of an 11-pair, if an 11-pair was found;
* an empty list, if an 11-pair was not found.
*/
private boolean containsPairSum11(List<Integer> selectedCards) {
/* *** TO BE CHANGED INTO findPairSum11 IN ACTIVITY 11 *** */
for (int sk1 = 0; sk1 < selectedCards.size(); sk1++) {
int k1 = selectedCards.get(sk1).intValue();
for (int sk2 = sk1 + 1; sk2 < selectedCards.size(); sk2++) {
int k2 = selectedCards.get(sk2).intValue();
if (cardAt(k1).pointValue() + cardAt(k2).pointValue() == 11) {
return true;
}
}
}
return false;
}
/**
* Look for a JQK in the selected cards.
* @param selectedCards selects a subset of this board. It is list
* of indexes into this board that are searched
* to find a JQK group.
* @return a list of the indexes of a JQK, if a JQK was found;
* an empty list, if a JQK was not found.
*/
private boolean containsJQK(List<Integer> selectedCards) {
/* *** TO BE CHANGED INTO findJQK IN ACTIVITY 11 *** */
boolean foundJack = false;
boolean foundQueen = false;
boolean foundKing = false;
for (Integer kObj : selectedCards) {
int k = kObj.intValue();
if (cardAt(k).rank().equals("jack")) {
foundJack = true;
} else if (cardAt(k).rank().equals("queen")) {
foundQueen = true;
} else if (cardAt(k).rank().equals("king")) {
foundKing = true;
}
}
return foundJack && foundQueen && foundKing;
}
/**
* Looks for a legal play on the board. If one is found, it plays it.
* @return true if a legal play was found (and made); false othewise.
*/
public boolean playIfPossible() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 11 *** */
return false; // REPLACE !
}
/**
* Looks for a pair of non-face cards whose values sum to 11.
* If found, replace them with the next two cards in the deck.
* The simulation of this game uses this method.
* @return true if an 11-pair play was found (and made); false othewise.
*/
private boolean playPairSum11IfPossible() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 11 *** */
return false; // REPLACE !
}
/**
* Looks for a group of three face cards JQK.
* If found, replace them with the next three cards in the deck.
* The simulation of this game uses this method.
* @return true if a JQK play was found (and made); false othewise.
*/
private boolean playJQKIfPossible() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 11 *** */
return false; // REPLACE !
}
}