def myword(): word = Word("TASTY", coord=(5, 5), direction=0) layout = p.join(test_data, 'board.csv') board = Board(layout) word.squares = board.get_square_xy(word, word.x, word.y, word.direction) for sq in word.squares: sq.tile_used = True return word
def test_sq_collision_down_vert_true(ori, cword, direction, expected): "Test valid word collison on square's right reading across." sq = Square(1, 1, "tw") sq.parent_word = [ Word('AD', direction=1), ] assert sq.collision(ori, cword, direction, DICT) == expected
def test_sq_collision_left_horiz_true(ori, cword, direction, expected): "Test valid word collison on square's left reading across." sq = Square(1, 1, "tw") sq.parent_word = [ Word('BA', direction=0), ] assert sq.collision(ori, cword, direction, DICT) == expected
def move_extends_front_back(side, x, y, direction, board, label, candidate): """Check move is valid at front, up or back, down of a played word. Computes bonus words in validation of move. """ bonus_words = [] tmp_word = Word(side, coord=(x, y), direction=direction) tmp_squares = board.get_square_xy(tmp_word, x, y, tmp_word.direction) if tmp_squares is False: return (False, []) # Can't add to front, doesn't fit Board for side_letter, side_sq in zip(side, tmp_squares): if side_sq.free: # Playable free square, but are neighbour Squares collisions # Get that possible collision squares we need to test collision_squares = board.check_collisions(side_sq, direction, label) if collision_squares: for (ori, square_collides) in collision_squares: collision_word = square_collides.collision_word( ori, candidate, tmp_word.direction, side_letter) # If collision_word is False .. collision detected if not collision_word: return (False, []) else: bonus_words += collision_word elif side_sq.tile_letter == side_letter: continue else: return (False, []) return (True, bonus_words)
def test_first_word(myrack, myboard, mytilebag): """Test first played is highest scoring word that passes through center.""" myrack.compute_all_play_word_scores(myboard, mytilebag) myrack.first_word(myboard) assert (myrack.best_first_word.__hash__() == Word('DECAF', coord=(5, 5), direction=0).__hash__())
def player2_turn(self): """Player 2 takes a turn.""" word = Word(self.rack.opponent_word) word.coord = self.coord word.direction = self.direction word.squares = self.board.get_square_xy(word, word.x, word.y, word.direction) word.compute_word_score(word.squares, self.tilebag) return word
def test_collides(myboard, target, ori, test_yes): """Test adjacent Square for possible collision.""" # test_yes = True when adjacent square is occupied (word collides) adjx, adjy = { 'left': (target.x, target.y - 1), 'right': (target.x, target.y + 1), 'up': (target.x - 1, target.y), 'down': (target.x + 1, target.y) }[ori] adjsq = myboard.get_square_xy(Word('X'), adjx, adjy, 0)[0] if test_yes: adjsq.free = False test_yes = adjsq observed = myboard.collides(target, ori) assert observed == test_yes
def test_play_word(myboard, word, expected): """Can play word, test upating main Square attributes.""" for i, (l, x, y, _r) in enumerate(expected): b = myboard.get_square_xy(Word(l), x, y, 0)[0] assert b.tile_letter == "" assert b.free assert b.x == x assert b.y == y assert b.parent_word == [] myboard.play_word(word) for i, (l, x, y, r) in enumerate(expected): assert word.squares[i].tile_letter == l assert word.squares[i].tile_used == r assert not word.squares[i].free assert word.squares[i].parent_word == [word] assert word.squares[i].x == x assert word.squares[i].y == y
def test_check_collision(myboard, target, direction, ending, expected): """Test that we can detect colliding words.""" # TODO: Test only checks collisions on extended words. Generalize. # This is a wrapper to collides function. # Test by making all adjacent squares occupied if collision expected for ori in ['left', 'right', 'up', 'down']: adjx, adjy = { 'left': (target.x, target.y - 1), 'right': (target.x, target.y + 1), 'up': (target.x - 1, target.y), 'down': (target.x + 1, target.y) }[ori] adjsq = myboard.get_square_xy(Word('X'), adjx, adjy, 0)[0] adjsq.free = not expected len_expected = 0 if not expected else 3 observed = myboard.check_collisions(target, direction, ending) assert len(observed) == len_expected
def myboard(): layout = p.join(test_data, 'board.csv') board = Board(layout) return board def test_board_instansiate(myboard): """Can instansiate square object.""" assert isinstance(myboard.grid, DataFrame) assert isinstance(myboard.tile_grid, DataFrame) @pytest.mark.parametrize( "word, x, y, direction, expected", [(Word("CAT"), 1, 1, 0, [Square(1, 1, 'dw'), Square(1, 2, 'sl'), Square(1, 3, 'sl')]), (Word("CAT"), 1, 1, 1, [Square(1, 1, 'dw'), Square(2, 1, 'sl'), Square(3, 1, 'sl')]), (Word("CAT"), 1, 8, 0, [Square(1, 8, 'sl'), Square(1, 9, 'dw'), Square(1, 10, 'sl')]), (Word("CAT"), 1, 9, 0, False), (Word("CAT"), 8, 1, 1, [Square(8, 1, 'sl'), Square(9, 1, 'dw'), Square(10, 1, 'sl')]), (Word("CAT"), 9, 1, 1, False)])
def myall_played_words(): p = PlayedWords() p.add_word(Word("WATCH", coord=(5, 5), direction=0, score=52, player=1)) p.add_word(Word("WHEN", coord=(5, 5), direction=1, score=20, player=2)) return p
def myword2(): return Word("WHEN", coord=(5, 5), direction=1, score=20, player=2)
def myword1(): return Word("WATCH", coord=(5, 5), direction=0, score=52, player=1)
def myword(): return Word("TASTY", coord=(5, 5), direction=0)
@pytest.fixture def mytilebag(): return TileBag(p.join(test_data, 'tiles.csv')) def test_word_instansiate(myword): """Can instansiate Word object.""" assert isinstance(myword, Word) assert myword.word == "TASTY" assert myword.x == 5 assert myword.y == 5 assert len(myword) == 5 @pytest.mark.parametrize("word, squares, total", [ (Word("CAT"), [Square(1, 1, 'dw'), Square(1, 2, 'sl'), Square(1, 3, 'sl')], 12), (Word("FROG"), [ Square(1, 1, 'dw'), Square(2, 1, 'sl'), Square(3, 1, 'sl'), Square(4, 1, 'tl') ], 30), (Word("HORSE"), [ Square(5, 5, 'sl'), Square(5, 6, 'dw'), Square(5, 7, 'sl'), Square(5, 8, 'sl'), Square(5, 9, 'tw') ], 42),