-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
76 lines (63 loc) · 2.53 KB
/
utils.py
File metadata and controls
76 lines (63 loc) · 2.53 KB
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
import random
alphabet = "abcdefghijklmnopqrstuvwxyz1234567890"
VIDE, PION_BLANC, PION_NOIR, TOUR_BLANC, TOUR_NOIR, CAVALIER_BLANC, CAVALIER_NOIR, FOU_BLANC, FOU_NOIR, REINE_BLANC, REINE_NOIR, ROI_BLANC, ROI_NOIR = \
'O', 'P', 'p', 'T', 't', 'C', 'c', 'F', 'f', 'R', 'r', 'K', 'k'
def generate_random_name():
name = ['c-']
for i in range(10):
name.append(random.choice(alphabet))
return "".join(name)
def create_blank_game():
"""
Sens de lecture de haut en bas puis de gauche à droite
:return text version of blank game
"""
tableau = [
TOUR_NOIR, CAVALIER_NOIR, FOU_NOIR, REINE_NOIR, ROI_NOIR, FOU_NOIR, CAVALIER_NOIR, TOUR_NOIR,
PION_NOIR * 8,
VIDE * 8 * 4,
PION_BLANC * 8,
TOUR_BLANC, CAVALIER_BLANC, FOU_BLANC, REINE_BLANC, ROI_BLANC, FOU_BLANC, CAVALIER_BLANC, TOUR_BLANC
]
tableau = "".join(tableau)
return tableau
def place_to_coordinates(place):
"""
Return a number indicating the coordinate in single-dimension array containing the description of the chess game using the place in chess
:param place: str matching the regex [abcdefgh][12345678]
:return: coordinate of corresponding place
"""
letter, num = list(place)
y = 8 - int(num)
x = ord(letter) - ord('a')
return 8 * y + x
def coordinates_to_place(index):
"""
Return the matching place on the board on a certain index. Index go from up to bottom and then from left to right
:param index: int
:return: Char[2]
"""
y, x = index // 8, index % 8
row = str(8 - y)
column = chr(x + ord('a'))
return column + row
def can_go_to(piece1, piece2):
"""
Returns True if one piece is black (lowercase) and the other is white (uppercase) and vice-versa
Used to determine if one piece can eat the other.
Return False otherwise
:param piece1: Char
:param piece2: Char
:return: Bool
"""
return ((piece1.isupper() and piece2.islower()) or (piece1.islower() and piece2.isupper())) or 'O' in {piece1,
piece2}
def are_differents(piece1, piece2):
"""
Same as can_go_to but return False if p1 or p2 == 'O'
:param piece1: Char
:param piece2: Char
:return: Bool
"""
return ((piece1.isupper() and piece2.islower()) or (piece1.islower() and piece2.isupper())) and 'O' not in {piece1,
piece2}