def test_contents(): rack = Rack(Bag()) rack.rack_tiles = ['A', 'A', 'B', 'B', 'C', 'D', 'E'] assert 'ABC' in rack assert 'AAB' in rack assert 'AAABBB' not in rack assert ['D', 'A', 'B'] in rack assert ['D', 'A', 'B', 'D'] not in rack
def test_get_a_tile_in_rack(): rack = Rack(Bag()) letter = rack.rack_tiles[0] assert len(rack.rack_tiles) == RACK_SIZE tile = rack.get_tile(letter) assert len(rack.rack_tiles) == (RACK_SIZE - 1) assert tile is not None assert tile == letter
def test_replenish(): rack = Rack(Bag()) for i in range(13): rack.get_tiles(str(rack)) assert len(rack.rack_tiles) == 0 rack.replenish_tiles() assert len(rack.rack_tiles) == RACK_SIZE rack.get_tiles(str(rack)) assert len(rack.rack_tiles) == 0 rack.replenish_tiles() assert len(rack.rack_tiles) < RACK_SIZE
def __init__(self, bag: Bag, view, name: str): """ Create a new game player """ self.view = view self.score = 0 self.pass_counter = 0 self.name = name self.rack = Rack(bag)
def play_on_square(self, row, index, played_tiles, rack: Rack): valid_moves = [] # get all the letters we could play on this square without making nonsense in the corresponding column: valid_cross_plays = [chr(64 + x) for x in range(1, 27) if read_bit(row.this_row_crosschecks[index], x)] # filter that to ones we can actually use with tiles from our rack (all of them if we have a blank!) valid_tiles = valid_cross_plays if '@' in rack else [x for x in valid_cross_plays if x in rack] # for each of the playable tiles, stick it in the square for tile_letter in valid_tiles: tile = rack.get_tile(tile_letter) played_tiles[index] = tile row.existing_letters[index] = ord(tile)-64 if row.word_at(index) in self.game.lexicon: new_move = Move(row, np.where(played_tiles)[0][0], [tile for tile in played_tiles if tile]) new_move.played_squares = np.where(played_tiles)[0] new_move.calculate_score() #DEBUG: #print(self.name+": Considering move: "+str(new_move)) valid_moves.append(new_move) if len(rack) > 0: # if we still have tiles left # try extending into the next square on the left, only if we've made a middle part of a real word: if self.game.lexicon.contains_infix(row.word_at(index)): valid_moves.extend(self.extend_left(index, played_tiles, row, rack)) # and if we've made the start of a word yet, try extending that to the right if self.game.lexicon.contains_prefix(row.word_at(index)): valid_moves.extend(self.extend_right(index, played_tiles, row, rack)) # return the tile to the rack rack.add_tile(tile) # set board square back to no letter row.existing_letters[index] = 0 # remove tile from list of played tiles: played_tiles[index] = None return valid_moves
def test_create_rack(): rack = Rack(Bag())
def test_overflow_rack(): rack = Rack(Bag()) with pytest.raises(ValueError) as e_info: rack.add_tiles(['A', 'B']) assert len(rack.rack_tiles) == RACK_SIZE
def test_add_too_many_tiles(): rack = Rack(Bag()) with pytest.raises(IndexError) as e_info: rack.add_tile('A') # adds tile to a new (hence full) rack
def test_rack_size(): rack = Rack(Bag()) assert len(rack.rack_tiles) == RACK_SIZE