viligen

workshop_Ines

Feb 7th, 2022
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.25 KB | None | 0 0
  1. class InvalidColumnError(Exception):
  2.     pass
  3.  
  4.  
  5. class FullColumnError(Exception):
  6.     pass
  7.  
  8.  
  9. def print_matrix(ma):
  10.     # print matrix
  11.     for el in ma:
  12.         print(el)
  13.  
  14.  
  15. def validate_column_choice(selected_column_num, max_column_index):
  16.     # verify player choice of column number of is correct
  17.     if not (0 <= selected_column_num <= max_column_index):
  18.         raise InvalidColumnError
  19.  
  20.  
  21. def place_player_choice(ma, selected_column_index, player_num):
  22.     # Place player marker on the spot.
  23.     # Check if the column  is full if so - throw error.
  24.  
  25.     rows_count = len(ma)
  26.     for row_index in range(rows_count - 1, -1, -1):
  27.         current_element = ma[row_index][selected_column_index]
  28.         if current_element == 0:
  29.             ma[row_index][selected_column_index] = player_num
  30.             return row_index, selected_column_index
  31.     raise FullColumnError
  32.  
  33.  
  34. def is_player_num(ma, row, col, player_num):
  35.     if col < 0 or row < 0:
  36.         return False
  37.     try:
  38.         if ma[row][col] == player_num:
  39.             return True
  40.     except IndexError:
  41.         return False
  42.     return False
  43.  
  44.  
  45. def is_horizontal(ma, row, col, player_num, slots_count):
  46.     """
  47.    We check for both sides, because we can have input like this:
  48.    1
  49.    1
  50.    2
  51.    2
  52.    4
  53.    4
  54.    3
  55.    If we do not check for both there will be winner, but we can not now that.
  56.    Also we have to check for this condition:
  57.    1
  58.    1
  59.    2
  60.    2
  61.    5
  62.    5
  63.    6
  64.    6
  65.    7
  66.    7
  67.    3
  68.    [True, True, True, False, True, True, True] - This is not a winner!
  69.    """
  70.     right = []
  71.     for index in range(slots_count):
  72.         if is_player_num(ma, row, col+index, player_num):
  73.             right.append(True)
  74.         else:
  75.             break
  76.     left = []
  77.     for index in range(slots_count):
  78.         if is_player_num(ma, row, col-index, player_num):
  79.             left.append(True)
  80.         else:
  81.             break
  82.     # count_right = [ for index in range(slots_count) if ].count(True)
  83.     # count_left = [is_player_num(ma, row, col-index, player_num) for index in range(slots_count)].count(True)
  84.     # It should be strict '>' because we are counting the current element as well
  85.     return len(left + right) > slots_count
  86.  
  87.  
  88. def is_right_diagonal(ma, row, col, player_num, slots_count):
  89.     """
  90.    We check for both (up right and left down) so we can look for the same problem -
  91.    adding element which is not only to one side with 4 but for both
  92.    """
  93.     right_up_count = [is_player_num(ma, row-index, col+index, player_num) for index in range(slots_count)].count(True)
  94.     left_down_count = [is_player_num(ma, row+index, col-index, player_num) for index in range(slots_count)].count(True)
  95.     return (right_up_count + left_down_count) > 4
  96.  
  97.  
  98. def is_left_diagonal(ma, row, col, player_num, slots_count):
  99.     """
  100.    We check for both (up left and right down) so we can look for the same problem -
  101.    adding element which is not only to one side with 4 but for both
  102.    """
  103.     left_up_count = [is_player_num(ma, row-index, col-index, player_num) for index in range(slots_count)].count(True)
  104.     right_down_count = [is_player_num(ma, row+index, col+index, player_num) for index in range(slots_count)].count(True)
  105.     return (left_up_count + right_down_count) > 4
  106.  
  107.  
  108. def is_winner(ma, row, col, player_num, slots_count=4):
  109.     """
  110.    We check for horizontal (both sides)
  111.    Only down (because we fill the matrix from bottom to top).
  112.    Check for right and left diagonal
  113.    """
  114.     is_down = all([is_player_num(ma, row+index, col, player_num) for index in range(slots_count)])
  115.     if any(
  116.             [
  117.                 is_horizontal(ma, row, col, player_num, slots_count),
  118.                 is_right_diagonal(ma, row, col, player_num, slots_count),
  119.                 is_left_diagonal(ma, row, col, player_num, slots_count),
  120.                 is_down,
  121.             ]
  122.     ):
  123.         return True
  124.     return False
  125.  
  126.  
  127. rows_count = 6
  128. cols_count = 7
  129.  
  130. # create matrix
  131. matrix = [[0 for _ in range(cols_count)] for row_num in range(rows_count)]
  132. # Print initial board
  133. print_matrix(matrix)
  134.  
  135. player_num = 1
  136. while True:
  137.     # Decide correct player num (only 1 and 2 are possible - only 2 players)
  138.     player_num = 2 if player_num % 2 == 0 else 1
  139.     try:
  140.         # Read column choice from input
  141.         colum_num = int(input(f"Player {player_num}, please choose a column: ")) - 1
  142.         validate_column_choice(colum_num, cols_count-1)
  143.         row, col = place_player_choice(matrix, colum_num, player_num)
  144.         if is_winner(matrix, row, col, player_num):
  145.             print_matrix(matrix)
  146.             print(f"The winner is player {player_num}")
  147.             break
  148.         print_matrix(matrix)
  149.     except InvalidColumnError:
  150.         # Not in range of the game 1-7
  151.         print(f"This column is not valid. Please select a "
  152.               f"number between {1} and {cols_count}")
  153.         continue
  154.     except ValueError:
  155.         # Not a valid number
  156.         print("Please select a valid digit!")
  157.         continue
  158.     except FullColumnError:
  159.         # This is already a full column
  160.         print(f"This column is already full! Please, select other column number!")
  161.         continue
  162.  
  163.     # Only if the turn was successful, we go to the next player
  164.     player_num += 1
  165.  
Advertisement
Add Comment
Please, Sign In to add comment