CS110: Fall 2021

Intro to Computer Programming with Python

CS110: Fall 2021

Assignments > HW5: Tic Tac Toe

Due on Mon, 11/22 @ 11:59PM. 8 Points.

Homework Starter Files

LEARNING OBJECTIVES:

  1. Practice working with loops
  2. Practice working with sequences
  3. Practice with logic
  4. Practice writing more complex programs

In this assignment, you will be making a game of tic tac toe, where a player will be playing the computer (see video below).

Your Tasks

Please write a program that accomplishes the following tasks:

  1. Ask the user whether they want to be x or o (and ensure your program honors their choice).
  2. Ask the user if they want to go first (and ensure your program honors their choice) Prompt the user for their move (they’ll enter a number from 1-9), which corresponds to where they want to go (as displayed below):
              7 | 8 | 9 
             ———————————
              4 | 5 | 6 
             ——————————— 
              1 | 2 | 3 
    
    • If the user does not enter a number from 1-9, tell them their entry was invalid and that they should try again (use try/except as well as if/else).
    • If the user enters a valid number that is not empty, tell them that slot is already taken.
  3. If it’s the computer’s turn, have the computer randomly select a square, given the available choices (using the random module).
  4. After each move, be sure to redraw the game board
  5. If there is a win / loss / tie, print an appropriate message and terminate the game

Caveats

There are many solutions on the Internet for this assignment. DO NOT USE THEM. We know what they are, and can tell if you copied all or parts of them. We want you to invent your own strategy, given what you have learned. Like all things, you will get out of this assignment what you put into it.

Extra Credit Options (up to 2 points total)

We highly recommend that you pursue these optional enhancements, to encourage you to think about one way (of many) that ‘artificial intelligence’ might work:

  1. Make your computer ‘smart’ so that the computer always selects the optimal move. To do this, consider how the computer could:
    • (1 point) Always block the user from winning (when possible)
    • (1 point) If the computer is about to win, make sure it selects the correct square!
    • (1 points): Other enhancements (see strategy discussion): https://www.wikihow.com/Win-at-Tic-Tac-Toe
  2. (1 point) Draw a line through the winning pattern on the game board.

Checklist Before You Submit

Before you submit, make sure you’ve tested that your program does the following: