-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositionTracker.java
276 lines (227 loc) · 6.4 KB
/
positionTracker.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
*
* Chris Fortier
* term project
*
* positionTracker.java
*
* class is instantiated and keeps track of every position in the game.
*
*/
import java.util.*;
import java.lang.Math.*;
public class positionTracker
{
//instance variables
private String output = "";
private boolean positionDebug = false;
private int diceRoll = -1;
//create an array of chars that represent each of the available boxes on the gameboard
char[][] gameBoardArray = new char[25][35];
//create two dimension array to track position of each piece
int[][] trackPieces = new int[6][2];
//create simple array to track position number to position character
char[] pieceChar = new char[6];
/*
* main constructor
* @param debug - boolean for debug mode
*/
positionTracker(boolean debug)
{
//set dubug
positionDebug = debug;
setOutput("Hello from position tracker");
//initialize board
for (int row = 0; row < 25; row++) {
for (int column = 0; column < 35; column++) {
gameBoardArray[row][column] = ' ';
}
}
//set pieceChar
pieceChar[0] = 'M';
pieceChar[1] = 'S';
pieceChar[2] = 'W';
pieceChar[3] = 'G';
pieceChar[4] = 'B';
pieceChar[5] = 'P';
//set initial positions
setInitialPositions();
//if (positionDebug) printGameBoardArray();
}
/*
* @param piece - int identifiying which piece is active - calculate available positions
*/
public void calcAvailablePositions(int piece)
{
//get row and column of current piece
int currentRow = trackPieces[piece][0];
int currentColumn = trackPieces[piece][1];
//variables
int differenceRow = -1;
int differenceColumn = -1;
int absDifference = -1;
//computes distance availble from current position
for (int row = 0; row < 25; row++) {
for (int column = 0; column < 35; column++) {
//get the differences in x and y values
differenceColumn = Math.abs(currentColumn - column);
differenceRow = Math.abs(currentRow - row);
absDifference = differenceColumn + differenceRow;
//if absDifference is less than diceRoll thne mark with letter A for available
if (absDifference <= diceRoll) {
//only update blank spaces
if (gameBoardArray[row][column] == ' ') {
gameBoardArray[row][column] = 'A';
}
}
//debug
/*
if (positionDebug) {
System.out.println("\ncurrentRow: " + currentRow + " - row[" + row + "] = " + differenceRow);
System.out.println("currentColumn: " + currentColumn + " - column[" + column + "] = " + differenceColumn);
System.out.println("absDifference: " + absDifference +" diceRoll: " + diceRoll);
} */
}
} //end for loops
//debug - reprint gameBoardArray
if (positionDebug) printGameBoardArray();
}
/*
* sets initial positions for each piece
*/
public void setInitialPositions()
{
//only called during initialization. Does not check to see if space is available.
//gameBoardArray[row][col] = piece;
//debug
if (positionDebug) { System.out.println("setInitialPostions(). Called"); }
/*set each position manually:
* gameBoard[row][column] = (char)piece
* trackPieces: first index is the piece.
* second index [0] = row
* second index [1] = column
*/
//set Colonel Mustard
gameBoardArray[5][7] = 'M';
trackPieces[0][0] = 5;
trackPieces[0][1] = 7;
//set Miss Scarlett
gameBoardArray[5][17] = 'S';
trackPieces[1][0] = 5;
trackPieces[1][1] = 17;
//set Mrs. White
gameBoardArray[5][27] = 'W';
trackPieces[2][0] = 5;
trackPieces[2][1] = 27;
//set Green
gameBoardArray[18][7] = 'G';
trackPieces[3][0] = 18;
trackPieces[3][1] = 7;
//set Mrs. Peacock
gameBoardArray[18][17] = 'B';
trackPieces[4][0] = 18;
trackPieces[4][1] = 17;
//set Mrs. Purple
gameBoardArray[18][27] = 'P';
trackPieces[5][0] = 18;
trackPieces[5][1] = 27;
}
/*
* accepts two integers representing a location on the board. Checks to see if it is available and sets that position
* @param row - int row position
* @param column - int column position
* @param piece - int current piece
*/
public int setPosition(int row, int column, int piece)
{
//if the space is available, clear the old position and place it in the new one.
if (gameBoardArray[row][column] == 'A') {
//clear the prior position
gameBoardArray[trackPieces[piece][0]][trackPieces[piece][1]] = ' ';
//set the position
gameBoardArray[row][column] = pieceChar[piece];
trackPieces[piece][0] = row;
trackPieces[piece][1] = column;
setOutput(pieceChar[piece] + " was successfully placed at row[" + row + "] / column[" + column + "]");
//debug - reprint gameBoardArray
if (positionDebug) printGameBoardArray();
return 1; //1 for success
} else {
//position is blocked
setOutput("row[" + row + "] / column[" + column + "] is not available");
//debug - reprint gameBoardArray
if (positionDebug) printGameBoardArray();
return -1; //-1 for failure
}
}
/*
* roll dice, set variable, and return integer for number of available spaces
* @return roll - int for number of spaces available
*/
public int rollDice()
{
//random number generater that returns a number from 1 to 12
Random generator = new Random();
int roll = generator.nextInt(11) + 2;
//set instance variable
diceRoll = roll;
return roll;
}
/*
* @return diceRoll - value of roll
*/
public int getDiceRoll()
{
return diceRoll;
}
/*
* @param s - string to send to output
*/
public void setOutput(String s)
{
output = s;
}
/*
* @return output - String output
*/
public String getOutput()
{
return output;
}
/*
* printGameBoardArray() to standard out
*/
public void printGameBoardArray()
{
for (int row = 0; row <= 24; row++){
System.out.println("\n");
for (int column = 0; column < 35; column++) {
System.out.print("|" + gameBoardArray[row][column] + "|");
}
}
System.out.println("\n");
}
/*
* resetBoard() - clears board leaving only current pieces in their position
*/
public void resetBoard()
{
for (int row = 0; row <= 24; row++){
for (int column = 0; column < 35; column++) {
//clear 'A' from cells. Leaving only the actual positions
if (gameBoardArray[row][column] == 'A') {
gameBoardArray[row][column] = ' ';
}
}
}
//debug
if (positionDebug) printGameBoardArray();
}
/*
* @return gameBoardArray - returns the gameBoardArray
*/
public char[][] getGameBoardArray()
{
return gameBoardArray;
}
}