2nd Semester Intro to Computer Science

What you will learn

Project

Using Python, students will create a Tic-Tac-Toe game. This project has two parts

  1. Designing the game so that two users can play Tic-Tac-Toe against one another.
  2. Creating a Tic-Tac-Toe checker which will check the board to see if Xs or Os have won the game.

Overview

Tic-Tac-Toe is a game in which one player draws X’s and another player draws O’s inside a set of nine squares and each player tries to be the first to fill a row, column, or diagonal of squares with either X’s or O’s. We will be writing an interactive Tic-Tac-Toe program. At the end of each turn the computer will check to see if X’s have won the game or if the O’s have won the game.

Behavior

Implementation Details

Alternate Project

Modified by Brian Weinfeld

Using loops, variables and functions in Python, you will create a Tic-Tac-Toe game for two human players.

Overview

Tic-Tac-Toe is a game in which one player draws X’s and another player draws O’s inside a set of nine squares and each player tries to be the first to fill a row, column, or diagonal of squares with either X’s or O’s.

Behavior

Welcome to Tic-Tac-Toe!
'X' what is your name: Alice
'O' what is your name: Bob
Game Start
 | |
 | |
 | |

Alice what row is your move: 1
Alice what col is your move: 1
 | |
 |X|
 | |

Bob what row is your move: 0
Bob what col is your move: 0
O| |
 |X|
 | |

Alice what row is your move: 2
Alice what col is your move: 0
O| |
 |X|
X| |

Bob what row is your move: 2
Bob what col is your move: 0
Invalid move. Try again.

Bob what row is your move: 4
Bob what col is your move: 4
Invalid move. Try again.

Bob what row is your move: 0
Bob what col is your move: 1
O|O|
 |X|
X| |

Alice what row is your move: 0
Alice what col is your move: 2
O|O|X
 |X|
X| |

X wins!

Would you like to play again? no
Goodbye

Implementation Details

The gameboard should be stored in a list of lists. Each of the internal lists represents one row of the gameboard. If the element in the list is the empty string (the empty string is ‘’), then the spot is unoccupied. If the element in the list is an ‘X’ or an ‘O’, then it is occupied by that player.

Begin by completing the below functions. Do your best to ensure that these functions work properly before moving onto the rest of the project.

def print_gameboard(gameboard):
  # finish
def check_winner(gameboard):
  # finish
def check_tied(gameboard):
  # finish
def make_move(gameboard, name, symbol):
  # finish

After completing and testing the above functions, complete the following steps to create the main game loop.

Challenge

Super Challenge

The super challenge will require knowledge that has not been taught yet. You will need to do additional research on your own. Good luck!

Rubric

Student Name: ___________ Date: ____________

Student uses for loops (4.01)

3 2 1 0 Points
Student uses 3 or more for loops correctly Student use three for loops correctly Student uses one or two for loops correctly No evidence the student can use a for loop  
Student always scopes the variables in for loops correctly Student usually scopes the variables in for loops correctly Student rarely scopes the variables in for loops correctly. No evidence Student can correctly scope variables in a for loop  
      Sub Total  

Students can c use the range and len functions (4.02)

3 2 1 0 Points
Student always uses the range and len functions correctly Student generally uses the range and len functions correctly Sometimes Student uses the range and len functions correctly No evidence to that the student can use the range or len functions correctly  
      Sub Total  

Students can use nested for loops (4.03)

3 2 1 0 Points
Student uses 2 or more nested for loops Student uses 1 nested for loop Student attempted a nested for loop, but it was not correct No evidence that Student can correctly use a nested for loop  
      Sub Total  

Student can use nested lists (4.04)

3 2 1 0 Points
Student correctly uses nested lists __ Student attempts to use a nested list No evidence that the student can use a nested list  
      Sub Total  

Student can decompose a problem to create a program from a brief

3 2 1 0 Points
Student program runs without error The students program has a few errors, but it does not impact the program’s functionality Student program has errors that impact the program’s functionality Student program is not functional  
____ Students submitted documentation showing planning for most variables and functions. Students submitted documentation showing planning for a few variables and functions. No evidence of planning  
      Sub Total  

Student uses naming/ syntax conventions and comments to increase readability

2 1 0 Points
Syntax conventions are generally used Sometimes syntax conventions are used No evidence of syntax conventions to aid in code readability  
All variables have clear names Some variables have clear names No evidence of using variable names to aid in code read ability  
___ Student comments aid code readability No evidence of using comments to aid in code readability.  
    Sub Total  

Final Grade

Points Possible Points Earned x Weight Total Points
31 __ X ___  

Check your understanding

Pick an answer for each question. You can try again until you get it right.

1. Why store a 3 by 3 game board as a list of lists?

  • A flat list cannot hold strings
  • Lists of lists run faster
  • A row and column can each be reached with an index
  • It takes up less code than variables

Two indexes, such as board[row][column], match how a grid works.

2. In board[1][2], what do the two numbers mean?

  • Row 1 and column 2, counting from 0
  • Column 1 and row 2, counting from 1
  • Value 1 and value 2
  • The first and second items of the board

The first index picks the inner list (row) and the second picks the item in it (column).

3. Which structure is most natural for visiting every square of the board?

  • A single if statement
  • A while True loop only
  • A loop over the rows with an inner loop over the columns
  • Calling len once

A nested loop touches each cell of a list of lists.

Downloads