Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Based on this input.txt file: XOO OO XXX And this code: import tictactoe as ttt def __read_board_from_file(filename): board_file = open(filename) board_lines = board_file.read() board_file.close() return

Based on this input.txt file:

XOO OO XXX

And this code:

import tictactoe as ttt def __read_board_from_file(filename): board_file = open(filename) board_lines = board_file.read() board_file.close() return board_lines.split(' ') def main(): list_of_rows = __read_board_from_file("input.txt") board = ttt.TicTacToeBoard(list_of_rows) print(board) winner = board.get_winner() print ("Result: %s wins" % (str(winner))) if __name__ == "__main__": main()

Add a new test

Let's test the get_winner function some more by adding a second test. Create a new file "input2.txt" with a different board state than the one given in "input.txt". Make sure the two boards have different winners.

Add code to main() so that your input2.txt is tested IN ADDITION TO input.txt (i.e. don't remove the test using input.txt). Be sure you know what the correct answer is before you run the code.

Refactor and automate the tests

Notice that you have the same or very similar code repeated twice in order to test with both input boards. Also notice that we need to read and interpret the output to know if the program worked right; if it says X wins, we need to confirm that X should have been considered the winner, and this requires manually looking at the input file and requires thinking.

It would be nice if a) we didn't have repeated code and b) we could have the program tell us whether it works without having to interpret the results.

Let's fix this by extracting a helper function to run a single test and tell us if it passes.

Create a function in main.py called confirm_result that takes 2 parameters:

A tic tac toe board and

an indication of who should be declared winner (either a string 'X' or 'O' or the special value None -- note that None and None are not the same thing).

In other words, confirm_result takes the parameter for get_winner (the board) and the answer that get_winner is supposed to return (i.e. the expected result). These two together form a test case.

Write the code for confirm_result. It should test get_winner, print information about what is being tested, and print "PASS" if get_winner got the right answer (i.e. if the actual results from get_winner matches the expected results) and print FAIL otherwise.

Re-write main() so it uses your new confirm_result instead of calling get_winner directly. All printing should be removed from main() because confirm_result will do the appropriate printing. Notice how much shorter and clearer main() has become.

[Extra credit] Re-write main() so it does not read the boards from files (hard-code the boards directly into the python source instead). Now, add more tests to main() using confirm_result as appropriate.

Note: The code for get_winner is contained in another code file that is not needed to complete this assignment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions