分别用四种三个方块组成的图块填充棋盘,因而会空白一个,所以由用户指定一个棋盘方块,然后电脑计算出把剩余棋盘填充完。

黑色由用户指定:

StarttMain.java代码:

package com.jz.start;import javax.swing.UnsupportedLookAndFeelException;import com.jz.algorithm.Algorithm;
import com.jz.ui.ChessJFrame;/*** 游戏开始* @author Jayszxs**/
public class StartMain {public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {ChessJFrame scj = new ChessJFrame();scj.setVisible(true);}}

ChessBoard.java代码:

package com.jz.ui;import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;import javax.swing.JOptionPane;
import javax.swing.JPanel;public class ChessBoard extends JPanel implements MouseListener{public static final int MARGIN = 30; // 边距public static final int GRID_SPAN = 30; // 网格间距private final static int ROWS = 4;    //行数private final static int COLS = 4; //列数Point[] chessList = new Point[(ROWS + 1) * (COLS + 1)]; // 初始每个数组元素为null//   XYDrawn[] pointDrawn = new XYDrawn[ROWS * COLS]; // 用来存储已经画的坐标List<XYDrawn> xyDrawn = new ArrayList<XYDrawn>();Color[] color = new Color[4];int chessCount; // 当前棋盘的方块个数int point;public static boolean userStartPoint = false; //标记用户是否设置第一个方块public ChessBoard() {color[0] = new Color(255,0,0);color[1] = new Color(0,255,0);color[2] = new Color(0,0,255);color[3] = new Color(100,100,100);addMouseListener(this); //添加鼠标监听addMouseMotionListener(new MouseMotionListener() { // 匿名内部类:设置光标移动时的图像public void mouseDragged(MouseEvent e) {}public void mouseMoved(MouseEvent e) {               // 落在棋盘外,不能下if (e.getX() < MARGIN || e.getX() > COLS * MARGIN + MARGIN || e.getY() < MARGIN || e.getY() > ROWS * MARGIN + MARGIN || userStartPoint) {setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); // 设置成默认形状}else {setCursor(new Cursor(Cursor.HAND_CURSOR)); // 设置成手型}}});}// 1.开始绘制public void paintComponent(Graphics g) {super.paintComponent(g);// 1.1绘制网格for (int i = 0; i <= ROWS; i++) { // 画横线g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + COLS* GRID_SPAN, MARGIN + i * GRID_SPAN);}for (int i = 0; i <= COLS; i++) {// 画直线g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN,MARGIN + ROWS * GRID_SPAN);}// 1.2绘制方块for (int i = 0; i < chessCount; i++) {int xPos = chessList[i].getX() * GRID_SPAN + MARGIN; // 网格交叉点的x坐标int yPos = chessList[i].getY() * GRID_SPAN + MARGIN;// 网格交叉点的y坐标//           System.out.println(xPos + "  " + yPos);
//          System.out.println(chessList[i].getX() + "  " + chessList[i].getY());g.setColor(chessList[i].getColor()); // 设置颜色//绘制方块g.fillRect(xPos - Point.DIAMETER / 2 + 15, yPos - Point.DIAMETER / 2 + 15,Point.DIAMETER, Point.DIAMETER);}};@Override//覆盖鼠标按下方法public void mousePressed(MouseEvent e) {//判断用户是否设置第一个方块if (userStartPoint) {return;}int xIndex = (e.getX() - MARGIN) / GRID_SPAN; // 将鼠标点击的坐标位置转换成网格索引。int yIndex = (e.getY() - MARGIN) / GRID_SPAN;// System.out.println(xIndex + " " + yIndex);// System.out.println(e.getX() + " " + e.getY());// 落在棋盘外,不能下if (e.getX() < MARGIN || e.getX() > COLS * MARGIN + MARGIN || e.getY() < MARGIN || e.getY() > ROWS * MARGIN + MARGIN) {return;}Point ch = new Point(xIndex, yIndex, Color.black);xyDrawn.add(new XYDrawn(xIndex, yIndex));chessList[chessCount++] = ch;repaint(); // 通知系统重新绘制userStartPoint = true; //标记用户已经设置第一个方块//       System.out.println(ch.getX() + " " + ch.getY());}@Override//覆盖鼠标点击方法public void mouseClicked(MouseEvent e) {}@Override//覆盖鼠标输入方法public void mouseEntered(MouseEvent e) {}@Override//覆盖鼠标退出方法public void mouseExited(MouseEvent e) {}@Override//覆盖鼠标释放方法public void mouseReleased(MouseEvent e) {}public void BinarySearch(int x, int y, int col) {if (!userStartPoint) {return;}int x1, y1;List<XYDrawn> xAndy = new ArrayList<XYDrawn>();// 1if ((x < 2 && x >= 0) && (y < 2 && y >= 0)) {if (x == 0){x1 = x + 1;}else {x1 = x - 1;}if (y == 0) {y1 = y + 1;} else {y1 = y - 1;}xAndy.add(new XYDrawn(x1, y1));xAndy.add(new XYDrawn(x1, y));xAndy.add(new XYDrawn(x, y1));// 2   }else if ((x < 4 && x >= 2) && (y < 2 && y >= 0)) {if (x == 2){x1 = x + 1;}else {x1 = x - 1;}if (y == 0) {y1 = y + 1;} else {y1 = y - 1;}xAndy.add(new XYDrawn(x1, y1));xAndy.add(new XYDrawn(x1, y));xAndy.add(new XYDrawn(x, y1));// 3    } else if ((x < 2 && x >= 0) && (y < 4 && y >= 2)) {if (x == 0){x1 = x + 1;}else {x1 = x - 1;}if (y == 2) {y1 = y + 1;} else {y1 = y - 1;}xAndy.add(new XYDrawn(x1, y1));xAndy.add(new XYDrawn(x1, y));xAndy.add(new XYDrawn(x, y1));// 4} else {if (x == 2){x1 = x + 1;}else {x1 = x - 1;}if (y == 2) {y1 = y + 1;} else {y1 = y - 1;}xAndy.add(new XYDrawn(x1, y1));xAndy.add(new XYDrawn(x1, y));xAndy.add(new XYDrawn(x, y1));}for (XYDrawn xy : xAndy) {Point ch = new Point(xy.getX(), xy.getY(), color[col]);//xyDrawn.add(xy);chessList[chessCount++] = ch;repaint(); // 通知系统重新绘制}}public void BinarySearch2(int x, int y) {if ((x < 2 && x >= 0) && (y < 2 && y >= 0)) {Point ch = new Point(2, 1, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(2, 1));ch = new Point(1, 2, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(1, 2));ch = new Point(2, 2, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(2, 2));repaint(); // 通知系统重新绘制}else if ((x < 4 && x >= 2) && (y < 2 && y >= 0)) {Point ch = new Point(1, 1, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(1, 1));ch = new Point(1, 2, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(1, 2));ch = new Point(2, 2, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(2, 2));repaint(); // 通知系统重新绘制} else if ((x < 2 && x >= 0) && (y < 4 && y >= 2)) {Point ch = new Point(1, 1, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(1, 1));ch = new Point(2, 1, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(2, 1));ch = new Point(2, 2, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(2, 2));repaint(); // 通知系统重新绘制} else {Point ch = new Point(1, 1, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(1, 1));ch = new Point(2, 1, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(2, 1));ch = new Point(1, 2, Color.lightGray);chessList[chessCount++] = ch;xyDrawn.add(new XYDrawn(1, 2));repaint(); // 通知系统重新绘制}}public void restartGame() {// 清除棋子for (int i = 0; i < chessList.length; i++)chessList[i] = null;repaint();chessCount = 0;point = 0;userStartPoint = false;xyDrawn = new ArrayList<XYDrawn>();}public void goGame() {int xIndex = chessList[0].getX();int yIndex = chessList[0].getY();BinarySearch2(xIndex, yIndex);int i = 0;for (XYDrawn xy : xyDrawn) {BinarySearch(xy.getX(), xy.getY(), i);if (i >= 3) {return;}i++;}}
}

ChessJFrame.java代码:

package com.jz.ui;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;/*** 游戏框架类* @author Jayszxs**/
public class ChessJFrame extends JFrame {/*** */private static final long serialVersionUID = 1L;private ChessBoard chessBoard; //棋盘面板private JPanel toolbar; //工具条面板//开始、重新开始、退出按钮
//  public static Button startButton = new Button("开始");
//  public static Button restartButton = new Button("重新开始");
//  public static Button exitButton = new Button("退出");public static Button startButton; public static Button restartButton;public static Button exitButton;/*** 棋盘游戏框架初始化* @throws UnsupportedLookAndFeelException * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */public ChessJFrame() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {setBounds(800, 300, 200, 250); //设置框架的大小setTitle("棋盘游戏");    //设置框架名称//设置窗口的风格
//      String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//      UIManager.setLookAndFeel(lookAndFeel);// 1.初始化工具条面板和棋盘面板toolbar = new JPanel();chessBoard = new ChessBoard();// 2.初始化按钮startButton = new Button("开始");restartButton = new Button("重新开始");exitButton = new Button("退出");// 3.将工具条面板用FlowLayout布局toolbar.setLayout(new FlowLayout(FlowLayout.RIGHT));// 4.将按钮添加到工具条面板上toolbar.add(startButton);    //开始按钮toolbar.add(restartButton);   //重新开始按钮toolbar.add(exitButton);    //退出按钮// 5.将工具条面板布局到面板的下方add(toolbar,BorderLayout.SOUTH);// 将棋盘添加到面板中add(chessBoard);// 6.设置界面默认关闭事件setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 7.自动识别大小//pack();// 8.将按钮添加到监听器MyItemListener lis = new MyItemListener();startButton.addActionListener(lis);restartButton.addActionListener(lis);exitButton.addActionListener(lis);}/*** 事件监听器* @author Jayszxs**/private class MyItemListener implements ActionListener{@Override/*** 事件监听器类*/public void actionPerformed(ActionEvent e) {// 1.取的事件源Object obj = e.getSource();// 2.判断取的事件是什么if (obj == ChessJFrame.startButton) {//取的“开始”按钮chessBoard.goGame();}else if (obj == ChessJFrame.restartButton) {//取的“重新开始”按钮chessBoard.restartGame();}else if (obj == ChessJFrame.exitButton) {//取的“退出”按钮if (JOptionPane.showConfirmDialog(null, "是否退出?", "提示", JOptionPane.YES_NO_OPTION) == 1) {   //提示用户是否退出return;}System.exit(0);}}}}

Point.java代码:

package com.jz.ui;import java.awt.Color;/*** 方块的设计* @author Jayszxs**/
public class Point {private int x; // 棋盘中的x索引private int y; // 棋盘中的y索引private Color color;//颜色public static final int DIAMETER = 25;//直径public Point(int x, int y, Color color) {this.x = x;this.y = y;this.color = color;}public int getX() {// 拿到棋盘中的x索引return x;}public int getY() {// 拿到棋盘中的Y索引return y;}public Color getColor() {//得到颜色return color;}
}

XYDrawn.java代码:

package com.jz.ui;public class XYDrawn {private int x;private int y;public XYDrawn(int x, int y) {this.x = x;this.y = y;}public int getX() {return x;}public int getY() {return y;}}

Java 4X4棋盘游戏相关推荐

  1. java jpanel添加背景_java – 将背景图像添加到JPanel

    我正在用 Java构建棋盘游戏.对于游戏板本身,我试图将板的图像作为整个JPanel的背景,填充JFrame.我找到了一种方法来做到这一点,但只有本地存储的文件,它需要能够从GUI内部的图像中获取图像 ...

  2. JS+Canvas的棋盘游戏和Java的动态结合

    二维数组的题做了有几个了,感觉很有趣,随后想到想做一个五子棋的游戏. 因为前端知识匮乏,但感觉只是Java中去用二维数组做的话还是不太难的. 首先生成15*15的棋盘,(先不考虑前后端交互),其后Ha ...

  3. java简单纸牌游戏_2020年最佳2人棋盘游戏:拼凑,Kingdomino等

    假期呆在房子里?这些是打发时间的完美游戏. 由于持续不断的 危机,今年的假期可能看起来要安静 得多.但是安静不一定意味着坏.我们中的大多数人都将放弃大型聚会,如果您要在家中聚会,那么 棋盘游戏 是打发 ...

  4. java 自定义异常 未回滚_抛出自定义异常,spring AOP事务不回滚的解决方案

    spring AOP 默认对RuntimeException()异常或是其子类进行事务回滚,也就是说 事务回滚:throw new RuntimeException("xxxxxxxxxxx ...

  5. 12、Java Swing计算器界面的实现

    在本节之前已经详细介绍了 Swing 中容器.布局管理器以及常用的基本组件.本案例将综合运用这些知识实现一个计算器的布局.在本实例中使用两种布局管理器来进行界面设计. 计算器界面可以分成两部分,即显示 ...

  6. java判断44数组是否是4阶幻方_2015蓝桥杯决赛Java A组 第二题--四阶幻方

    //把1~16的数字填入4x4的方格中,使得行.列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方. // //四阶幻方可能有很多方案.如果固定左上角为1,请计算一共有多少种方案. //比如: ...

  7. HD1281棋盘游戏(匹配+好题)

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. Java Fork/Join 框架

    转载自 http://www.importnew.com/27334.html Doug Lea 大神关于Java 7引入的他写的Fork/Join框架的论文. 响应式编程(Reactive Prog ...

  9. hdu 1281 棋盘游戏 (二分匹配)

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

最新文章

  1. 业界丨一文详解腾讯布局AI生态的四大战略
  2. 怎么用feign远程调用别人的接口_spring cloud-openFeign声明式远程调用
  3. python怎么做图形界面-图形界面
  4. Numpy中np.dot()与np.matmul()的区别(矩阵乘积、矩阵乘法、矩阵相乘)
  5. 13 个 Python 新手练级项目
  6. Allegro padstack
  7. [内核摘要] 虚拟文件系统
  8. jquery的ajax用法
  9. 图神经网络(GNN)综述
  10. Tomcat的8080、8005、8009端口
  11. C++基础:如何去使用extern?
  12. 没有提取码怎么获取百度网盘资源?
  13. 周杰伦 青花瓷 蒲公英的约定 我不配 彩虹 歌词和下载
  14. 胡晓明的“出埃及记”,他将带领支付宝驶向何方?
  15. 嵌入式实操----基于RT1170 首板硬件之CAN BUS TJA1043显示调试(十八)
  16. 切尔西为切赫提供新岗位 蓝军盼其“回家”
  17. Linux添加硬盘并挂载(超细致)
  18. python+vue 税务申报系统
  19. USB协议学习笔记 - 虚拟串口Virtual Port Com LED控制
  20. 颜色选择器---Kodu少儿编程第九天

热门文章

  1. 绝对式编码器工作原理是什么?如何安装绝对式编码器?
  2. 【晶体学基础】空间群和对称性
  3. JS排序(正序/倒序)
  4. C语言一元多项式相加(链表)
  5. [聊一聊系列]聊一聊移动调试那些事儿
  6. 基于服务的云计算模式(IaaS、PaaS、SaaS)
  7. 数字IC笔试题(2)——降低动态IR DROP
  8. python实现易统计自动打卡
  9. python中文字体下载_解决Linux系统下python matplotlib中文字体显示问题
  10. 最欠揍的成语谜语(CSDN 转载)