User Tools

Site Tools


user:bh011695:start:tic_tac_toe

Tic Tac Toe Game in Python

Necessary Functions

  1. menu() – Offers the following options: read instructions, play a game, or exit
  2. instructions() – prints the instructions to the user
  3. playGame() – runs through a single player tic tac toe game against the computer
  4. newBoard() – creates a new playing board
  5. printBoard() – prints the current board to the terminal
  6. isWinner() – checks to see if the latest move wins
  7. playerMove() – alllows the player to make a move
  8. cpuMove() – allows the computer to make a move

Function Logic

menu()

print "'i' to read instructions, 'p' to play, or 'q' to quit: "
get choice

if choice is 'i'
  instructions()
else if choice is 'p'
  playGame()
else
  return
  

instructions()

print "Welcome to the Tic Tac Toe help menu. Playing a game is simple. "
print "When you, the player, are prompted to make a move, enter a number "
print "from 0 - 8. The board will look as follows:"
print " 0 | 1 | 2 "
print "-----------"
print " 3 | 4 | 5 "
print "-----------"
print " 6 | 7 | 8 "
print "If a square already has a piece in it, an X or O will be displayed."
print "If you try to occupy a square that already has a piece in it, an "
print "error message will be displayed and you will be prompted to make "
print "another move."
print "The object of the game is to have 3 of your pieces occupy 3 spaces "
print "in a row. The first to do this wins the game. If neither player has "
print "managed to occupy 3 spaces in a row horizontally, vertically, or "
print "diagonally then a tie occurs."

newBoard()

board = new Board

for i = 0 to 9
  board[i] = ""

return board

printBoard()

print “\n\t”, board[0], “|”, board[1], “|”, board[2] print “\t”, “——” print “\t”, board[3], “|”, board[4], “|”, board[5] print “\t”, “——” print “\t”, board[6], “|”, board[7], “|”, board[8], “\n”

user/bh011695/start/tic_tac_toe.txt · Last modified: 2012/02/02 20:08 by bh011695