Skip to content

Instantly share code, notes, and snippets.

@n2o
Last active June 30, 2017 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n2o/4fa7d0ca40a5460cde7bdd59adebe5a3 to your computer and use it in GitHub Desktop.
Save n2o/4fa7d0ca40a5460cde7bdd59adebe5a3 to your computer and use it in GitHub Desktop.
Professional Programming: Results of practical exercises, week 6

Results (so far) from our practical exercises: Solving the Mars Rover Kata

// build.gradle

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'jacoco'

repositories {
    jcenter()
}

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile "org.spockframework:spock-core:1.1-groovy-2.4-rc-2"
}

Created tests using the Spock Framework:

import spock.lang.Specification
import spock.lang.Unroll

class RoverSpec extends Specification {
    def "Looking north, doing nothing, still looking north"() {
        given:
        def r = new Rover(1, 5, "N")
        when:
        r.resolve([])
        then:
        r == new Rover(1, 5, "N")
    }

    def "Looking north, turning right, results east"() {
        given:
        def r = new Rover(1, 5, "N")
        when:
        r.resolve(["r"])
        then:
        r == new Rover(1, 5, "E")
    }

    def "Looking east, turning right, looking south"() {
        given:
        def r = new Rover(1, 5, "E")
        when:
        r.resolve(["r"])
        then:
        r == new Rover(1, 5, "S")
    }

    def "Looking north, turning left, looking west"() {
        given:
        def r = new Rover(1, 5, "N")
        when:
        r.resolve(["l"])
        then:
        r == new Rover(1, 5, "W")
    }

    def "Looking north (1, 5), go one step forwards, facing north (1, 4)"() {
        given:
        def r = new Rover(1, 5, "N")
        when:
        r.resolve(["f"])
        then:
        r == new Rover(1, 4, "N")
    }

    def sum(int a, int b) {
        return a + b
    }

    @Unroll
    def 'The sum of #a + #b is #c'() {
        expect:
        sum(a, b) == c
        where:
        a | b || c
        1 | 2 || 3
        2 | 2 || 4
    }
}

And our source code, written in Java:

// src/main/java/Rover.java

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Rover {
    private int posX, posY;
    private String dir;
    private Map<String, String> rightDirections = new HashMap<>();

    void initDirections() {
        rightDirections.put("N", "E");
        rightDirections.put("E", "S");
        rightDirections.put("S", "W");
        rightDirections.put("W", "N");
    }

    Rover(int x, int y, String dir) {
        this.posX = x;
        this.posY = y;
        this.dir = dir;
        initDirections();
    }

    void resolve(List<String> moves) {
        if (moves.isEmpty()) return;
        String move = moves.get(0);
        if (move.equals("r")) {
            this.dir = rightDirections.get(this.dir);
        } else if (move.equals("l")) {
            this.dir = rightDirections.get(rightDirections.get(rightDirections.get(this.dir)));
        } else if (move.equals("f")) {
            this.posY--;
        }
    }

    @Override
    public boolean equals(Object obj) {
        Rover that = (Rover) obj;
        return this.posX == that.posX &&
               this.posY == that.posY &&
               this.dir.equals(that.dir);
    }
    @Override
    public int hashCode() {
        return 42;  // do something useful here!
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment