-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeNode.py
71 lines (64 loc) · 2.05 KB
/
snakeNode.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from dataclasses import dataclass
from typing import Optional
from customExceptions import BrokenGameLogicException
from positionHolder import _PositionHolder
from direction import Direction
@dataclass
class SnakeNode(_PositionHolder):
nextNode: Optional['SnakeNode'] = None
def getNewNodeShiftedInto(
self,
direction: Direction,
nextNode: Optional['SnakeNode'] = None
) -> 'SnakeNode':
return SnakeNode(
self.position.getNewPositionShiftedInto(direction),
nextNode,
)
@property
def selfToNextNodeDirection(self):
return self.__calcDirection(
Direction.BOTTOM,
Direction.TOP,
Direction.LEFT,
Direction.RIGHT,
)
@property
def nextNodeToSelfDirection(self):
return self.__calcDirection(
Direction.TOP,
Direction.BOTTOM,
Direction.RIGHT,
Direction.LEFT,
)
def __calcDirection(
self,
whenSelfYBigger: Direction,
whenSelfYSmaller: Direction,
whenSelfXBigger: Direction,
whenSelfXLower: Direction
) -> Direction:
if self.nextNode is None:
raise BrokenGameLogicException(
'Cannot get directionOfNextNode for node without next node'
)
match [
self.position.x - self.nextNode.position.x,
self.position.y - self.nextNode.position.y
]:
case [0, 1]:
return whenSelfYBigger
case [0, -1]:
return whenSelfYSmaller
case [1, 0]:
return whenSelfXBigger
case [-1, 0]:
return whenSelfXLower
case _:
raise BrokenGameLogicException(
f'''Impossible situation when nextNode.position={
self.nextNode.position
} and self.position={
self.position
} are not left-right-top-bottom neighbours'''
)