Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. import random
  2.  
  3. def drawBoard(board):
  4. print(" | |")
  5. print(" " + board[7] + " | " + board[8] + " | " + board[9])
  6. print(" | |")
  7. print("-----------")
  8. print(" | |")
  9. print(" " + board[4] + " | " + board[5] + " | " + board[6])
  10. print(" | |")
  11. print("-----------")
  12. print(" | |")
  13. print(" " + board[1] + " | " + board[2] + " | " + board[3])
  14. print(" | |")
  15.  
  16. def inputPlayerLetter():
  17. letter = ""
  18. while not (letter == "X" or letter == "O"):
  19. print("Do you want to be X or O?")
  20. letter = input().upper()
  21.  
  22. if letter == "X":
  23. return ["X", "O"]
  24. else:
  25. return ["O", "X"]
  26.  
  27. def whoGoesFirst():
  28. if random.randint(0, 1) == 0:
  29. return "computer"
  30. else:
  31. return "player"
  32.  
  33. def playAgain():
  34. print("Do you want to play again? (yes or no)")
  35. return input().lower().startswith("y")
  36.  
  37. def makeMove(board, letter, move):
  38. board[move] = letter
  39.  
  40. def isWinner(bo, le):
  41. return ((bo[7] == le and bo == le and bo[9] == le) or
  42. (bo[4] == le and bo[5] == le and bo[6] == le) or
  43. (bo[1] == le and bo[2] == le and bo[3] == le) or
  44. (bo[7] == le and bo[4] == le and bo[1] == le) or
  45. (bo[8] == le and bo[5] == le and bo[2] == le) or
  46. (bo[9] == le and bo[6] == le and bo[3] == le) or
  47. (bo[7] == le and bo[5] == le and bo[3] == le) or
  48. (bo[9] == le and bo[5] == le and bo[1] == le))
  49.  
  50. def getBoardCopy(board):
  51. dupeBoard = []
  52.  
  53. for i in board:
  54. dupeBoard.append(i)
  55.  
  56. return dupeBoard
  57.  
  58. def isSpaceFree(board, move):
  59. return board[move] == " "
  60.  
  61. def getPlayerMove(board):
  62. move = ' '
  63. while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
  64. print("What is your next move? (1-9)")
  65. move = input()
  66. return int(move)
  67.  
  68. def chooseRandomMoveFromList(board, movesList):
  69. possibleMoves = []
  70. for i in movesList:
  71. if isSpaceFree(board, i):
  72. possibleMoves.append(i)
  73.  
  74. if len(possibleMoves) != 0:
  75. return random.choice(possibleMoves)
  76. else:
  77. return None
  78.  
  79. def getComputerMove(board, computerLetter):
  80. if computerLetter == "X":
  81. playerLetter = "O"
  82. else:
  83. playerLetter = "X"
  84.  
  85. for i in range(1, 10):
  86. copy = getBoardCopy(board)
  87. if isSpaceFree(copy, i):
  88. makeMove(copy, computerLetter, i)
  89. if isWinner(copy, computerLetter):
  90. return i
  91.  
  92. for i in range(1, 10):
  93. copy = getBoardCopy(board)
  94. if isSpaceFree(copy, i):
  95. makeMove(copy, playerLetter, i)
  96. if isWinner(copy, playerLetter):
  97. return i
  98.  
  99. move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
  100. if move != None:
  101. return move
  102.  
  103. if isSpaceFree(board, 5):
  104. return 5
  105.  
  106. return chooseRandomMoveFromList(board, [2, 4, 6, 8])
  107.  
  108. def isBoardFull(board):
  109. for i in range(1, 10):
  110. if isSpaceFree(board, i):
  111. return False
  112. return True
  113.  
  114. print("Welcome to Tic Tac Toe!")
  115.  
  116. while True:
  117. theBoard = [" "] * 10
  118. playerLetter, computerLetter = inputPlayerLetter()
  119. turn = whoGoesFirst()
  120. print("The " + turn + " will go first.")
  121. gameIsplaying = True
  122.  
  123. while gameIsplaying:
  124. if turn == "player":
  125. drawBoard(theBoard)
  126. move = getPlayerMove(theBoard)
  127. makeMove(theBoard, playerLetter, move)
  128.  
  129. if isWinner(theBoard, playerLetter):
  130. drawBoard(theBoard)
  131. print("Hooray! You have won the game!")
  132. gameIsplaying = False
  133. else:
  134. if isBoardFull(theBoard):
  135. drawBoard(theBoard)
  136. print("The game is a tie!")
  137. break
  138. else:
  139. turn = "computer"
  140.  
  141. else:
  142. move = getComputerMove(theBoard, computerLetter)
  143. makeMove(theBoard, computerLetter, move)
  144.  
  145. if isWinner(theBoard, computerLetter):
  146. drawBoard(theBoard)
  147. print("You lose.")
  148. gameIsplaying = False
  149. else:
  150. if isBoardFull(theBoard):
  151. drawBoard(theBoard)
  152. print("The game is a tie!")
  153. break
  154. else:
  155. turn = "player"
  156.  
  157. if not playAgain():
  158. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement