-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
186 lines (149 loc) · 3.59 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
public class Board {
public char[][] board;
public Board(){
board = new char[7][7];
for(int i=0; i<board.length; i++){
for(int j=0; j<board[i].length; j++){
board[i][j] = ' ';
}
}
}
public void rotateBoard(){
char[][] c = new char[7][7];
for(int i=0; i<board.length; i++){
for(int j=0; j<board[i].length; j++){
c[j][(int)Math.abs(i-6)] = board[i][j];
}
}
for(int i=0; i<board.length; i++){
for(int j=0; j<board[i].length; j++){
board[i][j] = c[i][j];
}
}
}
public int putPiece(int column, char color){
if(column > 6 || column < 0)
return -1;
for(int i=6; i>=0; i--){
if(this.board[i][column] == ' '){
this.board[i][column] = color;
return i;
}
}
return -1;
}
public char checkAlignment(int row, int column){
if(checkVertical(row,column) || checkHorizontal(row,column) ||
checkDiagonal(row,column) || checkAntiDiagonal(row,column))
return this.board[row][column];
else
return ' ';
}
private boolean checkVertical(int row, int column){
char c = this.board[row][column];
int count = 0;
for(int i = 6; i >= 0; i--){//loop thru column for character, reset when not char
if(this.board[i][column] == c){
count++;
if(count >= 4)
return true;
}
else if(this.board[i][column] != c){
count = 0;
}
}
return false;
}
private boolean checkHorizontal(int row, int column){
char c = this.board[row][column];
int count = 0;
for(int i = 6; i >= 0; i--){//loop through row
if(this.board[row][i] == c){
count++;
if(count >= 4)
return true;
}
else if(this.board[row][i] != c){
count = 0;
}
}
return false;
}
private boolean checkDiagonal(int row, int column){
char c = this.board[row][column];
int count = 0;
int i = row;
int j = column;
while(i >= 0 && j < this.board[i].length
&& this.board[i][j] == c){
//go up diagonal until you don't see the matching piece
i--;
j++;
}
//then go back down diagonal and count like normal
i++;
j--;
while(i < this.board.length && j >= 0){
if(this.board[i][j] == c){
count++;
if(count >= 4)
return true;
}
else if(this.board[i][j] != c){
count = 0;
}
i++;
j--;
}
return false;
}
private boolean checkAntiDiagonal(int row, int column){
char c = this.board[row][column];
int count = 0;
int i = row;
int j = column;
while(i >= 0 && j >= 0
&& this.board[i][j] == c){
//go up AntiDiagonal until you don't see the matching piece
i--;
j--;
}
//then go back down AntiDiagonal and count like normal
i++;
j++;
while(i < this.board.length && j < this.board[i].length){
if(this.board[i][j] == c){
count++;
if(count >= 4)
return true;
}
else if(this.board[i][j] != c){
count = 0;
}
i++;
j++;
}
return false;
}
public void printScreen(){
String board = " 0 1 2 3 4 5 6 \n";
for(int i=0; i < this.board.length; i++){
board += String.format("%d" , i);
for(int j = 0; j < this.board[i].length; j++){
board += "|" + this.board[i][j];
}
board += "|\n";
}
System.out.println("Red = O\nYellow = X");
System.out.println(board);
}
public static void main(String[] args){
System.out.println("Ran");
Board b = new Board();
b.board[2][2] = 'X';
b.board[3][4] = 'O';
b.board[1][2] = 'Q';
b.rotateBoard();
b.printScreen();
}
}