DaStuckey

Python TicTacToe

Aug 30th, 2022 (edited)
1,623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | Source Code | 0 0
  1. import math
  2.  
  3.  
  4. def end_game():
  5. restart = input("Do you want to play again?:")
  6. if restart.lower() == "yes":
  7. game(0)
  8. elif restart.lower() == "no":
  9. quit()
  10. else:
  11. print("What was that again? PLease type Yes no No")
  12. end_game()
  13. quit() # End's the game if someone wins
  14.  
  15.  
  16. def build_board(board):
  17. for x in range(3):
  18. print(board[x][0], " | ", board[x][1], " | ", board[x][2])
  19. if x < 2:
  20. print("-------------") # Function to build board
  21.  
  22.  
  23. def check_win(board, is_tie):
  24. print(is_tie)
  25. for x in range(3):
  26. c = 0
  27. d = 0
  28. for y in range(2):
  29. if board[x][y] == board[x][y + 1] and board[x][y + 1] == "X" and board[x][y] != " " or board[y][x] == \
  30. board[y + 1][x] and board[y][x] \
  31. != " " and board[y + 1][x] == "X":
  32. c = c + 1
  33. if c == 2:
  34. print("X wins")
  35. end_game()
  36. if board[x][y] == board[x][y + 1] and board[x][y + 1] == "O" and board[x][y] != " " or board[y][x] == \
  37. board[y + 1][x] and board[y][x] \
  38. != " " and board[y + 1][x] == "O":
  39. d = d + 1
  40. if d == 2:
  41. print("O wins")
  42. end_game()
  43. if board[0][0] == board[1][1] and board[1][1] == board[2][2] and board[1][1] != " " or board[0][2] == board[1][1] \
  44. and board[1][1] == board[2][0] and board[1][1] != " ":
  45. print(board[1][1], " Wins!") # Checks if won by diagonal
  46. end_game()
  47. if is_tie == 9:
  48. print("Cat's game") # Checks if Cat's Game
  49. end_game()
  50.  
  51.  
  52. def who_turn_now(is_tie):
  53. if is_tie % 2 == 0: # Figures out if it is X or O's turn
  54. return "X"
  55. else:
  56. return "O"
  57.  
  58.  
  59. def position_finder(board, is_tie, turn):
  60. position = input("\n" + str(turn) + "'s Turn" + "\nWhere would you like to go:\n")
  61. try:
  62. spot = int(position) - 1
  63. if board[math.floor(spot / int(3))][spot % int(3)] == " ":
  64. board[math.floor(spot / int(3))][spot % int(3)] = turn
  65.  
  66. else:
  67. print("Please go in a blank spot")
  68. build_board(board)
  69. position_finder(board, is_tie, turn)
  70. except:
  71. print("Please Type a whole number 1-9") # Catches Errors
  72. build_board(board)
  73. position_finder(board, is_tie, turn)
  74.  
  75.  
  76. def game(is_tie):
  77. loop = 0 # Helps the game play
  78. board = [[" ", " ", " "],
  79. [" ", " ", " "],
  80. [" ", " ", " "]] # Board 2dArray
  81. build_board(board)
  82. while loop != 1: # Never ending loop
  83. turn = who_turn_now(is_tie) # plays the game
  84. position_finder(board, is_tie, turn)
  85. is_tie = is_tie + 1
  86. build_board(board)
  87. check_win(board, is_tie)
  88.  
  89.  
  90. game(0) # runs the game
  91.  
Advertisement
Add Comment
Please, Sign In to add comment