-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha3_driver.py
98 lines (69 loc) · 2.84 KB
/
a3_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
from tkinter.filedialog import askopenfile
import a3
def print_board(board):
''' (list of str) -> NoneType
Display the contents of board.
'''
for row_idx in range(len(board)):
print(a3.make_str_from_row(board, row_idx))
def get_players_list():
'''() -> list of [str, int] list
Prompt the player(s) to enter their names and return a list of player info
as a two-item list with name and score, respectively.
'''
players = []
player = input('Enter player 1 name: ')
while player.strip() or not players:
player = player.strip()
if player in players:
print("A player by that name is already playing.")
if player:
players.append([player, 0])
if players:
print('Leave a blank player name to begin playing.')
player = input('Enter player {num} name: '.format(num=len(players) + 1))
return players
def play_game(players, board, words):
''' (list of [str, int] list, list of list of str, list of str) -> NoneType
Play the game with players, board and words.
'''
num_remaining = a3.num_words_on_board(board, words) - len(found_words)
player_num = 0
while num_remaining > 0:
print_headers(players, board, found_words, num_remaining)
guess = input("[{player}] Enter a word (or blank to pass): ".format(
player=players[player_num % len(players)][0]))
guess = guess.strip().upper()
if a3.is_valid_word(words, guess) and a3.board_contains_word(board, guess) and \
not guess in found_words:
a3.update_score(players[player_num % len(players)], guess)
found_words.append(guess)
num_remaining = a3.num_words_on_board(board, words) - len(found_words)
player_num += 1
print("Game over!\n")
def print_headers(players, board, found_words, num_remaining):
''' (list of [str, int] list, list of list of str, list of str, int) -> NoneType
Play the score, board, and some other details.
'''
print_score(players)
print_board(board)
print('Words remaining: {num} words left.'.format(num=num_remaining))
print('Words found: ' + (' '.join(found_words) or 'No words found, yet.'))
def print_score(players):
''' (list of [str, int] list) -> NoneType
Print the scores for each of the players.
'''
for name, score in players:
print(' ' + str(score).rjust(3) + '\t' + name)
# Load the word list and board
words_file = askopenfile(mode='r', title='Select word list file')
words = a3.read_words(words_file)
words_file.close()
board_file = askopenfile(mode='r', title='Select board file')
board = a3.read_board(board_file)
board_file.close()
found_words = []
players = get_players_list()
play_game(players, board, words)
print_score(players)