pawlack

Untitled

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