def make_move(self, move): ''' Make a move. If no colors are removed, unmake the move. Arguments: move -- a 2-tuple of locations Return value: none ''' assert u.is_move(move) (loc1, loc2) = move if not ls.is_adjacent(loc1, loc2): msg = f'Invalid move: non-adjacent locations: {loc1}, {loc2}' raise InvalidMove(msg) if not loc1 in self.rep: msg = f'Invalid move: unoccupied location: {loc1}' raise InvalidMove(msg) if not loc2 in self.rep: msg = f'Invalid move: unoccupied location: {loc2}' raise InvalidMove(msg) # Save previous representation in history. self.history.append(self.rep.copy()) # Make the move. self.rep = dr.swap_locations(self.rep, loc1, loc2) (self.rep, self.removed) = dr.remove_connected_groups(self.rep) # Unmake the move if nothing was removed. if self.removed == {}: self.undo()
def display_moves(moves): ''' Print the set of possible moves in a readable form. Arguments: moves -- a list of possible moves Return value: none ''' assert type(moves) is set for move in moves: assert u.is_move(move) # Convert the moves into a list. moves = list(moves) moves.sort() if moves == []: print('No moves are available!') else: print('Possible moves:\n') for i in range(len(moves)): s = f'{i}: ' ((r1, c1), (r2, c2)) = moves[i] s += f'({r1}, {c1}) -- ({r2}, {c2})' print(s) print()
def display_moves(game, moves): global moveslist ''' Print the set of possible moves in a readable form. Arguments: moves -- a list of possible moves Return value: none ''' assert type(moves) is set for move in moves: assert u.is_move(move) # Convert the moves into a list. moves = list(moves) moves.sort() moveslist = moves for text in game.movetexts: game.cm.delete(text) game.movetexts = [] t = game.cm.create_text(50, 30, text='Possible Moves:') game.movetexts.append(t) if moves == []: messagebox.showwarning('Error', 'No moves are available!') t = game.cm.create_text(50, 50, text='None') game.movetexts.append(t) else: for i in range(len(moves)): s = f'{i}: ' ((r1, c1), (r2, c2)) = moves[i] s += f'({r1}, {c1}) -- ({r2}, {c2})' t = game.cm.create_text(50 + 100 * divmod(i, 15)[0], \ 50 + 20 * divmod(i, 15)[1], text=s) game.movetexts.append(t)