-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
194 lines (172 loc) · 6.12 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
185
186
187
188
189
190
191
192
193
194
import java.util.Stack;
public class Board
{
// Initialize new empty board variable
private char[][] boardGrid;
// Default empty-board constructor. 8x8 slots.
public Board()
{
this.boardGrid = new char[8][8];
for(int y = 0; y < this.boardGrid.length; y++)
{
for(int x = 0; x < this.boardGrid[y].length; x++)
{
this.boardGrid[y][x] = '*';
}
}
}
// Two-argument constructor, allows for custom board sizes.
// Sets minimum size to 4x4, since it's... uh... Connect FOUR?
public Board(int sizeY, int sizeX)
{
if(sizeY < 4 || sizeX < 4)
{
System.out.println("The minimum size for a board must be at least 4x4.");
System.out.println("Setting grid size to 4x4...");
this.boardGrid = new char[4][4];
}
else
{
this.boardGrid = new char[sizeY][sizeX];
}
for(int y = 0; y < this.boardGrid.length; y++)
{
for(int x = 0; x < this.boardGrid[y].length; x++)
{
this.boardGrid[y][x] = '*';
}
}
}
// Getters
public char[][] getBoardGrid()
{
return this.boardGrid;
}
// Methods
public void updateBoardGrid(Stack<Token> tokenStack)
{
if(!tokenStack.empty())
{
int y = tokenStack.peek().getYCoord();
int x = tokenStack.peek().getXCoord();
if (tokenStack.peek().getColor() == 'y')
{
this.boardGrid[y][x] = 'Y';
}
else
{
this.boardGrid[y][x] = 'R';
}
}
else;
}
//checkForVictory() searches the provided token stack for contiguous tokens in a vector, and
//calls the seekVector() helper function to find other tokens in a line. It only checks the last
//token pushed onto the stack, so it is executed every time a new token is played.
public boolean checkForVictory(Stack<Token> tokenStack)
{
int vectorDirectionY;
int vectorDirectionX;
Token lastTokenPlaced = tokenStack.peek();
for(Token t : tokenStack)
{
if((Math.abs(lastTokenPlaced.getYCoord() - t.getYCoord()) <= 1)
&& (Math.abs(lastTokenPlaced.getXCoord() - t.getXCoord()) <= 1)
&& lastTokenPlaced != t)
{
if(lastTokenPlaced.getColor() == t.getColor())
{
vectorDirectionY = lastTokenPlaced.getYCoord() - t.getYCoord();
vectorDirectionX = lastTokenPlaced.getXCoord() - t.getXCoord();
if(findWinningVector(tokenStack, t, vectorDirectionY, vectorDirectionX))
{
return true;
}
}
}
}
return false;
}
private boolean findWinningVector(Stack<Token> tokenStack, Token nextToken, int vectorDirectionY, int vectorDirectionX)
{
Token reverseToken = findReverseToken(tokenStack, nextToken, vectorDirectionY, vectorDirectionX);
int vectorMagnitude = findVectorMagnitude(tokenStack, nextToken, vectorDirectionY, vectorDirectionX, 2);
if(vectorMagnitude >= 4)
{
return true;
}
else
{
if(findVectorMagnitude(tokenStack, reverseToken, (vectorDirectionY * -1), (vectorDirectionX * -1), 1) >= 4)
{
return true;
}
}
return false;
}
private int findVectorMagnitude(Stack<Token> tokenStack, Token nextToken, int vectorDirectionY, int vectorDirectionX, int initialMagnitude)
{
boolean foundNextToken = false;
int vectorMagnitude = initialMagnitude;
do
{
for(Token t : tokenStack)
{
if((nextToken.getYCoord() == t.getYCoord() + vectorDirectionY)
&& (nextToken.getXCoord() == t.getXCoord() + vectorDirectionX)
&& t.getColor() == nextToken.getColor())
{
vectorMagnitude++;
nextToken = t;
foundNextToken = true;
break;
}
else
{
foundNextToken = false;
}
}
}while(foundNextToken);
return vectorMagnitude;
}
private Token findReverseToken(Stack<Token> tokenStack, Token nextToken, int vectorDirectionY, int vectorDirectionX)
{
boolean foundNextToken = false;
Token reverseToken = nextToken;
do
{
for(Token t : tokenStack)
{
if((nextToken.getYCoord() == t.getYCoord() + vectorDirectionY) && (nextToken.getXCoord() == t.getXCoord() + vectorDirectionX))
{
if(t.getColor() == nextToken.getColor())
{
reverseToken = t;
nextToken = t;
foundNextToken = true;
break;
}
}
else
{
foundNextToken = false;
}
}
}while(foundNextToken);
return reverseToken;
}
public void printBoardGrid()
{
System.out.println((char)218 + " --1--2--3--4--5--6--7--8--" + (char)191);
for(int y = 0; y < this.boardGrid.length; y++)
{
System.out.printf(" %d | ", y);
for(int x = 0; x < this.boardGrid[y].length; x++)
{
System.out.print(" " + this.boardGrid[y][x] + " ");
}
System.out.printf(" |%n");
}
System.out.println((char)192 + " --------------------------" + (char)217);
}
}