-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwinCondition.java
More file actions
206 lines (192 loc) · 5.72 KB
/
Copy pathwinCondition.java
File metadata and controls
206 lines (192 loc) · 5.72 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
public class winCondition {
int seqNum;//Amount needed in sequence of eachother
int boardLength, boardHeight;//board dimensions
int currHor, currVer;//for the search method
public winCondition(Board _board)
{
//
if(_board.get_connect_four() == false)//If not connect 4 must be connect 5
{
seqNum = 5;
boardLength = 9;
boardHeight = 6;
}
else
{
seqNum = 4;
boardLength = 7;
boardHeight = 6;
}
}
//row & col would be the position it was just placed in
public boolean gameOver(Board _board, Coin c, int row, int col)
{
boolean isOver = false; //win check used for this whole method
String currColor = c.getColor(); //player color
Coin[][] currBoard = _board.getBoard(); //duplicates the board into a new board
int inARow = 0; //keeping track of the amount of the same color in a row
//In theory we would only need to check a few directions for the end of a sequence, because if the piece itself
//is already the tail then you could go the other directions to check for a victory
//you never need to check for vertical aka 8, because a new piece will always be on top
//so you would only need to check down left, down right, and horizontal
//Direction is correlating the numpad 7 8 9
// 4 5 6
// 1 2 3
//I am attempting to get to the leftmost digit from each of these, checking each direction and then going from there
//firstly going to just check the vertical win condition
//if it is already the tail in ANY situation, then you only need to scan 4 directions, downwards, down and to the right, to the right, and upwards and to the right
if(!isOver)//horizontal 4-6
{
inARow = 0;
giveTail(currBoard, row, col - 1, currColor, 4); inARow = 0;
for(int i = currHor; i < boardLength; i++)
{
if(currBoard[row][i].getColor() == currColor)
{
inARow++;
if(inARow == seqNum)
{
isOver = true;
break;
}
}
else//if next position downwards is not same color, breaks the loop and determines no vertical victory
{
inARow = 0;
break;
}
}
}
if(row <= boardHeight - 4)
{
if(!isOver)//if it's too small to get a vertical win just don't check
{
inARow = 0;
for(int i = row; i < boardHeight; i++)//no need to get to the tail end, since vertical win condition would mean this would be on top anyways 8-2
{
if(currBoard[i][col].getColor() == currColor)
{
inARow++;
if(inARow == seqNum)
{
isOver = true;
break;
}
}
else//if next position downwards is not same color, breaks the loop and determines no vertical victory
{
inARow = 0;
break;
}
}
}
if(!isOver)//upleft scan, for down right victory scan 7-3
{
inARow = 0;
giveTail(currBoard, row - 1, col - 1, currColor, 7);
boolean isBroke = false;
int i = currVer - 1, j = currHor - 1;
//initially made the mistake of using a for loop here, i cannot do that because it would iterate through everything between
do {
if(i >= boardHeight - 1 || j > boardLength - 1)//out of bounds
{
isBroke = true;
}
if(currBoard[i][j].getColor() == currColor || !isBroke)
{
inARow++;
}
if(currBoard[i][j].getColor() != currColor)//Then end checks, basically if either they win or there is a break in the pattern then it will trigger this block
{
isBroke = true;
inARow = 0;
}
if(inARow == seqNum)
{
isOver = true;
isBroke = true;
}
i++;
j++;
}while(!isBroke);
}
if(!isOver)//upright scan, for up right victory scan 1-9
{
inARow = 0;
giveTail(currBoard, row + 1, col - 1, currColor, 1);
boolean isBroke = false;
//initially made the mistake of using a for loop here, i cannot do that because it would iterate through everything between
do {
int i = currVer, j = currHor;
if(currBoard[i][j].getColor() == currColor)
{
inARow++;
}
if(currBoard[i][j].getColor() != currColor)//Then end checks, basically if either they win or there is a break in the pattern then it will trigger this block
{
isBroke = true;
inARow = 0;
}
if(inARow == seqNum)
{
isOver = true;
isBroke = true;
}
i--;
j++;
}while(!isBroke);
}
}
return isOver; //replace with isOver before done
}
public void giveTail(Coin[][] c, int row, int col, String currColor, int direction)
{
boolean preemptiveTest;
boolean inBounds = (row < boardHeight && col < boardLength && col >= 0 && row >= 0);
if(inBounds)
{
preemptiveTest = (c[row][col].getColor() == currColor);//since the call in the other code is going to preemptively adjust the digits you can make this call in advance and declare a variable
}
else
{
preemptiveTest = false;
}
switch(direction)
{
case 7:
if(!inBounds||!preemptiveTest)//cannot go left or cannot go higher
{
currHor = col + 1;
currVer = row + 1;
break;
}//reverting the change made by the recursion and then breaking out of the switch case statement and thus the recursion
if(preemptiveTest) //checking if most top left piece of the player color, will recurse until it is
{
giveTail(c, row - 1, col - 1, currColor, direction);
}
break;
case 4:
if(!inBounds||!preemptiveTest)
{
currVer = row;
currHor = col + 1;
}
if(preemptiveTest)//checking for leftmost
{
giveTail(c, row, col - 1, currColor, direction);
}
break;
case 1:
if(!inBounds||!preemptiveTest)
{
currVer = row - 1;
currHor = col + 1;
}
if(preemptiveTest)//checking for leftmost and downmost
{
giveTail(c, row + 1, col - 1, currColor, direction);
}
break;
}
}
}