· 5 years ago · Dec 23, 2020, 11:02 AM
1from car import Car
2EMPTY = '-'
3VERTICAL = 0 # for testing
4HORIZONTAL = 1 # for testing
5ERROR = "Error"
6
7class Board:
8 """
9 Class for game board objects. Boards are the setting main structure where the game to takes place.
10 Each round in game is set on a single board. Board objects track the different car objects in play
11 using a dictionary, and can determine what are all the possible_moves for each car object.
12 Every board has size parameters (width, hieght),which in this task should have a pre-determined
13 permanent value (7), a printable matrix representing the game board itself and the coordinates
14 a car object needs to win the game.
15 """
16
17 def __init__(self):
18 self._WIDTH = 7 # permanent value set by task guidelines
19 self._HEIGHT = 7 # permanent value set by task guidelines
20 self._VICTORY = 3, 7 # permanent value set by task guidelines
21 self.matrix = [[EMPTY] * self._WIDTH] * self._HEIGHT # create an empty board
22 self.cars_on_board = dict # dictionary of all existing cars
23
24 def __str__(self):
25 """
26 This function is called when a board object is to be printed.
27 :return: A string of the current status of the board
28 """
29 matrix_string = ""
30 for y in range(len(self.matrix)):
31 for x in range(len(self.matrix[y])):
32 matrix_string = matrix_string + ' ' + str(self.matrix[y][x])
33 matrix_string = matrix_string + '\n'
34 return matrix_string
35
36 def cell_list(self):
37 """ This function returns the coordinates of cells in this board
38 :return: list of coordinates
39 """
40 lst = []
41 for y in range(self._HEIGHT):
42 for x in range(self._WIDTH):
43 temp_tuple = (y, x)
44 lst.append(temp_tuple)
45 return lst
46
47 def possible_moves(self):
48 """ This function returns the legal moves of all cars in this board
49 :return: list of tuples of the form (name,movekey,description)
50 representing legal moves
51 """
52 legal_moves = []
53 for car in self.cars_on_board.items(): # get all car objects on board (consider using set over dict)
54 car: Car # find a way to fix loop so as to not need this line
55 possible_moves:dict = car.possible_moves()
56 for movekey in possible_moves.keys():
57 required_cell = (car.movement_requirements(movekey))
58 if self.cell_content(required_cell) is None: # if cell is empty (cell_content returns None), move is valid
59 move_temp = (car.get_name(),movekey,possible_moves[movekey])
60 legal_moves.append(move_temp)
61 else: # if required cell is not empty, do not add move to legal_moves list
62 continue
63 # From the provided example car_config.json file, the return value could be
64 # [('O','d',"some description"),('R','r',"some description"),('O','u',"some description")]
65 return legal_moves
66
67 def target_location(self):
68 """
69 This function returns the coordinates of the location which is to be filled for victory.
70 :return: (row,col) of goal location
71 """
72 return self._VICTORY
73
74 def cell_content(self, coordinate):
75 """
76 Checks if the given coordinates are empty.
77 :param coordinate: tuple of (row,col) of the coordinate to check
78 :return: The name if the car in coordinate, None if empty
79 """
80 if coordinate[0] > self._HEIGHT or coordinate[1] > self._WIDTH:
81 print("ERROR: coordinates are outside of board")
82 return ERROR
83 cell = self.matrix[coordinate[0]][coordinate[1]]
84 if cell == EMPTY: # cell is empty
85 return None
86 else:
87 return cell
88
89 def add_car(self, car):
90 """
91 Adds a car to the game.
92 :param car: car object of car to add
93 :return: True upon success. False if failed
94 """
95 # fail: location taken, car already exist on board
96 # Note: consider how to position car, where to add it's length (in what direction - if it's on the edge of
97 # the board, but has plenty of room in the other way, should add it in that direction)
98 # car data is valid (name, orientation, location) by default:
99 # "You may assume the car is a legal car object following the API"
100 if car.get_name() in self.cars_on_board.keys():
101 self.cars_on_board.update({car.get_name():car})
102 return True
103 else:
104 return False
105 return None # invalid input
106 def move_car(self, name, movekey):
107 """
108 moves car one step in given direction.
109 :param name: name of the car to move
110 :param movekey: Key of move in car to activate
111 :return: True upon success, False otherwise
112 """
113 car = self.cars_on_board[name]
114 origin_y = car.location()[0]
115 origin_x = car.location()[1]
116 # consider adding a check to see move is in possible_moves
117 if movekey == 'u':
118 car.location()[0] + 1
119 self.matrix[origin_y][origin_x] = EMPTY # update the cell the car left
120 elif movekey == 'd':
121 car.location()[0] - 1
122 self.matrix[origin_y][origin_x] = EMPTY
123 elif movekey == 'r':
124 car.location()[1] + 1
125 self.matrix[origin_y][origin_x] = EMPTY
126 elif movekey == 'l':
127 car.location()[1] - 1
128 self.matrix[origin_y][origin_x] = EMPTY
129 else:
130 return None # invalid movekey input
131
132
133
134########################################################################################################
135"""TESTING"""
136
137b1 = Board()
138# print(b1)
139
140# #################
141# # cell list
142# print("\nTEST POSSIBLE CELL LIST")
143# # 1
144# got = b1.cell_list()
145# expected = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4),
146# (1, 5), (1, 6), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 0), (3, 1), (3, 2),
147# (3, 3), (3, 4), (3, 5), (3, 6), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 0),
148# (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5),
149# (6, 6)]
150# if got == expected:
151# print('1) success')
152# else:
153# print('1) fail')
154# print(' ', got)
155#
156# #################
157# # possible moves
158# print("\nTEST POSSIBLE MOVES")
159# # 1
160# # cj1, cj2 = Car('O', 2, (2,3), 0), Car('R', 2, (0,0), 1)
161# # b1.cars_on_board = {cj1, cj2}
162# # got = b1.possible_moves()
163# # expected = [('O','d',"down"),('R','r',"right"),('O','u',"up")]
164# # if got == expected:
165# # print('1) success')
166# # else:
167# # print('1) fail')
168# # print(' ', got)
169#
170#
171# #################
172# # target location
173# print("\nTEST TARGET LOCATION")
174# # 1
175# got = b1.target_location()
176# expected = (3, 7)
177# if got == expected:
178# print('1) success')
179# else:
180# print('1) fail')
181# print(' ', got)
182
183#################
184# # cell content
185# print("\nTEST CELL CONTENT")
186# # 1
187# got = b1.cell_content((0, 0))
188# expected = None
189# if got == expected:
190# print('1) success')
191# else:
192# print('1) fail')
193# print(' ', got)
194#
195# # 2
196# coordinates = (1, 1)
197# b1.matrix[coordinates[0]][coordinates[1]] = 'Y'
198# got = b1.cell_content((1, 1))
199# expected = 'Y'
200# if got == expected:
201# print('2) success')
202# else:
203# print('2) fail')
204# print(' ', got)
205
206#################
207# add_car
208print("\nTEST ADD CAR")
209c0 = Car('G': (1 ,(6,6), HORIZONTAL))
210b1.cars_on_board = {('G': (1 ,(6,6), HORIZONTAL))}
211# 1
212c1 = Car('Y', 2, (3, 0), HORIZONTAL)
213got = b1.add_car(c1)
214expected = True
215if got == expected:
216 print('1) success')
217else:
218 print('1) fail')
219 print(' ', got)
220
221# 2 required cells already taken (by c1)
222c2 = Car('R', 1, (3, 0), HORIZONTAL)
223got = b1.add_car(c2)
224expected = True
225if got == expected:
226 print('2) success')
227else:
228 print('2) fail')
229 print(' ', got)
230
231# 3 car already exist on the game board
232c3 = Car('Y', 1, (5, 2), VERTICAL)
233got = b1.add_car(c3)
234expected = False
235if got == expected:
236 print('3) success')
237else:
238 print('3) fail')
239 print(' ', got)
240
241
242# move car
243# b1