-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtle.py
74 lines (56 loc) · 1.64 KB
/
Turtle.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
from Color import *
from TurtlePart import *
class TurtleException(Exception):
pass
class Turtle:
ROUGE = Color("R")
BLEU = Color("B")
JAUNE = Color("J")
VERT = Color("V")
CORPS = TurtlePart("C")
TETE = TurtlePart("T")
ASSOC_PART = { "C" : CORPS, "T" : TETE }
ASSOC_COLOR = {
"R" : ROUGE,
"B" : BLEU,
"J" : JAUNE,
"V" : VERT
}
def __init__(self, init_string):
"""Initialisation
Exemples :
CR == Corps Rouge
TV == Tete Verte
"""
if len(init_string) != 2:
raise TurtleException
self.init_string = init_string
turtle_part_string = init_string[0]
try:
self.part = Turtle.ASSOC_PART[turtle_part_string]
except KeyError:
raise TurtlePartException
color_string = init_string[1]
try:
self.color = Turtle.ASSOC_COLOR[color_string]
except KeyError:
raise ColorException
def equals(self, other):
"""Test d'egalite entre tortues"""
return (self.color.equals(other.color)) and (self.part.equals(other.part))
def copy(self):
"""Retourne une copie de la tortue"""
return self
def __str__(self):
"""Affichage des caracteristiques de la tortue
Exemples :
TB
CJ
"""
return "%s%s" % (self.part, self.color)
def matches(self, other):
"""Test si une tortue peut s'emboiter avec une autre"""
if not self.color.matches(other.color):
return False
else:
return (self.part.matches(other.part))