def test_get_words_constrained(self): trieRoot = Trie.words() board = Board() testCoord = Coord(4, 8) startWord = "cabriole" startCoord = Coord(3, 7) board.place(("c", "a", "b", "r", "i", "o", "l", "e"), startCoord, True) tiles = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] start = StartSequence( 4, 6, [None, "a", None, None, None, None, None, None, None], False) plays = trieRoot.get_words_constrained(start, tiles, board) plays = [tuple(play) for play in plays] self.assertEqual( set(plays), {('b', None), ('b', None, 'd'), ('b', None, 'd', 'e'), ('b', None, 'd', 'g', 'e'), ('b', None, 'a'), ('b', None, 'a', 'e', 'd'), ('b', None, 'g'), ('f', None), ('f', None, 'd'), ('f', None, 'd', 'e'), ('f', None, 'd', 'g', 'e'), ('f', None, 'c', 'e'), ('f', None, 'c', 'e', 'd'), ('f', None, 'c', 'a', 'd', 'e'), ('f', None, 'g'), ('d', None), ('d', None, 'b'), ('d', None, 'c', 'e'), ('d', None, 'g'), ('a', None), ('c', None, 'b'), ('c', None, 'f', 'e'), ('c', None, 'd'), ('c', None, 'd', 'e'), ('c', None, 'd', 'g', 'e'), ('c', None, 'g', 'e'), ('c', None, 'g', 'e', 'd'), ('g', None, 'b'), ('g', None, 'e'), ('g', None, 'e', 'd'), ('g', None, 'd')}) board.place(plays[10], Coord(start.x, start.y), start.ish)
def test_get_char(self): trieRoot = Trie.words() template = ["c", "a", "b", None, "i", "o", "l", "e"] self.assertEqual(trieRoot.get_chars(template), ["r"]) template = ["c", "a", None] invalid_template = ["s", None, None, "e"] self.assertRaises(RuntimeError, lambda: trieRoot.get_chars(invalid_template))
def test_make_play(self): b = Board(3) b.place(("b", "u", "g"), Coord(1, 0), False) tiles = ["a", "e", "i", "o", "u", "y"] ai = Ai("", tiles) trie = Trie.words() successful_play = ai.make_play(trie, b) self.assertTrue(successful_play)
def test_remove_tiles(self): b = Board(3) b.place(("b", "u", "g"), Coord(1, 0), False) tiles = ["a", "e", "i", "o", "u", "y"] ai = Ai("", tiles) trie = Trie.words() successful_play = ai.make_play(trie, b) self.assertNotEqual(len(ai.tiles), 7)
def test_score_words(self): trieRoot = Trie.words() b5 = Board(15) play = StartSequence(6, 7, ["b", "u", "g"], True) score = trieRoot.score_play(play, b5) self.assertEqual(score, 12) b5.place(["p", "a", "r", "k"], Coord(7, 5), False) play = StartSequence(1, 4, ["z", "e", "a", "l", "o", "t", "s"], True) score = trieRoot.score_play(play, b5) self.assertEqual(score, 42)
def __init__(self): self.bag = Bag() self.board = Board() self.trie = Trie.words() tiles1, tiles2 = [], [] for _ in range(7): tiles1.append(self.bag.draw_tile()) tiles2.append(self.bag.draw_tile()) self.player1 = Ai("Player 1", tiles1) self.player2 = Ai("Player 2", tiles2) self.cur_player = self.player1 self.skipped_turns = 0
def __init__(self, size = 15, trie = None): if size < 1 or size % 2 == 0: raise RuntimeError(f"Invalid board dimension {size}") mid = size // 2 self.size = size self.trie = trie if trie else Trie.words() self.tiles = [[None] * size for _ in range(size)] self.crosschecks = [[CrosscheckSquare() for _ in range(size)] for _ in range(size)] self.neighbors = [[False] * size for _ in range(size)] self.neighbors[mid][mid] = True self.neighbors_set = {Coord(mid, mid)}
def test_make_play_fail(self): b = Board(3) b.place(["b", "u", "g"], Coord(0, 0), True) b.place(["a", "g", "o"], Coord(0, 1), True) # b.place(["a", "g"], Coord(0, 2), True) b.place(["x", "x"], Coord(0, 2), True) tiles = ["e", "i", "u", "y"] ai = Ai("", tiles) trie = Trie.words() successful_play = ai.make_play(trie, b) self.assertFalse(successful_play)
def test_get_words_constrained_full(self): trieRoot = Trie.words() b4 = Board(3) b4.place(("b", "u", "g"), Coord(1, 0), False) starts = b4.get_starts(6) words = [] tiles = ["a", "e", "i", "o", "u", "y"] for start in starts: words.extend(trieRoot.get_words_constrained(start, tiles, b4)) words = [tuple(word) for word in words] self.assertEqual( set(words), {('a', None, 'o'), ('a', None, 'y'), ('o', None, 'e'), ('o', None, 'i'), (None, 'e'), (None, 'a'), (None, 'i'), (None, 'o'), (None, 'y'), (None, 'o'), ('e', None, 'o'), ('a', None), ('a', None, 'e'), ('a', None, 'o')})
def test_update_neighbors(self): trieRoot = Trie.words() board = Board() testCoord = Coord(4, 8) startWord = "cabriole" startCoord = Coord(3, 7) board.place(["c", "a", "b", "r", "i", "o", "l", "e"], startCoord, True) # not a neighbor self.assertEqual(board.neighbors[9][4], False) # occupied self.assertEqual(board.neighbors[7][6], False) # should be neighbors self.assertEqual(board.neighbors[7][2], True) self.assertEqual(board.neighbors[6][9], True) self.assertEqual(board.neighbors[8][9], True) self.assertEqual(board.neighbors[7][11], True) starts = board.get_starts(5)
def test_crosscheck(self): trieRoot = Trie.words() board = Board() testCoord = Coord(4, 8) # should be empty set expected = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} self.assertEqual(board.get_v_check(testCoord), expected) startWord = "cabriole" startCoord = Coord(3, 7) board.place_word(startWord, startCoord, True) board.update_state(startCoord, len(startWord), True) # after placing "cabriole" at (7,3), v crosscheck for (8, 4) should be: expected = {'i', 's', 'e', 'w', 'g', 'm', 'n', 'l', 'd', 'a', 'y', 'x', 't', 'h', 'r'} self.assertEqual(board.get_v_check(testCoord), expected) board = Board() board.place_word(startWord, Coord(7, 3), False) board.update_state(Coord(7, 3), len(startWord), False) self.assertEqual(board.get_h_check(Coord(8, 4)), expected)