-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square.py
executable file
·104 lines (77 loc) · 3.11 KB
/
Square.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
class Square:
# class variables defined before __init__ ('constructor')
# access with self.x_files OR Square.x_files OR self.__class__.x_files
# I think self.__class__ is the most pythonic?
x_files = 'abcdefgh'
y_ranks = '12345678'
def __init__(self, x_pos, y_pos, occupant=None):
self._pos = (x_pos, y_pos)
self._is_black = self.determine_is_black(x_pos, y_pos)
self._occupant = occupant
self._name = self.get_square_name_from_pos()
# define static method on square class that will determine square position based on a square.name input - this position will then be used in the board class to access the square instance from board.squares
# no reason to have setter/deleter for square position - will never change
@property
def pos(self):
return self._pos
@property
def name(self):
return self._name
# @name.setter
# def name(self, name):
# name = self.get_square_name_from_pos()
# self._name = name
def get_square_name_from_pos(self):
x_pos, y_pos = self.pos
x_file = self.__class__.x_files[x_pos]
y_rank = self.__class__.y_ranks[y_pos]
return x_file + y_rank
# def get_square_pos_from_name(self, name):
# x_file = name[0]
# y_rank = name[1]
# getter method for square color
@property
def is_black(self):
return self._is_black
# setter method for square color
# @is_black.setter
# def set_is_black(self, is_black):
# self._is_black = is_black
# determines if square is black or not based on square position
# method called in square constructor upon instantiation
def determine_is_black(self, x, y):
if x % 2 == 0 and y % 2 == 0:
return True
elif x % 2 != 0 and y % 2 != 0:
return True
else:
return False
@property
def occupant(self):
return self._occupant
# occupant will be an instance of a piece on which we can call various methods
@occupant.setter
def occupant(self, newOccupant):
if self.is_occupied() and newOccupant is not None:
print('SPOT IS OCCUPIED - MOVE NOT MADE')
elif not self.is_occupied() and newOccupant is None:
print('SPOT IS ALREADY EMPTY')
else:
self._occupant = newOccupant
# I don't think we need a deleter - instead we want to set newOccupant to None
# @occupant.deleter
# def occupant(self):
# if not self.is_occupied():
# print('NO PIECE IN THIS SQUARE - PIECE NOT DELETED')
# else:
# self._occupant = None
def is_occupied(self):
return self.occupant is not None
def is_open(self):
return self.occupant is None
def __str__(self):
color = 'White' if self.is_black is False else 'Black'
return f'''Pos: {self.pos}, Name: {self.name}, Color: {color}, Occupant: {self.occupant}'''
def __repr__(self):
color = 'White' if self.is_black is False else 'Black'
return f'''Pos: {self.pos}, Name: {self.name}, Color: {color}, Occupant: {self.occupant}'''