java GUI实现五子棋游戏

 更新时间:2020年02月25日 17:14:52   作者:桥苯环萘我老婆  
这篇文章主要为大家详细介绍了java GUI实现五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现五子棋游戏GUI,供大家参考,具体内容如下

引用包

//{Cynthia Zhang}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Image;
import com.sun.image.codec.jpeg.*;

前期预设

//extends JApplet {

 // Indicate which player has a turn, initially it is the X player
 private char whoseTurn = 'X';
 final int SIZE = 15;
 static boolean ISOVER = false;

 // Create and initialize cells
 private final Cell[][] cell = new Cell[SIZE][SIZE];

 // Create and initialize a status label
 private final JLabel jlblStatus = new JLabel("X's turn to play",JLabel.CENTER);

设置背景板

// Initialize UI
 @Override
 public void init() {
 // Panel p to hold cells
 JPanel p = new JPanel();
 p.setLayout(new GridLayout(SIZE, SIZE, 0, 0));
 for (int i = 0; i < SIZE; i++) {
  for (int j = 0; j < SIZE; j++) {
  Cell ce = new Cell();
  ce.setBackground(new Color(150,88,42)); // 背景色绝美!
  p.add(cell[i][j] = ce);
  }
 }
 // Set line borders on the cells panel and the status label
 p.setBackground(new Color(143,105,94)); // 背景色绝美!
 jlblStatus.setBorder(new LineBorder(new Color(255,255,255), 2)); // 白框框加宽!
 
 // Place the panel and the label to the applet
 this.getContentPane().add(p, BorderLayout.CENTER);
 this.getContentPane().add(jlblStatus, BorderLayout.SOUTH);
 }

主要框架段落

// This main method enables the applet to run as an application
public static void main(String[] args) {
 // Create a frame
 JFrame frame = new JFrame("Tic Tac Toe");

 // Create an instance of the applet
 Homework8 applet = new Homework8();

 // Add the applet instance to the frame
 frame.getContentPane().add(applet, BorderLayout.CENTER);

 // Invoke init() and start()
 applet.init();
 applet.start();

 // Display the frame
 frame.setSize(600, 600);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setVisible(true);
 }

判断是否满了

// Determine if the cells are all occupied
 public boolean isFull() {
 for (int i = 0; i < SIZE; i++) {
  for (int j = 0; j < SIZE; j++) {
  if (cell[i][j].getToken() == ' ') {
   return false;
  }
  }
 }
 return true;
 }

判断是否赢了

和八皇后有点像,可以考虑那种数组优化四个方向,这里比较懒

// Determine if the player with the specified token wins
 public boolean isWon(char token) {
 int winNum = 5; // define the max num for a rule
 for (int indexX = 0; indexX < SIZE; indexX++) {
  for (int indexY = 0; indexY < SIZE; indexY++){
  // in row check cell[indexX][indexY...indexY+winNum-1] 
  if (indexY+winNum-1<SIZE){
   boolean flag=true;
   for (int y =indexY;y<indexY+winNum;y++)
   if (cell[indexX][y].getToken() != token){
    flag=false; break;
   }
   if (flag==true) return true;
  }
  
  // in row check cell[indexX...indexX+winNum-1][indexY] 
  if (indexX+winNum-1<SIZE){
   boolean flag=true;
   for (int x =indexX;x<indexX+winNum;x++)
   if (cell[x][indexY].getToken() != token){
    flag=false; break;
   }
   if (flag==true) return true;
  }
  
  // check cell[indexX...indexX+winNum-1][indexY...indexY+winNum-1)
  if (indexX+winNum-1<SIZE && indexY+winNum-1<SIZE){
   boolean flag=true;
   for (int x =indexX,y=indexY;x<indexX+winNum;x++,y++)
   if (cell[x][y].getToken() != token){
    flag=false; break;
   }
   if (flag==true) return true;
  }
  
  // check cell[indexX...indexX+winNum-1][indexY...indexY+winNum-1)
  if (indexX+winNum-1<SIZE && indexY-winNum+1>=0){
   boolean flag=true;
   for (int x =indexX,y=indexY;x<indexX+winNum;x++,y--)
   if (cell[x][y].getToken() != token){
    flag=false; break;
   }
   if (flag==true) return true;
  }
  }
 }
 return false;
 }

设置棋子

// An inner class for a cell
 public class Cell extends JPanel implements MouseListener {

 // Token used for this cell
 private char token = ' ';

 public Cell() {
  setBorder(new LineBorder(Color.black, 1)); // Set cell's border
  addMouseListener(this); // Register listener
 }

 // The getter method for token
 public char getToken() {
  return token;
 }

 // The setter method for token
 public void setToken(char c) {
  token = c;
  repaint();
 }

导入图片

// Paint the cell
 @Override
 public void paintComponent(Graphics g) {
  if (token == 'X') {
  ImageIcon icon = new ImageIcon("C:\\Users\\Lenovo\\Desktop\\Black.png");
  Image image = icon.getImage();
  g.drawImage(image,0,0,35,35,this); 
  }else if (token=='O'){
  ImageIcon icon = new ImageIcon("C:\\Users\\Lenovo\\Desktop\\White.png");
  Image image = icon.getImage();
  g.drawImage(image,0,0,35,35,this);  
  }
  super.paintComponents(g);
 }

游戏结束的锁定与弹窗

// Handle mouse click on a cell
 @Override
 public void mouseClicked(MouseEvent e) {
  if (ISOVER) return; // if game is over, any issue should be forbidden
  int response=-1;
  if (token == ' ') // If cell is not occupied
  {
  if (whoseTurn == 'X') // If it is the X player's turn
  {
   setToken('X'); // Set token in the cell
   whoseTurn = 'O'; // Change the turn
   jlblStatus.setText("The White's Turn"); // Display status
   if (isWon('X')) {
   jlblStatus.setText("The Black Won! The Game Is Over!");
   response = JOptionPane.showConfirmDialog(null, "The Black Won! The Game Is Over!\n"
    +"Do you want to quit?","提示",JOptionPane.YES_NO_OPTION);
   
   ISOVER = true;
   if (response == 0) System.exit(0); // choose "Yes" than quit;
   } 
  } else if (whoseTurn == 'O') // If it is the O player's turn
  {
   setToken('O'); // Set token in the cell
   whoseTurn = 'X'; // Change the turn
   jlblStatus.setText("The Black's Turn"); // Display status
   if (isWon('O')) {
   jlblStatus.setText("The White Won! The Game Is Over!");
   response = JOptionPane.showConfirmDialog(null, "The White Won! The Game Is Over!\n"
    +"Do you want to quit?","提示",JOptionPane.YES_NO_OPTION);
   ISOVER = true;
   
   if (response == 0) System.exit(0); // choose "Yes" than quit;
   } 
  }
  if (isFull()) {
   jlblStatus.setText("Plain Game! The Game Is Over!");
   response = JOptionPane.showConfirmDialog(null, "Plain Game! The Game Is Over!\n"
    +"Do you want to quit?","提示",JOptionPane.YES_NO_OPTION);
   ISOVER = true;
   if (response == 0) System.exit(0); // choose "Yes" than quit;
  }
  }
  
 }

其他棋子信息

@Override
 public void mousePressed(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }

 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }

 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }

 @Override
 public void mouseExited(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }
 }
}

图片显示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot整合Mybatis Generator自动生成代码

    SpringBoot整合Mybatis Generator自动生成代码

    SpringBoot 整合 Mybatis Generator自动生成dao、entity、mapper.xml实现单表增删改查。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • springboot如何通过@PropertySource加载自定义yml文件

    springboot如何通过@PropertySource加载自定义yml文件

    这篇文章主要介绍了springboot如何通过@PropertySource加载自定义yml文件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • JAVA布局管理器与面板组合代码实例

    JAVA布局管理器与面板组合代码实例

    这篇文章主要介绍了JAVA布局管理器与面板组合代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java的NIO之通道channel详解

    Java的NIO之通道channel详解

    这篇文章主要介绍了Java的NIO之通道channel详解,通道channel由java.nio.channels 包定义的,Channel 表示IO源与目标打开的连接,Channel类类似于传统的"流",只不过Channel本身不能直接访问数据,Channel只能与Buffer进行交互,需要的朋友可以参考下
    2023-10-10
  • Mybatis拦截器实现分页

    Mybatis拦截器实现分页

    本文介绍使用Mybatis拦截器,实现分页;并且在dao层,直接返回自定义的分页对象。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • Java中的可变参数常见用法实例总结

    Java中的可变参数常见用法实例总结

    这篇文章主要介绍了Java中的可变参数常见用法,结合实例形式总结分析了java可变参数的常见功能、使用方法及相关操作注意事项,需要的朋友可以参考下
    2019-10-10
  • Springboot整合Dozer实现深度复制的方法

    Springboot整合Dozer实现深度复制的方法

    Dozer是一种Java Bean到Java Bean的映射器,递归地将数据从一个对象复制到另一个对象,它是一个强大的,通用的,灵活的,可重用的和可配置的开源映射框架,本文给大家介绍Springboot整合Dozer的相关知识,感兴趣的朋友跟随小编一起看看吧
    2022-03-03
  • 在SpringBoot项目中整合拦截器的详细步骤

    在SpringBoot项目中整合拦截器的详细步骤

    在系统中经常需要在处理用户请求之前和之后执行一些行为,例如检测用户的权限,或者将请求的信息记录到日志中,即平时所说的"权限检测"及"日志记录",下面这篇文章主要给大家介绍了关于在SpringBoot项目中整合拦截器的相关资料,需要的朋友可以参考下
    2022-09-09
  • Java花式解决'分割回文串 ii'问题详解

    Java花式解决'分割回文串 ii'问题详解

    最学习动态规划思想的路上,遇见了‘分割回文串问题’,如临大敌啊,题目听起来蛮简单,思考起来却也没那么容易,本文将为大家详细介绍几种解决分割回文串 ii问题的办法,需要的可以参考一下
    2021-12-12
  • Java之打印String对象的地址

    Java之打印String对象的地址

    这篇文章主要介绍了Java之打印String对象的地址,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09

最新评论