Breaking Bad (Code)
- Practice making test cases and test suites
- Get more practice writing helper functions
- Create a new PyCharm project. Remember to use your last name in the
- Download the starter code and add it to
your project. Please replace ??????? with your name in the @author tag.
- Download a sample tic tac toe board and add
it to the project.
Have a look around
Now that you have a project and some code, explore to see what you have:
- Read the code. Can you figure out what each line is doing? If there
are functions you don't know, look them up at the
python documentation site.
- Run the code. What happens? Is the output correct?
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
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
Let's fix this by extracting a helper function to run a single test and
tell us if it passes.
- Create a function called confirm_result that takes 2
parameters: a board and an indication of who should be declared winner
(either a string 'X' or 'O' or the special value None). In other words,
confirm_result takes an input for get_winner (the
board) and the answer that get_winner is supposed to return (i.e
the expected result). The two parameters form a test case.
- Write the code for confirm_result. It should test get_winner and print
"PASS" if the correct winner (or tie) is indicated, and should print
"FAIL" if get_winner gets an incorrect answer.
- 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). Now, add more
tests to main() using confirm_result as appropriate.
How to turn in this lab
Before turning in any program in this class, remember this mantra:
Just because it works doesn't mean it's good.
Part of your grade will also come from things like how understandable
and readable your code is. You will also be graded on the neatness,
presentation, and style of your program code.
For all labs, turn in only an electronic version. Make a tarball
(see Lab 1) or zip file containing your entire project and email it to me
Ask for help if you're having problems!