-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect4.java
More file actions
executable file
·279 lines (142 loc) · 3.17 KB
/
Connect4.java
File metadata and controls
executable file
·279 lines (142 loc) · 3.17 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
int w = 7, h= 11, bs =155;
int last[]={0,0}; //yx
int[][]board = new int [h][w];
int player =1;
void setup()
{
size(displayWidth,displayHeight);
ellipseMode(CORNER);
}
int p(int y, int x)
{// p stands for piece
int a;
if(y < 0 || x < 0|| y >= h || x >= w )
{
a=0;
}
else
{
a=board[y][x];
}
return a;
}
int getWinner()
{ //check row
for(int y = 0; y<h; y++)
for(int x=0;x<w;x++)
if (p(y,x) !=0 && p(y,x) == p(y,x+1) && p(y,x) == p(y,x+2) && p(y,x) == p(y,x+3))
return p(y,x);
// check column
for(int y = 0; y<h; y++)
for(int x=0;x<w;x++)
if(p(y,x) !=0 && p(y,x)==p(y+1,x) && p(y,x)==p(y+2,x) && p(y,x)==p(y+3,x))
return p(y,x);
// check diagonal
for(int y = 0; y<h; y++)
for(int x=0;x<w;x++)
for (int d= -1; d<= 1; d+=2)
if(p(y,x) !=0 && p(y,x) == p(y+1*d,x+1) && p(y,x) == p(y+2*d,x+2) && p(y,x) == p(y+3*d,x+3))
return p(y,x);
//check for tie
for(int y = 0; y<h; y++)
for(int x=0;x<w;x++)
if (p(y,x)==0) return 0; //winner
return -1; // tie
}
// check where to drop the piece
int nextSpace(int x)
{
for(int y = h-1; y>=0; y--)
if (board[y][x] == 0)
return y;
return -1;
}
void mousePressed()
{
int x = mouseX/bs;
int y = nextSpace(x);
last[0]=y;
last[1]=x;
if(y>=0)
{
board[y][x] = player;
if(player == 1)//switch next player to move
player=2;
do{
x=randomWithRange(0,6);
}while(nextSpace(x)==-1);
y= nextSpace(x);
if(getWinner()==0)
{
board[y][x]= player;}
player=1;
}
}
void draw()
{
if(getWinner() ==0)
{
for(int j =0; j<h; j++)
for(int i =0; i<w; i++)
{
fill(240,48,48);//42,250,212);
rect(i * bs,j*bs,bs,bs); // grid
if(board [j] [i] > 0)
{
fill(0, board [j][i] == 2?255:0,board[j][i] == 1?255:0); //colours of circles
ellipse (i*bs,j*bs,bs,bs);
}
}
}
else // restart
{
for(int j =0; j<h; j++)
for(int i =0; i<w; i++)
{
fill(240,48,48);
rect(i * bs,j*bs,bs,bs); // grid
if(board [j] [i] > 0)
{
fill(0, board [j][i] == 2?255:0,board[j][i] == 1?255:0); //colours of circles
ellipse (i*bs,j*bs,bs,bs);
}
}
fill(0, board [last[0]][last[1]] == 2?255:0,board[last[0]][last[1]] == 1?255:0); //colours of circles
ellipse (last[1]*bs,last[0]*bs,bs,bs);
String winner="";
fill(255,255,255,80);
rect(0,0,10000,10000);
if(getWinner()==1)
{
winner="BLUE";
}
else
{
winner="GREEN";
}
fill(255);
PFont mono;
mono= loadFont("Aharoni-Bold-48.vlw");
textFont(mono);
text("Winner: " +winner + " PLAYER", 130,200);
text("(Tap here to restart)",130,400);
mousePressed=false;
if(mouseY<400)
{
player =1;
for(int y = 0; y<h; y++)
for(int x=0;x<w;x++)
board [y] [x] = 0;
}
}
fill(0);
PFont mo;
mo= loadFont("Aharoni-Bold-48.vlw");
textFont(mo);
text("CONNECT 4 Created by Jerome Alve",100,1800);
}
int randomWithRange(int min, int max)
{
int range= (max-min)+1;
return (int)(Math.random()*range) + min;
}