一个demo

简介: package com.entity; /*2015-7-18*/ public class Rover { private CurrentPosition position; public Rover(CurrentPosition position) { super(); this.

 

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, // 0
    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

 

相关文章
|
6月前
|
JSON 负载均衡 网络协议
Rpcx (一):详解【介绍、基础示例 demo】
Rpcx (一):详解【介绍、基础示例 demo】
131 2
|
中间件 开发者
SofaBolt最简单Demo
SofaBolt最简单Demo
228 0
SofaBolt最简单Demo
|
C++
c++ demo1 比较大小
线程比大小
47 0
|
C++
c++ demo 05 类
c++ demo 05 类
77 0
|
C++
c++ demo02 类型大小
c++ demo02 类型大小
58 0
|
JavaScript 前端开发 关系型数据库
一个Demo--blog
昨天一天上班摸鱼。。。闲着没事,就写了个小的博客网站。 自己搭框架的时候发现,很多东西自己其实已经忘得差不多了,这里跟大家分享分享一些容易健忘的地方。
130 0
|
API Python
demo1
demo1
197 0
|
Web App开发 应用服务中间件 Docker
K8s-Demo实现
Kubernates的基础界面 常用的操作   将创建好的yaml文件通过Create按钮创建所需资源项目。 Dashbord:   可以通过Dashbord查看集群详情:cpu、memory、filesystemm、network等,同样也可以定制化符合自己需求的格式页面。
1898 0
|
API
LeapMotion Demo2
原文:LeapMotion Demo2    官方doc有四个手势,最近尝试实现对握拳的识别,并能在我的程序界面上体现出来。
1068 0