华容道游戏简介:
华容道,古老的中国游戏,以其变化多端、百玩不厌的特点与魔方、独立钻石棋一起被国外智力专家并称为“智力游戏界的三个不可思议”。它与七巧板、九连环等中国传统益智玩具还有个代名词叫作“中国的难题”。华容道游戏取自著名的三国故事,曹操在赤壁大战中被刘备和孙权的“苦肉计”、“火烧连营”打败,被迫退逃到华容道,又遇上诸葛亮的伏兵,关羽为了报答曹操对他的恩情,明逼实让,终于帮助曹操逃出了华容道。游戏就是依照“曹瞒兵败走华容,正与关公狭路逢。只为当初恩义重,放开金锁走蛟龙”这一故事情节,通过移动各个棋子,帮助曹操从初始位置移到棋盘最下方中部,从出口逃走。不允许跨越棋子,还要设法用最少的步数把曹操移到出口。曹操逃出华容道的最大障碍是关羽,关羽立马华容道,一夫当关,万夫莫开。关羽与曹操当然是解开这一游戏的关键。四个刘备军兵是最灵活的,也最容易对付,如何发挥他们的作用也要充分考虑周全。“华容道”有一个带二十个小方格的棋盘,代表华容道。棋盘下方有一个两方格边长的出口,是供曹操逃走的。棋盘上共摆有十个大小不一样的棋子,它们分别代表曹操、张飞、赵云、马超、黄忠和关羽,还有四个卒。“华容道”有几十种布阵方法,如“横刀立马”、“近在咫尺”、“过五关”、“水泄不通”、“小燕出巢”等等玩法。棋盘上仅有两个小方格空着,玩法就是通过这两个空格移动棋子,用最少的步数把曹操移出华容道。
很小的时候就用父亲的手机玩过这个游戏,记得当时用了好久才过得去一关,现在同样觉得真的挺难的一个游戏。
但当时却玩的不亦乐乎,因为并没有别的游戏可以选择,记得当时就觉得这个游戏的人物角色太丑了,让我这个三国迷无法忍受。没想到好多年后的今年终于可以自己做出这个简单的小游戏,把每个角色定义成自己喜欢的样子,也算是圆了小时候的一个梦想。
程序主要由一个框架类和功能类构成。
(1)、框架类构造游戏主窗口,游戏页面七大操作按钮和十个游戏角色的创建和初始化,以及地图的构建。
(2)、功能类包括鼠标操作方法和键盘操作方法的添加,以及人物角色移动方法的具体实现。
算法主要体现在人物的移动上。主要是操作方式,鼠标和键盘都可进行操作,由用户进行选择使用哪种方式来进行游戏。
(1)、角色移动算法分析:
组件调用getBounds()方法可以返回一个和自己大小相等,位置相同的Rectangle对象,但Rectangle没有可视的外观,仅封装组件的位置和大小,因此可以用组件返回的Rectangle对象判断位置和大小信息,检查移动后的Rectangle对象和其他组件的是否相交即可。
(2)、若选择键盘,初始给特定一个小兵一个焦点,即从它开始进行移动,若玩家按下某方向键,当前小兵需要往某方向移动但是它不能往某方向移动(在边界处或者被其他人物阻挡),则选择当前可以往该方向移动的角色进行移动。
算法具体实现:遍历每一个角色,若当前角色可以执行该操作,用当前角色进行该操作。
用事先写好的goc方法判断其是否可以移动。
goc方法为boolen类型方法
Return True or False ,表示当前角色是否可进行移动
精确操作:键盘操作模式下,先用鼠标点击某个角色(鼠标点击即可给当前用户得到焦点),然后使用方向键进行移动。
(3)、鼠标操作:玩家需注意点击当前角色人物的位置。
鼠标操作下的移动与键盘略有不同,不存在需要自动获取焦点的问题,但需要注意的是当同时点击一个角色的下半部分和右半部分时,也就是说当前的操作同时触发两个鼠标事件,那么它就会直接向右下方进行移动,但游戏的规则中其实是不允许这样进行移动的。
这里通过把一个角色抽象为一个矩阵,然后模拟出它的长和宽,对点击的位置进行一定的数学限制即可实现。
比如向下可以以这样的条件进行限制
If(y>h/2&&x>w/3&&x<(w*2)/3)
其他位置如法炮制,就可以完美的解决这个细节上的问题。
开始界面:
游戏背景界面:
游戏帮助界面:
键盘操作界面:
鼠标操作界面:
游戏胜利界面:
注:以上素材gif图片均来自网络,感谢原作者,这里仅供娱乐。
贴下两个类的代码:(还没有进行良好的封装)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener {
private static final int ERROR_MESSAGE = 0;
private static final int WARNING_MESSAGE = 0;
int cnt=0;
Person person[]=new Person[10];
JButton left,right,above,below;
JButton restart=new JButton("重新开始");
JButton about=new JButton("游戏背景");
JButton help=new JButton("游戏帮助");
JButton mouse=new JButton("鼠标操作");
JButton key=new JButton("键盘操作");
JButton message=new JButton("当前步数:"+cnt);
JButton begin=new JButton("开始游戏");
JButton star=new JButton();
String name[]={"曹操","关羽","张飞","黄忠","马超","赵云","兵","兵","兵","兵"};
public Hua_Rong_Road(){
JOptionPane.showMessageDialog(this, "开始游戏前,请先阅读下方的游戏帮助,游戏背景可自行了解.");
init();
//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(200,200,640,800);//设置窗体初始位置以及大小的一个函数
setVisible(true);//窗口可见
//person[9].requestFocus();//获取焦点需要卸载setVisible后面才行
validate();//使用validate方法是容器再次布置其组件,确保布局有效
}
public void init(){
setLayout(null);
message.setBackground(Color.ORANGE);
add(restart);
restart.setBounds(120, 640, 100, 50);
//restart.setBackground(Color.RED);
restart.addActionListener(this);
add(about);
about.addActionListener(this);
about.setBounds(250,640,100,50);
add(mouse);
mouse.setBounds(280,40,100,50);
mouse.addActionListener(this);
add(key);
key.setBounds(400, 40, 100, 50);
key.addActionListener(this);
add(help);
help.setBounds(380, 640, 100, 50);
help.addActionListener(this);
add(message);
message.setBounds(110,40,160,50);
ImageIcon starr=new ImageIcon("timg (5).gif");
star.setIcon(starr);
star.setBounds(108,208,400,400);
add(star);
add(begin);
begin.addActionListener(this);
begin.setBounds(250,140,100,50);
setVisible(true);
left=new JButton();
right=new JButton();
above=new JButton();
below=new JButton();
add(left);
add(right);
add(above);
add(below);
//边界类
left.setBounds(98, 98, 10, 520);
right.setBounds(508,98,10,520);
above.setBounds(98, 98, 420, 10);
below.setBounds(98, 608, 420, 10);
validate();
}
//游戏布局
public void map1()
{
for(int k=0;k<name.length;k++)
{
person[k]=new Person(k,name[k]);
add(person[k]);
}
person[0].setBounds(208,108,200,200);//曹操
ImageIcon caocao=new ImageIcon("timg.gif");
person[0].setIcon(caocao);
person[1].setBounds(208,308,200,100);//关羽
ImageIcon guanyu=new ImageIcon("timg (4).gif");
person[1].setIcon(guanyu);
person[2].setBounds(108,308,100,200);//张飞
ImageIcon zhangfei=new ImageIcon("timg (3).gif");
person[2].setIcon(zhangfei);
person[3].setBounds(408,308,100,200);//黄忠
ImageIcon huangzhong=new ImageIcon("2.gif");
person[3].setIcon(huangzhong);
person[4].setBounds(108,108,100,200);//马超
ImageIcon machao=new ImageIcon("3.gif");
person[4].setIcon(machao);
person[5].setBounds(408,108,100,200);//赵云
ImageIcon zhaoyun=new ImageIcon("4.gif");
person[5].setIcon(zhaoyun);
person[6].setBounds(108,508,100,100);//
ImageIcon bing1=new ImageIcon("6.gif");
person[6].setIcon(bing1);
person[7].setBounds(408,508,100,100);
ImageIcon bing2=new ImageIcon("7.gif");
person[7].setIcon(bing2);
person[8].setBounds(208,408,100,100);
ImageIcon bing3=new ImageIcon("8.gif");
person[8].setIcon(bing3);
person[9].setBounds(308,408,100,100);
ImageIcon bing4=new ImageIcon("9.gif");
person[9].setIcon(bing4);
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){//键盘按下
Person man=(Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN)//下键
gok(man,below);
if(e.getKeyCode()==KeyEvent.VK_UP)//上键
gok(man,above);
if(e.getKeyCode()==KeyEvent.VK_LEFT)//左键
gok(man,left);
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键
gok(man,right);
}
//键盘模式下的移动
public void gok(Person man,JButton direction){
cnt++;
message.setText("当前步数:"+cnt);
boolean move=true;//可以移动
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below)
y=y+100;
else if(direction==above)
y=y-100;
else if(direction==left)
x=x-100;
else if(direction==right)
x=x+100;
manRect.setLocation(x,y);
Rectangle directionRect=direction.getBounds();
for(int k=0;k<10;k++){
Rectangle personRect=person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!=k)){
//intersects为矩形类的一个方法,可以判断是否相交
for(Person man2:person){//遍历数组
if(goc(man2,direction)==true){
return;
}
}
move=false;
}
}
if(manRect.intersects(directionRect)){
for(Person man2:person){
if(goc(man2,direction)==true){
return;
}
}
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
int cx,cy;//曹操的位置
cx=person[0].getBounds().x;
cy=person[0].getBounds().y;
if(cx==208&&cy==208)
{
win();
return ;
}
}
public void win()
{
JOptionPane.showMessageDialog(this, "恭喜少侠,成功帮曹操脱险,日后必大富大贵!\n"
+ "操作"+cnt+"步.震惊天下!");
JButton winn=new JButton();
ImageIcon winner=new ImageIcon("timg (1).gif");
winn.setIcon(winner);
winn.setBounds(108,108,400,500);
add(winn);
setVisible(true);
for(int k=0;k<name.length;k++)
this.remove(person[k]);
}
//判断是否可以进行移动
public boolean goc(Person man,JButton direction){
boolean move=true;//可以移动
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below)
y=y+100;
else if(direction==above)
y=y-100;
else if(direction==left)
x=x-100;
else if(direction==right)
x=x+100;
manRect.setLocation(x,y);
Rectangle directionRect=direction.getBounds();
for(int k=0;k<10;k++){
Rectangle personRect=person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!=k))
move=false;
}
if(manRect.intersects(directionRect))
move=false;
if(move==true)
man.setLocation(x,y);
return move;
}
public void gom(Person man,JButton direction){
cnt++;
message.setText("当前步数:"+cnt);
boolean move=true;//可以移动
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below)
y=y+100;
else if(direction==above)
y=y-100;
else if(direction==left)
x=x-100;
else if(direction==right)
x=x+100;
manRect.setLocation(x,y);
Rectangle directionRect=direction.getBounds();
for(int k=0;k<10;k++){
Rectangle personRect=person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!=k))
move=false;
}
if(manRect.intersects(directionRect))
move=false;
if(move==true)
man.setLocation(x,y);
int cx,cy;//曹操的位置
cx=person[0].getBounds().x;
cy=person[0].getBounds().y;
if(cx==208&&cy==208)//正确位置应该为408,这里为了快速结束游戏,设置较为简单
{
win();
return ;
}
}
@Override
//重新开始新的一局游戏
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();
if(b==restart)
{
dispose();
new Hua_Rong_Road();
}
if(b==about)
{
JOptionPane.showMessageDialog(this, "华容道游戏取自著名的三国故事,曹操在赤壁大战中被\n"
+ "刘备和孙权的“苦肉计”、“火烧连营”打败,被迫退逃到华容道,又遇上诸葛亮的伏兵,\n"
+ "关羽为了报答曹操对他的恩情,明逼实让,终于帮助曹操逃出了华容道。\n"
+ "曹操逃出华容道的最大障碍是关羽,关羽立马华容道,一夫当关,万夫莫开。\n"
+ "关羽与曹操当然是解开这一游戏的关键。\n"
+ "四个刘备军兵是最灵活的,也最容易对付,如何发挥他们的作用也要充分考虑周全。\n"
+ "“华容道”有一个带二十个小方格的棋盘,代表华容道。\n"
+ "棋盘下方有一个两方格边长的出口,是供曹操逃走的。" + "");
}
if(b==help)
{
JOptionPane.showMessageDialog(this, "胜利条件:曹操到达地图中下方位置!\n"
+ "点击开始游戏后,先在上方选择游戏方式\n"
+ "键盘操作:使用小键盘的上下左右方向键控制角色的移动\n"
+ "精确操作:键盘操作模式下,先用鼠标点击某个角色,然后使用方向键进行移动。\n"
+ "鼠标操作:玩家通过点击当前角色人物的不同位置进行相应移动。\n"
+ "注意,不能往左下,右下,左上,右上进行移动。\n"
+ "选择完成操作方式中途尽量不要更换", "开始之前必看", WARNING_MESSAGE);
}
if(b==key)
{
b.setBackground(Color.green);
for(int k=0;k<name.length;k++)
{
person[k].addKeyListener(this);
}
person[9].requestFocus();//获取焦点
}
if(b==mouse)
{
b.setBackground(Color.yellow);
for(int k=0;k<name.length;k++)
person[k].addMouseListener(this);
}
if(b==begin)
{
b.setBackground(Color.yellow);
this.remove(begin);
this.remove(star);
map1();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
Person man=(Person)e.getSource();
int x=-1,y=-1;
x=e.getX();
y=e.getY();
int w=man.getBounds().width;
int h=man.getBounds().height;
if(y>h/2&&x>w/3&&x<(w*2)/3)
{
gom(man,below);//下面
}
if(y<h/2&&x>w/3&&x<(w*2)/3)
{
gom(man,above);//上面
}
if(x<w/2&&y>h/3&&y<(h*2)/3)
{
gom(man,left);//左
}
if(x>w/2&&y>h/3&&y<(h*2)/3)
{
gom(man,right);//右
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Person extends JButton implements FocusListener{
int number;
Person(int number,String s){
this.number=number;
addFocusListener(this);
}
public void focusGained(FocusEvent e){
//setBackground(Color.GREEN);
}
public void focusLost(FocusEvent e){
//setBackground(Color.RED);
}
}
若需要运行,只需要new一个Hua_Rong_Road()即可。
《Java程序设计实用教程(第二版)》——耿祥义 张跃平编著。