Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. # Tic Tac Toe for RC
  2. print("Welcome to a game of Tic Tac Toe!")
  3. print("-Coded in Python 3 by Ian Ross for The Recurse Center-\n")
  4.  
  5. # Displays board in its current state
  6. def display_board(board):
  7. print(board[0], "|", board[1], "|", board[2])
  8. print("---------")
  9. print(board[3], "|", board[4], "|", board[5])
  10. print("---------")
  11. print(board[6], "|", board[7], "|", board[8])
  12.  
  13. # Checks to see if there is a winning combo of squares
  14. def is_winner(letter):
  15. # Horizontal combos
  16. if board[0] == letter and board[1] == letter and board[2] == letter:
  17. return True
  18. if board[3] == letter and board[4] == letter and board[5] == letter:
  19. return True
  20. if board[6] == letter and board[7] == letter and board[8] == letter:
  21. return True
  22.  
  23. # Vertical combos
  24. if board[0] == letter and board[3] == letter and board[6] == letter:
  25. return True
  26. if board[1] == letter and board[4] == letter and board[7] == letter:
  27. return True
  28. if board[2] == letter and board[5] == letter and board[8] == letter:
  29. return True
  30.  
  31. # Diagonal combos
  32. if board[0] == letter and board[4] == letter and board[8] == letter:
  33. return True
  34. if board[6] == letter and board[4] == letter and board[2] == letter:
  35. return True
  36.  
  37. # Checks to make sure any given square is taken
  38. def is_space_free(square):
  39. return board[square] == " "
  40.  
  41. # Returns true if there is no longer a square without an X or O
  42. def is_board_full():
  43. for square in board:
  44. if square == " ":
  45. return False
  46. return True
  47.  
  48. def get_move(letter):
  49. print("\nType which square you'd like to fill on the board")
  50. print("1 is the top left, 5 is the middle, and 9 is the bottom right \n")
  51.  
  52. # Represented as 1 - 9 as it is easier for the user than 0 - 8
  53. square = int(input("\nWhich square do you choose for " + letter +"? (1 - 9) : "))
  54. return (square - 1)
  55.  
  56. def failed_move():
  57. print("Sorry, that square is taken! You have lost your turn!\n")
  58.  
  59. def make_move(square, letter):
  60. if is_space_free(square):
  61. board[square] = letter
  62. else:
  63. failed_move()
  64.  
  65. # Main loop (besides the game loop) to potentially allow for resetting with an AI
  66. while True:
  67. begin = input("Would you like to play a game? (Yes/No) : ")
  68. if begin.lower() == "yes":
  69. playing_game = True
  70. elif begin.lower() == "no":
  71. print("That's too bad! Not sure why you ran this program, to be honest...")
  72. playing_game = False
  73.  
  74. # Setting the board
  75. # Representation
  76. board = [" ", " ", " ", # [0, 1, 2,
  77. " ", " ", " ", # 3, 4, 5,
  78. " ", " ", " "] # 6, 7, 8]
  79. letter = "X"
  80.  
  81. # Game loop
  82. while playing_game:
  83. # First Turn
  84. if letter == "X":
  85. # Display the board
  86. display_board(board)
  87. move = get_move(letter)
  88. make_move(move, letter)
  89.  
  90. # Before the letter switches it goe sthrough a win/draw check
  91. if is_winner(letter):
  92. display_board(board)
  93. print("\nCongratulations! " + letter + " won the game!")
  94. break
  95. else:
  96. if is_board_full():
  97. display_board(board)
  98. print("\nThe game is a draw!")
  99. break
  100. else:
  101. letter = "O"
  102.  
  103. elif letter == "O":
  104. # Display the board
  105. display_board(board)
  106. move = get_move(letter)
  107. make_move(move, letter)
  108.  
  109. if is_winner(letter):
  110. display_board(board)
  111. print("\nCongratulations! " + letter + " won the game!")
  112. break
  113. else:
  114. letter = "X"
  115. break
  116.  
  117. print("\nThanks for checking out my little project!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement