pawlack

Untitled

Nov 28th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | None | 0 0
  1. import sys
  2. from random import randrange
  3.  
  4. X = "X"
  5. O = "O"
  6. E = " "
  7. game = [[0,0,0],[0,0,0],[0,0,0]]
  8. avaible_moves = [["a0", "b0", "c0"], ["a1", "b1", "c1"], ["a2", "b2", "c2"]]
  9. move_was_valid = True
  10. winning_player = 1
  11. ai_moves = ["a0", "b0", "c0", "a1", "b1", "c1", "a2", "b2", "c2"]
  12.  
  13. welcome_message = """
  14. ####################################
  15. #                                  #
  16. #          TIC - TAC - TOE         #
  17. #    by Pawelek                    #
  18. #                     v 0.02       #
  19. ####################################
  20.  
  21. proper input example: b2, or a1
  22.  
  23. """
  24.  
  25. def DisplayGameMap():
  26.     print("    A    B    C")
  27.     for count, row in enumerate(game):
  28.         print(count, row)
  29.  
  30. def PurgeGameMap():
  31.     for x in range(len(game)):
  32.         for y in range(len(game[x])):
  33.             game[x][y] = E
  34.     global ai_moves
  35.     ai_moves = ["a0", "b0", "c0", "a1", "b1", "c1", "a2", "b2", "c2"]
  36.  
  37. def Player_1_move():
  38.     DisplayGameMap()
  39.     chosen_row = 0
  40.     chosen_column = 0
  41.     move_available = False
  42.     global winning_player
  43.     winning_player = 1
  44.     players_move = input("Player 1 Choose your move: ")
  45.  
  46.  
  47.     for x in range(len(avaible_moves)):
  48.         for y in range(len(avaible_moves[x])):
  49.             if avaible_moves[x][y] == players_move:
  50.                 chosen_row = x
  51.                 chosen_column = y
  52.                 move_available = True
  53.  
  54.  
  55.     if move_available == True and game[chosen_row][chosen_column] == E:
  56.         game[chosen_row][chosen_column] = X
  57.         move_was_valid = True
  58.         ai_moves.remove(players_move)
  59.         game_loop(2)
  60.  
  61.     elif move_available == True and game[chosen_row][chosen_column] != E:
  62.         print("WARNING Field already taken")
  63.         move_was_valid = False
  64.         game_loop(1)
  65.  
  66.     elif move_available != True:
  67.         print("WARNING Invalid move")
  68.         move_was_valid = False
  69.         game_loop(1)
  70.    
  71. def Player_2_move():
  72.     DisplayGameMap()
  73.     chosen_row = 0
  74.     chosen_column = 0
  75.     move_available = False
  76.     global winning_player
  77.     winning_player = 2
  78.     players_move = ai_moves[randrange(0, len(ai_moves)-1)]
  79.     print("Player 2 move: ", players_move)
  80.  
  81.     for x in range(len(avaible_moves)):
  82.         for y in range(len(avaible_moves[x])):
  83.             if avaible_moves[x][y] == players_move:
  84.                 chosen_row = x
  85.                 chosen_column = y
  86.                 move_available = True
  87.  
  88.  
  89.     if move_available == True and game[chosen_row][chosen_column] == E:
  90.         game[chosen_row][chosen_column] = O
  91.         move_was_valid = True
  92.         game_loop(1)
  93.    
  94.     if move_available == True and game[chosen_row][chosen_column] != E:
  95.         print("WARNING Field already taken")
  96.         move_was_valid = False
  97.         game_loop(2)
  98.  
  99.     elif move_available != True:
  100.         print("WARNING Invalid move")
  101.         move_was_valid = False
  102.         game_loop(2)
  103.  
  104.  
  105. def game_loop(player_no):
  106.     check_for_winners()
  107.     if player_no == 1:
  108.         Player_1_move()
  109.     elif player_no == 2:
  110.         Player_2_move()
  111.  
  112. def ask_for_replay():
  113.     player_answer = input("Do you want to play again? [Y/N]: ")
  114.     if player_answer == "Y" or player_answer == "y":
  115.         PurgeGameMap()
  116.         game_loop(1)
  117.     elif player_answer == "N" or player_answer == "n":
  118.         sys.exit()
  119.     else:
  120.         print("Invalid Input")
  121.         ask_for_replay()
  122.  
  123. def check_for_winners():
  124.     if check_for_horizontal_winner() == True or chceck_for_vertical_winner() == True or check_for_diagonal_winner() == True:
  125.         DisplayGameMap()
  126.         print("Congratulations, Player ", winning_player, " has won!")
  127.         ask_for_replay()
  128.     elif check_for_tie() == True:
  129.         DisplayGameMap()
  130.         print("There is a tie")
  131.         ask_for_replay()
  132.  
  133.  
  134.  
  135.  
  136. def check_for_horizontal_winner():
  137.     there_is_winner = False
  138.     for row in game:
  139.         if row.count(row[0]) == len(row) and row[0] != E:
  140.             #print("WINNER!"")
  141.             there_is_winner = True
  142.     return there_is_winner
  143.  
  144. def chceck_for_vertical_winner():
  145.     there_is_winner = False
  146.     for i in range(len(game[0])):
  147.         check = []
  148.         for row in game:
  149.             check.append(row[i])
  150.         if check.count(check[0]) == len(check) and check[0] != E:
  151.             #print("WInner!")
  152.             there_is_winner = True
  153.     return there_is_winner
  154.  
  155. def check_for_diagonal_winner():
  156.     there_is_winner = False
  157.     diags = []
  158.     for ix in range(len(game)):
  159.         diags.append(game[ix][ix])
  160.     if diags.count(diags[0]) == len(diags) and diags[0] != E:
  161.         there_is_winner = True
  162.  
  163.     diags = []
  164.     for col, row, in enumerate(reversed(range(len(game)))):
  165.         diags.append(game[row][col])
  166.     if diags.count(diags[0]) == len(diags) and diags[0] != E:
  167.         there_is_winner = True
  168.  
  169.     return there_is_winner
  170.  
  171. def check_for_tie():
  172.     there_is_tie = True
  173.     for row in range(len(game)):
  174.         for column in range(len(game[row])):
  175.             if game[row][column] == E:
  176.                 there_is_tie = False
  177.     return there_is_tie
  178.  
  179.  
  180.  
  181. print(welcome_message)      
  182. PurgeGameMap()
  183. game_loop(1)
Advertisement
Add Comment
Please, Sign In to add comment