Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit 1940145

Browse files
author
Brecht Verhoeve
committed
Change string moves to enum
1 parent 7119bd5 commit 1940145

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"python.pythonPath": "venv/bin/python3",
3-
"spellright.language": [],
3+
"spellright.language": [
4+
"en"
5+
],
46
"spellright.documentTypes": [
57
"markdown",
68
"latex"

viper/game.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Dict
44
from .random_viper import RandomViper
55
from .viper import Viper
6+
from .move import Move
67

78
class Game:
89
"""A game of snake
@@ -28,10 +29,10 @@ def get_viper(self):
2829
return self.viper
2930

3031
def move(self):
31-
move: str = self.viper.move()
32+
move: Move = self.viper.move()
3233

3334
move_response: Dict = {
34-
'move': move,
35+
'move': move.value,
3536
'shout': self.viper.shout
3637
}
3738

viper/move.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from enum import Enum
2+
3+
class Move(Enum):
4+
"""Enum of available moves for a viper
5+
"""
6+
UP = 'up'
7+
DOWN = 'down'
8+
LEFT = 'left'
9+
RIGHT = 'right'

viper/random_viper.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import random
22

33
from .viper import Viper
4+
from .move import Move
45

56
class RandomViper(Viper):
67

@@ -12,5 +13,5 @@ def head(self) -> str:
1213
def tail(self) -> str:
1314
return 'freckled'
1415

15-
def move(self) -> str:
16-
return random.choice(Viper.MOVES)
16+
def move(self) -> Move:
17+
return random.choice(list(Move))

viper/viper.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import abc
2-
from typing import Dict
2+
from typing import Dict, List
33
import logging
44
import random
55

6+
from .move import Move
7+
68
class Viper(abc.ABC):
79
"""An abstract viper
810
"""
@@ -22,13 +24,6 @@ class Viper(abc.ABC):
2224
'regular'
2325
]
2426

25-
MOVES = [
26-
'up',
27-
'down',
28-
'left',
29-
'right'
30-
]
31-
3227
viper_id: int
3328
name: str
3429
health: int
@@ -46,6 +41,10 @@ def __init__(self, viper_id: int, name: str, health: int, body: Dict, shout: str
4641
def __str__(self):
4742
return f'Viper with name {self.name} and color: ${self.color}.'
4843

44+
####################
45+
# Abstract methods #
46+
####################
47+
4948
@property
5049
@abc.abstractmethod
5150
def head(self) -> str:
@@ -56,13 +55,22 @@ def head(self) -> str:
5655
def tail(self) -> str:
5756
pass
5857

58+
@abc.abstractmethod
59+
def move(self) -> Move:
60+
pass
61+
62+
###################
63+
# General methods #
64+
###################
65+
5966
def get_config(self) -> Dict:
6067
return {
6168
'color': self.color,
6269
'headType': self.head,
6370
'tailType': self.tail
6471
}
6572

66-
@abc.abstractmethod
67-
def move(self):
68-
pass
73+
def determine_valid_moves(self) -> List[Move]:
74+
pass
75+
76+

0 commit comments

Comments
 (0)