Python Forum
Beginner - help for an exercice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner - help for an exercice
#1


Hi guys, i was following the course : Learn to program, the fundamuntales - By Toronto on Coursera
And i made the mistake to stop the program near from the end, because i wanted to focus on maths for few weeks, now i'm quite lost even if i watched every videos again, and so it's hard for me to make this assignment, it's the last one

Here's my task (sorry but it's a big lecture) :
For A3, you will implement a word search game. The game involves a rectangular board of uppercase letters that is read from a file. For example, here are the file contents representing a (tiny) 2 row by 4 column board:
1 ANTT
2 XSOB
The game also involves a non-empty words list read from a file. For example, here are example file contents for a words list:

To make it a bit more challenging, there may be words in the words list that do not appear in the board, and the word list is not shown to the players.
1 ANT
2 BOX
3 SOB
4 TO
The object of the game is for the players to view the board and find words (remember that the words list is unknown to the players). Words may be contained in rows (from left to right) or columns (from top to bottom), but not backwards. When a player correctly guesses a word that occurs in the words list, that player is awarded points according to a scoring system described in the starter code. The game ends when all words on the board that appear in the words list have been guessed.

The player with the highest score wins.

The words from the words list and the letters of the board are made up of alphabetic, uppercase characters.

Representing a board and a words list in Python

A board is a list of list of str such as [['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']].

So i have 4 files :
board1.txt which contains the board :
EFJAJCOWSS
....
wordlist1.txt which contains the words i have to find :
COWS
...

a3.py :
Quote:"""A board is a list of list of str. For example, the board
ANTT
XSOB
is represented as the list
[['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']]

A word list is a list of str. For example, the list of words
ANT
BOX
SOB
TO
is represented as the list
['ANT', 'BOX', 'SOB', 'TO']
"""


def is_valid_word(wordlist, word):
""" (list of str, str) -> bool

Return True if and only if word is an element of wordlist.

>>> is_valid_word(['ANT', 'BOX', 'SOB', 'TO'], 'TO')
True
"""


def make_str_from_row(board, row_index):
""" (list of list of str, int) -> str

Return the characters from the row of the board with index row_index
as a single string.

>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
'ANTT'
"""


def make_str_from_column(board, column_index):
""" (list of list of str, int) -> str

Return the characters from the column of the board with index column_index
as a single string.

>>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
'NS'
"""


def board_contains_word_in_row(board, word):
""" (list of list of str, str) -> bool

Return True if and only if one or more of the rows of the board contains
word.

Precondition: board has at least one row and one column, and word is a
valid word.

>>> board_contains_word_in_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'SOB')
True
"""

for row_index in range(len(board)):
if word in make_str_from_row(board, row_index):
return True

return False


def board_contains_word_in_column(board, word):
""" (list of list of str, str) -> bool

Return True if and only if one or more of the columns of the board
contains word.

Precondition: board has at least one row and one column, and word is a
valid word.

>>> board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'NO')
False
"""


def board_contains_word(board, word):
""" (list of list of str, str) -> bool

Return True if and only if word appears in board.

Precondition: board has at least one row and one column.

>>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
True
"""


def word_score(word):
""" (str) -> int

Return the point value the word earns.

Word length: < 3: 0 points
3-6: 1 point per character for all characters in word
7-9: 2 points per character for all characters in word
10+: 3 points per character for all characters in word

>>> word_score('DRUDGERY')
16
"""


def update_score(player_info, word):
""" ([str, int] list, str) -> NoneType

player_info is a list with the player's name and score. Update player_info
by adding the point value word earns to the player's score.

>>> update_score(['Jonathan', 4], 'ANT')
"""


def num_words_on_board(board, words):
""" (list of list of str, list of str) -> int

Return how many words appear on board.

>>> num_words_on_board([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], ['ANT', 'BOX', 'SOB', 'TO'])
3
"""


def read_words(words_file):
""" (file open for reading) -> list of str

Return a list of all words (with newlines removed) from open file
words_file.

Precondition: Each line of the file contains a word in uppercase characters
from the standard English alphabet.
"""


def read_board(board_file):
""" (file open for reading) -> list of list of str

Return a board read from open file board_file. The board file will contain
one row of the board per line. Newlines are not included in the board.
"""

and a3driver.py :
Quote: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 words list.
words_file = askopenfile(mode='r', title='Select word list file')
words = a3.read_words(words_file)
words_file.close()

# Load the board.
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)

So my task is to complete the a3.py functions

i suppose i have to start by read the files (board1 and wordlist1) that's also the first function to complete i guess (the one without name and body)
should i call tkinter ? should i directly open it with open() ? and which approach should i use

Thanks for your help, sorry for the long post..
Reply
#2
Since you can't use 'A3driver.py' until you have 'A3.py', then creating 'A3.py' would be the logical first step.  Since there is no import or need for tkinter in 'A3.py', there is no need to worry about it at this point. The functions themselves are fairly well explained as to what their purpose is, it is up to you to create the code that results in what is described in the description. The best way to approach this, imho, is to look over the entire script and make sure you understand what each function does. If a function 'stands' on it's own (requires nothing from another function) write those first. If a function requires the output from another function, you need to write the function that supply's the output before you write the one that requires the output.

If you get stuck, we are here to help. Be sure to post your code, output and any errors (in their entirety) between the proper tags. There are enough links on this site that you can click for information on how to properly post your code.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Arithmetic exercice with python pythonuser1 4 2,057 Apr-11-2020, 04:00 PM
Last Post: pythonuser1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020