一个demo
package com.entity; /*2015-7-18*/
public class Rover {
private CurrentPosition position; public Rover(CurrentPosition position) {
super();
this.position = position;
} public String doTask(String input) {
for (int i = 0; i < input.length(); i++) {
char singleCommand = input.charAt(i);
this.position.doTask(Command.valueOf(String.valueOf(singleCommand)));
} return this.position.toString();
} }
package com.entity; import javax.swing.JOptionPane; /*2015-7-18*/
public class CurrentPosition {
private Position position;
private int currentX;
private int currentY;
private int maxX;
private int maxY; public CurrentPosition(Position position, int x, int y) {
super();
this.position = position;
this.currentX = x;
this.currentY = y;
} public void setBounds(int maxX, int maxY) {
this.maxX = maxX;
this.maxY = maxY;
} public CurrentPosition(String orginal, int x, int y) {
this(Position.valueOf(orginal), x, y);
} public void doTask(Command command) {
int targetOrdinal = -1;
switch (command) {
case L:
if (this.position.ordinal() == 0) {
targetOrdinal = 3;
} else {
targetOrdinal = this.position.ordinal() - 1;
}
break;
case R:
if (this.position.ordinal() == 3) {
targetOrdinal = 0;
} else {
targetOrdinal = this.position.ordinal() + 1;
}
break;
case M:
targetOrdinal = this.position.ordinal();
changeXY();
break;
default:
JOptionPane.showMessageDialog(null, "Invalid command:" + command + ".Must L、R、M");
break;
}
this.position = this.position.valueOf(targetOrdinal);
} private void changeXY() {
switch (this.position) {
case E:
this.currentX++;
break;
case S:
this.currentY--;
break;
case W:
this.currentX--;
break;
case N:
this.currentY++;
break;
}
} @Override
public String toString() {
if (currentX > maxX || currentY > maxY) {
return "RIP";
}
return currentX + " " + currentY + " " + position;
// return "CurrentPostion [position=" + position + ", x=" + currentX +
// ", y=" + currentY + "]";
} } enum Command {
L,
R,
M;
} enum Position {
E, //
S,
W,
N;
public Position valueOf(int ordinal) {
if (ordinal >= values().length || ordinal < 0) {
throw new IllegalArgumentException("invalid :" + ordinal);
}
return values()[ordinal];
}
}
package com.entity; import static org.junit.Assert.assertEquals; import org.junit.Test; /*2015-7-18*/
public class RoverTest {
@Test
public void shoultGet13N_when_LMLMLMLMM_is_Given() {
CurrentPosition currentPostion = new CurrentPosition("N", 1, 2);
currentPostion.setBounds(5, 5);
Rover rover = new Rover(currentPostion);
assertEquals(rover.doTask("LMLMLMLMM"), "1 3 N");
} @Test
public void shoultGet51E_when_MMRMMRMRRM_is_Given() {
CurrentPosition currentPostion = new CurrentPosition("E", 3, 3);
currentPostion.setBounds(5, 5);
Rover rover = new Rover(currentPostion);
assertEquals(rover.doTask("MMRMMRMRRM"), "5 1 E");
} @Test
public void shoultGetRIP_when_MMM_is_Given() {
CurrentPosition currentPostion = new CurrentPosition("N", 4, 4);
currentPostion.setBounds(5, 5);
Rover rover = new Rover(currentPostion);
assertEquals(rover.doTask("MMRMMRMRRM"), "RIP");
} }
input:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM
4 4 N
MMM
package com; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; import com.entity.CurrentPosition;
import com.entity.Rover; /*2015-7-18*/
public class Controller {
public static void main(String[] args) throws IOException {
File input = new File("input.txt");
BufferedReader reader = new BufferedReader(new FileReader(input));
String line;
int currentLine = 1;
int maxX = -1;
int maxY = -1;
CurrentPosition currentPostion = null;
while ((line = reader.readLine()) != null) {
if (currentLine == 1) {
String[] bounds = line.split(" ");
maxX = Integer.parseInt(bounds[0]);
maxY = Integer.parseInt(bounds[1]);
} else if (currentLine % 2 == 0) {
String[] location = line.split(" ");
currentPostion = new CurrentPosition(location[2], Integer.parseInt(location[0]), Integer.parseInt(location[1]));
currentPostion.setBounds(maxX, maxY);
} else {
Rover rover = new Rover(currentPostion);
System.out.println(rover.doTask(line));
}
currentLine++;
}
reader.close(); } }
Output:
1 3 N
5 1 E
RIP
http://www.cnblogs.com/softidea/p/3760235.html
一个demo的更多相关文章
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
- 初识nginx之第一个demo
商城项目做了一个多月了,想到必须用到负载均衡,简单了解了一下nginx,首先分享第一个demo,五月份上线后,会继续分享一系列相关知识. 在nginx根目录下,用了一个园友的批处理文件nginx.ba ...
- springMvc的第一个demo
1.下载jar包 http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.3.RELEASE/ 2.下载源码 j ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)
关于duilib的历史,我也就不多说了,能看到这篇文章的人都是有一定了解才能找到这个的. 我直接说下对这个库的基本使用吧. 我个人对一些好技术都是比较感兴趣的. 因为个人原因 喜欢接触一个好技术. 所 ...
- 白盒测试之gtest第一个demo
认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...
- 在VS中实现webService的一个demo(图解)
在VS中实现webService的一个demo(图解) 先创建一个web项目,创建好web项目后,添加新建项——web服务 在新建好的web服务文件中写如下代码: 生成当前解决方案. 新建一个winf ...
- Cocos2d-x 学习(1)—— 通过Cocos Studio创建第一个Demo
近期在工作上有了比較大的转变,自学情绪也慢慢高涨,本来一直在研究unity的技术.由于换了工作会開始接触cocos2d-x.但并不意味着停止研究unity,以后有时间还是会继续的. 公司的cocos2 ...
- 使用android的mediaplayer做成 一个demo,欢迎测试使用
附件是为一个定制视频产品而简单的写了一个demo,用来说明android的mediaplayer是如何使用的. http://files.cnblogs.com/guobaPlayer/palyerD ...
- [置顶]
一个demo学会css
全栈工程师开发手册 (作者:栾鹏) 一个demo学会css css选择器全解 css操作语法全解 学习了css权威指南这本书,自己喜欢边学边总结边写demo,所以写了这篇文章,包含了大部分的css编程 ...
随机推荐
- Swift的属性,方法,下标脚本以及继承
从这篇章节起,Swift编程语言指南大部分的重要内容在于概念,代码并非太多.理解Swift的面向对象理念,语法以及类结构,构造析构过程对于非常好的应用Swift语言将会有比較大的帮助. 属性 存储属性 ...
- css实现背景渐变色效果
webkit内核的浏览器,例如(chrome,safari等) background:-webkit-gradient(linear,0 0,0 100%,from(#000000),to(#ffff ...
- pygame各个模块概述
在pygame中,有很多模块,每个模块对应着不同的功能,如果我们知道这些模块是做什么的,那么,对我们的游戏开发会起到关键性的作用. 我们就说说pygame中的各个模块吧!!! #pygame modu ...
- 所有城市list每次从页面花1段时间抽取后写入到数组,
所有城市list每次从页面花1段时间抽取后写入到数组,
- altKey,ctrlKey,shiftKey
<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ...
- Java重命名文件
File file = new File("D:\\aa\a.txt"); file.renameTo(new File("D:\\aa\\b.txt" ...
- 利用Sambaserver在Ubuntu系统和Win7系统间共享目录
1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...
- IP Camera 和 Web Camera 差分
一直以来,,没太注意IP camera 和 Web Camera之间的差,这两个摄像头,昨天晚上.闲来无事Google少数,我们发现,还有两者之间的差异. 1) IP Camera IP Camera ...
- 强大的数据库查询工具Database.NET 9.4.5018.42
原文:强大的数据库查询工具Database.NET 9.4.5018.42 强大的数据库查询工具Database.NET 9.4.5018.42 两个工具的下载地址,两个软件都是绿色免安装的,直接双击 ...
- mysql安装前的系统准备工作(转)
一.系统环境总结: