Java编写的五子棋游戏

来源:互联网 发布:淘宝开业牌匾 编辑:程序博客网 时间:2024/05/01 17:03

代码部分:

package sailor;

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 javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FiveChessFrame extends JFrame implements MouseListener, Runnable
{

 private static final long serialVersionUID = 1L;
 //屏幕的宽度
 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
 //屏幕的高度
 int heigth = Toolkit.getDefaultToolkit().getScreenSize().height;
 //保存背景图片
 BufferedImage bgImage = null;
 //棋子的坐标
 int x = 0;
 int y = 0;
 //保存棋子的坐标,0表示没有棋子,1表示黑子,2表示白子
 int[][] allChess = new int[19][19];
 //标识当前状态是应该黑子还是白子落子,true表示黑子落子,false表示白子落子
 boolean isBlack = true;
 //标识当前状态游戏是否可以继续
 boolean canPlay = true;
 //提示信息
 String message = " 黑方先行";
 //最大时间
 int maxTime = 0;
 //做倒计时的线程类
 Thread t = new Thread(this);
 //保存两方剩余时间
 int blackTime = 0;
 int whiteTime = 0;
 //双方剩余时间信息
 String blackMessage = "无限制";
 String whiteMessage = "无限制";
 
 public FiveChessFrame()
 {
  //设置标题
  setTitle("Java小游戏之 五子棋");
  //设置窗口大小
  setSize(500,500);
  //设置窗口出现位置
  setLocation((width - 500)/2,(heigth - 500)/2);
  //将窗口的关闭方式设置为默认关闭后程序结束
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //为窗口设置监听器
  addMouseListener(this);
  //将窗口设置为可见
  setVisible(true);
  
  t.start();
  t.suspend();
  //背景图片路径
  String imagePath = "";
  //获得背景图片
  try {
   imagePath = System.getProperty("user.dir")+"/bin/image/background.jpg" ;
   bgImage = ImageIO.read(new File(imagePath.replaceAll("\\\\", "/")));
  } catch (IOException e) {
   e.printStackTrace();
  }
  repaint();
 }
 
 public void paint(Graphics g)
 {
  BufferedImage bi = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
  
  Graphics g2 = bi.createGraphics();
  //设置颜色为黑色
  g2.setColor(Color.black);
  //绘制背景
  g2.drawImage(bgImage,1,20,this);
  //设置标题字体
  g2.setFont(new Font("宋体",0,20));
  //输出标题信息
  g2.drawString("游戏信息:"+ message, 130, 60);
  //设置信息字体,输出时间信息
  g2.setFont(new Font("宋体",0,14));
  g2.drawString("黑方时间:"+blackMessage,30,470);
  g2.drawString("白方时间:"+whiteMessage, 260, 470);
  //绘制棋盘
  for(int i = 0;i<19;i++)
  {
   g2.drawLine(10, 70+20*i, 370, 70+20*i);
   g2.drawLine(10+20*i, 70, 10+20*i, 430);
  }
  //绘制全部黑白棋子
  for(int i=0;i<19;i++)
  {
   for(int j=0;j<19;j++)
   {
    if(allChess[i][j] == 1)
    {
     int X = i*20+10;
     int Y = j*20+70;
     g2.setColor(Color.black);
     g2.fillOval(X-7, Y-7, 14, 14);
    }
    if(allChess[i][j] == 2)
    {
     int X = i*20 + 10;
     int Y = j*20 + 70;
     g2.setColor(Color.white);
     g2.fillOval(X-7, Y-7, 14, 14);
          
    }
   }
  }
  
  g.drawImage(bi, 0, 0, this);
 }

 public void mouseClicked(MouseEvent arg0) {
  
 }

 public void mouseEntered(MouseEvent arg0) {
  
 }

 public void mouseExited(MouseEvent arg0) {
  
 }

 public void mousePressed(MouseEvent arg0)
 {
  x = arg0.getX();
  y = arg0.getY();
  
  if(x>=10&&x<=370&&y>=70&&y<=430)
  {
   if(canPlay == true)
   {
    x = x / 20;
    y = (y-60) / 20;
    if(allChess[x][y] == 0)
    {
     if(isBlack == true)
     {
      allChess[x][y] = 1;
      isBlack = false;
      message = "轮到白方";
     }
     else
     {
      allChess[x][y] = 2;
      isBlack = true;
      message = "轮到黑方";
     }
     //判断是否形成五连
     boolean winFlag = this.checkWin();
     
     if(winFlag == true)
     {
      JOptionPane.showMessageDialog(this, "游戏结束,"+(allChess[x][y] == 1?"黑方":"白方")+"获胜");
      canPlay = false;
     }
    }
    else
    {
     JOptionPane.showMessageDialog(this, "当前位置已有棋子,请重新落子!");
    }
   }
   repaint();
  }
  
  if(x>=400&&x<=470&&y>=70&&y<=100)
  {
   int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
   if(result == 0)
   {
    for(int i=0;i<19;i++)
    {
     for(int j=0;j<19;j++)
     {
      allChess[i][j]=0;
     }
    }
     
    message = "黑方先行";
     
    isBlack = true;
    blackTime = maxTime;
    whiteTime = maxTime;

    blackMessage = "无限制";
    whiteMessage = "无限制";

    canPlay = true;
    repaint();
   }
  }
  if (x >= 400 && x <= 470 && y >= 120&& y <= 150)
  {
   String input = JOptionPane.showInputDialog("请输入游戏的最大时间(单位:秒),如果输入0,表示没有时间限制:");
   try {
    maxTime = Integer.parseInt(input);
    if (maxTime < 0)
    {
     JOptionPane.showMessageDialog(this, "请输入正确信息,不允许输入负数!");
    }
    if (maxTime == 0)
    {
     int result = JOptionPane.showConfirmDialog(this,"设置完成,是否重新开始游戏?");
     if (result == 0)
     {
      for (int i = 0; i < 19; i++)
      {
       for (int j = 0; j < 19; j++)
       {
        allChess[i][j] = 0;
       }
      }
      message = "黑方先行";
      isBlack = true;
      blackTime = 0;
      whiteTime = 0;
      blackMessage = "无限制";
      whiteMessage = "无限制";
      canPlay = true;
      repaint();
     }
    }
    if (maxTime > 0)
    {
     int result = JOptionPane.showConfirmDialog(this,"设置完成,是否重新开始游戏?");
     if (result == 0)
     {
      for (int i = 0; i < 19; i++)
      {
       for (int j = 0; j < 19; j++)
       {
        allChess[i][j] = 0;
       }
      }
      message = "黑方先行";
      isBlack = true;
      blackTime = maxTime;
      whiteTime = maxTime;
      blackMessage = maxTime / 3600 + ":"
         + (maxTime / 60 - maxTime / 3600 * 60) + ":"
         + (maxTime - maxTime / 60 * 60);
      whiteMessage = maxTime / 3600 + ":"
         + (maxTime / 60 - maxTime / 3600 * 60) + ":"
         + (maxTime - maxTime / 60 * 60);
      t.resume();
      canPlay = true;
      repaint();
     }
    }
   } catch (NumberFormatException e1)
   {
    JOptionPane.showMessageDialog(this, "请正确输入信息!");
   }
  }
  if (x >= 400 && x <= 470 && y >= 170&& y <= 200)
  {
   JOptionPane.showMessageDialog(this,"这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
  }
  if (x >= 400 && x <= 470 && y >= 270&& y <= 300)
  {
   int result = JOptionPane.showConfirmDialog(this, "是否确认认输?");
   if (result == 0)
   {
    if (isBlack)
    {
     JOptionPane.showMessageDialog(this, "黑方已经认输,游戏结束!");
    }
    else
    {
     JOptionPane.showMessageDialog(this, "白方已经认输,游戏结束!");
    }
    canPlay = false;
   }
  }
  
  if (x >= 400 && x <= 470 && y >= 320&& y <= 350)
  {
   JOptionPane.showMessageDialog(this,"感谢李霞老师、周丹丹学姐、成松松学长对本程序的帮助");
  }
  if (x >= 400 && x <= 470 && y >= 370&& y <= 400)
  {
   JOptionPane.showMessageDialog(this, "游戏结束");
   System.exit(0);
  }
 }

 boolean checkWin()
 {
  boolean flag = false;
  int count = 1;
  int color = allChess[x][y];
  //判断横向
  count = this.checkCount(1,0,color);
  if(count>=5)
  {
   flag = true;
  }
  else
  {
   //判断纵向
   count = this.checkCount(0,1,color);
   if(count >= 5)
   {
    flag = true;
   }
   else
   {
    //判断右上、左下方向
    count = this.checkCount(1,-1,color);
    if(count >= 5)
    {
     flag = true;
    }
    else
    {
     //判断右下、左上方向
     count = this.checkCount(1,1,color);
     if(count >= 5)
     {
      flag = true;
     }
    }
   }
  }
  return flag;
 }

 int checkCount(int xChange, int yChange, int color)
 {
  int count = 1;
  int X = xChange;
  int Y = yChange;
  
  while(x+xChange>=0&&x+xChange<19&&y+yChange>=0&&y+yChange<19&&color==allChess[x+xChange][y+yChange])
  {
   count++;
   if(xChange!=0)
    xChange++;
   if(yChange!=0)
   {
    if(yChange>0)
     yChange++;
    else
     yChange--;
   }
  }
  xChange = X;
  yChange = Y;
  while(x-xChange>=0&&x-xChange<19&&y-yChange>=0&&y-yChange<19 && color == allChess[x - xChange][y - yChange])
  {
   count++;
   if (xChange != 0)
    xChange++;
   if (yChange != 0)
   {
    if (yChange > 0)
     yChange++;
    else
     yChange--;
   }
  }
  return count;
 }

 public void mouseReleased(MouseEvent arg0) {
  
 }

 public void run()
 {
  if (maxTime > 0)
  {
   while (true)
   {
    if (isBlack)
    {
     blackTime--;
     if (blackTime == 0)
     {
      JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!");
      canPlay = false;
     }
    }
    else
    {
     whiteTime--;
     if (whiteTime == 0)
     {
      JOptionPane.showMessageDialog(this, "白方超时,游戏结束!");
      canPlay = false;
     }
    }
    blackMessage = blackTime / 3600 + ":"
      + (blackTime / 60 - blackTime / 3600 * 60) + ":"
      + (blackTime - blackTime / 60 * 60);
    whiteMessage = whiteTime / 3600 + ":"
      + (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
      + (whiteTime - whiteTime / 60 * 60);
    if(whiteTime>=0 && blackTime>=0)
     repaint();
    try
    {
     Thread.sleep(1000);
    } catch (InterruptedException e)
    {
     e.printStackTrace();
    }
   } 
  }
 }
}

 

原创粉丝点击