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的更多相关文章

  1. angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”

    曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...

  2. 初识nginx之第一个demo

    商城项目做了一个多月了,想到必须用到负载均衡,简单了解了一下nginx,首先分享第一个demo,五月份上线后,会继续分享一系列相关知识. 在nginx根目录下,用了一个园友的批处理文件nginx.ba ...

  3. springMvc的第一个demo

    1.下载jar包 http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.3.RELEASE/ 2.下载源码 j ...

  4. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  5. 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)

    关于duilib的历史,我也就不多说了,能看到这篇文章的人都是有一定了解才能找到这个的. 我直接说下对这个库的基本使用吧. 我个人对一些好技术都是比较感兴趣的. 因为个人原因 喜欢接触一个好技术. 所 ...

  6. 白盒测试之gtest第一个demo

    认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...

  7. 在VS中实现webService的一个demo(图解)

    在VS中实现webService的一个demo(图解) 先创建一个web项目,创建好web项目后,添加新建项——web服务 在新建好的web服务文件中写如下代码: 生成当前解决方案. 新建一个winf ...

  8. Cocos2d-x 学习(1)—— 通过Cocos Studio创建第一个Demo

    近期在工作上有了比較大的转变,自学情绪也慢慢高涨,本来一直在研究unity的技术.由于换了工作会開始接触cocos2d-x.但并不意味着停止研究unity,以后有时间还是会继续的. 公司的cocos2 ...

  9. 使用android的mediaplayer做成 一个demo,欢迎测试使用

    附件是为一个定制视频产品而简单的写了一个demo,用来说明android的mediaplayer是如何使用的. http://files.cnblogs.com/guobaPlayer/palyerD ...

  10. [置顶] 一个demo学会css

    全栈工程师开发手册 (作者:栾鹏) 一个demo学会css css选择器全解 css操作语法全解 学习了css权威指南这本书,自己喜欢边学边总结边写demo,所以写了这篇文章,包含了大部分的css编程 ...

随机推荐

  1. PHP实现冒泡排序、双向冒泡排序算法

    冒泡排序(Bubble Sort),是一种较简单的.稳定的排序算法.冒泡排序算法步骤:比较相邻的元素,如果第一个比第二个大,就交换他们两个的位置:对每对相邻的元素执行同样的操作,这样一趟下来,最后的元 ...

  2. XSS学习笔记(一个)-点击劫持

    所谓XSS这个场景被触发XSS地方,在大多数情况下,攻击者被嵌入在网页中(问题)该恶意脚本(Cross site Scripting),这里的攻击始终触发浏览器端,攻击的者的目的.一般都是获取用户的C ...

  3. 可兼容IE的jquery.cookie函数方法

    前言 在开发过程中,因为之前有接触过Discuz,就直接拿其common.js里面的getcookie和setcookie方法来使用,做到后面在使用IE来测试的时候,发现这两个方法子啊IE下不起作用, ...

  4. 关于git的ssh-key:解决本地多个ssh-key的问题

    在设置github的时候,官方的说明文档要求备份当前的id_rsa.然后生成一份新的私钥用于github的登陆.假设真这样做,那么新的私钥是无法再继续登陆之前的机器的. 这样的方法有点暴力- 还好ss ...

  5. php查找字符串是否存在

    strstr //搜索字符串在另一字符串中的首次出现(对大小写敏感) //该函数返回字符串的其余部分(从匹配点).如未找到则返回 false stristr //查找字符串在另一字符串中第一次出现的位 ...

  6. Windows Phone开发(29):隔离存储C

    原文:Windows Phone开发(29):隔离存储C 本文是隔离存储的第三节,大家先喝杯咖啡放松,今天的内容也是非常简单,我们就聊一件东东--用户设置. 当然了,可能翻译为应用程序设置合适一些,不 ...

  7. Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)

    转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...

  8. MySQL与Oracle的语法区别详细对比 (转)

    Oracle和mysql的一些简单命令对比 1) SQL> select to_char(sysdate,'yyyy-mm-dd') from dual; SQL> select to_c ...

  9. 採用Android中的httpclient框架发送post请求

    /** * 採用httpclientPost请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得 ...

  10. 查看文章strncpy()功能更好的文章

    strncpy()功能 原型:extern char *strncpy(char *dest, char *src, int n);    使用方法:#include <string.h> ...