コード例 #1
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_stemming_words():
    b = Board()
    letters = 'cat'
    indecies = [(7, 7), (7, 8), (7, 9)]
    b.place_tiles(letters, indecies)
    letters_2 = 'snake'
    indecies_2 = [(7, 10), (8, 10), (9, 10), (10, 10), (11, 10)]
    assert (b.get_stemming_words(letters_2, indecies_2) == ['cats'])
コード例 #2
0
ファイル: play_game.py プロジェクト: ahliu13/myscrabble
 def __init__(self, player_names):
     self.bag = Bag()
     self.board = Board()
     self.players = []
     self.set_player_list(player_names)
     self.game_done = False
     self.player_turn_index = 0
     self.set_curr_player()
     self.fill_player_trays()
コード例 #3
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_is_valid_move():
    b = Board()
    letters = 'cat'
    indecies = [(7, 7), (7, 8), (7, 9)]
    assert (b.is_valid_move(letters, indecies))
    letters = 'snake'
    indecies = [(7, 10), (8, 10), (9, 10), (10, 10), (11, 10)]
    assert (not b.is_valid_move(letters, indecies)
            )  # Isn't starting via the center
    letters = 'snake'
    indecies = [(7, 24)]
    assert (not b.is_valid_move(letters, indecies)
            )  # Isn't starting via the center
コード例 #4
0
ファイル: play_game.py プロジェクト: ahliu13/myscrabble
def play_game(player_names):
    g = Game(player_names)
    b = Board()
    while (not g.game_done):
        g.play_turn()
        g.update_current_player()
        g.update_game_done()
    g.tally_game_score()
    return
コード例 #5
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_is_in_single_col():
    b = Board()
    one_elt = [(0, 0)]
    assert (b.is_in_single_column(one_elt))
    row = [(0, 0), (0, 1)]
    assert (not b.is_in_single_column(row))
    col = [(0, 1), (1, 1)]
    assert (b.is_in_single_column(col))
    diag = [(0, 1), (1, 0)]
    assert (not b.is_in_single_column(diag))
    diag_2 = [(0, 15), (15, 0)]
    assert (not b.is_in_single_column(diag_2))
    long_row = [(0, 1), (0, 2), (0, 3), (0, 16)]
    assert (not b.is_in_single_column(long_row))
    long_col = [(1, 0), (2, 0), (3, 0), (16, 0)]  # Not Continuous
    assert (b.is_in_single_column(long_col))
コード例 #6
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_main_word():
    letters = 'cat'
    indecies = [(7, 7), (7, 8), (7, 9)]
    b = Board()
    assert (b.get_main_word(letters, indecies) == 'cat')
    b.place_tiles(letters, indecies)
    assert (b.get_main_word(letters, indecies) == 'cat')
    letters_2 = 'snake'
    indecies_2 = [(7, 10), (8, 10), (9, 10), (10, 10), (11, 10)]
    assert (b.get_main_word(letters_2, indecies_2) == 'snake')
コード例 #7
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_is_in_single_row():
    b = Board()
    one_elt = [(0, 0)]
    assert (b.is_in_single_row(one_elt))
    row = [(0, 0), (0, 1)]
    assert (b.is_in_single_row(row))
    col = [(0, 1), (1, 1)]
    assert (not b.is_in_single_row(col))
    diag = [(0, 1), (1, 0)]
    assert (not b.is_in_single_row(diag))
    diag_2 = [(0, 15), (15, 15)]
    assert (not b.is_in_single_row(diag_2))
    long_row = [(0, 1), (0, 2), (0, 3), (0, 16)]
    assert (b.is_in_single_row(long_row))
コード例 #8
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_all_spaces_available():
    b = Board()
    indecies = [(7, 7)]
    assert (b.all_spaces_available(indecies))
    indecies_2 = [(7, 8), (7, 9), (7, 10)]
    b.place_tiles('hih', indecies_2)
    assert (not b.all_spaces_available(indecies_2))
コード例 #9
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_score_letter():
    b = Board()
    letters = 'HELLO'
    indecies = [(7, 7), (7, 8), (7, 9), (7, 10), (7, 11)]
    assert (b.score_letter(letters, indecies, 7, 7) == 4)
    assert (b.score_letter(letters, indecies, 7, 8) == 1)
    assert (b.score_letter(letters, indecies, 7, 9) == 1)
    assert (b.score_letter(letters, indecies, 7, 10) == 1)
    assert (b.score_letter(letters, indecies, 7,
                           11) == 2)  # Double Letter Score there
コード例 #10
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_place_tiles():
    letters = 'hi'
    indecies = [(7, 7), (7, 8)]
    b = Board()
    assert (len(b.occupied) == 0)
    assert (len(b.bordering) == 1)  # Just center
    b.place_tiles(letters, indecies)
    assert (len(set(indecies).intersection(b.bordering)) == 0)
    assert (b.occupied == set(indecies))
    indecies_vert = [(4, 7), (5, 7), (6, 7)]
    b.place_tiles('who', indecies_vert)
    new_occ = set(indecies_vert).union(set(indecies))
    assert (b.occupied == new_occ)
    assert (b.bordering.intersection(new_occ) == set())
コード例 #11
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_board_init_special_char_cnt(n=15):
    """ Checks if the board has the correct numbers of special
       characters such as *, Triple Word Marker, etc """
    b = Board()
    assert (len(b.current_state) == n)
    for r in b.current_state:
        assert (len(r) == n)
    cntr_cnt = 0
    empty_cnt = 0
    dl_cnt = 0
    dw_cnt = 0
    tl_cnt = 0
    tw_cnt = 0
    for r in xrange(0, len(b.current_state)):
        for c in xrange(0, len(b.current_state[r])):
            val = b.current_state[r][c]
            if val == TRIPLE_WORD:
                tw_cnt += 1
            elif val == DOUBLE_WORD:
                dw_cnt += 1
            elif val == TRIPLE_LETTER:
                tl_cnt += 1
            elif val == DOUBLE_LETTER:
                dl_cnt += 1
            elif val == CENTER_MARKER:
                cntr_cnt += 1
            elif val == '':
                empty_cnt += 1
            else:
                return False
                # Raise error
    assert (tw_cnt == 8)
    assert (dw_cnt == 16)
    assert (tl_cnt == 12)
    assert (dl_cnt == 24)
    assert (cntr_cnt == 1)
    special_chars = cntr_cnt + dl_cnt + dw_cnt + tl_cnt + tw_cnt
    assert (empty_cnt == (n * n - special_chars))
コード例 #12
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_is_bordering_with_other_words():
    b = Board()
    center_row = [(7, 7), (7, 8), (7, 9), (7, 10), (7, 11)]
    b.place_tiles('hello', center_row)
    assert (not b.is_bordering_with_other_words([(7, 7)]))
    assert (b.is_bordering_with_other_words([(6, 7)]))
コード例 #13
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_score_main_word():
    b = Board()
    letters = 'HELLO'
    indecies = [(7, 7), (7, 8), (7, 9), (7, 10), (7, 11)]
    score = b.get_main_word_score(letters, indecies)
    assert (score == 18)  # Center mark at 7,7 and double letter at 7.11
コード例 #14
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_index_list_main_word():
    b = Board()
    letters = 'HELLO'
    indecies = [(7, 7), (7, 8), (7, 9), (7, 10), (7, 11)]
    new_index_list = b.get_index_list_main_word(letters, indecies)
    assert (new_index_list == indecies)
コード例 #15
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_corresponding_letter_index():
    b = Board()
    letters = 'HELLO'
    indecies = [(7, 7), (7, 8), (7, 9), (7, 10), (7, 11)]
    assert (b.get_corresponding_letter_index(letters, indecies, 7, 7) == 0)
コード例 #16
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_potential_word():
    letters = 'at'
    indecies = [(7, 7), (7, 8)]
    b = Board()
    assert (b.get_potential_word(7, 7, 7, 8, 'at') == 'at')
    b.place_tiles(letters, indecies)
    assert (b.get_potential_word(7, 7, 7, 8, 'at') == 'at')
    b.place_tiles('cs', [(7, 6), (7, 9)])
    assert (b.get_potential_word(7, 7, 6, 9, 'cs') == 'cats')
    b = Board()
    assert (b.get_potential_word(8, 11, 10, 10, 'nake') == 'nake')
    # Refresh
    letters = 'cat'
    indecies = [(7, 7), (7, 8), (7, 9)]
    b = Board()
    b.place_tiles(letters, indecies)
    letters_2 = 'snake'
    indecies_2 = [(7, 10), (8, 10), (9, 10), (10, 10), (11, 10)]
    assert (b.get_potential_word(7, 11, 10, 10, letters_2) == 'snake')
コード例 #17
0
ファイル: play_game.py プロジェクト: ahliu13/myscrabble
class Game:
    def __init__(self, player_names):
        self.bag = Bag()
        self.board = Board()
        self.players = []
        self.set_player_list(player_names)
        self.game_done = False
        self.player_turn_index = 0
        self.set_curr_player()
        self.fill_player_trays()

    def set_player_list(self, player_names):
        for name in player_names:
            self.players.append(Player(name))

    def fill_player_trays(self):
        for p in self.players:
            while len(p.tray) != MAX_TRAY_LEN:
                letter = self.bag.draw_random_letter()
                p.tray.append(letter)

    def set_curr_player(self):
        self.curr_player = self.players[
            0]  # TODO: update with get first player index(players, bag)

    def play_turn(self):
        """ Once we have set the current player, this logic directs the input to the appropriate
        submethod of play letters, pass, exchange, or end game"""
        self.print_turn_prompt()
        option = get_input()
        while option not in set({PLAY, PASS, EXCHANGE, END_GAME}):
            print 'Looks like the number you entered was invalid'
            print 'Please try again or hit ctrl C to abandon the game'
            option = get_input()
        if option == PLAY:
            self.play_letters()
        elif option == PASS:
            self.pass_turn()
        elif option == EXCHANGE:
            self.exchange_letters()
        else:
            self.game_done = True

    def print_turn_prompt(self):
        print 'Your turn {0}'.format(self.curr_player.name)
        print 'The board is:'
        print 'Your tray is {0}'.format(self.curr_player.tray)
        self.board.print_board_current_state()
        print 'Please Enter One of the Following Options:'
        print '{0}: Play'.format(PLAY)
        print '{0}: Pass'.format(PASS)
        print '{0}: Exchange Tiles'.format(EXCHANGE)
        print '{0}: End Game & Tally Score'.format(END_GAME)

    def play_letters(self):
        """ If the player opts to lay tiles, we enter here"""
        is_play_done = False
        while (not is_play_done):
            print 'Please enter the letters you would like to play capitalized and seperated by spaces'
            print 'For example A A B C'
            letters = get_letter_input_from_player().split()
            print 'Please enter the indecies you would like to play in the from (r,c) seperated by whitespace'
            print 'For example (1,2) (1,3) (1,4)'
            indecies_s = get_indecies_input_from_player().split()
            indecies = []
            for i in indecies_s:
                indecies.append(make_tuple(i))
            if self.board.is_valid_move(letters, indecies):
                score = self.board.score_play(letters, indecies)
                self.curr_player.score += score
                self.board.place_tiles(letters, indecies)
                for letter in letters:
                    self.curr_player.remove_letter(letter)
                # remove the tiles from the player tray
                self.refill_curr_player_tray()
                is_play_done = True
                print ''
                print(
                    'Awesome, you placed the letters, got a score of {0} and the '
                    'board is the following and now the board is the following'
                ).format(score)
                self.board.print_board_current_state()
                print ''
            else:
                print 'Looks like you may have entered the wrong format of either the letters or indecies - please try again'

    def pass_turn(self):
        print 'Passing'

    def exchange_letters(self):
        """The player provides a list of letters they would like to exchange - if valid,
        we swap them out for new ones"""
        is_exchange_done = False
        while (not is_exchange_done):
            print 'Please enter a list of tiles you would like to swap out'
            tiles = get_input().split()
            if self.can_exchange_tiles(tiles):
                for t in tiles:
                    self.curr_player.remove_letter(t)
                    self.bag.insert_letter(t)
                    new_lett = self.bag.draw_random_letter()
                    self.curr_player.tray.append(new_lett)
                print 'Your new tray is {0}'.format(self.curr_player.tray)
                is_exchange_done = True
                print
            else:
                print 'Oops, looks like your entry was invalid'

    def can_exchange_tiles(self, list_to_exchange):
        """ Checks whether the given list of letters to
            exchange are in the letter tray of the player
        """
        player_tray_set = set(self.curr_player.tray)
        for letter in list_to_exchange:
            if letter not in player_tray_set:
                return False
        return True

    def refill_curr_player_tray(self):
        """Re-fills the current player's tray once they have played letters"""
        while len(self.curr_player.tray) < MAX_TRAY_LEN:
            letter = self.bag.draw_random_letter()
            self.curr_player.tray.append(letter)

    def tally_game_score(self):
        """Tallys up and prints the player scores"""
        print 'Game is over - scores are as follows'
        scores = {}
        for p in self.players:
            if p.score in scores:
                scores[p.score].append(p.name)
            else:
                scores[p.score] = [p.name]
        print str(scores)

    def update_current_player(self):
        """We have an array of players - we increment till we reach
        the end of the list, then wrap back around
        """
        i = self.player_turn_index
        i = i + 1
        i = i % len(self.players)
        self.player_turn_index = i
        self.curr_player = self.players[i]

    def update_game_done(self):
        if len(self.curr_player.tray) == 0 and len(self.bag.letters) == 0:
            self.game_done = True
コード例 #18
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_is_word():
    b = Board()
    assert (b.is_word('hello'))
コード例 #19
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_get_row_and_col_bounds():  # TODO: comments as to what you're testing
    b = Board()
    letters = 'at'
    indecies = [(7, 7), (7, 8)]
    assert (b.get_left_column(letters, indecies, 7, 7) == 7)
    assert (b.get_right_column(letters, indecies, 7, 7) == 8)
    assert (b.get_top_row(letters, indecies, 7, 7) == 7)
    assert (b.get_bottom_row(letters, indecies, 7, 7) == 7)
    b.place_tiles(letters, indecies)
    assert (b.get_left_column(letters, indecies, 7, 7) == 7)
    assert (b.get_right_column(letters, indecies, 7, 7) == 8)
    assert (b.get_top_row(letters, indecies, 7, 7) == 7)
    assert (b.get_bottom_row(letters, indecies, 7, 7) == 7)
    letters = 'c'
    indecies = [(7, 6)]
    assert (b.get_left_column(letters, indecies, 7, 7) == 6)
    assert (b.get_right_column(letters, indecies, 7, 7) == 8)
    assert (b.get_top_row(letters, indecies, 7, 7) == 7)
    assert (b.get_bottom_row(letters, indecies, 7, 7) == 7)
    b.place_tiles(letters, indecies)
    assert (b.get_left_column(letters, indecies, 7, 7) == 6)
    assert (b.get_right_column(letters, indecies, 7, 7) == 8)
    assert (b.get_top_row(letters, indecies, 7, 7) == 7)
    assert (b.get_bottom_row(letters, indecies, 7, 7) == 7)
    letters = 'snake'
    indecies = [(7, 9), (8, 9), (9, 9), (10, 9), (11, 9)]
    # on the first letter of cats
    assert (b.get_left_column(letters, indecies, 7, 7) == 6)
    assert (b.get_right_column(letters, indecies, 7, 7) == 9)
    assert (b.get_top_row(letters, indecies, 7, 7) == 7)
    assert (b.get_bottom_row(letters, indecies, 7, 7) == 7)
    # from snake
    assert (b.get_left_column(letters, indecies, 8, 9) == 9)
    assert (b.get_right_column(letters, indecies, 8, 9) == 9)
    assert (b.get_top_row(letters, indecies, 8, 9) == 7)
    assert (b.get_bottom_row(letters, indecies, 8, 9) == 11)
コード例 #20
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_is_in_bounds():
    b = Board()
    assert (not b.is_in_bounds([(-1, -1)]))
    assert (b.is_in_bounds([(0, 0)]))
    assert (not b.is_in_bounds([(15, 15)]))
コード例 #21
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_board_setup():
    b = Board()
    assert (len(b.occupied) == 0)
    assert (len(b.bordering) == 1)
    assert ((7, 7) in b.bordering)
コード例 #22
0
ファイル: test_board.py プロジェクト: ahliu13/myscrabble
def test_letter_score():
    b = Board()
    assert (b.get_letter_value('O') == 1)