User Tools

Site Tools


user:bh011695:tictactoe

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
user:bh011695:tictactoe [2012/02/14 18:43] bh011695user:bh011695:tictactoe [2012/02/14 19:24] (current) bh011695
Line 1: Line 1:
 +====== 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() ====
 +<code>
 +print "*insert game instructions here*"
 +</code>
 +
 +==== firstPlayer() ====
 +<code>
 +select a number between 0 & 1 at random
 +
 +if the number is 0 then goesFirst is O
 +otherwise goesFirst is X
 +
 +return goesFirst
 +</code>
 +
 +==== choosePiece() ====
 +<code>
 +print "Play as X or O: "
 +get playerPiece
 +
 +return playerPiece
 +</code>
 +
 +==== createBoard() ====
 +<code>
 +board = []
 +
 +for i from 0 to 9
 + append " " to board
 +
 +return board
 +</code>
 +
 +==== displayBoard(gameBoard) ====
 +<code>
 +for i from 0 to 9
 + print gameBoard[i]
 +</code>
 +
 +==== legalMoves(gameBoard) ====
 +<code>
 +legalMoves = []
 +for i from 0 to 9
 +  if gameBoard[i] == EMPTY
 +    append gameBoard[i] to legalMoves
 +return legalMoves
 +</code>
 +
 +==== winner(gameBoard) ====
 +<code>
 +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
 +</code>
 +
 +==== playerMove(gameBoard, playerPiece) ====
 +<code>
 +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
 +</code>
 +
 +==== cpuMove(gameBoard, cpuPiece) ====
 +<code>
 +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
 +    
 +</code>
 +
 +==== nextTurn(currTurn) ====
 +<code>
 +if currTurn is "X"
 +  nextTurn = "O"
 +else
 +  nextTurn = "X"
 +return nextTurn
 +</code>
 +
 +==== congrats(gameBoard) ====
 +<code>
 +if winner(gameBoard) is not "Tie"
 +  print "Congratulations", winner(gameBoard), "you win!"
 +else
 +  "It's a tie!"
 +</code>