viligen

workshop_Atanas

Feb 8th, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. class Player:
  2.     def __init__(self, name, sign):
  3.         self.name = name
  4.         self.sign = sign
  5.  
  6.  
  7. def read_player_name(helper_text):
  8.     while True:
  9.         player_name = input(helper_text).strip()
  10.         if player_name:
  11.             return player_name
  12.  
  13.  
  14. def read_players():
  15.     first_player_name = read_player_name('Player one name: ')
  16.     while True:
  17.         second_player_name = read_player_name(f'Player two name: ')
  18.         if second_player_name != first_player_name:
  19.             break
  20.  
  21.     while True:
  22.         first_player_sign = input(f'{first_player_name} would you like to play with "X" or "O"? ').upper()
  23.         if first_player_sign == 'X' or first_player_sign == 'O':
  24.             break
  25.  
  26.     second_player_sign = 'O' if first_player_sign == 'X' else 'X'
  27.  
  28.     return Player(first_player_name, first_player_sign), Player(second_player_name, second_player_sign)
  29.  
  30.  
  31. def print_board_numeration():
  32.     print('This is the numeration of the board:')
  33.     print('|  1  |  2  |  3  |')
  34.     print('|  4  |  5  |  6  |')
  35.     print('|  7  |  8  |  9  |')
  36.  
  37.  
  38. def init_board():
  39.     size = 3
  40.     result = []
  41.     for _ in range(size):
  42.         result.append([None] * size)
  43.     return result
  44.  
  45.  
  46. def is_valid_position(board, board_mapper, position):
  47.     if position < 1 or position > 9:
  48.         return False
  49.     row, col = board_mapper[position]
  50.     return board[row][col] is None
  51.  
  52.  
  53. def print_board(board):
  54.     for row in board:
  55.         print('|  ', end='')
  56.         print('  |  '.join([x if x is not None else ' ' for x in row]), end='')
  57.         print('  |')
  58.  
  59.  
  60. def has_won(board, sign):
  61.     # rows
  62.     for row in board:
  63.         # ['X', None, 'O']
  64.         # [True, False, False]
  65.         # ['X', 'X', 'X']
  66.         # [True, True, True]
  67.         if all([x == sign for x in row]):
  68.             return True
  69.  
  70.     # cols
  71.     for col in range(len(board)):
  72.         col_win = True
  73.         for row in range(len(board)):
  74.             if board[row][col] != sign:
  75.                 col_win = False
  76.                 break
  77.         if col_win:
  78.             return True
  79.  
  80.     # diagonals
  81.     left_diagonal_win = True
  82.     right_diagonal_win = True
  83.     for idx in range(len(board)):
  84.         if board[idx][idx] != sign:
  85.             left_diagonal_win = False
  86.         if board[idx][len(board) - 1 - idx] != sign:
  87.             right_diagonal_win = False
  88.     return left_diagonal_win or right_diagonal_win
  89.  
  90.  
  91. def is_draw(board):
  92.     for row in board:
  93.         if not all([x is not None for x in row]):
  94.             return False
  95.     return True
  96.  
  97.  
  98. first_player, second_player = read_players()
  99.  
  100. print_board_numeration()
  101.  
  102. print(f'{first_player.name} starts first!')
  103.  
  104. board = init_board()
  105. board_mapper = {
  106.     1: [0, 0],
  107.     2: [0, 1],
  108.     3: [0, 2],
  109.     4: [1, 0],
  110.     5: [1, 1],
  111.     6: [1, 2],
  112.     7: [2, 0],
  113.     8: [2, 1],
  114.     9: [2, 2],
  115. }
  116.  
  117. turn = 1
  118.  
  119. while True:
  120.     current_player = first_player if turn % 2 != 0 else second_player
  121.     position = int(input(f'{current_player.name} choose a free position [1-9]: '))
  122.  
  123.     if not is_valid_position(board, board_mapper, position):
  124.         continue
  125.  
  126.     row, col = board_mapper[position]
  127.     board[row][col] = current_player.sign
  128.  
  129.     print_board(board)
  130.  
  131.     if has_won(board, current_player.sign):
  132.         print(f'{current_player.name} won!')
  133.         break
  134.  
  135.     if is_draw(board):
  136.         print('draw!')
  137.         break
  138.  
  139.     turn += 1
Advertisement
Add Comment
Please, Sign In to add comment