====== Tic Tac Toe Game ======
===== Functions =====
^ Function ^ Parameters ^ Return Value ^ Purpose |
| isntr() |None| None | display instructions |
| firstPlayer() | None | goesFirst| selects which piece will move first |
| choosePiece() | None | playerPiece | allows the player to choose a playing piece |
| createBoard() | None | gameBoard | creates a game board |
| displayBoard() | gameBoard | None | prints a game board to the screen |
| legalMoves() | gameBoard | legalMoves | finds/returns a list of legal moves |
| winner() | gameBoard | winner or "TIE" | checks for and returns a winner |
| playerMove() | gameBoard, playerPiece | playerMove | allows player to move and returns space of that move |
| cpuMove() | gameBoard, cpuPiece | cpuMove | allows cpu to move and returns space of that piece |
| nextTurn() | currTurn | nextPiece | selects next piece to move and returns that piece |
| congrats() | winner | None | congratulations the game's winner |
===== Pseudocode =====
==== instr() ====
print "*insert game instructions here*"
==== firstPlayer() ====
select a number between 0 & 1 at random
if the number is 0 then goesFirst is O
otherwise goesFirst is X
return goesFirst
==== choosePiece() ====
print "Play as X or O: "
get playerPiece
return playerPiece
==== createBoard() ====
board = []
for i from 0 to 9
append " " to board
return board
==== displayBoard(gameBoard) ====
for i from 0 to 9
print gameBoard[i]
==== legalMoves(gameBoard) ====
legalMoves = []
for i from 0 to 9
if gameBoard[i] == EMPTY
append gameBoard[i] to legalMoves
return legalMoves
==== winner(gameBoard) ====
WINS = ((0, 1, 2), (3, 4, 5), (6, 7, 8),
(0, 3, 6), (1, 4, 7), (2, 5, 8),
(0, 4, 8), (2, 4, 6))
for row from 0 to 8
if gameBoard[row[0]] == gameBoard[row[1]] == gameBoard[row[2]] != " ":
winner = gameBoard[row[0]]
return winner
else if no empty spaces
return "Tie"
return None
==== playerMove(gameBoard, playerPiece) ====
print "Enter a move (0-8): "
get move
while move is not from 0 to 8 and board[move] is not in legalMoves
print "Enter a move (0-8): "
get move
return move
==== cpuMove(gameBoard, cpuPiece) ====
boardCopy = gameBoard[:]
BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
print "Taking square"
//If computer can win
for move in legalMoves(boardCopy)
boardCopy[move] = computer
if winner(boardCopy) is computer
print move
return move
boardCopy[move] = " "
//if the player can win
for move in legalMoves(boardCopy)
boardCopy[move] = computer
if winner(boardCopy) is player
print move
return move
boardCopy[move] = " "
//otherwise no winner pick best square
for move in BEST_MOVES
if move in legalMoves(boardCopy)
print move
return move
==== nextTurn(currTurn) ====
if currTurn is "X"
nextTurn = "O"
else
nextTurn = "X"
return nextTurn
==== congrats(gameBoard) ====
if winner(gameBoard) is not "Tie"
print "Congratulations", winner(gameBoard), "you win!"
else
"It's a tie!"