Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SnakeGame:
- def __init__(self, width: int, height: int, foods: List[List[int]]):
- self.maxRow = height
- self.maxCol = width
- self.foodList = []
- self.currentX = 0
- self.currentY = 0
- self.score = 0
- for food in foods:
- x, y = food
- self.foodList.append((x,y))
- self.dir = {
- 'L': (0, -1),
- 'R': (0, 1),
- 'D': (1, 0),
- 'U': (-1, 0)
- }
- def move(self, direction: str) -> int:
- x = self.currentX + self.dir[direction][0]
- y = self.currentY + self.dir[direction][1]
- if (self.checkBoundary(self.maxRow, self.maxCol, x, y) == False):
- return -1
- self.currentX = x
- self.currentY = y
- if self.score < len(self.foodList) and self.foodList[self.score][0] == x and self.foodList[self.score][1] == y:
- self.score += 1
- return self.score
- else:
- return self.score
- def checkBoundary(self, r, c, posx, posy):
- if ( 0 <= posx < r and 0 <= posy < c):
- return True
- else:
- return False
- # Your SnakeGame object will be instantiated and called as such:
- # obj = SnakeGame(width, height, food)
- # param_1 = obj.move(direction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement