Java五子棋游戏源代码

jopen 7年前

FiveChessFrame.java

package myGameFrame;    import java.awt.Color;  import java.awt.Font;  import java.awt.Graphics;  import java.awt.Toolkit;  import java.awt.event.MouseEvent;  import java.awt.event.MouseListener;  import java.awt.image.BufferedImage;  import java.io.File;  import java.io.IOException;  import java.util.Collections;    import javax.imageio.ImageIO;  import javax.swing.JFrame;  import javax.swing.JOptionPane;    public class FiveChessFrame extends JFrame implements MouseListener {      int width=Toolkit.getDefaultToolkit().getScreenSize().width;      int height=Toolkit.getDefaultToolkit().getScreenSize().height;      BufferedImage bgImage=null;      //保存棋子的坐标      int x=0;int y=0;      //保存之前下过的全部棋子的坐标      //其中的数据内容:0,没有棋子 1,黑子  3,白子      int[][] allChess=new int[19][19];      //标识当前是黑棋还是白棋      boolean isBlack=true;      //表示当前游戏是否可以继续      boolean canPlay=true;   public FiveChessFrame()   {    setTitle("五子棋");    setSize(500, 500);    setLocation((width-500)/2,(height-500)/2);    setResizable(false);    addMouseListener(this);    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    try {     bgImage=ImageIO.read(new File("D:/bg.jpg"));    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }    setVisible(true);   }   public void paint(Graphics g)   {    g.drawImage(this.bgImage, 0,20,this);    g.setFont(new Font("黑体",Font.BOLD,20));    g.drawString("游戏信息", 120, 60);    g.setFont(new Font("送体",Font.BOLD,12));    g.drawString("黑方时间", 60, 480);    g.drawString("白方时间", 260, 480);    //横360,竖360,左上12,73,右上372,73,左下12,433,右下372,433    int y_index=0;    for (int i = 0; i < 19; i++) {     g.drawLine(12, 73+y_index, 372, 73+y_index);     y_index+=20;    }    int x_index=0;    for (int i = 0; i < 19; i++) {     g.drawLine(12+x_index, 73, 12+x_index, 433);     x_index+=20;    }    g.fillOval(70, 131, 4, 4);    g.fillOval(310, 131, 4, 4);    g.fillOval(70, 371, 4, 4);    g.fillOval(310, 371, 4, 4);    //绘制全部棋子    for (int i = 0; i<19; i++) {     for (int j = 0; j < 19; j++) {      if(this.allChess[i][j]==1)      {       //画黑子       int tempX=i*20+12;       int tempY=j*20+73;       g.fillOval(tempX-7, tempY-7, 14, 14);      }      if(this.allChess[i][j]==2)      {       //画白子       int tempX=i*20+12;       int tempY=j*20+73;       g.setColor(Color.WHITE);       g.fillOval(tempX-7, tempY-7, 14, 14);       g.setColor(Color.BLACK);       g.drawOval(tempX-7, tempY-7, 14, 14);      }     }    }   }   public static void main(String[] args) {        }   @Override   public void mouseClicked(MouseEvent e) {    // TODO Auto-generated method stub       }     @Override   public void mouseEntered(MouseEvent e) {    // TODO Auto-generated method stub       }     @Override   public void mouseExited(MouseEvent e) {    // TODO Auto-generated method stub       }     @Override   public void mousePressed(MouseEvent e) {    if(canPlay==false)    {     return;    }    System.out.println(e.getX()+"---"+e.getY());     x=e.getX();     y=e.getY();    if(x>=12&&x<=372&&y>=73&&y<433)    {     x=(x-12)/20;     y=(y-73)/20;     if(allChess[x][y]==0)     {      if(this.isBlack==true)      {       allChess[x][y]=1;       isBlack=false;      }      else      {       allChess[x][y]=2;       isBlack=true;      }      //判断这个棋子是否和其他棋子连成5个      boolean winFlag=this.Check_Win();      if(winFlag)      {       JOptionPane.showMessageDialog(this, "游戏结束,"+(allChess[x][y]==1?"黑方":"白方")+"获胜");       canPlay=false;      }     }     else     {      JOptionPane.showMessageDialog(this, "当前已有棋子,不能再下了!");     }          this.repaint();    }   }      private boolean Check_Win()      {       boolean flag=false;       //保存共有多少相同颜色的棋子相连       int count=1;       //先判断横向,特点,y坐标相同,即allChess[x][y]中y相同       //判断这个棋子的颜色       int color=this.allChess[x][y];  //     if(color==allChess[x+1][y])  //     {  //      count++;  //      if(color==allChess[x+2][y])  //      {  //       count++;  //      }  //     }       //用循环来做棋子相连的判断!  //     int i=1;//横向向右判断  //     while(color==this.allChess[x+i][y])  //     {  //      count++;  //      i++;  //     }  //     //横向向左判断  //     i=1;  //     while(color==this.allChess[x-i][y])  //     {  //      count++;  //      i++;  //     }  //     if(count>=5)  //     {  //      flag=true;  //     }       //综向判断  //     int i2=1;  //     int count2=1;  //     while(color==this.allChess[x][y+i2])  //     {  //      count2++;  //      i2++;  //     }  //     i2=1;  //     while(color==this.allChess[x][y-i2])  //     {  //      count2++;  //      i2++;  //     }  //     if(count2>=5)  //     {  //      flag=true;  //     }  //     //斜方向判断,y=x方向  //     int i3=1;  //     int count3=1;  //     while(color==this.allChess[x+i3][y-i3])  //     {  //      count3++;  //      i3++;  //     }  //     i3=1;  //     while(color==this.allChess[x-i3][y+i3])  //     {  //      count3++;  //      i3++;  //     }  //     if(count3>=5)  //     {  //      flag=true;  //     }  //     //斜方向,y=-x  //     int i4=1;  //     int count4=1;  //     while(color==this.allChess[x-i4][y-i4])  //     {  //      count4++;  //      i4++;  //     }  //     i4=1;  //     while(color==this.allChess[x+i4][y+i4])  //     {  //      count4++;  //      i4++;  //     }  //     if(count4>=5)  //     {  //      flag=true;  //     }       //判断横向          count=Check_Count(1, 0, color);          if(count>=5)          {           flag=true;          }          else          {           //判断纵向           count=Check_Count(0, 1, color);           if(count>=5)           {            flag=true;           }           else           {            //判断右上,左下            count=Check_Count(1, -1, color);            if(count>=5)            {             flag=true;            }            else            {             //判断左上,右下             count=Check_Count(1, 1, color);             if(count>=5)             {              flag=true;             }            }           }          }       return flag;      }      //判断棋子连接的数量      private int Check_Count(int xChange,int yChange,int color)      {       int count=1;       int tempX=xChange;       int tempY=yChange;       while(color==this.allChess[x+xChange][y+yChange])           {            count++;            if(xChange!=0)            {             xChange++;            }            if(yChange!=0)            {             if(yChange>0)             yChange++;             if(yChange<0)             yChange--;            }           }       xChange=tempX;yChange=tempY;       while(color==this.allChess[x-xChange][y-yChange])       {        count++;        if(xChange!=0)        {         xChange++;        }        if(yChange!=0)        {         if(yChange>0)            yChange++;            if(yChange<0)            yChange--;        }       }       return count;      }   @Override   public void mouseReleased(MouseEvent e) {    // TODO Auto-generated method stub       }    }

myChessFrame.java

package myGameFrame;    import java.awt.Color;  import java.awt.Graphics;  import java.awt.Toolkit;  import java.awt.event.MouseEvent;  import java.awt.event.MouseListener;  import java.awt.image.BufferedImage;  import java.io.File;  import java.io.IOException;    import javax.imageio.ImageIO;  import javax.swing.JFrame;  import javax.swing.JOptionPane;    public class myChessFrame extends JFrame implements MouseListener {       public myChessFrame()       {             this.setTitle("五子棋");        this.setSize(500,400);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     this.addMouseListener(this);     this.setVisible(true);       }     @Override   public void mouseClicked(MouseEvent e) {    repaint();   }     @Override   public void mouseEntered(MouseEvent e) {    // TODO Auto-generated method stub       }     @Override   public void mouseExited(MouseEvent e) {    // TODO Auto-generated method stub       }   public void paint(Graphics g)   {    g.setColor(new Color(244,100,20));    g.setColor(Color.red);    g.drawString("asdjkadslkd",10,10);    g.drawOval(10, 10, 300, 100);    BufferedImage image=null;    try {     image = ImageIO.read(new File("D:/1.bmp"));    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }    g.drawImage(image,100,100,this);   }   @Override   public void mousePressed(MouseEvent e) {    // TODO Auto-generated method stub       }     @Override   public void mouseReleased(MouseEvent e) {    // TODO Auto-generated method stub       }  }

Test.java

package myGameTest;  import myGameFrame.*;    import java.awt.Toolkit;  import javax.swing.JFrame;  import javax.swing.JOptionPane;    public class Test {     /**    * @param args    */   public static void main(String[] args) {    new FiveChessFrame();      //  int x=130;  //  System.out.println((x-10)/20);   }    }