-1
class Compass(Enum):
    NORTH = (0, 'N')
    WEST = (1, 'W')
    SOUTH = (2, 'S')
    EAST = (3, 'E')
    def __init__(self, id, shortname):
        self.id = id
        self.shortname = shortname

    @staticmethod
    def tovalue():
        return list(map(lambda c: c.value, Compass))

    @staticmethod
    def tolist():
        return list(map(lambda c: c.shortname, Compass))

    @staticmethod
    def toint():
        return list(map(lambda c: c.id, Compass))


    def succ(self):
        int_list = Compass.toint()
        max_int = max(int_list)
        min_int = min(int_list)
        v = self.id +1
        if v > max_int:
            v = self.id = min_int

        for thing in self.tovalue():
            if v in thing:
                return thing


class Rover:

    def __init__(self, x: int, y: int, compass):
        self.x = x
        self.y = y
        self.compass = compass
        for thing in Compass.tovalue():
            if self.compass in thing:
                self.compass = Compass(thing)


        compass_pairs = Compass.tovalue()




    def left(self):
        new_compass = Compass((self.compass.succ()))
        self.compass = new_compass

a = Rover(1,2,'N')
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)
a.left()
print(a.compass)

So basically I create a rover here and turn it left So 4 turns naturally bring you back to the starting direction Which is North in this case The code works for the first 4 turns But the next cycle it fucks up for some reason

Compass.NORTH
Compass.WEST
Compass.SOUTH
Compass.EAST
Compass.NORTH
Compass.WEST
Compass.SOUTH
Compass.NORTH
Compass.WEST

this is the result of the prints The first north is not a turn its the staring position So it should've been South to east but its south to north

  • When you inspected/printed values and/or conditions at various points in your program was there an obvious place where it was misbehaving? If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Feb 28 '21 at 15:56
  • I use pycharm, I tried printing key conditions and the thing that was messed was the int list i think, When N was entered the list would go [3,1,2,3] so the min would become 3. – ChrisCross Feb 28 '21 at 18:40

1 Answers1

1

The problem occurs because you set the id attribute to zero with

    if v > max_int:
        v = self.id = min_int

Don't set the id attribute when rolling over from east to north.

    def succ(self):
        int_list = Compass.toint()
        max_int = max(int_list)
        min_int = min(int_list)
        v = self.id +1
        if v > max_int:
            # v = self.id = min_int
            v = min_int

        for thing in self.tovalue():
            if v in thing:
                return thing

a = Rover(1,2,'N')
print(a.compass,a.compass.id)
for _ in range(15):
    a.left()
    print(a.compass,a.compass.id, end='| ')

Compass.NORTH 0
Compass.WEST 1| Compass.SOUTH 2| Compass.EAST 3| Compass.NORTH 0| Compass.WEST 1| Compass.SOUTH 2| Compass.EAST 3| Compass.NORTH 0| Compass.WEST 1| Compass.SOUTH 2| 
Compass.EAST 3| Compass.NORTH 0| Compass.WEST 1| Compass.SOUTH 2| Compass.EAST 3|
wwii
  • 23,232
  • 7
  • 37
  • 77
  • Oh my god THANK YOU, thats why it only broke when it hit the minimum this makes so much sense. I realised it was that part of the code that was the problem but didnt understand why it changed the enumid but now it makes so much sense! – ChrisCross Mar 01 '21 at 16:52