import java.applet.*; import java.awt.*; import java.awt.event.*; public class g_dominos extends Applet implements Runnable{ Thread th = null; Image os; Graphics og; int td[][] = new int[7][8]; int vd[][] = new int[7][8]; int cd[][] = new int[7][8]; int ddd[][] = new int[28][2]; int ddn[][] = new int[28][2]; int chd[][] = new int[7][7]; int ifg,dn,ch; int sp1,sp2,sn1,sn2; int px,py,ax,ay; int cf,cco; int c1=-1,c2=-1; public void init(){ os = createImage(550,270); og = os.getGraphics(); setBackground(Color.white); while(sn2 == 0){ d_set(); } addMouseListener( new MouseAdapter(){ public void mousePressed(MouseEvent e){ if((e.getX()>30)&&(e.getX()<270)&&(e.getY()>30)&&(e.getY()<240)){ px = (e.getX()-30)/30; py = (e.getY()-30)/30; if(cf == 1){ if(cd[py][px] == -1){ ax = px ; ay = py ; cd[py][px] = cco; cf = -1; }else{ sp1 = cd[py][px]; for(int tx = 0;tx < 8;tx ++){ for(int ty = 0;ty < 7;ty ++){ if(cd[ty][tx] == sp1){ if(c1 == -1){ c1 = vd[ty][tx]; }else{ c2 = vd[ty][tx]; } cd[ty][tx] = -1; }else if(cd[ty][tx] > sp1){ cd[ty][tx] --; } } } chd[c1][c2] = 0; chd[c2][c1] = 0; c1 = -1 ; c2 = -1 ; cco --; } }else{ if(cd[py][px] == -1){ if(((px==ax)&&(py==ay-1))||((px==ax)&&(py==ay+1))||((px==ax-1)&&(py==ay))||((px==ax+1)&&(py==ay))){ if(chd[vd[py][px]][vd[ay][ax]] != 1){ cd[py][px] = cco; chd[vd[py][px]][vd[ay][ax]] = 1; chd[vd[ay][ax]][vd[py][px]] = 1; cco ++; cf = 1; } } }else{ if((px==ax)&&(py==ay)){ cd[ay][ax] = -1; cf = 1; } } } }else if((e.getX()>300)&&(e.getX()<390)&&(e.getY()>170)&&(e.getY()<200)){ for(int t1 = 0;t1 < 8;t1 ++){ for(int t2 = 0;t2 < 7;t2 ++){ cd[t2][t1] = -1; } } for(int t1 = 0;t1 < 7;t1 ++){ for(int t2 = 0;t2 < 7;t2 ++){ chd[t1][t2] = 0; } } cf = 1 ; cco = 1 ; }else if((e.getX()>300)&&(e.getX()<390)&&(e.getY()>210)&&(e.getY()<240)){ sn2 = 0; while(sn2 == 0){ d_set(); } } repaint(); } } ); } public void paint(Graphics g){ og.setColor(Color.white); og.fillRect(0,0,550,270); og.setColor(Color.black); for(int tx = 0;tx < 8;tx ++){ for(int ty = 0;ty < 7;ty ++){ d_dice(tx,ty); } } for(int ix = 0;ix < 7;ix ++){ for(int iy = ix;iy < 7;iy ++){ if(chd[iy][ix] == 0){ og.setFont(new Font("",Font.PLAIN,14)); og.setColor(Color.gray); }else{ og.setFont(new Font("",Font.BOLD,15)); og.setColor(Color.blue); } og.drawString(""+ix+"-"+iy,iy*35+290,ix*30+50); } } for(int ix = 0;ix < 7;ix ++){ for(int iy = 0;iy < 7;iy ++){ if(cd[iy][ix] != -1){ if(cd[iy][ix] == cd[iy][ix+1]){ og.setColor(Color.red); og.drawRoundRect(30*ix+31,30*iy+31,58,28,9,9); } } } } for(int ix = 0;ix < 8;ix ++){ for(int iy = 0;iy < 6;iy ++){ if(cd[iy][ix] != -1){ if(cd[iy][ix] == cd[iy+1][ix]){ og.setColor(Color.red); og.drawRoundRect(30*ix+31,30*iy+31,28,58,9,9); } } } } og.setColor(Color.lightGray); og.fillRoundRect(300,170,90,30,15,15); og.fillRoundRect(300,210,90,30,15,15); og.setColor(Color.darkGray); og.drawRoundRect(300,170,90,30,15,15); og.drawRoundRect(300,210,90,30,15,15); og.setColor(Color.black); og.setFont(new Font("",Font.BOLD,16)); og.drawString("retry",325,190); og.drawString("shuffle",317,230); sp1 = 1; for(int t1 = 0;t1 < 7;t1 ++){ for(int t2 = 0;t2 < 7;t2 ++){ sp1 = sp1 * chd[t1][t2] ; } } if(sp1 == 1){ og.setFont(new Font("",Font.BOLD,36)); og.setColor(Color.red); og.drawString("clear!!",350,130); } g.drawImage(os,0,0,this); } public void d_dice(int dx,int dy){ if(cd[dy][dx] == -1){ og.setColor(Color.gray); og.drawRect(30*dx+30,30*dy+30,30,30); switch (vd[dy][dx]){ case 0 : break ; case 1 : og.setColor(Color.red) ; og.fillOval(30*dx+38,30*dy+38,13,13) ; break ; case 2 : og.setColor(Color.black) ; if(ddd[td[dy][dx]-1][0] == -1){ og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; }else{ og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; } break ; case 3 : og.setColor(Color.black) ; if(ddd[td[dy][dx]-1][0] == -1){ og.fillOval(30*dx+41,30*dy+41,8,8) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; }else{ og.fillOval(30*dx+41,30*dy+41,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; } break ; case 4 : og.setColor(Color.black) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; break ; case 5 : og.setColor(Color.black) ; og.fillOval(30*dx+41,30*dy+41,8,8) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; break ; case 6 : og.setColor(Color.black) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; if(ddd[td[dy][dx]-1][0] == -1){ og.fillOval(30*dx+32,30*dy+41,8,8) ; og.fillOval(30*dx+50,30*dy+41,8,8) ; }else{ og.fillOval(30*dx+41,30*dy+32,8,8) ; og.fillOval(30*dx+41,30*dy+50,8,8) ; } break ; } }else{ og.setColor(Color.black); og.fillRoundRect(30*dx+32,30*dy+32,27,27,9,9); og.setColor(Color.gray); og.drawRect(30*dx+30,30*dy+30,30,30); switch (vd[dy][dx]){ case 0 : break ; case 1 : og.setColor(Color.red) ; og.fillOval(30*dx+38,30*dy+38,13,13) ; break ; case 2 : og.setColor(Color.white) ; if(ddd[td[dy][dx]-1][0] == -1){ og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; }else{ og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; } break ; case 3 : og.setColor(Color.white) ; if(ddd[td[dy][dx]-1][0] == -1){ og.fillOval(30*dx+41,30*dy+41,8,8) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; }else{ og.fillOval(30*dx+41,30*dy+41,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; } break ; case 4 : og.setColor(Color.white) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; break ; case 5 : og.setColor(Color.white) ; og.fillOval(30*dx+41,30*dy+41,8,8) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; break ; case 6 : og.setColor(Color.white) ; og.fillOval(30*dx+32,30*dy+32,8,8) ; og.fillOval(30*dx+50,30*dy+50,8,8) ; og.fillOval(30*dx+32,30*dy+50,8,8) ; og.fillOval(30*dx+50,30*dy+32,8,8) ; if(ddd[td[dy][dx]-1][0] == -1){ og.fillOval(30*dx+32,30*dy+41,8,8) ; og.fillOval(30*dx+50,30*dy+41,8,8) ; }else{ og.fillOval(30*dx+41,30*dy+32,8,8) ; og.fillOval(30*dx+41,30*dy+50,8,8) ; } break ; } } } public void d_set(){ cf = 1 ; cco = 1 ; for(int ip = 0;ip < 28;ip ++){ if(Math.random() < 0.5){ ddd[ip][0] = 1; }else{ ddd[ip][0] = -1; } if(Math.random() < 0.5){ ddd[ip][1] = 1; }else{ ddd[ip][1] = -1; } } ch = 0; for(int ix = 0;ix < 7;ix ++){ for(int iy = ix;iy < 7;iy ++){ if(ddd[ch][1] == 1){ ddn[ch][0] = ix; ddn[ch][1] = iy; }else{ ddn[ch][0] = iy; ddn[ch][1] = ix; } ch ++; } } for(int ct = 0;ct < 100;ct ++){ sp1 = (int)(Math.random() * 28); sp2 = (int)(Math.random() * 28); sn1 = ddn[sp1][0] ; sn2 = ddn[sp1][1] ; ddn[sp1][0] = ddn[sp2][0] ; ddn[sp1][1] = ddn[sp2][1] ; ddn[sp2][0] = sn1 ; ddn[sp2][1] = sn2 ; } ifg = 0 ; sn1 = 1 ; sn2 = 1; while(ifg == 0){ dn = 0; d_put(); sn1 ++; if(sn1 > 100){ ifg = 1; sn2 = 0; } } for(int t1 = 0;t1 < 7;t1 ++){ for(int t2 = 0;t2 < 7;t2 ++){ chd[t1][t2] = 0; } } } public void d_put(){ for(int t1 = 0;t1 < 8;t1 ++){ for(int t2 = 0;t2 < 7;t2 ++){ td[t2][t1] = 0; vd[t2][t1] = -1; cd[t2][t1] = -1; } } ifg = 1; for(int ty = 0;ty < 7;ty ++){ for(int tx = 0;tx < 8;tx ++){ if(ifg == 1){ if(td[ty][tx] == 0){ if(ddd[dn][0] == -1){ if(ty == 6){ ddd[dn][0] = 1; if(td[ty][tx+1] == 0){ td[ty][tx] = dn + 1; td[ty][tx+1] = dn + 1; vd[ty][tx] = ddn[dn][0]; vd[ty][tx+1] = ddn[dn][1]; dn = (dn + 1) % 28; }else{ ddd[td[ty][tx+1]-1][0] = 1; ifg = 0; } }else{ td[ty][tx] = dn + 1; td[ty+1][tx] = dn + 1; vd[ty][tx] = ddn[dn][0]; vd[ty+1][tx] = ddn[dn][1]; dn = (dn + 1) % 28; } }else{ if(tx == 7){ ddd[dn][0] = -1; if(ty == 6){ ifg = 0; }else{ td[ty][tx] = dn + 1; td[ty+1][tx] = dn + 1; vd[ty][tx] = ddn[dn][0]; vd[ty+1][tx] = ddn[dn][1]; dn = (dn + 1) % 28; } }else{ if(td[ty][tx+1] == 0){ td[ty][tx] = dn + 1; td[ty][tx+1] = dn + 1; vd[ty][tx] = ddn[dn][0]; vd[ty][tx+1] = ddn[dn][1]; dn = (dn + 1) % 28; }else{ if(tx == 6){ ddd[dn][0] = -1; td[ty][tx] = dn + 1; td[ty+1][tx] = dn + 1; vd[ty][tx] = ddn[dn][0]; vd[ty][tx+1] = ddn[dn][1]; dn = (dn + 1) % 28; }else{ ddd[td[ty][tx+1]-1][0] = 1; ifg = 0; } } } } } } } } } public void start(){ if(th == null){ th = new Thread(this); th.start(); } } public void stop(){ th = null; } public void run(){ while(th != null){ try{ Thread.sleep(100); } catch (InterruptedException e){ } } } public void update( Graphics g ){ paint( g ) ; } }