一个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

上一篇:C# 日志的配置流程


下一篇:poj 2253 Frogger(最短路 floyd)