-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathludo.java
544 lines (518 loc) · 16.7 KB
/
ludo.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Player {
int height,width,status,coin;
Pawn[] pa=new Pawn[4];
public Player(int height,int width) {
status=-1;
coin=0;
for(int i=0;i<4;i++) {
pa[i]=new Pawn(height,width);
}
}
public void draw(Graphics2D g) {
}
}
class Pawn {
int x,y;
int current;
int height,width;
public Pawn(int h,int w){
current=-1;
x=-1;
y=-1;
height=h;
width=w;
}
//func to draw pawn on board
public void draw(Graphics2D g, int i, int j,int play) {
if(current==-1) { //pawn not on board
int temp1=80+(height/2),temp2=50+(width/2); //initial pos of pawn
x=i;
y=j;
if(play==0) {
g.setColor(Color.RED);
}
else if(play==1) {
g.setColor(Color.GREEN);
}
else if(play==2) {
g.setColor(Color.YELLOW);
}
else if(play==3) {
g.setColor(Color.BLUE);
}
g.fillOval(temp1+5+(i*width),temp2+5+(j*height),width-10,height-10); // drawing pawn
g.setStroke(new BasicStroke(2)); //border thickness
g.setColor(Color.BLACK); //border color
g.drawOval(temp1+5+(i*width),temp2+5+(j*height),width-10,height-10); // drawing border
}
else
{ //pawn on board
int temp1=80,temp2=50;
x=Path.ax[play][current];
y=Path.ay[play][current];
if(play==0) {
g.setColor(Color.RED);
}
else if(play==1) {
g.setColor(Color.GREEN);
}
else if(play==2) {
g.setColor(Color.YELLOW);
}
else if(play==3) {
g.setColor(Color.BLUE);
}
g.fillOval(temp1+5+(x*width),temp2+5+(y*height),width-10,height-10);
g.setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
g.drawOval(temp1+5+(x*width),temp2+5+(y*height),width-10,height-10);
}
}
}
//path info
class Path {
static int[][] ax= {
{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,1,2,3,4,5,6},
{8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,7,7,7,7,7,7},
{13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,13,12,11,10,9,8},
{6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,7,7,7,7,7,7}
};
static int[][] ay= {
{6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,7,7,7,7,7,7},
{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,1,2,3,4,5,6},
{8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,7,7,7,7,7,7},
{13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,13,12,11,10,9,8}
};
static int[][] initialx= { //initial x coordinates of each player
{1,1,3,3},
{10,10,12,12},
{10,10,12,12},
{1,1,3,3}
};
static int[][] initialy= { //initial y coordinates of each player
{1,3,1,3},
{1,3,1,3},
{10,12,10,12},
{10,12,10,12}
};
}
// layout for board
class Layout {
int x,y,width,height;
public Layout(int xi,int yi) {
x=xi;
y=yi;
width=30;
height=30;
}
//draw board
public void draw(Graphics2D g){
g.setColor(Color.WHITE);
g.fillRect(x,y,15*width,15*height);
for(int i=0;i<6;i++) { //inner squares
g.setColor(Color.RED);
g.fillRect(x+(i*width),y, width, height);
g.fillRect(x, y+(i*height), width, height);
g.fillRect(x+(i*width),y+(5*height), width, height);
g.fillRect(x+(5*width), y+(i*height), width, height);
g.setColor(Color.GREEN);
g.fillRect(x+((i+9)*width),y, width, height);
g.fillRect(x+(9*width), y+(i*height), width, height);
g.fillRect(x+((i+9)*width),y+(5*height), width, height);
g.fillRect(x+(14*width), y+(i*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(x+((i+9)*width),y+(9*height), width, height);
g.fillRect(x+(9*width), y+((i+9)*height), width, height);
g.fillRect(x+((i+9)*width),y+(14*height), width, height);
g.fillRect(x+(14*width), y+((i+9)*height), width, height);
g.setColor(Color.BLUE);
g.fillRect(x+(i*width),y+(9*height), width, height);
g.fillRect(x, y+((i+9)*height), width, height);
g.fillRect(x+(i*width),y+(14*height), width, height);
g.fillRect(x+(5*width), y+((i+9)*height), width, height);
}
for(int i=1;i<6;i++) { //inner boxes
g.setColor(Color.RED);
g.fillRect(x+(i*width),y+(7*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(x+((8+i)*width),y+(7*height), width, height);
g.setColor(Color.GREEN);
g.fillRect(x+(7*width),y+(i*height), width, height);
g.setColor(Color.BLUE);
g.fillRect(x+((7)*width),y+((8+i)*height), width, height);
}
g.setColor(Color.RED);
g.fillRect(x+(1*width),y+(6*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(x+((13)*width),y+(8*height), width, height);
g.setColor(Color.GREEN);
g.fillRect(x+(8*width),y+(1*height), width, height);
g.setColor(Color.BLUE);
g.fillRect(x+((6)*width),y+((13)*height), width, height);
int temp1=x+45,temp2=y+45;
for(int i=0;i<2;i++) { //outer boxes
for(int j=0;j<2;j++) {
g.setColor(Color.RED);
g.fillRect(temp1+(2*i*width),temp2+(2*j*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+9*height, width, height);
g.setColor(Color.GREEN);
g.fillRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+0*height, width, height);
g.setColor(Color.BLUE);
g.fillRect(temp1+(2*i*width)+0*width,temp2+(2*j*height)+9*height, width, height);
}
}
g.setColor(Color.RED);
int xpoints0[] = {x+(6*width),x+(6*width), x+15+(7*width)};
int ypoints0[] = {y+(6*height),y+(9*height),y+15+(7*width)};
int npoints = 3;
g.fillPolygon(xpoints0, ypoints0, npoints); //triangle in the center
g.setColor(Color.YELLOW);
int xpoints1[] = {x+(9*width),x+(9*width), x+15+(7*width)};
int ypoints1[] = {y+(6*height),y+(9*height),y+15+(7*width)};
int npoints1= 3;
g.fillPolygon(xpoints1, ypoints1, npoints1);
g.setColor(Color.GREEN);
int xpoints2[] = {x+(6*width),x+(9*width), x+15+(7*width)};
int ypoints2[] = {y+(6*height),y+(6*height),y+15+(7*width)};
int npoints2= 3;
g.fillPolygon(xpoints2, ypoints2, npoints2);
g.setColor(Color.BLUE);
int xpoints3[] = {x+(6*width),x+(9*width), x+15+(7*width)};
int ypoints3[] = {y+(9*height),y+(9*height),y+15+(7*width)};
int npoints3= 3;
g.fillPolygon(xpoints3, ypoints3, npoints3);
g.setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
for(int i=0;i<3;i++) { //outer boxes
for(int j=0;j<6;j++) {
g.drawRect(x+((i+6)*width),y+(j*height), width, height);
g.drawRect(x+((j)*width),y+((i+6)*height), width, height);
g.drawRect(x+((i+6)*width),y+((j+9)*height), width, height);
g.drawRect(x+((j+9)*width),y+((i+6)*height), width, height);
}
}
g.drawRect(x+((1)*width),y+(1*height),4*width,4*height);
g.drawRect(x+((10)*width),y+(1*height),4*width,4*height);
g.drawRect(x+((1)*width),y+(10*height),4*width,4*height);
g.drawRect(x+((10)*width),y+(10*height),4*width,4*height);
g.drawRect(x,y,15*width,15*height);
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
g.drawRect(temp1+(2*i*width),temp2+(2*j*height), width, height);
g.drawRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+9*height, width, height);
g.drawRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+0*height, width, height);
g.drawRect(temp1+(2*i*width)+0*width,temp2+(2*j*height)+9*height, width, height);
}
}
g.drawPolygon(xpoints0, ypoints0, npoints);
g.drawPolygon(xpoints1, ypoints1, npoints1);
g.drawPolygon(xpoints2, ypoints2, npoints2);
g.drawPolygon(xpoints3, ypoints3, npoints3);
g.drawOval(x+5+(6*width),y+5+(2*height),width-10,height-10);
g.drawOval(x+5+(12*width),y+5+(6*height),width-10,height-10);
g.drawOval(x+5+(8*width),y+5+(12*height),width-10,height-10);
g.drawOval(x+5+(2*width),y+5+(8*height),width-10,height-10);
g.setFont(new Font("Arial",Font.BOLD, 40));
g.drawString("Player 1", 90, 35);
g.drawString("Player 2", 370, 35);
g.drawString("Player 3", 90, 540);
g.drawString("Player 4", 370, 540);
g.setFont(new Font("Arial", Font.BOLD, 30));
g.drawString("Press enter to roll dice", 600,50);
// g.drawString("Click on the token to move", 550,200);
}
}
class Build_Player {
Player[] pl=new Player[4];
int[][] initialx= { //initial x positions of the players
{1,1,3,3},
{10,10,12,12},
{10,10,12,12},
{1,1,3,3}
};
int[][] initialy= {//initial y positions of the players
{1,3,1,3},
{1,3,1,3},
{10,12,10,12},
{10,12,10,12}
};
//the player positions
public Build_Player(int height, int width) {
for(int i=0;i<4;i++) {
pl[i]=new Player(height,width);
}
}
//drawing the players
public void draw(Graphics2D g) {
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
pl[i].pa[j].draw(g,initialx[i][j],initialy[i][j],i);
}
}
}
}
class GameMoves extends JPanel implements KeyListener, ActionListener,MouseListener{
private static final long serialVersionUID = 1L;
Layout la;
Build_Player p;
Timer time;
int delay=10;
int current_player,dice;
int flag=0,roll,kill=0;
public GameMoves() {
setFocusTraversalKeysEnabled(false);
requestFocus();
current_player=0;
la = new Layout(80,50);
p=new Build_Player(la.height,la.width);
dice=0;
flag=0;
roll=0;
kill=0;
}
public void paint(Graphics g){
la.draw((Graphics2D)g);
p.draw((Graphics2D)g);
if(p.pl[current_player].coin==4) { //condition for winner
g.setColor(new Color(0XCFCFCF));
g.fillRect(590, 100, 380,130);
if(current_player==0) {
g.setColor(Color.RED);
}
else if(current_player==1) {
g.setColor(Color.GREEN);
}
else if(current_player==2) {
g.setColor(Color.YELLOW);
}
else if(current_player==3) {
g.setColor(Color.BLUE);
}
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Player "+(current_player+1)+" wins.", 600, 150);
g.drawString("Congratulations!!", 600, 200);
current_player=0;
la = new Layout(80,50);
p=new Build_Player(la.height,la.width);
dice=0;
flag=0;
roll=0;
kill=0;
}
else if(dice!=0){ //condition for dice roll
g.setColor(new Color(0XCFCFCF));
g.fillRect(590, 200, 300,200);
if(current_player==0) {
g.setColor(Color.RED);
}
else if(current_player==1) {
g.setColor(Color.GREEN);
}
else if(current_player==2) {
g.setColor(Color.YELLOW);
}
else if(current_player==3) {
g.setColor(Color.BLUE);
}
g.setFont(new Font("Arial", Font.BOLD, 30));
if(current_player==0){
g.drawString("RED's turn:", 600, 250);
}
else if(current_player==1){
g.drawString("GREEN's turn:", 600, 250);
}
else if(current_player==2){
g.drawString("YELLOW's turn:", 600, 250);
}
else if(current_player==3){
g.drawString("BLUE's turn:", 600, 250);
}
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString(""+dice, 840, 250);
}
if(flag==0&&dice!=0&&dice!=6&&kill==0) {
current_player=(current_player+1)%4;
}
kill=0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER&&flag==0) {
roll=0;
dice=1 + (int)(Math.random() * 6);
repaint();
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current!=-1&&p.pl[current_player].pa[i].current!=56&&(p.pl[current_player].pa[i].current+dice)<=56) {
flag=1;
break;
}
}
if(flag==0&&dice==6) {
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current==-1) {
flag=1;
// p.pl[current_player].pa[i].current=0;
break;
}
}
}
}
}
public void mouseClicked(MouseEvent e) {
if(flag==1) {
int x=e.getX();
int y=e.getY();
System.out.println(x+","+y);
x=x-80;
y=y-50;
x=x/30;
y=y/30;
int value=-1;
int active=0;
for(int i=0;i<4;i++) {
System.out.println(p.pl[current_player].pa[i].current);
}
if(dice==6) { //dice 6
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].x==x&&p.pl[current_player].pa[i].y==y&&(p.pl[current_player].pa[i].current)<=56){
value=i;
flag=0;
break;
}
}
if(value!=-1) { //coin on the clicked position
if(p.pl[current_player].pa[value].current==-1){
active=0;
}
else{
active=1;
}
if(active==1){
p.pl[current_player].pa[value].current+=dice;
if(p.pl[current_player].pa[value].current==56) {
p.pl[current_player].coin++;
}
int k=0;
int hou=p.pl[current_player].pa[value].current;
if((hou%13)!=0&&(hou%13)!=8&&hou<51){
for(int i=0;i<4;i++) {
if(i!=current_player) {
for(int j=0;j<4;j++) {
int tem1=Path.ax[current_player][p.pl[current_player].pa[value].current],tem2=Path.ay[current_player][p.pl[current_player].pa[value].current];
if(p.pl[i].pa[j].x==tem1&&p.pl[i].pa[j].y==tem2) {
p.pl[i].pa[j].current=-1;
kill=1;
k=1;
break;
}
}
}
if(k==1)
break;
}
}
}
else{
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current==-1) {
p.pl[current_player].pa[i].current=0;
flag=0;
break;
}
}
}
}
else{
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current==-1) {
p.pl[current_player].pa[i].current=0;
flag=0;
break;
}
}
}
}
else { //dice not 6
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].x==x&&p.pl[current_player].pa[i].y==y&&(p.pl[current_player].pa[i].current+dice)<=56) {
value=i;
flag=0;
break;
}
}
if(value!=-1) { //coin on the clicked position
if(p.pl[current_player].pa[value].current==-1){
active=0;
}
else{
active=1;
}
if(active==1){
p.pl[current_player].pa[value].current+=dice;
if(p.pl[current_player].pa[value].current==56) {
p.pl[current_player].coin++;
}
int k=0;
int hou=p.pl[current_player].pa[value].current;
if((hou%13)!=0&&(hou%13)!=8&&hou<51)
{
for(int i=0;i<4;i++) {
if(i!=current_player) {
for(int j=0;j<4;j++) {
int tem1=Path.ax[current_player][p.pl[current_player].pa[value].current],tem2=Path.ay[current_player][p.pl[current_player].pa[value].current];
if(p.pl[i].pa[j].x==tem1&&p.pl[i].pa[j].y==tem2) {
p.pl[i].pa[j].current=-1;
kill=1;
k=1;
break;
}
}
}
if(k==1){
break;
}
}
}
}
else{
flag=1;
}
}
}
repaint();
}
}
public void actionPerformed(ActionEvent e) {
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent arg0) {
}
}
class Main{
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setBounds(10,10,1000,600);
jframe.setBackground(new Color(0XCFCFCF));
jframe.setTitle("LUDO");
jframe.setResizable(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameMoves gm = new GameMoves();
gm.setFocusable(true);
gm.addKeyListener(gm);
gm.addMouseListener(gm);
jframe.add(gm);
jframe.setVisible(true);
}
}