-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
186 lines (152 loc) Β· 4.68 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations
import curses
from dataclasses import dataclass
from enum import Enum
from random import Random
from typing import FrozenSet
from typing import List
from typing import Optional
from typing import Tuple
random = Random(x=1389075)
@dataclass(frozen=True)
class Cell:
x: int
y: int
def move(self, direction: Direction) -> Cell:
return Cell(self.x + direction.value.x, self.y + direction.value.y)
@classmethod
def get_random(cls, max_x: int, max_y: int) -> Cell:
return cls(random.randint(0, max_x), random.randint(0, max_y))
class Direction(Enum):
value: Cell
UP = Cell(0, -1)
DOWN = Cell(0, 1)
LEFT = Cell(-1, 0)
RIGHT = Cell(1, 0)
def is_opposite(self, to: Direction) -> bool:
return self.value.x + to.value.x == 0 and self.value.y + to.value.y == 0
@dataclass(frozen=True)
class Snake:
cells: List[Cell]
direction: Direction
eaten_apples: int = 0
@property
def head(self) -> Cell:
return self.cells[0]
@property
def tail(self) -> List[Cell]:
return self.cells[1:]
def move(self) -> Snake:
new_head = self.head.move(self.direction)
new_tail = self.cells[:-1] if not self.eaten_apples else self.cells
return Snake(
cells=[new_head, *new_tail],
direction=self.direction,
eaten_apples=max(0, self.eaten_apples - 1)
)
def turn(self, direction: Optional[Direction]) -> Snake:
if not direction or direction.is_opposite(self.direction):
return self
return Snake(cells=self.cells, direction=direction)
def eat(self, apples: Apples) -> Tuple[Snake, Apples]:
if self.head not in apples.cells:
return self, apples
return (
Snake(
cells=self.cells,
direction=self.direction,
eaten_apples=self.eaten_apples + 1
),
Apples(
width=apples.width,
height=apples.height,
cells=apples.cells - {self.head}
).grow(self)
)
@dataclass(frozen=True)
class Apples:
width: int
height: int
cells: FrozenSet[Cell] = frozenset()
def grow(self, snake: Snake) -> Apples:
new_cell = Cell.get_random(self.width - 1, self.height - 1)
while new_cell in snake.cells:
new_cell = Cell.get_random(self.width - 1, self.height - 1)
return Apples(
width=self.width,
height=self.height,
cells=self.cells | {new_cell}
)
@dataclass(frozen=True)
class Game:
width: int
height: int
snake: Snake
apples: Apples
@property
def is_over(self) -> bool:
return (
self.snake.head in self.snake.tail or
not all(0 <= c.x < self.width and 0 <= c.y < self.height for c in self.snake.cells)
)
@property
def score(self) -> int:
return len(self.snake.cells)
def update(self, direction: Optional[Direction]) -> Game:
if self.is_over:
return self
new_snake, new_apples = self.snake.turn(direction).move().eat(self.apples)
return Game(
width=self.width,
height=self.height,
snake=new_snake,
apples=new_apples
)
def draw(window, game: Game) -> None:
window.clear()
window.box()
for c in game.apples.cells:
window.addstr(c.y + 1, c.x + 1, '*')
for c in game.snake.tail:
window.addstr(c.y + 1, c.x + 1, 'o')
window.addstr(game.snake.head.y + 1, game.snake.head.x + 1, 'Q')
if game.is_over:
window.addstr(0, 1, 'The game is over')
window.addstr(1, 1, f'Score is {game.score}')
window.refresh()
def main(window=curses.initscr()):
curses.curs_set(0)
curses.halfdelay(2)
direction = {
'w': Direction.UP,
'a': Direction.LEFT,
's': Direction.DOWN,
'd': Direction.RIGHT
}
height = 10
width = 20
window.resize(height + 2, width + 2)
game = Game(
width=width,
height=height,
snake=Snake(
cells=[Cell(3, 1), Cell(2, 1), Cell(1, 1)],
direction=Direction.RIGHT
),
apples=Apples(
width=width,
height=height,
cells=frozenset([Cell.get_random(width - 1, height - 1)])
)
)
input_ch = 0
pause = False
while chr(input_ch) != 'q':
draw(window, game)
input_ch = max(window.getch(), 0)
if chr(input_ch) == 'p':
pause = not pause
if not pause:
game = game.update(direction.get(chr(input_ch)))
if __name__ == '__main__':
curses.wrapper(main)