Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- Author: Matt Murch
- Last Modified: 01/22/2017
- Description: Two Player, Terminal Tic Tac Toe
- """
- import random
- from sys import exit
- playerOne = "X"
- playerTwo = "O"
- board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
- winningCombinations = [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)]
- #Prints the current 3x3 board
- def printBoard():
- print board[0], "|", board[1], "|", board [2], "\n", board[3], "|", board[4], "|", board[5], "\n", board[6], "|", board[7], "|", board[8]
- #Resets board to be filled with ' '
- def clearBoard():
- for a in range(0, len(board)):
- board[a] = ' '
- #Checks if any of the winning combinations equivalent and not blank. If so, it prints that that occupant is the winner and returns true.
- #If not, it checks if all 9 spaces on the board are occupied by either "X" or "O". If so, it prints a tie game and returns true. Otherwise, it returns false
- def checkGameEnd():
- for a in winningCombinations:
- if board[a[0]] == board[a[1]] == board[a[2]] != ' ':
- print "\n", board[a[0]], "is the winner!"
- printBoard()
- return True
- if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
- print "\nTie Game!"
- printBoard()
- return True
- else:
- return False
- #Requests an input. If the input is an empty spot on the board, it returns the value. Otherwise, it prints to try again. It will loop until a valid input is given.
- def getValidChoice():
- while True:
- try:
- a = input()
- if board[a] == ' ' and a >= 0:
- return a
- else:
- print "Invalid move. Try again"
- except:
- print "Invalid move. Try again"
- #Prints the current board, the current player's turn, and the 'key' board. Then sets the returned value from validChoice to the current player's letter in the board.
- #Then checks if the game is over. If so, returns to menu. Otherwise, recalls itself with the other player as the argument.
- def playerTurn(player):
- print "______________________________________________________________"
- print "______________________________________________________________"
- printBoard()
- print "\nIt is", player, "'s turn. Enter the position you would like to mark."
- print "0 | 1 | 2\n3 | 4 | 5\n6 | 7 | 8"
- board[getValidChoice()] = player
- if checkGameEnd():
- mainMenu()
- elif player == playerOne:
- playerTurn(playerTwo)
- else:
- playerTurn(playerOne)
- #Sets variable to random integer 0 or 1. If 0, calls playerOne's turn. Otherwise, calls playerTwo's turn.
- def newGame():
- x = random.randint(0, 1)
- if x == 0:
- playerTurn(playerOne)
- else:
- playerTurn(playerTwo)
- #Prints two options. If option 1 is input, calls a newGame. If option 2 is input, exits the program. Loops until valid input is given.
- def mainMenu():
- print "TIC TAC TOE\nEnter 1: Start a New Game\nEnter 2: Quit"
- while True:
- inputValue = raw_input()
- if inputValue == "1":
- clearBoard()
- newGame()
- elif inputValue == "2":
- exit()
- else:
- print "Sorry, that is not a valid input. Try again."
- if __name__ == "__main__":
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement