Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Author: Matt Murch
  4. Last Modified: 01/22/2017
  5. Description: Two Player, Terminal Tic Tac Toe
  6. """
  7. import random
  8. from sys import exit
  9.  
  10. playerOne = "X"
  11. playerTwo = "O"
  12. board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
  13. 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)]
  14.  
  15. #Prints the current 3x3 board
  16. def printBoard():
  17. print board[0], "|", board[1], "|", board [2], "\n", board[3], "|", board[4], "|", board[5], "\n", board[6], "|", board[7], "|", board[8]
  18.  
  19. #Resets board to be filled with ' '
  20. def clearBoard():
  21. for a in range(0, len(board)):
  22. board[a] = ' '
  23.  
  24. #Checks if any of the winning combinations equivalent and not blank. If so, it prints that that occupant is the winner and returns true.
  25. #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
  26. def checkGameEnd():
  27. for a in winningCombinations:
  28. if board[a[0]] == board[a[1]] == board[a[2]] != ' ':
  29. print "\n", board[a[0]], "is the winner!"
  30. printBoard()
  31. return True
  32. if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
  33. print "\nTie Game!"
  34. printBoard()
  35. return True
  36. else:
  37. return False
  38.  
  39. #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.
  40. def getValidChoice():
  41. while True:
  42. try:
  43. a = input()
  44. if board[a] == ' ' and a >= 0:
  45. return a
  46. else:
  47. print "Invalid move. Try again"
  48. except:
  49. print "Invalid move. Try again"
  50.  
  51. #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.
  52. #Then checks if the game is over. If so, returns to menu. Otherwise, recalls itself with the other player as the argument.
  53. def playerTurn(player):
  54. print "______________________________________________________________"
  55. print "______________________________________________________________"
  56. printBoard()
  57. print "\nIt is", player, "'s turn. Enter the position you would like to mark."
  58. print "0 | 1 | 2\n3 | 4 | 5\n6 | 7 | 8"
  59. board[getValidChoice()] = player
  60. if checkGameEnd():
  61. mainMenu()
  62. elif player == playerOne:
  63. playerTurn(playerTwo)
  64. else:
  65. playerTurn(playerOne)
  66.  
  67. #Sets variable to random integer 0 or 1. If 0, calls playerOne's turn. Otherwise, calls playerTwo's turn.
  68. def newGame():
  69. x = random.randint(0, 1)
  70. if x == 0:
  71. playerTurn(playerOne)
  72. else:
  73. playerTurn(playerTwo)
  74.  
  75. #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.
  76. def mainMenu():
  77. print "TIC TAC TOE\nEnter 1: Start a New Game\nEnter 2: Quit"
  78. while True:
  79. inputValue = raw_input()
  80. if inputValue == "1":
  81. clearBoard()
  82. newGame()
  83. elif inputValue == "2":
  84. exit()
  85. else:
  86. print "Sorry, that is not a valid input. Try again."
  87.  
  88. if __name__ == "__main__":
  89. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement