一共两个部分,分别是棋盘的控制现实和基本的接口
1 import javax.swing.*; 2 3 int each=35; 4 int side=35; 5 int len=14*each; 6 7 void setup(){ 8 size(800,600); 9 initChess(); 10 chessTableMake(); 11 } 12 void draw(){ 13 14 } 15 void mousePressed(){ 16 if(finished) return; 17 setChess(mouseX,mouseY); 18 } 19 void keyPressed(){ 20 if(key==‘r‘||key==‘R‘){ 21 setup(); 22 loop(); 23 } 24 }
init
1 int chess[][]=new int[15][15]; 2 int flag=0; 3 boolean finished=false; 4 //flag=1 black chess flag=-1 white chess 5 void initChess(){ 6 finished=false; 7 for(int i=0;i<15;i++) 8 for(int j=0;j<15;j++) 9 chess[i][j]=0; 10 flag=1; 11 } 12 void chessTableMake(){ 13 background(#5FFF4B); 14 //---------zi mu--------- 15 pushStyle(); 16 fill(#AF49F0); 17 textSize(20); 18 text(((flag==-1)?"White":"Black")+" Player Round",550,75); 19 popStyle(); 20 pushMatrix(); 21 translate(side,side); 22 fill(#AF49F0); 23 for(int i=0;i<15;i++){ 24 line(0,each*i,len,each*i); 25 line(each*i,0,each*i,len); 26 text(i+1,-25,each*i); 27 text(char(i+‘A‘),each*i,-15); 28 } 29 strokeWeight(5); 30 point(each*3,each*3); 31 point(each*3,each*11); 32 point(each*11,each*3); 33 point(each*11,each*11); 34 point(each*7,each*7); 35 strokeWeight(1); 36 for(int y=0;y<15;y++) 37 for(int x=0;x<15;x++) 38 switch(chess[x][y]){ 39 case -1:fill(255);ellipse(each*x,each*y,20,20);break; 40 case 1:fill(0);ellipse(each*x,each*y,20,20);break; 41 } 42 popMatrix(); 43 } 44 void setChess(int x,int y){ 45 if(x<side-5||x>side+len+5||y<side-5||y>side+len+5) return; 46 int tx=(x-side+5); 47 int ty=(y-side+5); 48 if(tx%each>=15||ty%each>=15)return; 49 tx/=each;ty/=each; 50 if(0!=chess[tx][ty])return; 51 chess[tx][ty]=flag; 52 flag*=-1; 53 chessTableMake(); 54 testVictory(tx,ty); 55 } 56 void testVictory(int x,int y){ 57 int tx=x,ty=y; 58 int count=0; 59 //na 60 tx=x;ty=y; 61 count=0; 62 for(int i=0;(tx+i>=0)&&(tx+i<=14)&&(ty+i>=0)&&(ty+i<=14)&&chess[tx+i][ty+i]==chess[x][y];i++,count++); 63 for(int i=0;(tx-i>=0)&&(tx-i<=14)&&(ty-i>=0)&&(ty-i<=14)&&chess[tx-i][ty-i]==chess[x][y];i++,count++); 64 if(count>5){victory(x,y);return;} 65 //pie 66 tx=x;ty=y; 67 count=0; 68 for(int i=0;(tx-i>=0)&&(tx-i<=14)&&(ty+i>=0)&&(ty+i<=14)&&chess[tx-i][ty+i]==chess[x][y];i++,count++); 69 for(int i=0;(tx+i>=0)&&(tx+i<=14)&&(ty-i>=0)&&(ty-i<=14)&&chess[tx+i][ty-i]==chess[x][y];i++,count++); 70 if(count>5){victory(x,y);return;} 71 //heng 72 tx=x;ty=y; 73 count=0; 74 for(int i=0;(tx-i>=0)&&(tx-i<=14)&&chess[tx-i][ty]==chess[x][y];i++,count++); 75 for(int i=0;(tx+i>=0)&&(tx+i<=14)&&chess[tx+i][ty]==chess[x][y];i++,count++); 76 if(count>5){victory(x,y);return;} 77 //shu 78 tx=x;ty=y; 79 count=0; 80 for(int i=0;(ty+i>=0)&&(ty+i<=14)&&chess[tx][ty+i]==chess[x][y];i++,count++); 81 for(int i=0;(ty-i>=0)&&(ty-i<=14)&&chess[tx][ty-i]==chess[x][y];i++,count++); 82 if(count>5){victory(x,y);return;} 83 } 84 void victory(int x,int y){ 85 pushStyle(); 86 fill(#5FFF4B); 87 noStroke(); 88 rect(550,50,250,100); 89 String player=(chess[x][y]==-1)?"White":"Black"; 90 JOptionPane.showMessageDialog(null, player+" player wins!\nPress R to restart", "Congratulation", JOptionPane.INFORMATION_MESSAGE); 91 finished=true; 92 fill(#AF49F0); 93 textSize(20); 94 text(player+" Player Wins",550,75); 95 popStyle(); 96 noLoop(); 97 }
时间: 2024-09-29 00:09:50