一个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编程 ...
随机推荐
- Learning Cocos2d-x for WP8(5)——详解Menu菜单
原文:Learning Cocos2d-x for WP8(5)--详解Menu菜单 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(5)——详解Menu菜单 菜单是游戏必不 ...
- GCC的使用(编译,链接,运行)
以下这三篇日志非常的好,真的非常的好.介绍使用gcc编译,链接,运行c程序. 1.http://lveyo.iteye.com/blog/240578 2.http://lveyo.iteye.com ...
- Android数字签名解析(二)
在Android数字签名解析(一)中,介绍了android进行签名的两种方式,当中用密钥对进行签名用到了signapk.jar这个java库. 以下我们就看看signapk签名实现过程,signapk ...
- HDU 1164 Eddy's research I【素数筛选法】
思路:将输入的这个数分成n个素数的相乘的结果,用一个数组存储起来.之后再输出就能够了 Eddy's research I Time Limit: 2000/1000 MS (Java/Others) ...
- Java并发编程(您不知道的线程池操作)
Java并发编程(您不知道的线程池操作) 这几篇博客,一直在谈线程,设想一下这个场景,如果并发的线程很多,然而每个线程如果执行的时间很多的话,这样的话,就会大量的降低系统的效率.这时候就可以采用线程池 ...
- 让浏览器支持 jquery ajax load 前进、后退 功能
BEGIN; 一般在做 ajax load 的时候,非常多人都不会考虑到须要浏览器支持前进后退功能,由于大部分人都不知道能够实现. 近期遇到这个问题,经过一小段研究,发现github已经有现成的开源工 ...
- poj 3399 Product(数学)
主题链接:http://poj.org/problem?id=3399 Product Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- 转载ECTouch1.0 修改后台广告管理中广告列表显示广告图片
http://www.ectouch.cn/topics/94.html 效果 操作: 1. 修改后台控制器文件 调用出相关字段信息. mobile\include\apps\admin\contro ...
- Vs2012在Linux应用程序开发(3):加入新平台hi3516
下面我们将VS2012添加一个新的平台支持,由于近来与哈斯hi3516.就选它吧! 1.1 复制平台文件 原来一直认为要让VS支持一个新的平台须要编写代码,某天在看MSBUILD文件夹的时候突 ...
- 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding
原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...