Walking Robot Simulation II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Robot:
    def __init__(self, width: int, height: int):
        self.w = width
        self.h = height
        self.x = 0
        self.y = 0
        self.dir = "East"

    def step(self, num: int) -> None:  # Changed method name to step
        num %= self.w * 2 + self.h * 2 - 4
        if num == 0:
            num = self.w * 2 + self.h * 2 - 4

        for _ in range(num):
            if self.dir == "East":
                if self.x == self.w - 1:
                    self.dir = "North"
                    self.y += 1
                else:
                    self.x += 1
            elif self.dir == "North":
                if self.y == self.h - 1:
                    self.dir = "West"
                    self.x -= 1
                else:
                    self.y += 1
            elif self.dir == "West":
                if self.x == 0:
                    self.dir = "South"
                    self.y -= 1
                else:
                    self.x -= 1
            elif self.dir == "South":
                if self.y == 0:
                    self.dir = "East"
                    self.x += 1
                else:
                    self.y -= 1

    def getPos(self) -> list[int]:
        return [self.x, self.y]

    def getDir(self) -> str:
        return self.dir