-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameGrid.java
134 lines (109 loc) · 3.19 KB
/
GameGrid.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
package tictactoe.game;
public class GameGrid {
/** A GameGrid is the grid of boxes that players use to play tictactoe.
It consists of a 3 x 3 matrix of GameSquares, each with valid
contents.
It must comply with the following:
- be able to access and modify GameSquares
- supply a method of printing an ascii representation of the board
- ?
*/
// Fields
Grid grid;
// Constructors
GameGrid() {
this.grid = new Grid();
}
// Methods
char getMark(int id) {
return this.grid.getMark(id);
}
void setMark(int id, char mark) {
this.grid.setMark(id, mark);
}
void setX(int id) {
this.grid.setX(id);
}
void setO(int id) {
this.grid.setO(id);
}
void print() {
/** A GameSquare is 7w x 5h chars, not including the borders.
Therefore, the Mark will be at position (4,3) in each square.
*/
int NUM_LINES = 17;
for (int lnCount = 1; lnCount <= NUM_LINES; ++lnCount) {
if ((lnCount % 3) == 0) {
if (lnCount==6 || lnCount==12) { printBorder(); }
// Shitty hardcoding below: vvv
// * Rows defined 0,1,2*
else if (lnCount==3) { printMarked(0); }
else if (lnCount==9) { printMarked(1); }
else if (lnCount==15) {printMarked(2); }
}
else { printFiller(); }
}
System.out.println();
}
private void printFiller() {
String fillerRow = " # # ";
System.out.println(fillerRow);
}
/** I'm a fuck and hardcoded the rows in based on lnCount */
private void printMarked(int row) {
int baseId = (row==0) ? (1) :
(row==1) ? (4) : (7);
String markedRow = " %c # %c # %c \n";
System.out.printf(markedRow, this.grid.getMark(baseId + 0),
this.grid.getMark(baseId + 1),
this.grid.getMark(baseId + 2));
}
private void printBorder() {
String borderRow = "#######################";
System.out.println(borderRow);
}
}
class Grid {
// grid of spaces
private char[][] grid;
Grid() {
this.grid = new char[3][3];
}
/** returns row given input id */
private int findRow(int id) {
int row = -1; // bogus init value
if (id >= 1 && id <= 3) {row = 0;}
else if (id >= 4 && id <= 6) {row = 1;}
else if (id >= 7 && id <= 9) {row = 2;}
return row;
}
/** returns col given input id */
private int findCol(int id) {
int rem = id % 3;
int col = (rem == 1) ? (0) :
(rem == 2) ? (1) : (2);
return col;
}
/** Requires: id is a valid square identifier (1-9)
Modifies: nothing.
Effects: Returns the mark in the spec'd square */
protected char getMark(int id) {
return this.grid[findRow(id)][findCol(id)];
}
/** Returns true is square is empty */
private boolean isEmpty(int id) {
return (getMark(id)!='X' && getMark(id)!='O');
}
/** Template for setX and setO */
protected void setMark(int id, char mark) {
//if (!isEmpty(id)) {exit();}
grid[findRow(id)][findCol(id)] = mark;
}
public void setX(int id) { setMark(id, 'X'); }
public void setO(int id) { setMark(id, 'O'); }
}
// C++ Ternary:
// var = (cond. expr) ? (valIfTrue) : (valIfFalse)
// Cascaded Ternary:
// var = (cond. expr) ? (valIfTrue) :
// (2nd cond. expr) ? (val2) : (val3)