Robot

mail@pastecode.io avatar
unknown
java
2 years ago
1.7 kB
3
Indexable
Never
package com.alt2;

import java.util.HashMap;


public class Robot {

    private int posX;
    private int posY;
    private double v;

    HashMap<String, Integer> directionMap = new HashMap<String, Integer>() {{
        put("NORTH", 90);
        put("SOUTH", 270);
        put("WEST", 0);
        put("EAST", 180);
    }};


    public Robot(int posX, int posY, int direction) {
        this.posX = posX;
        this.posY = posY;
        this.v = direction;
    }

    public int getPosX() {
        return posX;
    }

    public int getPosY() {
        return posY;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }


    public String getDirection() {
        for (String direction : directionMap.keySet()) {
            if (directionMap.get(direction) == this.v)
                return direction;
        }
        return "NORTH";
    }


    public void place(int posX, int posY, String direction) {

        this.posX = posX;
        this.posY = posY;
        this.v = directionMap.get(direction);
    }

    public void move() {

        if (getDirection().equals("NORTH")) this.posY += 1;
        if (getDirection().equals("SOUTH")) this.posY -= 1;
        if (getDirection().equals("WEST")) this.posX -= 1;
        if (getDirection().equals("EAST")) this.posX += 1;
    }

    public String print() {
        return this.posX + "," + this.posY + "," + getDirection();
    }

    public void rotate(int i) {

        this.v = this.v + i;
        if (v >= 360) this.v = 0;
    }
}