假想你在火星探索团队中负责软件开发。现在你要给登陆火星的探索小车编写控制程序,根据地球发送的控制指令来控制火星车的行动。
火星车收到的指令分为:
初始化信息:火星车的降落地点(x, y)和朝向(N, S, E, W)信息;
移动指令:火星车可以前进(M),一次移动X格;
移动指令:火星车可以前进(B),一次移动X格;
转向指令:火星车可以左转90度(L)或右转90度(R)。
由于地球和火星之间的距离很远,指令必须批量发送,火星车执行完整批指令之后,再回报自己所在的位置坐标和朝向。

package com.thoughtworks.b_mars_rover;
import java.util.ArrayList;
import java.util.List;public class Run {public static void main(String[] args) {// Instruct (指令类型,角度,移动距离)List<Instruct> instructs = new ArrayList<>();instructs.add(new Instruct(InstructType.MOVE, 0, 4));instructs.add(new Instruct(InstructType.BACK, 0, 4));instructs.add(new Instruct(InstructType.TURN_RIGHT, 37, 0));instructs.add(new Instruct(InstructType.MOVE, 0, 5));instructs.add(new Instruct(InstructType.BACK, 0, 5));instructs.add(new Instruct(InstructType.TURN_LEFT, 164, 5));instructs.add(new Instruct(InstructType.MOVE, 0, 5));instructs.add(new Instruct(InstructType.BACK, 0, 5));//初始化小车位置 x,y,朝向,顺时针角度MarsRover marsRover = new MarsRover(0, 0, "N", 0);System.out.println("初始化:小车朝向:"+marsRover.getFaceTo()+"----小车角度:"+marsRover.getAngle()+"---小车X轴位置:"+marsRover.getX()+"---小车Y轴距离:"+marsRover.getY());for (Instruct instruct : instructs) {if (instruct.getInstructType() == InstructType.BACK||instruct.getInstructType() == InstructType.MOVE) {marsRover.move(instruct);} else {marsRover.wheel(instruct);}System.out.println("小车朝向:"+marsRover.getFaceTo()+"----小车角度:"+marsRover.getAngle()+"---小车X轴位置:"+marsRover.getX()+"---小车Y轴距离:"+marsRover.getY());}}
}package com.thoughtworks.b_mars_rover;import com.thoughtworks.b_mars_rover.move.MoveEnum;
import com.thoughtworks.b_mars_rover.move.MoveRule;
import com.thoughtworks.b_mars_rover.move.MoveRuleDispatchCentre;
import lombok.Data;/*** 探测车x,y,朝向,角度*/
@Data
public class MarsRover {private final static String allDirection = "NESW";private double x;private double y;private String faceTo;/*** 角度为当前方向的顺时针角度*/private double angle;public MarsRover(double x, double y, String faceTo, double angle) {this.x = x;this.y = y;this.faceTo = faceTo;this.angle = angle;}//小车转弯public void wheel(Instruct wheelInstruct) {//增大一圈,避免为负double newAngel = this.getAngle() + wheelInstruct.getAngle();/*** 取探测车朝向* >=0时取90的余数与探险车的索引相加* <0时取90的余数与探险车的索引相减*/this.faceTo = newAngel >= 0 ? String.valueOf(allDirection.charAt((allDirection.indexOf(this.faceTo) + (int) ((newAngel + 360) / 90)) % 4)) :String.valueOf(allDirection.charAt(((allDirection.indexOf(this.faceTo) + (int) (newAngel + 360) / 90) + 4) % 4));this.angle = (newAngel + 360) % 90;}// 小车移动public void move(Instruct instruct) {MoveRule moveRule = null;if (instruct.getInstructType() == InstructType.MOVE) {moveRule = MoveRuleDispatchCentre.getMoveRule(MoveEnum.getInstance(this.getFaceTo()));} else {moveRule = MoveRuleDispatchCentre.getMoveRule(MoveEnum.BACK);}instruct.xyMoveDistance(this);moveRule.move(instruct, this);}
}package com.thoughtworks.b_mars_rover;import lombok.Getter;/*** 转向枚举*/
public enum InstructType {TURN_LEFT("L"), TURN_RIGHT("R"), MOVE("M"), BACK("B");@Getterprivate String InstructType;InstructType(String instructType) {InstructType = instructType;}public static InstructType getInstance(String instructType) {if (TURN_LEFT.getInstructType().equals(instructType)) {return TURN_LEFT;}return TURN_RIGHT;}
}package com.thoughtworks.b_mars_rover;import com.thoughtworks.b_mars_rover.move.MoveEnum;
import lombok.Data;/*** 指令对象*/
@Data
public class Instruct {private InstructType instructType;private double angle;private double moveDistance;private double xMoveDistance;private double yMoveDistance;public double getRealAngel(InstructType instructType, double angle) {/*** 转向大于360度,取余数*/angle = Math.abs(angle) >= 360 ? angle % 360 : angle;/*** 左转为负数,右转为正数*/return instructType == InstructType.TURN_LEFT ? -angle : angle;}/*** 移动小车之前必须先计算小车位移的X和Y距离** @param marsRover*/public void xyMoveDistance(MarsRover marsRover) {if (MoveEnum.getInstance(marsRover.getFaceTo()) == MoveEnum.EAST || MoveEnum.getInstance(marsRover.getFaceTo()) == MoveEnum.WEST) {moveToEastAndWest(marsRover);} else {moveToNorthAndSorth(marsRover);}}private void moveToNorthAndSorth(MarsRover marsRover) {this.setYMoveDistance(Math.cos(Math.toRadians(marsRover.getAngle())) * this.getMoveDistance());this.setXMoveDistance(Math.sin(Math.toRadians(marsRover.getAngle())) * this.getMoveDistance());}private void moveToEastAndWest(MarsRover marsRover) {this.setXMoveDistance(Math.cos(Math.toRadians(marsRover.getAngle())) * this.getMoveDistance());this.setYMoveDistance(Math.sin(Math.toRadians(marsRover.getAngle())) * this.getMoveDistance());}public Instruct(InstructType instructType, double angle, double moveDistance) {this.instructType = instructType;this.angle = getRealAngel(instructType, angle);this.moveDistance = moveDistance;}
}
package com.thoughtworks.b_mars_rover.move;import com.thoughtworks.b_mars_rover.Instruct;
import com.thoughtworks.b_mars_rover.InstructType;
import com.thoughtworks.b_mars_rover.MarsRover;/*** 后退*/
public class BackMove implements MoveRule {@Overridepublic void move(Instruct moveInstruct, MarsRover marsRover) {moveInstruct.setXMoveDistance(0 - moveInstruct.getXMoveDistance());moveInstruct.setYMoveDistance(0 - moveInstruct.getYMoveDistance());moveInstruct.setInstructType(InstructType.MOVE);MoveRule moveRule = MoveRuleDispatchCentre.getMoveRule(MoveEnum.getInstance(marsRover.getFaceTo()));moveRule.move(moveInstruct, marsRover);}
}package com.thoughtworks.b_mars_rover.move;import com.thoughtworks.b_mars_rover.Instruct;
import com.thoughtworks.b_mars_rover.MarsRover;/*** 小车朝向E到S*/
public class EastMove implements MoveRule {public static void main(String[] args) {System.out.println(Math.cos(Math.toRadians(37)) * 5);}@Overridepublic void move(Instruct eastInstruct, MarsRover marsRover) {marsRover.setX(marsRover.getX()+eastInstruct.getXMoveDistance());marsRover.setY(marsRover.getY()-eastInstruct.getYMoveDistance());}
}package com.thoughtworks.b_mars_rover.move;/*** 移动规则枚举*/
public enum MoveEnum {NORTH("N"), WEST("E"), SORTH("S"), EAST("W"), BACK("B");private String faceTo;MoveEnum(String faceTo) {this.faceTo = faceTo;}public static MoveEnum getInstance(String faceTo) {switch (faceTo) {case "N":return NORTH;case "E":return EAST;case "S":return SORTH;case "W":return WEST;default: {return BACK;}}}
}package com.thoughtworks.b_mars_rover.move;import com.thoughtworks.b_mars_rover.Instruct;
import com.thoughtworks.b_mars_rover.MarsRover;/*** 移动规则父类*/
public interface MoveRule {void move(Instruct moveInstruct, MarsRover marsRover);
}package com.thoughtworks.b_mars_rover.move;import java.util.HashMap;
import java.util.Map;public class MoveRuleDispatchCentre {private static Map<MoveEnum, MoveRule> moveTypes = new HashMap<>();static {moveTypes.put(MoveEnum.NORTH, new NorthMove());moveTypes.put(MoveEnum.WEST, new EastMove());moveTypes.put(MoveEnum.SORTH, new SouthMove());moveTypes.put(MoveEnum.EAST, new EastMove());moveTypes.put(MoveEnum.BACK, new BackMove());}public static MoveRule getMoveRule(MoveEnum moveEnum) {return moveTypes.get(moveEnum);}
}package com.thoughtworks.b_mars_rover.move;import com.thoughtworks.b_mars_rover.Instruct;
import com.thoughtworks.b_mars_rover.MarsRover;/*** 小车朝向N到E*/
public class NorthMove implements MoveRule {@Overridepublic void move(Instruct northInstruct, MarsRover marsRover) {marsRover.setX(marsRover.getX()+northInstruct.getXMoveDistance());marsRover.setY(marsRover.getY()+northInstruct.getYMoveDistance());}
}package com.thoughtworks.b_mars_rover.move;import com.thoughtworks.b_mars_rover.Instruct;
import com.thoughtworks.b_mars_rover.MarsRover;/*** 小车朝向S到W*/
public class SouthMove implements MoveRule {@Overridepublic void move(Instruct sorthInstruct, MarsRover marsRover) {marsRover.setX(marsRover.getX()-sorthInstruct.getXMoveDistance());marsRover.setY(marsRover.getY()-sorthInstruct.getYMoveDistance());}
}package com.thoughtworks.b_mars_rover.move;import com.thoughtworks.b_mars_rover.Instruct;
import com.thoughtworks.b_mars_rover.MarsRover;/*** 小车朝向W到N*/
public class WestMove implements MoveRule {@Overridepublic void move(Instruct westInstruct, MarsRover marsRover) {marsRover.setX(marsRover.getX()-westInstruct.getXMoveDistance());marsRover.setY(marsRover.getY()+westInstruct.getYMoveDistance());}
}

经典Java练习题 Mars Rover相关推荐

  1. 2016计算机二级java_2016计算机二级JAVA练习题及答案

    2016计算机二级JAVA练习题及答案 21.下列选项中,不能输出100个整数的.是( ). A.for(int i=0;i<100;i++) System.out.println(i); B. ...

  2. 贵港java_贵港人才网:经典java笔试题及答案分享

    经典java笔试题及谜底,共享与参考: 1.Anonymous Inner Class (匿名里面类) 是否能够extends(秉承)别的类,是否能够implements(完成)interface(接 ...

  3. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  4. 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】...

    [109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...

  5. 【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

    [054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m ...

  6. 经典java程序员的面试题及答案

    今天动力节点java培训机构小编为大家分享"经典java程序员的面试题及答案",希望通过此文能够帮助到正在找工作或是即将毕业的"你",下面就随小编一起看看经典j ...

  7. 20199计算机二级java答案_计算机二级Java练习题-2019.9

    是不是急于做大量的计算机等级考试题库,却因测试结果不尽人意而心慌不安?不要急!考无忧小编为大家准备了一些二级Java练习题,希望能帮助大家高效复习,轻松通关! 1.下列叙述中正确的是(). A.栈是& ...

  8. java char 计算_经典Java面试题之Java中Char类型的运算

    经典Java面试题之Java中Char类型的运算 char在java中称为"字符型",占2个字节.本文是百分网小编搜索整理的关于经典Java面试题之Java中Char类型的运算,有 ...

  9. 二级计算机java2017级_2017计算机等级二级考试java练习题及答案

    2017计算机等级二级考试java练习题及答案 JAVA是计算机二级考试的科玛之一,你知道计算机二级考试JAVA科目都考哪些知识吗?为了方便考生备考计算机二级考试JAVA 科目.下面是小编为大家带来的 ...

  10. 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】...

    [129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

最新文章

  1. c语言中的字节和元素,C语言指向数组元素的指针
  2. html5通html5通,HTML5 history详解
  3. deepfm代码参考
  4. WebStorm 2019.3.1安装教程
  5. PHP md5和js md5保持一致的方法
  6. Qt工作笔记-Qt生成dll或so,并且调用(含Liunx端与Windows端)
  7. JAVA线程并发数量控制_Java并发工具类(三):控制并发线程数的Semaphore
  8. 图书管理系统python代码课程设计报告_python代码实现图书管理系统
  9. 更轻松的获取APK文件安装时间
  10. 同一个字体,不同平台完全不同
  11. HTML-零基础入门
  12. 某内容管理系统最最最详细的代码审计
  13. node--压缩文件夹
  14. 动态搜索图书:可以按书名、作者、出版社以及价格范围进行搜索。(在IDEA中mybatis)
  15. keras读取h5文件load_weights、load代码操作
  16. 【建议收藏】2020年中高级Android大厂面试秘籍,为你保驾护航金三银四,直通大厂(Android高级篇上)...
  17. 云栖大会·南京峰会落下帷幕,阿里云都干了些什么?
  18. SNA---主要统计指标解释(2)
  19. 小学六年级走进计算机ppt,小学信息技术《走进计算机》PPT课件.ppt
  20. MyCat分库分表入门

热门文章

  1. win10下ant下载安装配置记录
  2. The summary of Java
  3. linux的目录以及作用
  4. win10误删的注册表能还原吗_win10注册表删错了怎么办_win10注册表删错东西如何恢复-win7之家...
  5. Java游戏聊斋聂小倩_【聊斋故事汇】之聂小倩(篇一)
  6. undo歌词中文音译_Undo - Sanna nielsen帮我看看这歌词翻译对么
  7. 干货 | 产品助理入门攻略(一枚入行3年的PM内心独白)
  8. 二维列表的转置(行列互换,首行变首列,尾行变尾列)
  9. X509证书信任管理器类的详解
  10. Intel处理器概述