Lab 2
Breaking Bad (Code)

Objectives

Preliminary Setup

  1. Create a new PyCharm project. Remember to use your last name in the project name.
  2. Download the starter code and add it to your project. Please replace ??????? with your name in the @author tag.
  3. 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:
  1. 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 documentation official python documentation site.
  2. Run the code. What happens? Is the output correct?

Add a new test

  1. 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.
  2. Add code to main() so that your input2.txt is tested ALONG WITH 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.

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.

  1. Create a function called confirm_result that takes 2 inputs: a board and an indication of who should be declared winner ('X', 'O', or None). In other words, confirm_result takes an input for get_winner (the board) and an indication of the expected (correct) result ('X', 'O', or None): the two parameters form a test case.
  2. 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.
  3. 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.
  4. [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 at cassa@union.edu.

Ask for help if you're having problems!