java图形界面五子棋源代码共享

来源:互联网 发布:js弹出警告窗口 编辑:程序博客网 时间:2024/05/29 09:50


有自己的加入也有书本的代码,整合,需要的素材网上找的。

是人机对战,但是没有AI,所以这个五子棋游戏的玩法就有改变了,最短

时间内让电脑也就是白棋获胜就赢了。奋斗(供学习 纪念之用)

import javax.swing.*;
import javax.imageio.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;


public class Gobang2 
{
//下面三个位图分别代表棋盘,黑棋,白棋
BufferedImage table;
BufferedImage black;
BufferedImage white;
//当鼠标移动时的选择框
BufferedImage selected;
//定义棋盘的大小
private static int BOARD_SIZE=15;
//定义棋盘宽,高多少个像素
private final int TABLE_WIDTH=500;
private final int TABLE_HEIGHT=501;
//定义棋盘坐标的像素值和棋盘数组之间的比率
private final int RATE=TABLE_WIDTH/BOARD_SIZE;
//定义棋盘坐标的像素值和棋盘数组之间的偏移距离
private final int X_OFFSET=12;
private final int Y_OFFSET=9;
//定义一个二维数组来当棋盘
private int[][] board=new int[BOARD_SIZE][BOARD_SIZE];
//五子棋游戏窗口
JFrame f=new JFrame("星星*五子棋游戏        玩法:在最短时间内让白棋赢");
//五子棋游戏棋盘对应的Canvas组件
ChessBoard chessBoard=new ChessBoard();
//当前选中点的坐标
private int selectedX=-1;
private int selectedY=-1;
    private Font fontGameOver = new Font("宋体", Font.BOLD, 50);
public void init()throws Exception
{
       ThreadLocalRandom rand= ThreadLocalRandom.current();
       table=ImageIO.read(new File("board.jpg"));
  black=ImageIO.read(new File("black.gif"));
  white=ImageIO.read(new File("white.gif"));
  selected=ImageIO.read(new File("select.gif"));
  //把每个元素赋值为0代表没有棋子
  for(int i=0;i<BOARD_SIZE;i++)
{
              for(int j=0;j<BOARD_SIZE;j++)
 {
 board[i][j]=0;
 }
  }
  chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));//设置组件的首选大小
  chessBoard.addMouseListener(new MouseAdapter()
{
  public void mouseClicked(MouseEvent e)
{
            //将用户鼠标事件的坐标转换成棋子数组的坐标
    int xPos=(int)((e.getX()-X_OFFSET)/RATE);
    int yPos=(int)((e.getY()-Y_OFFSET)/RATE);
board[xPos][yPos]=1;//黑棋
judge(1,xPos,yPos);
           int dxpos=rand.nextInt(0,14);
int dypos=rand.nextInt(0,14);
if(board[dxpos][dypos]!=1)
{
                board[dxpos][dypos]=2;//白棋
}
else
{
                dxpos=rand.nextInt(0,14);
dypos=rand.nextInt(0,14);
board[dxpos][dypos]=2;//白棋
}
    judge(2,dxpos,dypos);
chessBoard.repaint();
  }
  //当鼠标退出棋盘区后,复位选中点坐标 
  public void mouseExited(MouseEvent e)
{
  selectedX=-1;
  selectedY=-1;
  chessBoard.repaint();
  }
  });
  chessBoard.addMouseMotionListener(new MouseMotionAdapter()
{
  //鼠标移动时,改变选中点的坐标
  public void mouseMoved(MouseEvent e)
{
                selectedX=(e.getX()-X_OFFSET)/RATE;
   selectedY=(e.getY()-Y_OFFSET)/RATE;
chessBoard.repaint();
  }   
  });
       f.setResizable(false);//用户不能调整窗口大小
  f.add(chessBoard);
  f.pack();
  f.setVisible(true);
}
   


 public void judge(int a1,int x,int y )//判断
  {
        
  Graphics g=chessBoard.getGraphics();
  g.setColor(Color.RED);
 int count1=0,count2=0,count3=0,count4=0; 
 int a=x,b=y;
    
if(a1==1)
 {
 
 //横向
     while(x<=13&&board[x][y]==board[++x][y])
 {
              count1++;
 }
  x=a;
  y=b;
 while(x>=1&&board[x][y]==board[--x][y])
 {
 count1++;
 }
 if(count1==4)
 {
            g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------黑棋赢了!!!-----------");
  System.exit(0);
 
 }
 else
 {
              x=a;
 y=b;
 }
 //纵向
 while(y<=13&&board[x][y]==board[x][++y])
 {
 count2++;
 }
  x=a;
  y=b;
 while(y>=1&&board[x][y]==board[x][--y])
 {
 count2++;
 }
 if(count2==4)
 {
   g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------黑棋赢了!!!-----------");
   System.exit(0);
  
 }
 else
 {
              x=a;
 y=b;
 }
 //斜上
 while(y>=1&&x<=13&&board[x][y]==board[++x][--y])
 {
 count3++;
 }
  x=a;
  y=b;
 while(y<=13&&x>=1&&board[x][y]==board[--x][++y])
 {
 count3++;
 }
 if(count3==4)
 {
   g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------黑棋赢了!!!-----------");
   System.exit(0);
 
 }
 else
 {
 x=a;
     y=b;
 }
 //斜下
 while(y>=1&&x>=1&&board[x][y]==board[--x][--y])
 {
 count4++;
 }
 x=a;
 y=b;
          while(y<=13&&x<=13&&board[x][y]==board[++x][++y])
 {
 count4++;
 }
 if(count4==4)
 {
   g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------黑棋赢了!!!-----------");
   System.exit(0);
 
 }
 else
 {
               x=a;
      y=b;
 }
 }
        
else
 {
        //横向
     while(x<=13&&board[x][y]==board[++x][y])
 {
              count1++;
 }
  x=a;
  y=b;
 while(x>=1&&board[x][y]==board[--x][y])
 {
 count1++;
 }
 if(count1==4)
 {
   g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------白棋赢了!!!-----------");
    System.exit(0);
 
 }
 else
 {
              x=a;
 y=b;
 }
 //纵向
 while(y<=13&&board[x][y]==board[x][++y])
 {
 count2++;
 }
  x=a;
  y=b;
 while(y>=1&&board[x][y]==board[x][--y])
 {
 count2++;
 }
 if(count2==4)
 {
    g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------白棋赢了!!!-----------");
   System.exit(0);

 }
 else
 {
              x=a;
 y=b;
 }
 //斜上
 while(y>=1&&x<=13&&board[x][y]==board[++x][--y])
 {
 count3++;
 }
  x=a;
  y=b;
 while(y<=13&&x>=1&&board[x][y]==board[--x][++y])
 {
 count3++;
 }
 if(count3==4)
 {
   g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------白棋赢了!!!-----------");
   System.exit(0);
 
 }
 else
 {
 x=a;
     y=b;
 }
 //斜下
 while(y>=1&&x>=1&&board[x][y]==board[--x][--y])
 {
 count4++;
 }
 x=a;
 y=b;
          while(y<=13&&x<=13&&board[x][y]==board[++x][++y])
 {
 count4++;
 }
 if(count4==4)
 {
    g.setFont(fontGameOver);
g.drawString("游戏结束", 120, 180);
 System.out.println("---------白棋赢了!!!-----------");
  System.exit(0);
   
 }
 else
 {
               x=a;
      y=b;
 }


}
  }


public static void main(String[] args)throws Exception
{
Gobang2 gb2=new Gobang2();
gb2.init();
       
}
class ChessBoard extends JPanel
{
//重写JPanel的paint方法,实现绘画
public void paint(Graphics g)
{
//绘制五子棋棋盘
g.drawImage(table,0,0,null);
//绘制选中点的红框
if(selectedX>=0&&selectedY>=0)
{
               g.drawImage(selected,selectedX*RATE+X_OFFSET,
  selectedY*RATE+Y_OFFSET,null);
}
//遍历数组,绘制棋子
for(int i=0;i<BOARD_SIZE;i++)
{
for(int j=0;j<BOARD_SIZE;j++)
{
//绘制黑棋
if(board[i][j]==1)
{
g.drawImage(black,i*RATE+X_OFFSET,
j*RATE+Y_OFFSET,null);
}
//绘制白棋
if(board[i][j]==2)
{
                       g.drawImage(white,i*RATE+X_OFFSET,
j*RATE+Y_OFFSET,null);
}
}


}
}
}

}


0 0