public class Main {

    public static void main(String[] args) {
// TODO Auto-generated method stub
GameFrame frame = new GameFrame();
frame.setTitle("Game");
} }
import java.awt.*;
import java.awt.event.*;
import java.util.Random; import javax.swing.*;
import javax.swing.border.LineBorder; public class GameFrame extends JFrame {
/**
* 菜单栏
*/
JMenuBar menuBar;
/**
* "菜单"选项
*/
JMenu menu;
/**
* "帮助"选项
*/
JMenu help;
/**
* "重新开始"选项
*/
JMenuItem mItem1;
/**
* "退出"选项
*/
JMenuItem mItem2;
/**
* "关于"选项
*/
JMenuItem hItem;
/**
* 游戏面板
*/
JPanel panel;
/**
* 游戏中的按钮
*/
JButton buttons[] = new JButton[16];
/**
* 按钮监听器
*/
GameListener listener;
/**
* 背景颜色
*/
Color backgruond = Color.DARK_GRAY;
/**
* 前景颜色
*/
Color foreground = Color.WHITE;
/**
* 游戏中的字体
*/
Font font = new Font("Consolas", Font.PLAIN, 14); public GameFrame() {
// TODO Auto-generated constructor stub
setSize(500, 520);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE); menuBar = new JMenuBar();
menu = new JMenu("Menu");
menu.setForeground(foreground);
menu.setFont(font);
mItem1 = new JMenuItem("Restart");
mItem1.setFont(font);
mItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
init();
}
});
mItem2 = new JMenuItem("Exit");
mItem2.setFont(font);
mItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(mItem1);
menu.add(mItem2);
help = new JMenu("Help");
help.setForeground(foreground);
help.setFont(font);
hItem = new JMenuItem("About");
hItem.setFont(font);
hItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Maked by : 胡靖");
}
});
help.add(hItem);
menuBar.add(menu);
menuBar.add(help);
menuBar.setBackground(backgruond);
setJMenuBar(menuBar); panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
add(panel); setTitle("Loading...");
listener = new GameListener(buttons);
for (int i = 0; i < 16; i++) {
if (i == 15)
buttons[i] = new JButton(" ");
else
buttons[i] = new JButton("" + (i + 1));
buttons[i].setBackground(backgruond);
buttons[i].setForeground(foreground);
buttons[i].setBorder(new LineBorder(Color.GRAY));
buttons[i].setFont(new Font("Consolas", Font.PLAIN, 40));
buttons[i].addActionListener(listener);
panel.add(buttons[i]);
}
gameOn(); validate(); }
/**
* 初始话第一次游戏,并有动态初始化效果
*/
public void gameOn() {
for (int i = 0; i < 16; i++)
buttons[i].setEnabled(false);
menu.setEnabled(false);
help.setEnabled(false);
Random random = new Random();
int d[] = { -4, 1, -1, 4 };
int p = 15;
for (int i = 0; i < 1000; i++) { // 打乱的次数
int tmp = random.nextInt(4);
int next = p + d[tmp];
if (p % 4 == 0 && next % 4 == 3 || p % 4 == 3 && next % 4 == 0)
continue;
if (next >= 0 && next < 16) {
String s = buttons[p].getText();
buttons[p].setText(buttons[next].getText());
buttons[next].setText(s);
p = next;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
judge();
for (int i = 0; i < 16; i++)
buttons[i].setEnabled(true);
menu.setEnabled(true);
help.setEnabled(true);
}
/**
* 重新开始游戏的初始化,与第一次游戏使用的初始化方式不同
*/
public void init() {
for (int i = 0; i < 16; i++)
buttons[i].setEnabled(false);
Random random = new Random();
boolean flag[] = new boolean[16];
for (int i = 0; i < 16; i++) {
int tmp = random.nextInt(16);
while (flag[tmp])
tmp = random.nextInt(16);
if (tmp == 15)
buttons[i].setText(" ");
else
buttons[i].setText("" + (tmp + 1));
flag[tmp] = true;
}
judge();
for (int i = 0; i < 16; i++)
buttons[i].setEnabled(true);
}
/**
* 判断生成的序列是否可行,如果不可行交换任意相邻两个按钮的文本即可
*/
public void judge() {
int sum = 0;
int a[] = new int[16];
for (int i = 0; i < 16; i++) {
String s = buttons[i].getText();
if (s.equals(" "))
a[i] = 0;
else
a[i] = Integer.parseInt(s);
}
for (int i = 0; i < 15; i++) {
if (a[i] == 0) {
sum += i % 4;
sum += i / 4;
continue;
}
for (int j = i + 1; j < 16; j++)
if (a[i] > a[j])
sum++;
}
if (sum % 2 == 0) {
String s = buttons[0].getText();
buttons[0].setText(buttons[1].getText());
buttons[1].setText(s);
}
}
}
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JOptionPane; public class GameListener implements ActionListener {
/**
* 游戏中的按钮,传进来的参数
*/
JButton[] button = new JButton[16];
/**
* 四个方向
*/
int[] d = { -4, 1, -1, 4 }; public GameListener(JButton[] button) {
// TODO Auto-generated constructor stub
this.button = button;
}
/**
* 监听到动作后执行的方法
*/
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton jButton = (JButton) e.getSource();
if (jButton.getText().equals(" "))
return;
int position = 0;
for (int i = 0; i < 16; i++) {
if (jButton == button[i]) {
position = i;
break;
}
}
for (int i = 0; i < 4; i++) {
int next = position + d[i];
if (position % 4 == 0 && next % 4 == 3 || position % 4 == 3 && next % 4 == 0)
continue;
if (next >= 0 && next < 16 && button[next].getText().equals(" ")) {
String s = button[position].getText();
button[position].setText(button[next].getText());
button[next].setText(s);
break;
}
}
boolean win1 = true;
boolean win2 = true;
if (!button[15].getText().equals(" "))
win1 = false;
for (int i = 0; i < 15 && win1; i++)
if (!button[i].getText().equals("" + (i + 1)))
win1 = false;
if (!button[0].getText().equals(" "))
win2 = false;
for (int i = 1; i < 16 && win2; i++)
if (!button[i].getText().equals("" + (16 - i)))
win2 = false;
if (win1 || win2) {
for (int i = 0; i < 16; i++)
button[i].setEnabled(false);
JOptionPane.showMessageDialog(null, "Congratulations!\nYou win.");
}
} }

Java_Class 16方格拼图游戏的更多相关文章

  1. 拼图游戏js

    实现算法: 1. JavaScript动态生成拼图:通过生成16个div,且除最后一个div不使用背景图片以外,其他div都设置拼图图片为背景.然后通过调整background-position来实现 ...

  2. C#实现拼图游戏

    C#实现<拼图游戏> (下) 原理篇   前言:在 http://www.cnblogs.com/labixiaohei/p/6698887.html 程序设计 之 C#实现<拼图游 ...

  3. 利用Vue.js实现拼图游戏

    之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/luozhihao/p/5 ...

  4. atitit.html5 拼图游戏的解决之道.

    atitit.html5 拼图游戏的解决之道. 1. 拼图游戏的操作(点击法and 拖动法) 1 1. 支持键盘上.下.左.右键移动: 1 2. 支持点击空白模块中的上下左右箭头移动: 1 3. 支持 ...

  5. Vue.js实现拼图游戏

    Vue.js实现拼图游戏 之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/ ...

  6. 程序设计 之 C#实现《拼图游戏》 (下) 原理篇

    前言:在 http://www.cnblogs.com/labixiaohei/p/6698887.html 程序设计 之 C#实现<拼图游戏>(上),上传了各模块代码,而在本文中将详细剖 ...

  7. html+css+js实现网页拼图游戏

    代码地址如下:http://www.demodashi.com/demo/14449.html 项目描述 使用 html+js+css 实现一个网页拼图游戏,可支持简单,中等,困难三种难度. 演示效果 ...

  8. 一款html拼图游戏详解

    本文是爱编程原创翻译,转载请看清文末的转载要求,谢谢合作! 游戏介绍 这篇文章是献给web游戏开发者用简单的开发工具开发一款游戏.此文介绍了用html.css.javascript只需简单和几个步骤开 ...

  9. JavaScript拼图游戏

    今天是2016年最后一天上班了.最近几天都比较休闲,有时间空闲下来写写文档之类的. 2016过得真是快.感觉没做什么就过去了.想到之前想坚持每个月写一写博客都没坚持到.希望2017年可以吧. 无聊之余 ...

随机推荐

  1. jQuery使用方法

    使用jQuery的第一步,往往就是将一个选择表达式,放进构造函数jQuery()(简写为$),然后得到被选中的元素. 选择表达式可以是CSS选择器: 1 $(document)//选择整个文档对象2 ...

  2. Permission is only granted to system apps

    原文地址http://jingyan.baidu.com/article/9113f81b2e7a8c2b3314c711.html

  3. atitit 研发管理 要不要自己做引擎自己实现架构?.docx

    atitit 研发管理 要不要自己做引擎自己实现架构?.docx 1.1. 目前已经有很多引擎了,还要自己做吗??1 1.2. 答案是自己做更好,利大于弊1 2. 为什么要自己做??1 2.1. 从历 ...

  4. Ecshop :后台添加新功能 菜单及 管理权限 配置

    需求:在<商品管理>下增加一项[商品推广管理]功能 一. 添加菜单项 打开 /admin/includes/inc_menu.php 文件(后台框架左边菜单),在最后添加一行如下: $mo ...

  5. require.js笔记

    笔记参考来源:阮一峰  http://www.ruanyifeng.com/blog/2012/10/javascript_module.html   1. 浏览器端的模块只能采用“异步加载”方式 = ...

  6. python中常用的函数与库一

    1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...

  7. 查看Query Plan

    在执行一个查询语句时,查询优化器编译查询语句,产生一个足够好的Compiled Plan,将其缓存到plan cache中.Compiled plan是基于batch的,如果一个batch含有多个qu ...

  8. DBCC SHOW_STATISTICS 查看统计信息

    使用DBCC Show_Statistics 能够查看 表或Indexed view上的统计信息.Query optimizer使用统计信息进行estimate,生成高质量的qeury plan.统计 ...

  9. poj 2492A Bug's Life(并查集)

    /* 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 要求统计交配过 ...

  10. iOS_UIImage_Gif的合成

    /** 1. 数据获取 2. 创建Gif文件 3. 配置Gif属性 4. 单帧添加到gif */ github地址: https://github.com/mancongiOS/UIImage.git ...