package ui; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JOptionPane; import javax.swing.JPanel; public class ChessBorder extends JPanel implements MouseListener{ int[] result=new int[2]; int chessPoint[][]=new int[15][15]; // 1为黑子 2为红子 boolean player=true; //true:黑方 false:白方 白方:电脑 黑方:人 public ChessBorder() { addMouseListener(this); initBoard(); this.setSize(600,600); } public void initBoard() { for(int i=0;i<15;i++) for(int j=0;j<15;j++) chessPoint[i][j]=0; } //画棋盘 @Override protected void paintComponent(Graphics g) { for(int i=0;i<15;i++) { g.drawLine(40+i*40,40,40+i*40,600); } for(int i=0;i<15;i++) { g.drawLine(40,40+i*40,600,40+i*40); } for(int i=0;i<15;i++) for(int j=0;j<15;j++) { if(chessPoint[i][j]==1) { g.setColor(Color.BLACK); g.fillOval(40*i+20, 40*j+20, 40, 40); } if(chessPoint[i][j]==2) { g.setColor(Color.WHITE); g.fillOval(40*i+20,40*j+20,40,40); } } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub int x1=(e.getX()-40+20)/40; int y1=(e.getY()-40+20)/40; //System.out.println(e.getX()+":"+e.getY()); System.out.println(x1+","+y1); if(x1<0||x1>14||y1<0||y1>14) return; if(chessPoint[x1][y1]==0) { if(player) { chessPoint[x1][y1]=1; } player=!player; } repaint(); if(win(chessPoint,x1,y1)&&chessPoint[x1][y1]==1) { JOptionPane.showMessageDialog(this, "黑方赢"); player=!player; } } @Override public void mouseReleased(MouseEvent e) { JudgePoint judge=new JudgePoint(); int[]point=judge.getPoint(chessPoint); int x=point[0]; int y=point[1]; //System.out.println("x= "+point[0]+" y= "+point[1]); chessPoint[x][y]=2; player=!player; repaint(); if(win(chessPoint,x,y)&&chessPoint[x][y]==2) { JOptionPane.showMessageDialog(this, "白方赢"); player=!player; } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public boolean win(int a[][],int x,int y) { int count1=0; int count2=0; /* * 纵向判断 */ while(true) { //向上遍历 for(int j=y;j<a[x].length;j++) { if(a[x][y]==a[x][j]) { count1++; if((count1==5)||(count1+count2==5)) return true; } else break; } //向下遍历 for(int j=y;j>=0;j--) { if(a[x][y]==a[x][j]) { count2++; if((count2==5)||(count2+count1-1)==5) return true; } else break; } count1=0; count2=0; break; } /* * 横向判断 */ while(true) { //向左遍历 for(int i=x;i<a.length;i++) { if(a[x][y]==a[i][y]) { count1++; if((count1==5)||(count1+count2==5)) return true; } else break; } //向左遍历 for(int i=x;i>=0;i--) { if(a[x][y]==a[i][y]) { count2++; if((count2==5)||(count2+count1-1)==5) return true; } else break; } count1=0; count2=0; break; } /* * 右斜方向判断 */ while(true) { //斜上遍历 for(int i=x,j=y;i<a.length&&j>=0;i++,j--) { if(a[x][y]==a[i][j]) { count1++; if((count1==5)||(count1+count2==5)) return true; } else break; } //斜 for(int i=x,j=y;i>=0&&j<a[i].length;i--,j++) { if(a[x][y]==a[i][j]) { count2++; if((count2==5)||(count2+count1-1)==5) return true; } else break; } count1=0; count2=0; break; } while(true) { //斜上遍历 for(int i=x,j=y;i<a.length&&j<a[i].length;i++,j++) { if(a[x][y]==a[i][j]) { count1++; if((count1==5)||(count1+count2==5)) return true; } else break; } //斜 for(int i=x,j=y;i>=0&&j>=0;i--,j--) { if(a[x][y]==a[i][j]) { count2++; if((count2==5)||(count2+count1-1)==5) return true; } else break; } count1=0; count2=0; return false; } } public void restartGame() { for(int i=0;i<15;i++) for(int j=0;j<15;j++) { chessPoint[i][j]=0; } player=true; repaint(); } }
最近下载更多