一个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编程 ...
随机推荐
- gdb学习(一个)[再版]
概要 gdb是GNU debugger的缩写,是编程调试工具. 功能 1.启动程序,能够依照用户自己定义的要求随心所欲的执行程序. 2.可让被调试的程序在用户所指定的断点处停住 (断点能够是条件表达式 ...
- linux环境下的线程的创建问题
pthread_create函数用于创建一个线程 函数原型 #include<pthread.h> int pthread_create(pthread_t *restrict tidp, ...
- Deploy 11.2.0.3 RAC+DG on Windows 2008 R2 Step by Step
环境规划: 节点1: tc1 192.168.56.101 内存:2G 节点2: tc2 192.168.56.102 内存:2G 物理备库:tcdg192.168.56.108内存:1.5G 操作系 ...
- 三星Samsung 4.4.2该负责人制度,简化名单
installed uninstalled AccessControl.apk AllshareControlShare.apk AirMotionTryActually.apk AllshareFi ...
- CSS检测的高像素密度屏幕设备
iPhone4尽管是640px解析度,但它的屏幕宽度(device-width)目前只有320px和iPhone3G相同.只是iPhone4S的像素密度2. 然后使用meta viewport什么时候 ...
- Portlet MVC框架
Portlet MVC框架 16.1. 介绍 Spring不仅支持传统(基于Servlet)的Web开发,也支持JSR-168 Portlet开发. Portlet MVC框架尽可能多地采用Web ...
- php  性能优化
基础优化 1 不要随便复制变量. 有时候为了使 PHP 代码更加整洁,一些 PHP 新手(包含我)会把提前定义好的变量拷贝到一个名字更简短的变量中,事实上这样做的结果是添加了一倍的内存消耗,仅仅会使程 ...
- C#四舍五入保留两位小数
- .net 一些常用的工具来破解
在.net 我们经常提到破 Reflector\SimpleAssemblyExplorer和CFF Explore几个工具. 我们有一个非常简单的Windows Form方案,例如,说他们如何使用 ...
- Iphone CodeSign error: no provisioning profile at path 错
今天在退房手续,变化了profile,当再次进行整理编译项目时.就出现了这个问题,在xCode 6 开发环境下出现了, 出现这个错误提示Code Sign error: Provisioning pr ...