def possible_starts(self, word, horizontal): highest_start = max(0, (7 - len(word) + 1)) starts = [] for i in range(highest_start, 8): if horizontal: starts.append(Coordinate(i, 7)) else: starts.append(Coordinate(7, i)) return starts
def game(request): if request.method == 'POST': post_params = request.POST if request.session['game'] == None: word_lookup = WordLookup() state = setup_game_state(word_lookup, 'com1', 'com2', post_params['debug']) apply_setup_values(state, word_lookup, state.players[0], int(post_params['strategy1']), int(post_params['utility1'])) apply_setup_values(state, word_lookup, state.players[1], int(post_params['strategy2']), int(post_params['utility2'])) request.session['game'] = state else: state = request.session['game'] length = ScrabbleConfig.board_length css_grid = [[ ScrabbleConfig.board_layout(Coordinate(i, j)) for j in xrange(0, length) ] for i in xrange(0, length)] return render(request, 'game/game.html', { 'board': css_grid, 'board_length': length, 'post_params': post_params }) elif request.method == 'GET': return redirect('/')
def occupied_squares(self): res = {} for i in range(0, ScrabbleConfig.board_length): for j in range(0, ScrabbleConfig.board_length): s = self.grid[i][j] if s.tile: res[Coordinate(i, j)] = s return res
def valid_moves(self, c, word, o, board): letter = board.get(c).tile.letter unchecked_starts = [] for i in range(0, len(word)): if word.upper()[i] == letter: if o == Orientation.horizontal: if (c.x - i) >= 0 and (c.x + len(word) - i) <= 14: unchecked_starts.append(Coordinate(c.x - i, c.y)) else: if (c.y - i) >= 0 and (c.y + len(word) - i) <= 14: unchecked_starts.append(Coordinate(c.x, c.y - i)) vmoves = [] for start in unchecked_starts: coords_letters = [] for i in range(0, len(word)): coord = start.next(o, i) if not board.has_tile(coord): coords_letters.append((coord, Tile(word.upper()[i]))) if coords_letters: move = Move(dict(coords_letters), self.state) if move.is_valid: vmoves.append(move) if GameConfig.debug: the_pusher[GameConfig.debug_channel].trigger( 'debug', [{ 'x': c.x, 'y': c.y, 'tile': { 'letter': t.letter, 'score': t.score } } for c, t in move.letters.items()]) time.sleep(1) the_pusher[GameConfig.debug_channel].trigger( 'clear_debug') return vmoves
def duplicate_state(state): # dup state and clear associated objects new_state = deepcopy(state) new_state.players = [] new_state._bag = Bag() new_state._board = Board() # dup players for cp in state.computer_players: new_cp = ComputerPlayer(cp.name, False) # dup player's tiles and attrs for t in cp.tiles: new_cp.tiles.append(Tile(t.letter)) new_cp.score = cp.score new_cp.passes = cp.passes new_cp.utility_function = cp.utility_function new_state.players.append(new_cp) # dup bag tiles for t in state.tile_bag.inventory: new_state.tile_bag.inventory.append(Tile(t.letter)) # dup board state length = ScrabbleConfig.board_length new_state.playing_board.grid = [[ ScrabbleConfig.board_layout(Coordinate(i, j)) for j in xrange(0, length) ] for i in xrange(0, length)] # synch moves already done for i in range(0, length): for j in range(0, length): tile = state.playing_board.has_tile(Coordinate(i, j)) if tile: new_state.playing_board.put_tile_coord( Tile(tile.letter), Coordinate(i, j)) return new_state
def index(request): request.session['game'] = None length = ScrabbleConfig.board_length css_grid = [[ ScrabbleConfig.board_layout(Coordinate(i, j)) for j in xrange(0, length) ] for i in xrange(0, length)] return render(request, 'game/index.html', { 'board': css_grid, 'board_length': length })
def range(self): try: return Coordinate.between(self.first, self.last) except UnsupportedCoordinateException as e: raise InvalidMoveException(e.args[0])
def __init__(self): length = ScrabbleConfig.board_length self.grid = [[ ScrabbleConfig.board_layout(Coordinate(i, j)) for j in xrange(0, length) ] for i in xrange(0, length)]