This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:bh011695:tictactoe [2012/02/14 18:43] – bh011695 | user: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/ | ||
+ | | winner() | gameBoard | winner or " | ||
+ | | 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 " | ||
+ | </ | ||
+ | |||
+ | ==== 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 | ||
+ | | ||
+ | |||
+ | 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 " | ||
+ | return None | ||
+ | </ | ||
+ | |||
+ | ==== playerMove(gameBoard, | ||
+ | < | ||
+ | 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, | ||
+ | < | ||
+ | boardCopy = gameBoard[: | ||
+ | BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7) | ||
+ | |||
+ | print " | ||
+ | |||
+ | //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 " | ||
+ | nextTurn = " | ||
+ | else | ||
+ | nextTurn = " | ||
+ | return nextTurn | ||
+ | </ | ||
+ | |||
+ | ==== congrats(gameBoard) ==== | ||
+ | < | ||
+ | if winner(gameBoard) is not " | ||
+ | print " | ||
+ | else | ||
+ | " | ||
+ | </ |