小游戏之推箱子

warning: 这篇文章距离上次修改已过1357天,其中的内容可能已经有所变动。

这次是一个推箱子小游戏,因为 java 限制,不能直接获取按键响应,只能使用输入wasd的方式来控制移动。

基本上和走迷宫差不对,只不过逻辑上增加了对箱子位置的判断。

↓简陋的界面


演示视频


程序流程

推箱子流程图.jpg推箱子流程图.jpg


程序代码

import java.util.HashMap;
import java.util.Scanner;

public class BoxMan {
    private static char[][] map = {
            {' ', ' ',' ', '#', '#', '#', ' ', ' ', ' ',},
            {' ', ' ',' ', '#', '*', '#', ' ', ' ', ' ',},
            {' ',' ', ' ', '#', ' ', '#', ' ', ' ', ' ',},
            {' ', ' ',' ', '#', 'O', '#', '#', '#', '#',},
            {' ',' ', ' ', '#', 'M', 'O', ' ', '*', '#',},
            {'#','#', '#', '#', 'O', '#', '#', '#', '#',},
            {'#', '*', ' ','O', ' ', '#', ' ', ' ', ' ',},
            {'#','#', '#', '#', ' ', '#', ' ', ' ', ' ',},
            {' ',' ', ' ', '#', '*', '#', ' ', ' ', ' ',},
            {' ',' ', ' ', '#', '#', '#', ' ', ' ', ' ',},
    };
    //存储一些位置数据 格式 名称,位置(x,y)
    private static HashMap<String, String> coordinateMap = new HashMap<String, String>();
    private static boolean isGameEnd =false;

    public static void main(String[] args) {
        while (true){
            Scanner scanner=new Scanner(System.in);
            printfMap(map);
            findCoordinate(map);
            printfCoordinate();

            if (isGameEnd){
                System.out.println("恭喜,成功过关!");
                break;
            }else {
                doMove(scanner.nextLine());
            }
        }

    }
    //人物移动
    private static void doMove(String move){
        String[] manCoordinate = coordinateMap.get("MAN").split(",");
        int manX=Integer.valueOf(manCoordinate[0]);
        int manY=Integer.valueOf(manCoordinate[1]);


        //上
        if("w".equals(move)||"W".equals(move)){
            //判断是否是墙,或已经到位的箱子
            if('#'!=map[manX-1][manY]&&'@'!=map[manX-1][manY]){
                //前进方向上有箱子?有推箱子
                if('O'==map[manX-1][manY]){
                    //箱子前面是墙?不是推箱子
                    if('#'!=map[manX-2][manY]){
                        //箱子前面是终点?推箱子,并改变箱子的样子
                        if('*'==map[manX-2][manY]){
                            map[manX][manY]=' ';
                            map[manX-1][manY]='M';
                            map[manX-2][manY]='@';
                        }else{
                            map[manX][manY]=' ';
                            map[manX-1][manY]='M';
                            map[manX-2][manY]='O';
                        }
                    }
                }else{
                    map[manX][manY]=' ';
                    map[manX-1][manY]='M';
                }
            }
        }
        //下
        if("s".equals(move)||"S".equals(move)){
            if('#'!=map[manX+1][manY]&&'@'!=map[manX+1][manY]){
                if('O'==map[manX+1][manY]){
                    if('#'!=map[manX+2][manY]){
                        if('*'==map[manX+2][manY]){
                            map[manX][manY]=' ';
                            map[manX+1][manY]='M';
                            map[manX+2][manY]='@';
                        }else{
                            map[manX][manY]=' ';
                            map[manX+1][manY]='M';
                            map[manX+2][manY]='O';
                        }
                    }
                }else{
                    map[manX][manY]=' ';
                    map[manX+1][manY]='M';
                }
            }
        }
        //左
        if("a".equals(move)||"A".equals(move)){
            if('#'!=map[manX][manY-1]&&'@'!=map[manX][manY-1]){
                if('O'==map[manX][manY-1]){
                    if('#'!=map[manX][manY-2]){
                        if('*'==map[manX][manY-2]){
                            map[manX][manY]=' ';
                            map[manX][manY-1]='M';
                            map[manX][manY-2]='@';
                        }else{
                            map[manX][manY]=' ';
                            map[manX][manY-1]='M';
                            map[manX][manY-2]='O';
                        }
                    }
                }else{
                    map[manX][manY]=' ';
                    map[manX][manY-1]='M';
                }
            }
        }
        //右
        if("d".equals(move)||"D".equals(move)){
            if('#'!=map[manX][manY+1]&&'@'!=map[manX][manY+1]){
                if('O'==map[manX][manY+1]){
                    if('#'!=map[manX][manY+2]){
                        if('*'==map[manX][manY+2]){
                            map[manX][manY]=' ';
                            map[manX][manY+1]='M';
                            map[manX][manY+2]='@';
                        }else{
                            map[manX][manY]=' ';
                            map[manX][manY+1]='M';
                            map[manX][manY+2]='O';
                        }
                    }
                }else{
                    map[manX][manY]=' ';
                    map[manX][manY+1]='M';
                }
            }
        }
    }

    //自动寻找关键点坐标
    private static void findCoordinate(char[][] map) {
        int countMan = 0;
        int countBox = 0;
        int countEnd = 0;
        coordinateMap.clear();
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if ('M' == map[i][j]) {
                    countMan++;
                    coordinateMap.put("MAN", i + "," + j);
                }
                if ('O' == map[i][j]) {
                    countBox++;
                    coordinateMap.put("BOX" + countBox, i + "," + j);
                }
                if ('*' == map[i][j]) {
                    countEnd++;
                    coordinateMap.put("EMD" + countEnd, i + "," + j);
                }
            }
        }
        if(countBox!=countEnd){
            System.err.println("箱子或终点数量有误,请检查关卡地图!");
            System.exit(1);
        }
        if(countMan!=1){
            System.err.println("人物位置错误,请检查关卡地图!");
            System.exit(1);
        }
        if(countBox==0){
            isGameEnd =true;
        }

    }
    //打印寻找的坐标
    private static void printfCoordinate() {
        for (String key : coordinateMap.keySet()) {
            System.out.print("| "+key + ": " + coordinateMap.get(key)+" | ");
        }
        System.out.println();
    }
    //打印地图
    private static void printfMap(char[][] map) {
        for (char[] chars : map) {
            for (char c : chars) {
                System.out.print(c);
            }
            System.out.println();
        }
    }
}
最后修改于:2020年08月05日 16:48
评论已关闭