-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods.java
More file actions
256 lines (223 loc) · 5.85 KB
/
Methods.java
File metadata and controls
256 lines (223 loc) · 5.85 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
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
package project;
import java.util.Scanner;
public class Methods {
static public void printConnectFour(char[][] connectFour)
{
System.out.println(" 0 1 2 3 4 5 6");
for(int i=0;i<connectFour.length;i++)
{
for(int j=0;j<connectFour[i].length;j++)
{
System.out.print("|");
//if it has its default value print space
if(connectFour[i][j]=='\u0000')
System.out.print(" ");
else
System.out.print(connectFour[i][j]);
}
System.out.println("|");
}
}
static public int checkForError(int columnNumber,String message)
{
Scanner scan=new Scanner(System.in);
//the loop will run untill the user enter a number from 0 to 6
while(columnNumber<0 || columnNumber>6)
{
System.out.println("Wrong input!");
System.out.print(message);
columnNumber=scan.nextInt();
}
return columnNumber;
}
static public void fill(char[][] connectFour,int columnNumber,int turn)
{
char color=Methods.turnColor(turn);
for(int i=connectFour.length-1;i>=0;i--)
{
//if it has the default value then it's an empty place
if(connectFour[i][columnNumber]=='\u0000')
{
connectFour[i][columnNumber]=color;
return;//don't need to continue the code if he find an empty place
}
}
//if the return didn't work then this column is full
columnNumber=Methods.WrongColumn(columnNumber, turn);
//since we have another column number then go fill the array
Methods.fill(connectFour, columnNumber, turn);
}
static public int WrongColumn(int columnNumber,int turn)
{
System.out.println("Column "+columnNumber+" is full! Please choose another column");
//let the user choose another column
columnNumber=Methods.chooseColumn(turn);
return columnNumber;
}
static public int chooseColumn(int turn)
{
Scanner scan=new Scanner(System.in);
int columnNumber;
//if turn is odd then it's red's turn
if(turn%2==1)
{
System.out.print("Drop a red disk at column(0-6) : ");
columnNumber=scan.nextInt();
columnNumber=Methods.checkForError(columnNumber,"Drop a red disk at column(0-6) : ");
}
else
{
System.out.print("Drop a yellow disk at column(0-6) : ");
columnNumber=scan.nextInt();
columnNumber=Methods.checkForError(columnNumber,"Drop a red disk at column(0-6) : ");
}
return columnNumber;
}
static public char turnColor(int turn)
{
char color;
//if the turn is odd then it's red
if(turn%2==1)
color='R';
else
color='Y';
return color;
}
static public void win(char[][] connectFour,int turn)
{
char color=Methods.turnColor(turn);
Methods.winHorizontal(connectFour, color);
Methods.winVertical(connectFour, color);
Methods.winDiagonalRight(connectFour, color);
Methods.winDiagonalLeft(connectFour, color);
}
static public void winHorizontal(char[][] connectFour,char color)
{
int count=0;
//start from the bottom because it's the first to be filled
for(int i=connectFour.length-1;i>=0;i--)
{
for(int j=0;j<connectFour[i].length;j++)
{
if(connectFour[i][j]==color)
{
count++;
if(count==4)
{
Methods.winMessage(color, connectFour);
}
}
else//start from the beginning if they are not consecutive
count=0;
}
count=0;
}
}
static public void winVertical(char[][] connectFour,char color)
{
int count=0;
int noOfRows=connectFour.length;
for(int j=0;j<connectFour[noOfRows-1].length;j++)
{
for(int i=connectFour.length-1;i>=0;i--)
{
if(connectFour[i][j]==color)
{
count++;
if(count==4)
{
Methods.winMessage(color, connectFour);
}
}
else//start from the beginning if they are not consecutive
count=0;
}
count=0;
}
}
static public void winDiagonalRight(char[][] connectFour,char color)
{
//starting from button left and ascending to the right
int count=0;
int saver;
//3 is the limit no matter how big the connect4 is because if i<3 it can connect 4 diagonally
for(int i=connectFour.length-1;i>=3;i--)
{
//to save i because it changes in the nested loop
saver=i;
for(int j=0;j<connectFour[i].length;j++)
{
//another for with same parameters
//the loop below to get the diagonal shape and if the the diagonal shape isn't completed then the loop is over and j is incremented
for(int k=j;k<connectFour[i].length;k++)
{
if(connectFour[i][k]==color)
{
count++;
if(count==4)
{
Methods.winMessage(color, connectFour);
}
i--;
}
else//start from the beginning if they are not consecutive
{
count=0;
i=saver;
break;
}
}
i=saver;
}
i=saver;
}
}
static public void winDiagonalLeft(char[][] connectFour,char color)
{
//base is at bottom right and ascending to the left
int count=0,saver;
//3 is the limit no matter how big the connect4 is because if i<3 it can connect 4 diagonally
for(int i=connectFour.length-1;i>=3;i--)
{
//to save i because it changes in the nested loop
saver=i;
for(int j=connectFour[i].length-1;j>=0;j--)
{
for(int k=j;k>=0;k--)
{
if(connectFour[i][k]==color)
{
count++;
if(count==4)
{
Methods.winMessage(color, connectFour);
}
i--;
}
else//start from the beginning if they are not consecutive
{
count=0;
i=saver;
break;
}
}
}
i=saver;
}
}
static public void winMessage(char color,char[][] connectFour)
{
//first print the connect four to show the win
Methods.printConnectFour(connectFour);
if(color=='R')
{
System.out.println("Red wins!");
System.exit(0);
}
else
{
System.out.println("Yellow wins!");
System.exit(0);
}
}
}