推箱子java代码

合集下载

推箱子代码

推箱子代码
arr[sr][sc]=2;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
printf("%d",arr[i][j]);
printf("\n");
}
if(sr==0&&sc==N-1) {
printf("恭喜你成功了!\n");
break;
{ for(j=0;j<N;j++)
printf("%d",arr[i][j]);
printf("\n");
}
//人移动
int fx;
for(;;)
{ arr[pr][pc]=0;
arr[sr][sc]=0;
for(;;){ //输入移动的方向 定义 1左移,2右移,3上移,4下移
if(pr!=sr&&pc!=sc){ //不在同行且同列
if(fx==1){
if(pc>0) pc--;
else ;
}
if(fx==2){
if(pc<N-1) pc++;
else ;
}
sr=rand()%8+1;//箱子的随机位置
sc=rand()%8+1;
if(pr!=sr||pc!=sc) break;
}
arr[pr][pc]=1;//标示人的位置
arr[sr][sc]=2;//标示箱子的位置
intN;i++) //打印初始位置图

Java实现经典游戏推箱子的示例代码

Java实现经典游戏推箱子的示例代码

Java实现经典游戏推箱⼦的⽰例代码⽬录前⾔主要设计功能截图代码实现核⼼类声⾳播放类总结前⾔《推箱⼦》推箱⼦是⼀个古⽼的游戏,⽬的是在训练你的逻辑思考能⼒。

在⼀个狭⼩的仓库中,要求把⽊箱放到指定的位置,稍不⼩⼼就会出现箱⼦⽆法移动或者通道被堵住的情况,所以需要巧妙的利⽤有限的空间和通道,合理安排移动的次序和位置,才能顺利的完成任务。

游戏是⽤java语⾔实现,采⽤了swing技术进⾏了界⾯化处理,设计思路⽤了⾯向对象思想。

主要需求控制搬运⼯上下左右移动,来将箱⼦推到指定地点主要设计1、游戏⾯板⽣成显⽰2、地图⽣成算法3、⼈物移动算法4、播放背景⾳乐5、箱⼦移动算法6、全部箱⼦移动到指定位置,才算游戏过关功能截图游戏开始移动效果游戏过关代码实现核⼼类public class GameFrame extends JFrame implementsActionListener, MouseListener, KeyListener {// 实现动作事件监听器、⿏标事件监听器、键盘事件监听器// 当前的关卡数,默认为第⼀关,从1开始计数private int grade = 1;// row,column记载⼈的位置,分别表⽰⼆维数组中的⾏号和列号,即map[row][column]确定⼈的位置private int row = 7, column = 7;// leftX,leftY记载左上⾓图⽚的位置,避免图⽚从(0,0)坐标开始,因为是图⽚填充,从(0,0)开始不⾏private int leftX = 50, leftY = 50;// 记载地图的总共有多少⾏、多少列private int mapRow = 0, mapColumn = 0;// 记载屏幕窗⼝的宽度和⾼度private int width = 0, height = 0;private boolean acceptKey = true;// 程序所需要⽤到的图⽚private Image pics[] = null;// 图⽚数据private byte[][] map = null;// 地图数据private ArrayList list = new ArrayList();private SoundPlayerUtil soundPlayer;// 播放声⾳⼯具类/* 常量,即游戏中的资源 */private final static int WALL = 1;// 墙private final static int BOX = 2;// 箱⼦private final static int BOX_ON_END = 3;// 放到⽬的地的箱⼦private final static int END = 4;// ⽬的地private final static int MAN_DOWN = 5;// 向下的⼈private final static int MAN_LEFT = 6;// 向左的⼈private final static int MAN_RIGHT = 7;// 向右的⼈private final static int MAN_UP = 8;// 向上的⼈private final static int GRASS = 9;// 通道private final static int MAN_DOWN_ON_END = 10;// 站在⽬的地向下的⼈private final static int MAN_LEFT_ON_END = 11;// 站在⽬的地向左的⼈private final static int MAN_RIGHT_ON_END = 12;// 站在⽬的地向右的⼈private final static int MAN_UP_ON_END = 13;// 站在⽬的地向上的⼈private final static int MOVE_PIXEL = 30;// 表⽰每次移动30像素/*** 在构造⽅法GameFrame0中,调⽤initMap()法来初始化本关grade游戏地图,清空悔棋信* 息列表list,同时播放MIDI背景⾳乐。

用java实现推箱子(sokoban)游戏

用java实现推箱子(sokoban)游戏

推箱子游戏一、功能描述:➢可以通过面板上的按钮或是键盘上的pageup,pagedown键选择上下关➢可以通过面板上按钮或是键盘上的Backspace键后退,一直后退自己想要的位置,知道游戏开始时的位置。

➢可以通过面板上的按钮或是键盘上的字母r重新开始当前关卡游戏。

➢可以在复选框选择想要玩的关卡。

二、界面及运行截图三、源代码(三部分)1、地图类package box;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;//读取字符文件类FileReader import java.io.IOException;public class map {int[][] map=new int[20][20];int manX,manY;public map(int level){String filepath="mapc/"+level+".txt";File file = new File(filepath);FileReader fr = null;//利用FileReader流来读取一个文件中的数据BufferedReader br = null;//字符读到缓存里try {fr = new FileReader(file);br = new BufferedReader(fr);for (int i = 0; i < 15; i++){String line = br.readLine();//以行为单位,一次读一行利用BufferedReader 的readLine,读取分行文本byte[] point = line.getBytes();//将字符串转换为字节数组for (int j = 0; j < 15; j++) {map[i][j] = point[j] - 48;// 根据ASCall码表要减掉30H(十进制的48)if (map[i][j] == 5 || map[i][j] == 6 || map[i][j] == 7|| map[i][j] == 8){manX = i;manY = j;}}}}catch (FileNotFoundException e){e.printStackTrace();//深层次的输出异常调用的流程}catch (IOException e){e.printStackTrace();//深层次的输出异常调用的流程}catch(NullPointerException e){e.printStackTrace();//深层次的输出异常调用的流程}finally {if (br == null){try{br.close();}catch (IOException e){e.printStackTrace();}br = null;}if (fr == null){try {fr.close();} catch (IOException e){e.printStackTrace();}fr = null;}}}public int[][] getMap() {return map;}public int getManX() {return manX;}public int getManY() {return manY;}}2、游戏运行类package box;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.*;import javax.swing.*;public class pushbox extends JFrame implements KeyListener,ActionListener{ map map_level=new map(1); //首次获取地图int[][] map=new int[15][15]; //地图上每个位置所对应的图片int[][] map_restart=new int[15][15]; //备用图片static int manX,manY; //人所处的位置static int level=1; //所处关卡static int step=0;Image[] picture; //地图上的所有图片ImageIcon[] image=new ImageIcon[10];Stack<Integer> back_stack = new Stack<Integer>(); //存储所前行的路径JButton next,previous,back,restart,exit; //按钮:下一关,上一关,重新开始,退出String current_level="第"+level+"关"; //当前所处关卡(右上角显示文本)String[] level_button=new String[13]; //复选框的容JComboBox selectBox; //选关复选框final int Up = 0;final int Down = 1;final int Left = 2;final int Right = 3;int[] dir_x = {-1, 1, 0, 0};int[] dir_y = {0, 0, -1, 1};public pushbox(){super("推箱子");getmap(map);previous=new JButton("上一关");next=new JButton("下一关");restart=new JButton("重新开始");back=new JButton("后退");exit=new JButton("退出游戏");for(int i=0;i<13;i++)level_button[i]="第"+Integer.toString(i+1)+"关";selectBox=new JComboBox(level_button);FlowLayout layout=new FlowLayout(); //布局模式Container c=getContentPane();c.setLayout(layout);layout.setAlignment(FlowLayout.CENTER); //控件在布局中的位置c.setBackground(Color.CYAN); //背景色c.add(previous);c.add(next);c.add(back);c.add(restart);c.add(selectBox);c.add(exit);previous.addActionListener((ActionListener) this);previous.addKeyListener(this);next.addActionListener((ActionListener) this);next.addKeyListener(this);back.addActionListener(this);back.addKeyListener(this);restart.addActionListener((ActionListener) this);restart.addKeyListener(this);exit.addActionListener((ActionListener) this);exit.addKeyListener(this);selectBox.addItemListener( //监听复选框点击事件new ItemListener(){public void itemStateChanged(ItemEvent e){if(e.getStateChange()==e.SELECTED){step=0;back_stack.clear(); //每次重新选关后都要清空路径栈level=selectBox.getSelectedIndex()+1; //获取所选的关卡map_level=new map(level); //重新获取对应关卡的地图getmap(map);current_level="第"+level+"关"; //右上角提示所处关卡文本的更新repaint(); //重画地图requestFocus(); //重新获取焦点(因为按钮事件后键盘事件不起作用)}}});for(int k=0;k<10;k++){image[k]=new ImageIcon("picture/"+k+".png"); //获取图片}picture=new Image[]{image[0].getImage(),image[1].getImage(),image[2].getImage(),image[3].getImage(),image[4].getImage(),image[5].getImage(),image[6].getImage(),image[7].getImage(),image[8].getImage(),image[9].getImage()};this.setFocusable(true);this.addKeyListener((KeyListener)this);setSize(640,640);setVisible(true);}//判断是否通关(方法是,检查地图中是否还有箱子编号4)public Boolean pass(){int p=0;for(int i=0;i<15;i++)for(int j=0;j<15;j++){if(map[i][j]==4) p++;}if(p==0) return true;else return false;}//获取地图上每个位置所对应的图片编号public void getmap(int[][] map){for(int i=0;i<15;i++)for(int j=0;j<15;j++)map[i][j]=map_level.map[i][j];manX=map_level.manX;manY=map_level.manY;}public void paint(Graphics g){super.paint(g);//提示信息back_stack.push(Down + 5); //这里是一个细节,先将“DOWN”存入栈g.drawString(current_level,500,90);g.drawString("第"+step+"步",400,90);g.drawString("用键盘上的R重新开始,Q退出游戏",200,565);g.drawString("用键盘pageup,pagedown切换关卡",200,580);g.drawString("用键盘上的上下左右键控制人的移动",200,600);g.drawString("用键盘上的Backspace键后退",200,620);//在面板上画地图for (int i = 0; i < 15; i++)for (int j = 0; j < 15; j++)g.drawImage(picture[map[i][j]],100+30*j,100+30*i, this);}public void Step(Graphics g){g.setColor(Color.cyan);g.drawString("第"+step+"步",400,90);step++;g.setColor(Color.BLACK);g.drawString("第"+step+"步",400,90);}public void move(int dir, Graphics g){int man = dir + 5;int dx = dir_x[dir];int dy = dir_y[dir];int x = manX;int y = manY;manX += dx;manY += dy;int origin_picture = map[manX][manY];int next_picture = map[manX + dx][manY + dy];if((map[manX][manY]==4||map[manX][manY]==3)&&(map[manX+dx][manY+dy]==2||map[ manX+dx][manY+dy]==4||map[manX+dx][manY+dy]==3)) //上面是箱子,箱子上面是墙或箱子{manX -= dx;manY -= dy;return ;}if(map[manX][manY]==2) //上面是墙{manX -= dx;manY -= dy;return ;}if(map[manX][manY]==1||map[manX][manY]==9){ //人上面是草地或是目的地g.drawImage(picture[man],100+30*manY,manX*30+100,this); //人的朝向变化if(map[x][y]==9) //原来位置是目的地g.drawImage(picture[9],100+30*y,100+30*x,this);else{ //原来位置是草地g.drawImage(picture[1],100+30*y,x*30+100,this);map[x][y]=1;}Step(g);}if((map[manX][manY]==4||map[manX][manY]==3)&&map[manX+dx][manY+dy]==1){//上面是箱子,箱子上面是草地g.drawImage(picture[man],100+30*manY,manX*30+100,this); //人的朝向变化if(map[manX][manY]==3) //若是移动一大目的地的箱子,原箱子位置值为9map[manX][manY]=9;if(map[x][y]==9) //原来位置是目的地g.drawImage(picture[9],100+30*y,100+30*x,this);else{ //原来位置是草地g.drawImage(picture[1],100+30*y,x*30+100,this);map[x][y]=1;}g.drawImage(picture[4],100+30*(manY+dy),(manX+dx)*30+100,this); //草地位置变箱子map[manX+dx][manY+dy]=4;Step(g);}if((map[manX][manY]==4||map[manX][manY]==3)&&map[manX+dx][manY+dy]==9){ //上面是箱子,箱子上面是目的地g.drawImage(picture[man],100+30*manY,manX*30+100,this); //人的朝向变化if(map[manX][manY]==3) //若是移动一大目的地的箱子,原箱子位置值为9map[manX][manY]=9;else map[manX][manY]=0;if(map[x][y]==9) //原来位置是目的地g.drawImage(picture[9],100+30*y,100+30*x,this);else { //原来位置是草地g.drawImage(picture[1],100+30*y,x*30+100,this);map[x][y]=1;}g.drawImage(picture[3],100+30*(manY+dy),(manX+dx)*30+100,this); //目的地位置变箱子map[manX+dx][manY+dy]=3;Step(g);}back_stack.push(next_picture);back_stack.push(origin_picture);back_stack.push(manY);back_stack.push(manX);back_stack.push(man);if(pass())JOptionPane.showMessageDialog(null, "恭喜你过关了!"); }//键盘事件public void keyPressed(KeyEvent e) {Graphics g=getGraphics();int dir ;switch(e.getKeyCode()){case KeyEvent.VK_UP:dir = Up;move(dir, g);break;case KeyEvent.VK_DOWN :dir = Down;move(dir, g);break;case KeyEvent.VK_LEFT:dir = Left;move(dir, g);break;case KeyEvent.VK_RIGHT:dir = Right;move(dir, g);break;case KeyEvent.VK_BACK_SPACE:back.doClick();break;case KeyEvent.VK_PAGE_DOWN :next.doClick();break;case KeyEvent.VK_PAGE_UP:previous.doClick();break;case KeyEvent.VK_R:restart.doClick();break;case KeyEvent.VK_Q :exit.doClick();break;}}public void keyReleased(KeyEvent e) {}public void keyTyped(KeyEvent e) {}//悔步后退public void step_back(Graphics g){g.setColor(Color.cyan);g.drawString("第"+step+"步",400,90);step--;g.setColor(Color.BLACK);g.drawString("第"+step+"步",400,90);}public void Back(){if(back_stack.size()==2)return;int dir=back_stack.pop() - 5; //获取上一步的方向int s = back_stack.pop();int t = back_stack.pop();int origin_picture = back_stack.pop();int netx_picture = back_stack.pop();int pre_dir = back_stElement();Graphics g=getGraphics();int dx = dir_x[dir];int dy = dir_y[dir];g.drawImage(picture[origin_picture],100+30*t,100+30*s,this);g.drawImage(picture[netx_picture],100+30*(t+dy),100+30*(s+dx),this);g.drawImage(picture[pre_dir],100+30*(t-dy),100+30*(s-dx),this);manX = s - dx;manY = t - dy;map[s][t] = origin_picture;map[s+dx][t+dy] = netx_picture;map[s-dx][t-dy] = pre_dir;step_back(g);}//按钮事件监听public void actionPerformed(ActionEvent e) {if(e.getSource()==restart){ //重新开始back_stack.clear(); //清空路径栈step=0;getmap(map);repaint();}else if(e.getSource()==exit) //退出游戏this.setVisible(false);else if(e.getSource()==next){ //下一关back_stack.clear();step=0;level++;if(level!=14){map_level=new map(level);getmap(map);current_level="第"+level+"关";repaint();}}else if(e.getSource()==previous){ //上一关back_stack.clear();step=0;level--;if(level!=0){map_level=new map(level);getmap(map);current_level="第"+level+"关";repaint();}}else if(e.getSource()==back)Back();requestFocus();}}3、开始界面package box;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Game extends JFrame implements ActionListener{ Image image[];ImageIcon icon;JButton start,tip,exit;Container c;public Game(){super("推箱子");c=getContentPane();FlowLayout layout=new FlowLayout();c.setLayout(layout);layout.setAlignment(FlowLayout.CENTER);icon=new ImageIcon("picture/game.png");image=new Image[]{icon.getImage()};start=new JButton("开始游戏");exit=new JButton("退出游戏");tip=new JButton("游戏说明");c.add(start);c.add(tip);c.add(exit);start.addActionListener(this);tip.addActionListener(this);exit.addActionListener(this);setSize(640,480);setVisible(true);}public void paint(Graphics g){super.paint(g);g.drawImage(image[0],10,65,this);}public static void main(String args[]){Game g=new Game();g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==exit)this.setVisible(false);if(e.getSource()==start){this.setVisible(false);pushbox c=new pushbox();}if(e.getSource()==tip){String t="本游戏分为13关,难度逐关递增,可以自己选关";JOptionPane.showMessageDialog(null, t);}}}。

java_推箱子讲解

java_推箱子讲解

总共四个包,因为不能上传图片所以在第2个包中,导入图片(图片可以去网上下载一些)或者高手帮我解决一下。

不用导入图片也能玩!(求大神)下面是整个游戏的代码:1:package com.sj.xzq;class BackUpInfo {private int[][] map;private int manX;private int manY;public BackUpInfo(int[][] map, int manX, int manY) {this.map = map;this.manX = manX;this.manY = manY;}public int[][] getMap() {return map;}public int getManX() {return manX;}public int getManY() {return manY;}}2:package com.sj.xzq;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Stack;import javax.swing.JOptionPane;import javax.swing.JPanel;public class Game extends JPanel {private LoadMap lm;private int level;private int manX, manY;private int lastImg = 2;private int[][] map; // 地图所对应的二维数组private int[][] backMap; // 地图所对应的二维数组,这个数组用于保存上一步的地图信息private static final int LEN = 30; // 图片的长private static final int WIDTH = 30; // 图片的宽private static final int SPEED = 30; // 移动速度,每次移动一格private static Toolkit tk = Toolkit.getDefaultToolkit();private static Image[] imgs = null;private Stack<BackUpInfo> myStack = new Stack<BackUpInfo>();private BackUpInfo bp;static {imgs = new Image[] {tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/0.gif")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/1.gif")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/2.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/3.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/4.gif")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/5.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/6.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/7.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/8.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/9.GIF")) };}public Game(int level) {this.setBounds(0, 0, 600, 600);this.setVisible(true);lm = new LoadMap(level);map = lm.getMap();this.manX = lm.getManX();this.manY = lm.getManY();this.level = level;}public void paint(Graphics g) {for (int i = 0; i < 20; i++) {for (int j = 0; j < 20; j++) {g.drawImage(imgs[map[i][j]], i * LEN, j * WIDTH, this);}}g.setColor(Color.BLUE);g.setFont(new Font("仿宋", Font.BOLD, 18));g.drawString("关卡:" + level, 50, 50);}// 响应键盘事件,松开键盘才响应// 上下左右四个键,实现人物的移动。

推箱子游戏的代码)

推箱子游戏的代码)

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>typedef struct winer{int x,y;struct winer *p;}winer;char status [20][20];char far *printScreen=(char far* )0xB8000000;void putoutChar(int y,int x,char ch,char fc,char bc);void printWall(int x, int y);void printBox(int x, int y);void printBoxDes(int x, int y);void printDestination(int x, int y);void printDestination1(int x,int y,winer **win,winer **pw); void printMan(int x, int y);void init();winer *initStep1();winer *initStep2();winer *initStep3();winer *initStep4();void moveBoxSpacetoSpace(int x ,int y, char a);void moveBoxDestoSpace(int x ,int y, char a) ;void moveBoxSpacetoDes(int x, int y, char a);void moveBoxDestoDes(int x, int y, char a);int judge(int x, int y);void move(int x, int y, char a);void reset(int i);void putoutChar(int y,int x,char ch,char fc,char bc){printScreen[(x*160)+(y<<1)+0]=ch;printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;}void printWall(int x,int y){putoutChar(y-1,x-1,219,GREEN,BLACK);status[x][y]='w';}void printBox(int x,int y){putoutChar(y-1,x-1,10,WHITE,BLACK);status[x][y]='b';}void printDestination1(int x,int y,winer **win,winer **pw) {winer *qw;putoutChar(y-1,x-1,003,YELLOW,BLACK);status[x][y]='m';if(*win==NULL){*win=*pw=qw=(winer* )malloc(sizeof(winer));(*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;}else{qw=(winer* )malloc(sizeof(winer));qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;}}void printDestination(int x,int y){putoutChar(y-1,x-1,003,YELLOW,BLACK);status[x][y]='m';}void printMan(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}void printBoxDes(int x,int y){putoutChar(y-1,x-1,10,YELLOW,BLACK);status[x][y]='i';}void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)status[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(40,4);printf("Welcome to the box world!");gotoxy(40,6);printf("You can use up, down, left,");gotoxy(40,8);printf("right key to control it, or");gotoxy(40,10);printf("you can press Esc to quit it."); gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("April , 2007");}winer *initStep1(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=5;y<=9;y++)printWall(x+4,y+10);for(y=5,x=2;x<=5;x++)printWall(x+4,y+10);for(y=9,x=2;x<=5;x++)printWall(x+4,y+10);for(y=1,x=3;x<=8;x++)printWall(x+4,y+10);for(x=3,y=3;x<=5;x++)printWall(x+4,y+10);for(x=5,y=8;x<=9;x++)printWall(x+4,y+10);for(x=7,y=4;x<=9;x++)printWall(x+4,y+10);for(x=9,y=5;y<=7;y++)printWall(x+4,y+10);for(x=8,y=2;y<=3;y++)printWall(x+4,y+10);printWall(5+4,4+10);printWall(5+4,7+10);printWall(3+4,2+10);printBox(3+4,6+10);printBox(3+4,7+10);printBox(4+4,7+10);printDestination1(4+4,2+10,&win,&pw); printDestination1(5+4,2+10,&win,&pw); printDestination1(6+4,2+10,&win,&pw); printMan(2+4,8+10);return win;}winer *initStep2(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=4;y<=7;y++)printWall(x+4,y+10);for(x=2,y=2;y<=4;y++)printWall(x+4,y+10);for(x=2,y=7;x<=4;x++)printWall(x+4,y+10);for(x=4,y=1;x<=8;x++)printWall(x+4,y+10);for(x=8,y=2;y<=8;y++)printWall(x+4,y+10);for(x=4,y=8;x<=8;x++)for(x=4,y=6;x<=5;x++)printWall(x+4,y+10);for(x=3,y=2;x<=4;x++)printWall(x+4,y+10);for(x=4,y=4;x<=5;x++)printWall(x+4,y+10);printWall(6+4,3+10);printBox(3+4,5+10);printBox(6+4,6+10);printBox(7+4,3+10);printDestination1(5+4,7+10,&win,&pw); printDestination1(6+4,7+10,&win,&pw); printDestination1(7+4,7+10,&win,&pw); printMan(2+4,6+10);return win;}winer *initStep3(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=2;y<=8;y++)printWall(x+4,y+10);for(x=2,y=2;x<=4;x++)printWall(x+4,y+10);for(x=4,y=1;y<=3;y++)printWall(x+4,y+10);for(x=5,y=1;x<=8;x++)printWall(x+4,y+10);for(x=8,y=2;y<=5;y++)printWall(x+4,y+10);for(x=5,y=5;x<=7;x++)printWall(x+4,y+10);for(x=7,y=6;y<=9;y++)printWall(x+4,y+10);for(x=3,y=9;x<=6;x++)printWall(x+4,y+10);for(x=3,y=6;y<=8;y++)printWall(x+4,y+10);printWall(2+4,8+10);printWall(5+4,7+10);printBox(6+4,3+10);printBox(5+4,6+10);printDestination1(2+4,5+10,&win,&pw); printDestination1(2+4,6+10,&win,&pw); printDestination1(2+4,7+10,&win,&pw); printMan(2+4,4+10);return win;}winer *initStep4(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=1;y<=6;y++)printWall(x+4,y+10);for(x=2,y=7;y<=8;y++)printWall(x+4,y+10);for(x=2,y=1;x<=7;x++)printWall(x+4,y+10);for(x=7,y=2;y<=4;y++)printWall(x+4,y+10);for(x=6,y=4;y<=9;y++)printWall(x+4,y+10);for(x=3,y=9;x<=5;x++)printWall(x+4,y+10);for(x=3,y=3;y<=4;y++)printWall(x+4,y+10);printWall(3+4,8+10);printBox(3+4,5+10);printBox(4+4,4+10);printBox(4+4,6+10);printBox(5+4,5+10);printBox(5+4,3+10);printDestination1(3+4,7+10,&win,&pw); printDestination1(4+4,7+10,&win,&pw); printDestination1(5+4,7+10,&win,&pw); printDestination1(4+4,8+10,&win,&pw); printDestination1(5+4,8+10,&win,&pw); printMan(2+4,2+10);return win;}void moveBoxSpacetoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]=0;printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]=0;printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]=0;printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxDestoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]='m';printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]='m';printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]='m';printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxSpacetoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBoxDes(x-2,y);status[x-2][y]='i';break;case 'd':status[x+1][y]=0;printf(" ");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]=0;printf(" ");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]=0;printf(" ");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}void moveBoxDestoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]='m';printf(" ");printMan(x+1,y);status[x+2][y]='i'; break;case 'l':status[x][y-1]='m'; printf(" ");printBoxDes(x,y-2); printMan(x,y-1);status[x][y-2]='i'; break;case 'r':status[x][y+1]='m'; printf(" ");printBoxDes(x,y+2); printMan(x,y+1);status[x][y+2]='i'; break;default:break;}}int judge(int x,int y) {int i;switch(status[x][y]) {case 0:i=1;break;case 'w':i=0;break;case 'b':i=2;break;case 'i':i=4;break;case 'm':i=3;break;default:break;}return i;}void move(int x,int y,char a){switch(a){case 'u':if(!judge(x-1,y)){gotoxy(y,x);break;}else if(judge(x-1,y)==1||judge(x-1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x-1,y);break;}else{printf(" ");printMan(x-1,y);break;}}else if(judge(x-1,y)==2){if(judge(x-2,y)==1){moveBoxSpacetoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxSpacetoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}else if(judge(x-1,y)==4){if(judge(x-2,y)==1){moveBoxDestoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxDestoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}case 'd':if(!judge(x+1,y)){gotoxy(y,x);break;}else if(judge(x+1,y)==1||judge(x+1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x+1,y);break;}else{printf(" ");printMan(x+1,y);break;}}else if(judge(x+1,y)==2){if(judge(x+2,y)==1){moveBoxSpacetoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxSpacetoDes(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}else if(judge(x+1,y)==4){if(judge(x+2,y)==1){moveBoxDestoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxDestoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}case 'l':if(!judge(x,y-1)){gotoxy(y,x);break;}else if(judge(x,y-1)==1||judge(x,y-1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y-1);break;}else{printf(" ");printMan(x,y-1);break;}}else if(judge(x,y-1)==2){if(judge(x,y-2)==1){moveBoxSpacetoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxSpacetoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){{moveBoxDestoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxDestoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}case 'r':if(!judge(x,y+1)){gotoxy(y,x);break;}else if(judge(x,y+1)==1||judge(x,y+1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y+1);break;}else{printf(" ");printMan(x,y+1);break;}}else if(judge(x,y+1)==2){if(judge(x,y+2)==1){moveBoxSpacetoSpace(x,y,'r');printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxSpacetoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}else if(judge(x,y+1)==4){if(judge(x,y+2)==1){moveBoxDestoSpace(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxDestoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}default:break;}}void reset(int i){switch(i){case 0:init();initStep1();break;case 1:init();initStep2();break;case 2:init();initStep3();break;case 3:init();initStep4();break;default:break;}}void main(){int key;int x;int y;int s;int i=0;winer *win;winer *pw;_AL=3;_AH=0;geninterrupt(0x10); init();win=initStep1();do{_AH=3;geninterrupt(0x10); x=_DH+1;y=_DL+1;while(bioskey(1)==0);key=bioskey(0);switch(key){case 0x4800:move(x,y,'u');break;case 0x5000:move(x,y,'d');break;case 0x4b00:move(x,y,'l');break;case 0x4d00:move(x,y,'r');break;case 0x3920:reset(i);break;default:break;}s=0;pw=win;while(pw){if(status[pw->x][pw->y]=='m')s++;pw=pw->p;}if(s==0){free(win);gotoxy(25,2);printf("congratulate! You have done this step!"); getch();i++;switch(i){case 1:init();win=initStep2();break;case 2:init();win=initStep3();break;case 3:init();win=initStep4();break;case 4:gotoxy(15,21);printf("Congratulation! \n");gotoxy(15,23);printf("You have done all the steps, Welcome to play again!"); key=0x011b;getch();break;default:break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10);}。

推箱子代码

推箱子代码

import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.io.*;import javax.sound.midi.*;import java.util.Stack;public class Tuixiangzi{public static void main(String[] args){new mainFrame();}}class mainFrame extends JFrame implements ActionListener,ItemListener{JLabel lb;JLabel lb2;JButton btnrenew,btnlast,btnnext,btnchoose,btnfirst,btnover,btnmuc,btnback;mainpanel panel;Sound sound;JComboBox jc=new JComboBox();MenuItem renew=new MenuItem(" 重新开始");MenuItem back=new MenuItem(" 悔一步");MenuItem last=new MenuItem(" 上一关");MenuItem next=new MenuItem(" 下一关");MenuItem choose=new MenuItem(" 选关");MenuItem exit=new MenuItem(" 退出");MenuItem qin=new MenuItem(" 琴萧合奏");MenuItem po=new MenuItem(" 泡泡堂");MenuItem guang=new MenuItem(" 灌篮高手");MenuItem nor=new MenuItem(" 默认");MenuItem eye=new MenuItem(" eyes on me");MenuItem about=new MenuItem(" 关于推箱子...");mainFrame(){super("推箱子v2.0");setSize(720,720);setVisible(true);setResizable(false);setLocation(300,20);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Container cont=getContentPane();cont.setLayout(null);cont.setBackground(Color.black);Menu choice=new Menu(" 选项");choice.add(renew);choice.add(last);choice.add(next);choice.add(choose);choice.add(back);choice.addSeparator();choice.add(exit);renew.addActionListener(this);last.addActionListener(this);next.addActionListener(this);choose.addActionListener(this);exit.addActionListener(this);back.addActionListener(this);Menu setmuc=new Menu(" 设置音乐");setmuc.add(nor);setmuc.add(qin);setmuc.add(po);setmuc.add(guang);setmuc.add(eye);nor.addActionListener(this);qin.addActionListener(this);po.addActionListener(this);guang.addActionListener(this);eye.addActionListener(this);Menu help=new Menu(" 帮助");help.add(about);about.addActionListener(this);MenuBar bar=new MenuBar();bar.add(choice);bar.add(setmuc);bar.add(help);setMenuBar(bar);nor.setEnabled(false);lb=new JLabel("JA V A推箱子v2.0版!!!提供友情下载。

基于java的推箱子游戏(含源文件)

基于java的推箱子游戏(含源文件)

基于JAVA的推箱子游戏前言在这个充满竞争的社会里,随着生活节奏的不断加快,人们的生活水平越来越繁忙,因此,越来越多人感到无比的烦躁与巨大的压力,这些压力也带给人们诸多烦恼。

所以如何解除现代人的生活压力,释放心中苦闷已经成为现代人的共同心愿。

而本次的这个基于JAVA的推箱子小游戏正是使用JAVA语言并且结合于EditPlus编译器,为大家开发了一款适合的、休闲娱乐的游戏,在娱乐的同时也给忙碌的上班族们开拓了另一个思考问题的空间。

基于JAVA的推箱子小游戏主要分为初始化模块,画图模块,移动小人模块,移动箱子模块以及功能控制模块。

玩家是通过控制小人的移动来推动箱子,需要避过障碍物与死角才能将其推放到指定位置从而达到过关的目的。

每关的难易程度各异,不仅适合人们休闲娱乐,还有助于开发智力,提高人们思考问题能力与想象能力,改善思考方式,拓展思维。

关键词:JAVA;解压;推箱子;休闲娱乐AbstractIn this competitive society, along with the accelerating pace of life, people's living standard more and busier, therefore, more and more people feel very upset and enormous pressure, the pressure also gives people a lot of trouble. So how to relieve the pressure of modern life, release the pain has become the common aspiration of the people. But this time based on the JAVA Push Box Game is the use of JAVA language and the combination of EditPlus compiler; we developed a suitable, recreational game, in the entertainment but also for busy office workers to develop another thinking space.Based on the JAVA Push Box Game consists of initialization module, drawing module, mobile SIM module, mobile box module and control module. Game player is controlled through a small mobile drive box, need to avoid obstacles and dead angle can be pushed into the specified location so as to achieve the purpose of clearance. Each level of difficulty degree is different, not only for recreation, also contribute to the development of intelligence, improve people's thinking ability and imagination ability, improve the way of thinking, the development of thinking.Key Words:JAVA; Relieve pressure; Push box; Recreation and entertainment目录前言............................................................................................................................................. I Abstract ..................................................................................................................................... II 1.可行性研究 (2)1.1设计目的 (2)1.2可行性研究前提 (2)1.3可行性分析 (2)1.4结论意见 (3)2.需求分析 (3)2.1引言 (3)2.2游戏需求 (3)2.3软硬件需求 (4)硬件环境需求: (4)软件环境: (4)2.4接口控制 (4)2.5方案论证 (4)2.5.1.C++语言的优点 (4)2.5.2.Java语言的优点 (5)2.5.3.方案选择 (6)3.概要设计 (7)3.1游戏设计分析 (7)3.3.1.地图文件的引用 (7)3.3.2.程序运逻辑 (7)3.3.3.绘图 (7)3.3.4.显示平台 (7)3.2 注意事项 (8)3.3 游戏流程图 (8)4.详细设计 (9)4.1游戏总体结构与代码 (9)结论 (20)参考文献 (21)致谢 (22)引言在人类的社会生活当中,游戏占有很大的比重,并且随着社会的发展而不断发展。

推箱子游戏代码

推箱子游戏代码

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>typedef struct winer{int x,y;struct winer *p;}winer;char status [20][20];char far *printScreen=(char far* )0xB8000000;void putoutChar(int y,int x,char ch,char fc,char bc);void printWall(int x, int y);void printBox(int x, int y);void printBoxDes(int x, int y);void printDestination(int x, int y);void printDestination1(int x,int y,winer **win,winer **pw); void printMan(int x, int y);void init();winer *initStep1();winer *initStep2();winer *initStep3();winer *initStep4();void moveBoxSpacetoSpace(int x ,int y, char a);void moveBoxDestoSpace(int x ,int y, char a) ;void moveBoxSpacetoDes(int x, int y, char a);void moveBoxDestoDes(int x, int y, char a);int judge(int x, int y);void move(int x, int y, char a);void reset(int i);void putoutChar(int y,int x,char ch,char fc,char bc){printScreen[(x*160)+(y<<1)+0]=ch;printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;}void printWall(int x,int y){putoutChar(y-1,x-1,219,GREEN,BLACK);status[x][y]='w';}void printBox(int x,int y){putoutChar(y-1,x-1,10,WHITE,BLACK);status[x][y]='b';}void printDestination1(int x,int y,winer **win,winer **pw) {putoutChar(y-1,x-1,003,YELLOW,BLACK); status[x][y]='m';if(*win==NULL){*win=*pw=qw=(winer* )malloc(sizeof(winer)); (*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;}else{qw=(winer* )malloc(sizeof(winer));qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;}}void printDestination(int x,int y){putoutChar(y-1,x-1,003,YELLOW,BLACK); status[x][y]='m';}void printMan(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}void printBoxDes(int x,int y){putoutChar(y-1,x-1,10,YELLOW,BLACK);status[x][y]='i';}void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)status[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(40,4);printf("Welcome to the box world!");gotoxy(40,6);printf("You can use up, down, left,");gotoxy(40,8);printf("right key to control it, or");gotoxy(40,10);gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("December , 2015");}winer *initStep1(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=5;y<=9;y++)printWall(x+4,y+10);for(y=5,x=2;x<=5;x++)printWall(x+4,y+10);for(y=9,x=2;x<=5;x++)printWall(x+4,y+10);for(y=1,x=3;x<=8;x++)printWall(x+4,y+10);for(x=3,y=3;x<=5;x++)printWall(x+4,y+10);for(x=5,y=8;x<=9;x++)printWall(x+4,y+10);for(x=7,y=4;x<=9;x++)printWall(x+4,y+10);for(x=9,y=5;y<=7;y++)printWall(x+4,y+10);for(x=8,y=2;y<=3;y++)printWall(x+4,y+10);printWall(5+4,4+10);printWall(5+4,7+10);printWall(3+4,2+10);printBox(3+4,6+10);printBox(3+4,7+10);printBox(4+4,7+10);printDestination1(4+4,2+10,&win,&pw); printDestination1(5+4,2+10,&win,&pw); printDestination1(6+4,2+10,&win,&pw); printMan(2+4,8+10);return win;}winer *initStep2(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=4;y<=7;y++)printWall(x+4,y+10);for(x=2,y=2;y<=4;y++)printWall(x+4,y+10);for(x=2,y=7;x<=4;x++)for(x=4,y=1;x<=8;x++)printWall(x+4,y+10);for(x=8,y=2;y<=8;y++)printWall(x+4,y+10);for(x=4,y=8;x<=8;x++)printWall(x+4,y+10);for(x=4,y=6;x<=5;x++)printWall(x+4,y+10);for(x=3,y=2;x<=4;x++)printWall(x+4,y+10);for(x=4,y=4;x<=5;x++)printWall(x+4,y+10);printWall(6+4,3+10);printBox(3+4,5+10);printBox(6+4,6+10);printBox(7+4,3+10);printDestination1(5+4,7+10,&win,&pw); printDestination1(6+4,7+10,&win,&pw); printDestination1(7+4,7+10,&win,&pw); printMan(2+4,6+10);return win;}winer *initStep3(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=2;y<=8;y++)printWall(x+4,y+10);for(x=2,y=2;x<=4;x++)printWall(x+4,y+10);for(x=4,y=1;y<=3;y++)printWall(x+4,y+10);for(x=5,y=1;x<=8;x++)printWall(x+4,y+10);for(x=8,y=2;y<=5;y++)printWall(x+4,y+10);for(x=5,y=5;x<=7;x++)printWall(x+4,y+10);for(x=7,y=6;y<=9;y++)printWall(x+4,y+10);for(x=3,y=9;x<=6;x++)printWall(x+4,y+10);for(x=3,y=6;y<=8;y++)printWall(x+4,y+10);printWall(2+4,8+10);printWall(5+4,7+10);printBox(6+4,3+10);printBox(4+4,4+10);printBox(5+4,6+10);printDestination1(2+4,5+10,&win,&pw); printDestination1(2+4,6+10,&win,&pw); printDestination1(2+4,7+10,&win,&pw); printMan(2+4,4+10);return win;winer *initStep4(){int x;int y;winer *win=NULL;winer *pw;for(x=1,y=1;y<=6;y++)printWall(x+4,y+10);for(x=2,y=7;y<=8;y++)printWall(x+4,y+10);for(x=2,y=1;x<=7;x++)printWall(x+4,y+10);for(x=7,y=2;y<=4;y++)printWall(x+4,y+10);for(x=6,y=4;y<=9;y++)printWall(x+4,y+10);for(x=3,y=9;x<=5;x++)printWall(x+4,y+10);for(x=3,y=3;y<=4;y++)printWall(x+4,y+10);printWall(3+4,8+10);printBox(3+4,5+10);printBox(4+4,4+10);printBox(4+4,6+10);printBox(5+4,5+10);printBox(5+4,3+10);printDestination1(3+4,7+10,&win,&pw);printDestination1(4+4,7+10,&win,&pw);printDestination1(5+4,7+10,&win,&pw);printDestination1(4+4,8+10,&win,&pw);printDestination1(5+4,8+10,&win,&pw);printMan(2+4,2+10);return win;}void moveBoxSpacetoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf("");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]=0;printf("");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;status[x][y-1]=0;printf("");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]=0;printf("");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxDestoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf("");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]='m';printf("");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]='m';printf("");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]='m';printf("");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxSpacetoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf("");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]=0;printf("");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]=0;printf("");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]=0;printf("");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}void moveBoxDestoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf("");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]='m';printf("");printBoxDes(x+2,y);printMan(x+1,y);break;case 'l':status[x][y-1]='m';printf("");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]='m';printf("");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}int judge(int x,int y){int i;switch(status[x][y]){case 0:i=1;break;case 'w':i=0;break;case 'b':i=2;break;case 'i':i=4;break;case 'm':i=3;break;default:break;}return i;}void move(int x,int y,char a) {switch(a){case 'u':if(!judge(x-1,y)){gotoxy(y,x);}else if(judge(x-1,y)==1||judge(x-1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x-1,y);break;}else{printf("");printMan(x-1,y);break;}}else if(judge(x-1,y)==2){if(judge(x-2,y)==1){moveBoxSpacetoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxSpacetoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}else if(judge(x-1,y)==4){if(judge(x-2,y)==1){moveBoxDestoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxDestoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}if(!judge(x+1,y)){gotoxy(y,x);break;}else if(judge(x+1,y)==1||judge(x+1,y)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x+1,y);break;}else{printf("");printMan(x+1,y);break;}}else if(judge(x+1,y)==2){if(judge(x+2,y)==1){moveBoxSpacetoSpace(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxSpacetoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}else if(judge(x+1,y)==4){if(judge(x+2,y)==1){moveBoxDestoSpace(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxDestoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}gotoxy(y,x);break;}case 'l':if(!judge(x,y-1)){gotoxy(y,x);break;}else if(judge(x,y-1)==1||judge(x,y-1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y-1);break;}else{printf("");printMan(x,y-1);break;}}else if(judge(x,y-1)==2){if(judge(x,y-2)==1){moveBoxSpacetoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxSpacetoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){if(judge(x,y-2)==1){moveBoxDestoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxDestoDes(x,y,'l');printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}case 'r':if(!judge(x,y+1)){gotoxy(y,x);break;}else if(judge(x,y+1)==1||judge(x,y+1)==3) {if(judge(x,y)==3){printDestination(x,y);printMan(x,y+1);break;}else{printf("");printMan(x,y+1);break;}}else if(judge(x,y+1)==2){if(judge(x,y+2)==1){moveBoxSpacetoSpace(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxSpacetoDes(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}elsegotoxy(y,x);break;}else if(judge(x,y+1)==4){if(judge(x,y+2)==1){moveBoxDestoSpace(x,y,'r');if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);else if(judge(x,y+2)==3){moveBoxDestoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y+1,x);}elsegotoxy(y,x);break;}default:break;}}void reset(int i){switch(i){case 0:init();initStep1();break;case 1:init();initStep2();break;case 2:init();initStep3();break;case 3:init();initStep4();break;default:break;}}void main(){int key;int x;int y;int s;int i=0;winer *win;winer *pw;_AL=3;_AH=0;geninterrupt(0x10);win=initStep1();do{_AH=3;geninterrupt(0x10);x=_DH+1;y=_DL+1;while(bioskey(1)==0);key=bioskey(0);switch(key){case 0x4800:move(x,y,'u');break;case 0x5000:move(x,y,'d');break;case 0x4b00:move(x,y,'l');break;case 0x4d00:move(x,y,'r');break;case 0x3920:reset(i);break;default:break;}s=0;pw=win;while(pw){if(status[pw->x][pw->y]=='m')s++;pw=pw->p;}if(s==0){free(win);gotoxy(25,2);printf("congratulate! You have done this step!"); getch();i++;switch(i){case 1:init();win=initStep2();break;case 2:init();win=initStep3();break;case 3:init();win=initStep4();case 4:gotoxy(15,21);printf("Congratulation! \n");gotoxy(15,23);printf("You have done all the steps, Welcome to play again!"); key=0x011b;getch();break;default:break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10);}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import java.util.*;
import java.io.*;
public class Main{
int r;//地图行数
int c;//地图列数
int begx, begy;//箱子开始坐标
int endx, endy;//目标坐标
int begsx, begsy;//人开始坐标
char map[][];//地图
int[] dx ={-1, 1, 0, 0};//人和箱子都有四个方向可移动
int[] dy ={0, 0, 1, -1};
char[] P ={'N', 'S', 'E', 'W'};//表示箱子向某个方向移动
char[] M ={'n', 's', 'e', 'w'};//表示人向某个方向移动
Node f=new Node(0,0,0,0,"");
Node g=new Node(0,0,0,0,"");
node1 F=new node1(0,0,"");
node1 G=new node1(0,0,"");
int mark[][];//标志数组,表示地图上某一位置mark[i][j]是否访问过。

public Main(char[][] map,int r,int c,int begx,int begy,int endx,int endy,int begsx,int begsy){
this.map=map;
this.r=r;
this.c=c;
this.begx=begx;
this.begy=begy;
this.endx=endx;
this.endy=endy;
this.begsx=begsx;
this.begsy=begsy;
mark=new int[r][c];
}
public boolean ok(int x,int y) {
if (x >= 0 && x < r && y >= 0 && y < c) return true;
return false;
}
public boolean SToB(int bx,int by,int ex, int ey) {//人到箱子BFS
int[][] Mark1= new int[r][c]; //标志数组,表示地图上某一位置Mark1[i][j]是否访问过。

Queue<node1> P = new LinkedList<node1>();
Mark1[bx][by] = 1;
Mark1[f.bx][f.by] = 1;
F.x = bx; F.y = by;
F.ans = "";
if (bx == ex && by == ey) return true;//到达目标
P.offer(new node1(F.x,F.y,F.ans));
while (!P.isEmpty()) {
F = P.poll();
for (int i = 0; i < 4; ++i) {//向四个方向扩展
G.x = F.x + dx[i];
G.y = F.y + dy[i];
if (!ok(G.x, G.y) || map[G.x][G.y] == '#') continue;
if (Mark1[G.x][G.y]==0) {//此点没有访问过
Mark1[G.x][G.y] = 1;//标记为已访问
G.ans = F.ans + M[i];
if (G.x == ex && G.y == ey) {
F = G;
return true;
}
P.add(new node1(G.x,G.y,G.ans));
}
}
}
return false;
}
public boolean bfs() {//箱子向目标bfs
Queue<Node> Q = new LinkedList<Node>();
f.bx = begx; f.by = begy;//f:人与箱子所在的位置
f.px = begsx; f.py = begsy;
f.ans = "";
mark[begx][begy] = 1;
Q.offer(new Node(f.bx,f.by,f.px,f.py,f.ans));
while (!Q.isEmpty()) {
f = Q.poll();//出队列
for (int i = 0; i < 4; ++i) {//检查f的所有邻接点,向四个方向扩展int newx = f.bx + dx[i];
int newy = f.by + dy[i];//箱子前一位置
int tx = f.bx - dx[i];
int ty = f.by - dy[i];//箱子后一位置
if (!ok(newx, newy) || map[newx][newy] == '#' || !ok(tx, ty)
|| map[tx][ty] == '#' || mark[newx][newy]==1) continue;
if (SToB(f.px, f.py, tx, ty)) {//检查人能否来
g.bx = newx; g.by = newy;
g.px = f.bx; g.py = f.by;
g.ans = f.ans + F.ans + P[i];
if (g.bx == endx && g.by == endy)
{
return true;
}
mark[newx][newy] = 1;
Q.add(new Node(g.bx,g.by,g.px,g.py,g.ans));
}
}
}
return false;
}
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int r=0;
int c=0;
String s="";
int begx=0;
int begy=0;
int endx=0;
int endy=0;
int begsx=0;
int begsy=0;
char[][] map=null;
int t=1;
while(in.hasNext()){
r=in.nextInt();
c=in.nextInt();
if(r==0&&c==0) break;
map=new char[r][c];
for(int i = 0; i <r; i++){
s=in.next();
for(int j=0;j< c;j++){
map[i][j]=s.charAt(j);
if (map[i][j] == 'B') { begx = i; begy = j;}//箱子开始坐标
if (map[i][j] == 'T') { endx = i; endy = j;}//目标坐标
if (map[i][j] == 'S') { begsx = i;begsy = j;}//人开始坐标
}
}
Main ma=new Main(map,r,c,begx,begy,endx,endy,begsx,begsy);
if (ma.bfs()) {
System.out.println("Maze #"+t++);
System.out.println(ma.g.ans);
System.out.println();
}
else{
System.out.println("Maze #"+t++);
System.out.println("Impossible.");
System.out.println();
}
}
}
}
class node1{
int x;
int y;
String ans;
public node1(int x,int y,String ans){
this.x=x;
this.y=y;
this.ans=ans;
}
}
class Node{
int bx;
int by;
int px;
int py;
String ans;
public Node(int bx,int by,int px,int py,String ans) {
this.bx=bx;
this.by=by;
this.px=px;
this.py=py;
this.ans=ans;
}
}。

相关文档
最新文档