Advertisement
GeorgiLukanov87

Console_Game - Connecting Four

Oct 19th, 2022 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. # Just Run the code and enjoy :) !
  2. # The default board game SIZE is 6x7.
  3. # You can easily change the board-size on line = 106 and 107 by changing -> "rows_count" and "cows_count" with the size you wish!
  4.  
  5. from collections import deque
  6.  
  7.  
  8. class bcolors:
  9.     HEADER = '\033[95m'
  10.     OKBLUE = '\033[94m'
  11.     OKCYAN = '\033[96m'
  12.     OKGREEN = '\033[92m'
  13.     WARNING = '\033[93m'
  14.     FAIL = '\033[91m'
  15.     ENDC = '\033[0m'
  16.     BOLD = '\033[1m'
  17.     UNDERLINE = '\033[4m'
  18.  
  19.  
  20. class InvalidColumnError(Exception):
  21.     pass
  22.  
  23.  
  24. class FullColumnError(Exception):
  25.     pass
  26.  
  27.  
  28. # Print matrix.
  29. def print_matrix(M):
  30.     for sub_matrix in M:
  31.         print(f'{bcolors.HEADER}{bcolors.BOLD}[ {bcolors.ENDC}', end='')
  32.         for el in sub_matrix:
  33.             if el == 1:
  34.                 print(f'{bcolors.OKBLUE}{bcolors.BOLD}{el}{bcolors.BOLD}{bcolors.ENDC}', end=' ')
  35.             elif el == 2:
  36.                 print(f'{bcolors.FAIL}{bcolors.BOLD}{el}{bcolors.BOLD}{bcolors.ENDC}', end=' ')
  37.             else:
  38.                 print(f'{bcolors.OKGREEN}{el}{bcolors.ENDC}', end=' ')
  39.         print(f'{bcolors.HEADER}{bcolors.BOLD}]{bcolors.ENDC}')
  40.  
  41.  
  42. def validate_column_choice(selected_col_num, max_col_index):
  43.     # Verify player choice of column number of is correct.
  44.     if not (0 <= selected_col_num <= max_col_index):
  45.         raise InvalidColumnError
  46.  
  47.  
  48. def place_player_choice(M, selected_col_index, current_player_num):
  49.     # Place player marker on the spot.
  50.     # Check if the column  is full if so - throw error.
  51.     row_count = len(M)
  52.     for row_index in range(row_count - 1, -1, -1):
  53.         current_element = M[row_index][selected_col_index]
  54.         if current_element == 0:
  55.             M[row_index][selected_col_index] = current_player_num
  56.             return
  57.     raise FullColumnError
  58.  
  59.  
  60. def is_inside(r, c, N, M):
  61.     return 0 <= r < N and 0 <= c < M
  62.  
  63.  
  64. # All winning positions -> left,right,up,down and all diagonals.
  65. def check_for_win(ma, current_player):
  66.     win_happened = False
  67.     all_pos = {
  68.         'right': [(0, 1), (0, 2), (0, 3)],
  69.         'left': [(0, - 1), (0, - 2), (0, - 3)],
  70.         'up': [(- 1, 0), (- 2, 0), (- 3, 0)],
  71.         'down': [(1, 0), (2, 0), (3, 0)],
  72.  
  73.         'right_up': [(1, 1), (2, 2), (3, 3)],
  74.         'right_down': [(-1, 1), (-2, 2), (-3, 3)],
  75.         'left_up': [(-1, -1), (-2, -2), (-3, -3)],
  76.         'left_down': [(1, -1), (2, -2), (3, -3)],
  77.     }
  78.     for r_index in range(rows_count):
  79.         for c_index in range(cols_count):
  80.             if ma[r_index][c_index] == current_player:
  81.  
  82.                 for direction, positions in all_pos.items():
  83.                     win_steps = 0
  84.                     for curr_pos in positions:
  85.                         if not is_inside(r_index + curr_pos[0], c_index + curr_pos[1], rows_count, cols_count) or \
  86.                                 not ma[r_index + curr_pos[0]][c_index + curr_pos[1]] == current_player:
  87.                             continue
  88.                         else:
  89.                             win_steps += 1
  90.  
  91.                     if win_steps >= 3:
  92.                         win_happened = True
  93.                         # If True , break them all outer loops.
  94.                         break
  95.  
  96.                 if win_happened:
  97.                     break
  98.             if win_happened:
  99.                 break
  100.         if win_happened:
  101.             break
  102.  
  103.     return win_happened
  104.  
  105.  
  106. rows_count = 6
  107. cols_count = 7
  108. # create matrix
  109. matrix = [[0 for _ in range(cols_count)] for row_num in range(rows_count)]
  110. print_matrix(matrix)
  111. total_turns = 0
  112. turns = deque([1, 2])
  113. while True:
  114.     player_turn = turns[0]
  115.     try:
  116.         # Read column choice from input
  117.         colum_num = int(input(f"{bcolors.OKCYAN}Player {player_turn}{bcolors.ENDC}, please choose a column: ")) - 1
  118.         validate_column_choice(colum_num, cols_count - 1)
  119.         place_player_choice(matrix, colum_num, player_turn)
  120.         print_matrix(matrix)
  121.     except InvalidColumnError:
  122.         print(f"{bcolors.WARNING} This column is not valid. "
  123.               f"Please select a number between {bcolors.ENDC} "
  124.               f"{bcolors.HEADER}{bcolors.BOLD}1 and {cols_count}{bcolors.BOLD}{bcolors.ENDC} {bcolors.ENDC}")
  125.         continue
  126.     except ValueError:
  127.         print(f"{bcolors.WARNING} Please select a valid digit!{bcolors.ENDC}")
  128.         continue
  129.     except FullColumnError:
  130.         print(f"{bcolors.WARNING} This column is already full! Please, select other column number!{bcolors.ENDC}")
  131.         continue
  132.  
  133.     total_turns += 1
  134.     if total_turns >= 7:
  135.         if check_for_win(matrix, player_turn):
  136.             print()
  137.             if player_turn == 1:
  138.                 print(f'{bcolors.OKBLUE}{bcolors.BOLD}Player {player_turn} WINS :) !{bcolors.ENDC}')
  139.             elif player_turn == 2:
  140.                 print(f'{bcolors.FAIL}{bcolors.BOLD}Player {player_turn} WINS :) !{bcolors.ENDC}')
  141.             break
  142.  
  143.     turns.rotate()
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement