-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.java
More file actions
219 lines (189 loc) · 6.21 KB
/
Tile.java
File metadata and controls
219 lines (189 loc) · 6.21 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.util.Random;
import java.util.Stack;
/**
*
* @author Mikayla Sage, Diana Martinez, Emma Allen, Courtney Dixon
*
*/
public class Tile implements Cloneable {
public static Random rand = new Random(12);
// Fields
/**
* Array of integers that express the points/paths for each tile.
*/
private int[] points;
private char[] characters;
/**
* Default constructor.
*
* Calls the shuffleValues() method and returns a list of points/paths.
*/
public Tile() {
points = shuffleValues();
tileToChars();
}
/**
* Parameterized constructor.
*
* @param values
* An array of integers that indicate chosen points/paths.
*/
public Tile(int[] values) {
points = values;
tileToChars();
}
/**
* The shuffleValues() method creates a random, valid collection of points to instantiate tiles with.
*
* @return a random array of ints that represent points/paths for a tile.
*/
private static int[] shuffleValues() {
Stack<Integer> possibleValues = new Stack<>(); // create a stack to store possible values in
for (int i = 0; i < 8; i++) { // populate the stack with values 0 - 7
possibleValues.push(i);
}
int[] output = new int[8]; // create int array for output
for (int i = 0; i < 4; i++) {
int a = possibleValues.get(rand.nextInt(possibleValues.size())); // get random element
possibleValues.removeElement(a); // remove from list
int b = possibleValues.get(rand.nextInt(possibleValues.size())); // get pair element
possibleValues.removeElement(b); // remove from list
output[a] = b; // assign b to a index
output[b] = a; // assign a to b index
}
return output;
}
/**
* The printTile() method is a basic way to print out a tile in terms of points.
*/
public void printTile()
{
//StringBuilder output = new StringBuilder("[0] [1] [2] [3] [4] [5] [6] [7]\n");
System.out.print("[ ");
for (int location : points)
{
//output.append(" ").append(location).append(" ");
System.out.printf("%d ", location);
}
//System.out.println(output + "\n");
System.out.print("] ");
}
/**
* The rotateTile method allows for a tile to be rotated a number of rotations and a new Tile object to be returned.
* It will be rotated between 0 and 3 times clockwise.
*
* @param cwRotations
* Specifies how many times to rotate the tile (0 to 3 times clockwise)
* @return A new Tile object with the updated orientation.
* @throws Exception
* Thrown if the input (number of rotations) is invalid (below 0 or above 3).
*/
public Tile rotateTile(int cwRotations) {
if (cwRotations < 0 || cwRotations > 3) {
return null;
}
int[] legend = new int[8];
int[] newTile = new int[8];
switch(cwRotations) {
case 1: legend = new int[] {2, 3, 4, 5, 6, 7, 0, 1};
break;
case 2: legend = new int[] {4, 5, 6, 7, 0, 1, 2, 3};
break;
case 3: legend = new int[] {6, 7, 0, 1, 2, 3, 4, 5};
break;
default:
return this;
}
for (int i = 0; i < 8; i++) {
int ogValue = this.getPoints()[i]; // OG mapping
int newI = legend[i]; // new i for rotation
int newVal = legend[ogValue];
newTile[newI] = newVal;
}
return new Tile(newTile);
}
public int[] getPoints() {
return points;
}
public Tile clone()
{
Tile tileCopy;
try
{
tileCopy = (Tile) super.clone();
}
catch (CloneNotSupportedException e)
{
throw new RuntimeException
("Does not implement cloneable.");
}
tileCopy.points = points.clone();
tileCopy.characters = characters.clone();
return tileCopy;
}
public void tileToChars()
{
String possibilities = "abcd";
int ptr = 0;
characters = new char[points.length];
for (int i = 0; i < points.length; i++)
{
if (characters[i] == 0)
{
characters[i] = characters[points[i]] = possibilities.charAt(ptr);
ptr++;
}
}
}
/**
* 20.4.2020
*/
public String[] getRows()
{
String[] rows = new String[4];
rows[0] = " " + characters[0] + characters[1] + " ";
rows[1] = characters[7] + " " + " " + characters[2];
rows[2] = characters[6] + " " + " " + characters[3];
rows[3] = " " + characters[5] + characters[4] + " ";
//System.out.println(rows[0]);
//System.out.println(rows[1]);
//System.out.println(rows[2]);
//System.out.println(rows[3]);
return rows;
}
/**
* 20.4.2020
*/
public String toString()
{
return String.join("\n", this.getRows());
}
/**
* Sets the current location of a boat to the proper character.
* @param k point on tile that needs boat character
* @param c character representing human or computer boat
*/
public void setBoatLocation(int k, char c)
{
characters[k] = c;
}
/**
*
*
public String getRow(int r)
{
switch (r)
{
case 0:
return " " + characters[0] + characters[1] + " ";
case 1:
return characters[7] + " " + " " + characters[2];
case 2:
return characters[6] + " " + " " + characters[3];
case 3:
return " " + characters[5] + characters[4] + " ";
default:
break;
}
}*/
}