Advertisement
AbidBari

Tic tac toe but o not printing :'(

Jul 26th, 2020
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. import random
  2.  
  3. botin1 = random.randint(1, 9) - 1
  4. botin2 = random.randint(1, 9) - 1
  5. botin3 = random.randint(1, 9) - 1
  6. botin4 = random.randint(1, 9) - 1
  7. botin5 = random.randint(1, 9) - 1
  8.  
  9. #--------GLobal Variables--------
  10.  
  11. Board = ["-","-","-",
  12.          "-","-","-",
  13.          "-","-","-",]
  14.  
  15. Winner = None
  16. game_running = True
  17. current_player = "X"
  18.  
  19. #--------Display board--------
  20. def show_board():
  21.     print(Board[0] + "|" + Board[1] + "|" + Board[2])
  22.     print(Board[3] + "|" + Board[4] + "|" + Board[5])
  23.     print(Board[6] + "|" + Board[7] + "|" + Board[8])
  24.  
  25. #--------running the game-------
  26.  
  27. def play_game():
  28.     show_board()
  29.  
  30.     while game_running:
  31.         user_input_turn(current_player)
  32.         checking_if_game_over()
  33.         switch_player()
  34.     if Winner == "X" or Winner == "O":
  35.         print(Winner + " won.")
  36.     elif Winner == None:
  37.         print("tie")
  38. #--------Win or Tie---------
  39.  
  40. def checking_if_game_over():
  41.     check_for_winner()
  42.     check_if_tie()
  43.  
  44. def check_for_winner():
  45.     global Winner
  46.     #check rows
  47.     row_winner = check_rows()
  48.     #check columns
  49.     column_winner = check_columns()
  50.     #check diags
  51.     diag_winner = check_diags()
  52.  
  53.     #check the winner
  54.     if row_winner:
  55.         Winner = row_winner
  56.     elif column_winner:
  57.         Winner = column_winner
  58.     elif diag_winner:
  59.         Winner = diag_winner
  60.     return
  61. def check_rows():
  62.     global game_running
  63.     row_1 = Board[0] == Board[1] == Board[2] != "-"
  64.     row_2 = Board[3] == Board[4] == Board[5] != "-"
  65.     row_3 = Board[6] == Board[7] == Board[8] != "-"
  66.     if row_1 or row_2 or row_3:
  67.         game_running = False
  68.         if row_1:
  69.             return Board[0]
  70.         if row_2:
  71.             return Board[3]
  72.         if row_3:
  73.             return Board[7]
  74.     return
  75.  
  76. def check_columns():
  77.     global game_running
  78.     column_1 = Board[0] == Board[3] == Board[6] != "-"
  79.     column_2 = Board[1] == Board[4] == Board[7] != "-"
  80.     column_3 = Board[2] == Board[5] == Board[8] != "-"
  81.     if column_1 or column_2 or column_3:
  82.         game_running = False
  83.         if column_1:
  84.             return Board[0]
  85.         if column_2:
  86.             return Board[1]
  87.         if column_3:
  88.             return Board[2]
  89.     return
  90.  
  91. def check_diags():
  92.     global game_running
  93.     diag_1 = Board[0] == Board[4] == Board[8] != "-"
  94.     diag_2 = Board[2] == Board[4] == Board[6] != "-"
  95.     if diag_1 or diag_2:
  96.         game_running = False
  97.         if diag_1:
  98.             return Board[0]
  99.         if diag_2:
  100.             return Board[2]
  101.         return
  102.  
  103. #------checking tie------
  104. def check_if_tie():
  105.     global  game_running
  106.  
  107.     if "-" not in Board:
  108.         game_running = False
  109.     return
  110.  
  111. #------User move----------
  112.  
  113. def user_input_turn(player):
  114.     print(player + "'s turn.")
  115.     if current_player == "O":
  116.         if Board.count("X") == 1:
  117.             position = botin1
  118.         elif Board.count("X") == 2:
  119.             position = botin2
  120.         elif Board.count("X") == 3:
  121.             position = botin3
  122.         elif Board.count("X") == 4:
  123.             position = botin4
  124.         elif Board.count("X") == 5:
  125.             position = botin5
  126.         valid = False
  127.         while not valid:
  128.             if Board.count("X") == 5:
  129.                 position = botin1
  130.             elif Board.count("X") == 4:
  131.                 position = botin2
  132.             elif Board.count("X") == 1:
  133.                 position = botin3
  134.             elif Board.count("X") == 2:
  135.                 position = botin4
  136.             elif Board.count("X") == 3:
  137.                 position = botin5
  138.         if Board[position] == "-":
  139.               valid = True
  140.         else:
  141.             valid = False
  142.         Board[position] = player
  143.     else:
  144.         position = input("Choose a position from 1 to 9: ")
  145.         valid = False
  146.         while not valid:
  147.             while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  148.                 print("Please put an integer between 1 and 9 :(")
  149.                 position = input("Choose a position from 1 to 9: ")
  150.             position = int(position) - 1
  151.             if Board[position] == "-":
  152.                 valid = True
  153.             else:
  154.                 print("You can't go there,choose an empty position")
  155.         Board[position] = player
  156.  
  157.     show_board()
  158.  
  159. #------Switching current player-------
  160.  
  161. def switch_player():
  162.     global current_player
  163.     if current_player == "X":
  164.         current_player = "O"
  165.     else:
  166.         current_player = "X"
  167.     return
  168.  
  169. play_game()
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement