131.05.Lab

py

School

Brigham Young University, Idaho *

*We aren’t endorsed by this school

Course

124

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

3

Uploaded by BrigadierHeat19682

Report
# 1. Name: # -your name- # 2. Assignment Name: # Lab 05 : Sudoku Draft # 3. Assignment Description: # -describe what this program is meant to do- # 4. What was the hardest part? Be as specific as possible. # -a paragraph or two about how the assignment went for you- # 5. How long did it take for you to complete the assignment? # -total time in hours including reading the assignment and submitting the program- import json # A blank Sudoku board. blank_board = [ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], ] def read_board(filename): '''Read the previously existing board from the file if it exists''' # Read the file if it exists. try: file = open(filename, "r") board_text = file.read() board_json = json.loads(board_text) file.close() # Return the board array out of the dictionary return board_json['board'] # Generate a blank board otherwise. except: return blank_board def write_board(board, filename): '''Write the board to a file''' with open(filename, "w") as file: # Place the board (an array) into a dictionary for JSON board_json = {} board_json['board'] = board board_text = json.dumps(board_json) file.write(board_text) file.close() def display_board(board): '''Display a Sudoku board on the screen''' # Print the header. print(" A B C D E F G H I")
# For each row... for row in range(9): if row == 3 or row == 6: print(" -----+-----+-----") print(row + 1, " ", end='') # For each column... for column in range(9): separator = [' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '\n'] print(board[row][column] if board[row][column] != 0 else ' ', end=separator[column]) def parse_input(coordinate): '''Turn "A3" into (2, 0) where the third value is whether it is valid''' # Convert the text 'B' into the value 1. column = ord(coordinate[0]) - ord('A') # Convert the text '3' into the value 2. row = int(coordinate[1]) - 1 # Return a tuple return (row, column) def string_from_coordinate(row, column): '''Get the friendly location from the input string''' return chr(column + ord('A')) + chr(row + ord('1')) def play_one_round(board): '''Play one round of Sudoku. Return False if we are finished with the game''' # Prompt for input. coordinate = input("> ") # Quit if the user said to quit. if coordinate == "Q": return False # Determine if the input is a coordinate. (row, column) = parse_input(coordinate) # Prompt for the number that goes at row,column. number = int(input("What number goes in " + string_from_coordinate(row, column) + "? ")) # Place the number in the board. board[row][column] = number return True # Start with a game. board = read_board(input("Where is your board located? ")) display_board(board) # Play the game until the user enters 'Q'. print("Specify a coordinate to edit or 'Q' to save and quit") while play_one_round(board): display_board(board)
# Write the board to a file. write_board(board, input("What file would you like to write to? "))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help