Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Based on a file that resembles a tic tac toe board: XXO XOX XOX Please refactor the following code based on the requirements below: def

Based on a file that resembles a tic tac toe board:

XXO XOX XOX

Please refactor the following code based on the requirements below:

def print_board(): num_rows = len(board) num_cols = len(board[0]) for row_num, row in enumerate(board): row_str = '' for col_num, marker in enumerate(row): row_str += marker if col_num < num_cols - 1: row_str += ' | ' print(row_str) if row_num < num_rows - 1: print('---------') def row_all_same(the_board, row): return (board[row][0] == board[row][1] == board[row][2]) def column_all_same(column): return (column[0] == column[1] == column[2]) def diagonal_all_same(diagonal): return (diagonal[0] == diagonal[1] == diagonal[2]) def get_back_slash(): return [board[i][i] for i in range(len(board))] def get_forward_slash(): return [board[len(board) - i - 1][i] for i in range(len(board))] def columns(board): num_cols = len(board[0]) num_rows = len(board) to_return = [] for i in range(num_cols): col_str = '' for j in range(num_rows): col_str += board[j][i] to_return.append(col_str) return to_return def check_winner(): global winner for row_num, row in enumerate(board): if row_all_same(board, row_num): winner = board[row_num][0] return for col in columns(board): if column_all_same(col): winner = col[0] return if diagonal_all_same(get_back_slash()): winner = board[0][0] return if diagonal_all_same(get_forward_slash()): winner = board[2][0] return inputfile = 'input.txt' file = open(inputfile) board = file.read().split(' ') file.close() print_board() winner = '' check_winner() if winner != '': print(winner + ' WINS!!!!') else: print("TIE GAME!!!!") 

Fix it

Make a copy of badmain.py, calling it goodmain.py. You'll be making changes to goodmain.py so that it is improved over badmain.py.

The main task of this is to improve the helper functions (and the code that calls them) in several ways. By the end of this, goodmain should do all that badmain does, but it should be better written.

In all of the following steps, change goodmain while leaving badmain unchanged.

Step 1: Remove uses of global variables.

In each of the functions, carefully look at each use of a variable. Determine if the variable used is one of the parameters to the function, a local variable, or a variable defined in the global scope. For any function that uses globals, rewrite the function so that it no longer uses globals:

There are two main ways to remove uses of globals. If the global is being used to give the result of the function, perhaps it can be returned instead? If the global is being used to give input to the function, perhaps we can add it as a parameter to the function?

Feel free to change the names of variables. You might find it easier to make the locals and parameters have different names than the globals.

Rewrite the calls to the function so that the program still works.

Note: don't just search for the 'global' keyword. Functions can still use global variables without those variables being explicitly marked as 'global'.

Make sure that goodmain still gets the same answers as badmain.

Step 2: Move the main-line code

Create a main function that contains the main-line code.

Make sure that goodmain still gets the same answers as badmain. Note: if the program crashes or has different behavior now, that probably means you missed a use of a global variable in the previous step.

Step 3: Add appropriate documentation

For each of the functions, add a docstring immediately after the function signature. Recall that a Python docstring should be of the form: does this, returns that In addition to the short description, if the function has parameters, or returns something, you should have :param: and :return: tags, respectively. Make sure each of your docstrings explains the behavior (i.e. what the function does), not the implementation (i.e. how it does it).

Make sure that goodmain still gets the same answers as badmain.

Step 4: [Extra Credit] Abstract some helpers

Several of the helpers all have the same logic. Improve the code by deciding which helpers can be combined and then replacing them with only one. You'll need to change the calls to those functions.

Make sure that goodmain still gets the same answers as badmain.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

Recognize and discuss the causes of culture shock

Answered: 1 week ago

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago

Question

Have ground rules been established for the team?

Answered: 1 week ago