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;
        }
    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/601671.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

智能BI平台(后端)-- 项目介绍

文章目录 项目介绍需求分析基础架构图优化架构图技术栈 开个新坑&#xff0c;预计时间不会很长 项目介绍 BI商业智能&#xff1a;数据可视化&#xff0c;报表可视化系统 主流BI&#xff1a;帆软BI&#xff0c;小马BI&#xff0c;微软 Power BI 传统BI&#xff1a;[查看传统 BI…

精益生产咨询公司在企业转型中发挥的作用有哪些?

在全球化竞争日益激烈的今天&#xff0c;企业转型已成为许多组织求生存、谋发展的必经之路。而在这条道路上&#xff0c;精益生产咨询公司的作用愈发凸显&#xff0c;它们如同企业转型的得力助手&#xff0c;帮助企业在复杂的商业环境中找到新的增长点&#xff0c;实现更高效、…

I forgot my Plex Account PIN; how can I reset it? How can I change my PIN?

If you’ve set a PIN on your Plex account, it’s possible to reset or remove that PIN. Related Page: Plex Home Regular Plex Account If you know the current PIN If the current PIN is known, then simply edit the current PIN on the Settings > Users &…

Cisco NX-OS System Software - ACI 16.0(5h)

Cisco NX-OS System Software - ACI 16.0(5h) 适用于 ACI 模式下的 Cisco Nexus 9000 系列交换机 请访问原文链接&#xff1a;Cisco NX-OS System Software - ACI 16.0(5h)&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org Cis…

从零开始的软件测试学习之旅(七)接口测试流程及原则案例

接口测试三要素及案例 接口测试介绍接口预定义接口测试的主要作用测试接口流程如下接口测试三要素接口测试分类RESTful架构风格RESTful架构三要素要素一要素二要素三 RESTful架构风格实现restful架构案例接口测试流程接口测试原则功能测试自动化测性能测试 复习复盘 接口测试介…

AI视频教程下载:学会用AI创作文本图片音频视频

在不断发展的科技领域&#xff0c;人工智能 (AI) 是毋庸置疑的冠军&#xff0c;它是一种不断创新的力量&#xff0c;在我们的生活中扮演着越来越重要的角色。随着 2023 年的到来&#xff0c;我们诚挚地欢迎您加入人工智能精通课程的大门。 这不仅仅是一个课程&#xff0c;它专为…

外网访问内网电脑?

随着互联网的发展&#xff0c;越来越多的人需要在不同地区间进行远程访问和组网。而在复杂网络环境下&#xff0c;外网访问内网电脑常常成为一个令人头痛的问题。现在有一款名为【天联】的组网产品却解决了这个难题。 【天联】组网是由北京金万维科技有限公司自主研发的一款内网…

【管理篇】管理三步曲:任务执行(三)

目录标题 多任务并行如何应对?如何确保项目有效的执行项目执行过程中常见的问题1、目标不明确2、责任不明确3、流程不健全4、沟通不到位 如何有效执行任务 如何让流程机制有效的执行 研究任务管理&#xff0c;就是为了把事情做出来&#xff0c;产出实实在在的业绩和成果&#…

京东物流:表格技术在物流行业的敏捷应用实践

“物流大促期间&#xff0c;在出库单量积压的场景下&#xff0c;不同仓的生产操作人员需要在统一数据源的基础上进行基于自身仓情况的个性化查询分析&#xff0c;从而能够实时监控客单情况&#xff0c;防止积压。要想实现这样的功能&#xff0c;对数据分析平台的要求就非常高。…

通过 Java 操作 redis -- 基本通用命令

目录 使用 String 类型的 get 和 set 方法 使用通用命令 exists &#xff0c;del 使用通用命令 keys 使用通用命令 expire,ttl 使用通用命令 type 要想通过 Java 操作 redis&#xff0c;首先要连接上 redis 服务器&#xff0c;推荐看通过 Java 操作 redis -- 连接 redis 关…

深入探索van Emde Boas树:原理、操作与C语言实现

van Emde Boas (vEB) 树是一种高效的数据结构&#xff0c;用于处理整数集合。它是由荷兰计算机科学家Jan van Emde Boas在1977年提出的。vEB树在处理整数集合的查找、插入、删除和迭代操作时&#xff0c;能够以接近最优的时间复杂度运行。vEB树特别适合于那些元素数量在某个较小…

CSS引用

CSS定义 层叠样式表&#xff1a;&#xff08;Cascading Style Sheets,缩写为css&#xff09;,是一种样式表语言&#xff0c;用来描述HTML文档的呈现&#xff08;美化内容&#xff09; 书写位置&#xff1a;title标签下方添加style双标签&#xff0c;style标签里写入CSS代码 在s…

Spring Security 入门1

1. 概述 基本上&#xff0c;在所有的开发的系统中&#xff0c;都必须做认证(authentication)和授权(authorization)&#xff0c;以保证系统的安全性。 authentication [ɔ,θɛntɪ’keʃən] 认证 authorization [,ɔθərɪ’zeʃən] 授权 以论坛举例子&#xff1a; 【认证…

Covalent引入五个新网络运营商,提升去中心化特性和数据安全性

为了进一步扩大运营商基础以并践行去中心化网络基础设施的宗旨&#xff0c;Covalent Network&#xff08;CQT&#xff09;在网络中引入了五个新的区块样本生产者&#xff08;BSPs&#xff09;角色。该举措不仅重申了 Covalent Network&#xff08;CQT&#xff09;对社区驱动协议…

Dynamics 365入门:轻松创建您的首个应用

大家好&#xff0c;我是嘻嘻一个从事软件开发的老兵&#xff0c;需要交流可以加VX:lilinsans_weixin, 今天接上篇&#xff1a; 注册 Dynamics 365后&#xff0c;如果创建自己的第一个应用 注册完试用版可以以试用30天。今天我就分享一下如何创建第一个应用 1、Dynamics 36…

##08 数据加载与预处理:PyTorch的心脏

文章目录 前言深入理解torch.utils.data数据集(Dataset)数据加载器(DataLoader) 实战演练&#xff1a;创建自定义数据集数据转换(Transform)数据加载总结 前言 在深度学习的宇宙中&#xff0c;数据是燃料&#xff0c;模型是发动机。而在PyTorch的世界中&#xff0c;torch.util…

制作微信小程序的常见问题,2024新手小白入门必看

在当今高度数字化的世界&#xff0c;移动应用已经在日常生活和工作中不可或缺。在众多的应用程序中&#xff0c;有一个平台在中国市场上脱颖而出&#xff0c;占有绝对的一席之地——微信。 虽然被称为世界上最流行的消息和社交媒体平台之一&#xff0c;但微信提供了一个让其能…

计算机网络5——运输层1概述与UDP

文章目录 一、协议概述1、进程之间通信2、运输层的两个主要协议3、运输层的端口1&#xff09;服务器端使用的端口号2&#xff09;客户端使用的端口号 二、用户数据报协议 UDP1、UDP 概述2、案例分析3、UDP的首部格式 一、协议概述 1、进程之间通信 从通信和信息处理的角度看&…

邮件群发还能用吗

邮件群发仍然可以使用。不过&#xff0c;在进行邮件群发时&#xff0c;可能会遇到一些问题&#xff0c;如选择合适的邮件群发软件、应对垃圾邮件过滤器的挑战、管理收件人列表、邮件内容的个性化和定制、邮件投递的时间管理以及避免被列入黑名单等。 为了优化邮件群发的效果&a…

微信小程序知识点归纳(一)

前言&#xff1a;适用于有一定基础的前端开发同学&#xff0c;完成从网页开发到小程序开发的知识转换。 先立框架&#xff0c;后砌墙壁 回顾&#xff1a;了解微信小程序开发流程-CSDN博客 初始页面结构&#xff0c;三部分pages、utils、配置&#xff0c;分别存放页面、工具类…
最新文章