-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
184 lines (166 loc) · 4.62 KB
/
Board.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
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
import java.util.List;
import java.util.ArrayList;
/**
* This class represents a Board that can be used in a collection
* of solitaire games similar to Elevens. The variants differ in
* card removal and the board size.
*/
public abstract class Board {
/**
* The cards on this board.
*/
private Card[] cards;
/**
* The deck of cards being used to play the current game.
*/
private Deck deck;
/**
* Flag used to control debugging print statements.
*/
private static final boolean I_AM_DEBUGGING = false;
/**
* Creates a new <code>Board</code> instance.
* @param size the number of cards in the board
* @param ranks the names of the card ranks needed to create the deck
* @param suits the names of the card suits needed to create the deck
* @param pointValues the integer values of the cards needed to create
* the deck
*/
public Board(int size, String[] ranks, String[] suits, int[] pointValues) {
cards = new Card[size];
deck = new Deck(ranks, suits, pointValues);
if (I_AM_DEBUGGING) {
System.out.println(deck);
System.out.println("----------");
}
dealMyCards();
}
/**
* Start a new game by shuffling the deck and
* dealing some cards to this board.
*/
public void newGame() {
deck.shuffle();
dealMyCards();
}
/**
* Accesses the size of the board.
* Note that this is not the number of cards it contains,
* which will be smaller near the end of a winning game.
* @return the size of the board
*/
public int size() {
return cards.length;
}
/**
* Determines if the board is empty (has no cards).
* @return true if this board is empty; false otherwise.
*/
public boolean isEmpty() {
for (int k = 0; k < cards.length; k++) {
if (cards[k] != null) {
return false;
}
}
return true;
}
/**
* Deal a card to the kth position in this board.
* If the deck is empty, the kth card is set to null.
* @param k the index of the card to be dealt.
*/
public void deal(int k) {
cards[k] = deck.deal();
}
/**
* Accesses the deck's size.
* @return the number of undealt cards left in the deck.
*/
public int deckSize() {
return deck.size();
}
/**
* Accesses a card on the board.
* @return the card at position k on the board.
* @param k is the board position of the card to return.
*/
public Card cardAt(int k) {
return cards[k];
}
/**
* Replaces selected cards on the board by dealing new cards.
* @param selectedCards is a list of the indices of the
* cards to be replaced.
*/
public void replaceSelectedCards(List<Integer> selectedCards) {
for (Integer k : selectedCards) {
deal(k.intValue());
}
}
/**
* Gets the indexes of the actual (non-null) cards on the board.
*
* @return a List that contains the locations (indexes)
* of the non-null entries on the board.
*/
public List<Integer> cardIndexes() {
List<Integer> selected = new ArrayList<Integer>();
for (int k = 0; k < cards.length; k++) {
if (cards[k] != null) {
selected.add(new Integer(k));
}
}
return selected;
}
/**
* Generates and returns a string representation of this board.
* @return the string version of this board.
*/
public String toString() {
String s = "";
for (int k = 0; k < cards.length; k++) {
s = s + k + ": " + cards[k] + "\n";
}
return s;
}
/**
* Determine whether or not the game has been won,
* i.e. neither the board nor the deck has any more cards.
* @return true when the current game has been won;
* false otherwise.
*/
public boolean gameIsWon() {
if (deck.isEmpty()) {
for (Card c : cards) {
if (c != null) {
return false;
}
}
return true;
}
return false;
}
/**
* Method to be completed by the concrete class that determines
* if the selected cards form a valid group for removal.
* @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.
*/
public abstract boolean isLegal(List<Integer> selectedCards);
/**
* Method to be completed by the concrete class that determines
* if there are any legal plays left on the board.
* @return true if there is a legal play left on the board;
* false otherwise.
*/
public abstract boolean anotherPlayIsPossible();
/**
* Deal cards to this board to start the game.
*/
private void dealMyCards() {
for (int k = 0; k < cards.length; k++) {
cards[k] = deck.deal();
}
}
}