Advertisement
bAngelov

Connect 4 (Workshop)

Oct 6th, 2023 (edited)
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. ROWS = 6
  2. COLS = 7
  3. WINNER_LENGTH = 4
  4.  
  5.  
  6. class FullColumnError(Exception):
  7.     pass
  8.  
  9.  
  10. def print_matrix(matrix):
  11.     for row in matrix:
  12.         print(row)
  13.  
  14.  
  15. def is_valid_column_choice(selected_column_index):
  16.     return 0 <= selected_column_index < COLS
  17.  
  18.  
  19. def place_player_number(column_index, matrix, player_number):
  20.     for row_index in range(ROWS - 1, -1, -1):
  21.         if matrix[row_index][column_index] == 0:
  22.             matrix[row_index][column_index] = player_number
  23.             return row_index, column_index
  24.     else:
  25.         raise FullColumnError
  26.  
  27.  
  28. def is_valid_place(row, col):
  29.     return 0 <= row < ROWS and 0 <= col < COLS
  30.  
  31.  
  32. def is_winner(matrix, row, col, player):
  33.     begin = col - WINNER_LENGTH if col - WINNER_LENGTH > 0 else 0
  34.     subrow, subcol, left_diagonal, right_diagonal = matrix[row][begin:col + WINNER_LENGTH], [], [], []
  35.  
  36.     for current_row in range(row - WINNER_LENGTH, row + WINNER_LENGTH):
  37.         if is_valid_place(current_row, col):
  38.             subcol.append(matrix[current_row][col])
  39.         if is_valid_place(current_row, col - row + current_row):
  40.             left_diagonal.append(matrix[current_row][col - row + current_row])
  41.         if is_valid_place(current_row, col + row - current_row):
  42.             right_diagonal.append(matrix[current_row][col + row - current_row])
  43.  
  44.     return any(str(player) * WINNER_LENGTH in ''.join(map(str, lis)) for lis in
  45.                (subrow, subcol, left_diagonal, right_diagonal))
  46.  
  47.  
  48. matrix = [[0 for _ in range(COLS)] for _ in range(ROWS)]
  49. print_matrix(matrix)
  50.  
  51. player = 1
  52. while True:
  53.     try:
  54.         selected_column_number = int(input(f"Player {player}, please choose a column: "))
  55.         selected_column_index = selected_column_number - 1
  56.         if not is_valid_column_choice(selected_column_index):
  57.             raise ValueError
  58.         current_row, current_col = place_player_number(selected_column_index, matrix, player)
  59.         if is_winner(matrix, current_row, current_col, player):
  60.             print(f"Player {player} wins!")
  61.             print_matrix(matrix)
  62.             break
  63.         print_matrix(matrix)
  64.     except ValueError:
  65.         print(f"Player {player}, please select number between 1 and {COLS}")
  66.         continue
  67.     except FullColumnError:
  68.         print(f"Player {player}, this column is full, please select another one")
  69.         continue
  70.  
  71.     player += 1
  72.     player = 2 if player % 2 == 0 else 1
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement