java 推箱子

说明:刚入门的时候面试,有个老师傅说,你们喜欢打游戏,让你们写个简单的推箱子,能写出来就过

我说这多简单

结果说要用枚举类,数组来写

写得一踏糊涂,最后没通过

如今工作两年了,忽然想起来了这个事情,花费一点时间写完,发现确实很有收获,希望大家也能有所收获,坚持不懈在这条道路上越走越远



import java.util.Arrays;
import java.util.Scanner;

public class BoxStart {
    private static int mapX = 9;
    private static int mapY = 9;
    private static String[][] map = new String[mapX][mapY];


    public static void initMap(){
        for (int i = 0; i < map.length; i++) {
            Arrays.fill(map[i], Shape.road.getName());
        }
    }


    public static void printMap(){
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println("");
        }
    }


    public static void changeMap(int x,int y,String shape){
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(i==x && y==j){
                    map[i][j] = shape;
                }
            }
        }
    }


    public static Boolean finishStatus(){
        boolean status = true;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if (map[i][j].equals(Shape.WhiteBox.getName())) {
                    status = false;
                    break;
                }
            }
        }
        return !status;
    }


    public static void getRemoveAfter(String[] arr,int x , int y,boolean changeX,boolean changeY){
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(changeX && i == x){
                    map[i][j] = arr[j];
                }
                if(changeY && j == y){
                    map[i][j] = arr[i];
                }
            }
        }

    }

    public static String[] recursionArr(String[] arr,int peopleX,int x){
        for (int i = x; i < arr.length; i++) {
            if(i == x){
                if(i == arr.length-1){
                    return arr;
                }else {
                    String next = arr[i+1];
                    if(next.equals(Shape.road.getName())){
                        for (int m = x; m >=peopleX; m--){
                            arr[m+1] = arr[m];
                            if(m == peopleX){
                                arr[peopleX] = Shape.road.getName();
                            }
                        }
                        return arr;
                    }else if(next.equals(Shape.BlackBox.getName())){
                        if(x>peopleX) {
                            for (int m = x; m >= peopleX; m--) {
                                arr[m + 1] = arr[m];
                                if (m == peopleX) {
                                    arr[peopleX] = Shape.road.getName();
                                    arr[i + 1] = Shape.BlackBox.getName();
                                }
                            }
                        }
//                        System.out.println(arr);
                        return arr;
                    }else if(next.equals(Shape.WhiteBox.getName())){
                        return recursionArr(arr,peopleX,i+1);
                    }
                }
            }
        }
        return arr;
    }

    public static int[] getPeoplePosition(){
        int[] people = new int[2];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(map[i][j].equals(Shape.People.getName())){
                    people[0] = i;
                    people[1] = j;
                }
            }
        }
        return people;
    }

    public static void reverseArray(String[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            // 交换元素
            String temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            // 移动指针
            left++;
            right--;
        }
    }

    public static void upMove(){
        int[] position = getPeoplePosition();
        int peopleX = position[0];
        int peopleY = position[1];
        String [] arr = new String[mapX];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(j == peopleY){
                    arr[mapX-i-1] = map[i][j];
                }
            }
        }
        String[] res = recursionArr(arr,mapX-peopleX-1,mapX-peopleX-1);

        reverseArray(res);

        getRemoveAfter(res,peopleX,peopleY,false,true);
    }

    public static void downMove(){
        int[] position = getPeoplePosition();
        int peopleX = position[0];
        int peopleY = position[1];
        String [] arr = new String[mapX];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(j == peopleY){
                    arr[i] = map[i][j];
                }
            }
        }
        String[] res = recursionArr(arr,peopleX,peopleX);

//        reverseArray(res);

        getRemoveAfter(res,peopleX,peopleY,false,true);
    }

    public static void leftMove(){
        int[] position = getPeoplePosition();
        int peopleX = position[0];
        int peopleY = position[1];
        String [] arr = new String[mapY];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(i == peopleX){
                    arr[mapY-j-1] = map[i][j];
                }
            }
        }
        String[] res = recursionArr(arr,mapY-peopleY-1,mapY-peopleY-1);

        reverseArray(res);

        getRemoveAfter(res,peopleX,peopleY,true,false);
    }

    public static void rightMove(){
        int[] position = getPeoplePosition();
        int peopleX = position[0];
        int peopleY = position[1];
        String [] arr = new String[mapY];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if(i == peopleX){
                    arr[j] = map[i][j];
                }
            }
        }
        String[] res = recursionArr(arr,peopleY,peopleY);

//        reverseArray(res);

        getRemoveAfter(res,peopleX,peopleY,true,false);
    }

    public static void main(String[] args) {
        initMap();
        changeMap(5,5,Shape.People.getName());
        changeMap(7,1,Shape.WhiteBox.getName());
        changeMap(1,5,Shape.BlackBox.getName());
        printMap();
        while (finishStatus()){
            System.out.println("---------input w/a/s/d----------");
            Scanner scanner = new Scanner(System.in);
            String controls = scanner.next();
            if("w".equals(controls)){
                upMove();
            }
            if("a".equals(controls)){
                leftMove();
            }
            if("s".equals(controls)){
                downMove();
            }
            if("d".equals(controls)){
                rightMove();
            }

            printMap();
        }
        System.out.println("--------------------------");
        System.out.println("---------success----------");
        System.out.println("--------------------------");
    }

    public enum Shape {
        road(1, "*"),
        WhiteBox(2, "□"),
        BlackBox(3, "■"),
        People(4, "Ꙫ");


        private int code;
        private String name;

        Shape(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃鱼人士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值