PYTON PROGRAM IN PYTHON 3.3: Your program should work as follows: 1. Ask the use
ID: 3767800 • Letter: P
Question
PYTON PROGRAM IN PYTHON 3.3:
Your program should work as follows:
1. Ask the user for the name of a file that contains the board
2. Read in and store the board in a 2D list
3. Ask the user for a coordinate to start auto-filling from
-They may also choose to quit at this point
4. Ask the user for a symbol to auto-fill with
5. Ask the user if they would like to see a step-by-step of the auto-fill
6. Auto-fill the board starting at that point, using their chosen character
-This function must be recursive, and must make recursive calls to neighboring cells in the following order:
1. The cell above the current one
2. The cell to the right of the current one
3. The cell below the current one
4. The cell to the left of the current one
-If you don’t follow this order, your output won’t match ours
7. Print out the resulting board (and the step-by-step, if requested)
8. Allow the user to choose a new coordinate (or to quit)
You will need to validate the following things:
Getting the filename
o The filename must end in “.dat” or “.txt”
Getting the cells to start auto-filling from
o We guarantee the user will enter either
The character “Q”
Two integers, separated by a comma (e.g., 5,6 or -1,99 or 0,0 etc.)
o The row (the first number) must be
A valid index for the number of rows your board has (i.e., it starts at index 0, and goes up to index row_size – 1)
o The column (the second number) must be
A valid index for the number of columns your board has (i.e., it starts at index 0, and goes up to index column_size – 1)
o You must check both row and column
Getting the symbol to use in the auto-fill
o The symbol must be a single character
Getting the choice for step-by-step
o The user’s answer must be either “yes” or “no” in all lowercase.
def printBoard(board):
for i in board:
print(*i, sep='')
def autofill(board,x, y,):
if board[x][y] != "X":
board[x][y] = "X"
if x + 1 < len(board):
board = autofill(board,x+1, y)
if x - 1 >= 0:
board = autofill(board, x-1, y)
if y + 1 < len(board[x]):
board = autofill(board, x, y+1)
if y - 1 >= 0:
board = autofill(board, x, y-1)
return board
def main():
filename = input("Please enter a filename: ")
with open(filename, 'r') as f:
board = [list(line.strip()) for line in f]
printBoard(board)
square1 = ""
while square1 != "q":
square1 = input("Please enter a square to fill, or q to exit: ")
if square1 != "q":
x, y = map(int, square1.split(","))
x-=1 # note change
y-=1 # note change
board = autofill(board, x, y)
printBoard(board)
main()
This is the current code that I have. I have two problems I need to prompt the user to choose a symbol of their choice. The second is that I need to prompt the user to see if they would like to see each step of the recursion or not. If they choose to see each step then I need to print each separate step of the recursion. If they choose no the program can run normally. The file that is read via my program is a series of + - on the outer perimeter and = in the middle of the board.
+---+----- +
| | |
| | === |
| | |
+---+-----+
The board looks like this. Excuse its sloppiness.
Explanation / Answer
def Board_Design(board):
for i in board:
print(*i, sep='') # note change
def autofill(board,x, y):
if board[x][y] != "X":
board[x][y] = "X"
if x + 1 < len(board):
board = autofill(board,x+1, y)
if x - 1 >= 0:
board = autofill(board, x-1, y)
if y + 1 < len(board[x]):
board = autofill(board, x, y+1)
if y - 1 >= 0:
board = autofill(board, x, y-1)
return board
def main():
filename = input("Please enter a filename: ")
with open(filename, 'r') as f: # note change
board = [list(line.strip()) for line in f] # note change
Board_Design(board)
square1 = ""
while square1 != "q":
square1 = input("Please enter a square to fill, or q to exit: ")
if square1 != "q":
x, y = map(int, square1.split(",")) # note change
x-=1 # note change
y-=1 # note change
board = autofill(board, x, y) # note change
Board_Design(board)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.