viligen

tic_tac_toe_console_game

Feb 9th, 2022
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 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_players():
  8.     while True:
  9.         first_player_name = input("First player name: ").strip()
  10.         if first_player_name:
  11.             break
  12.     while True:
  13.         second_player_name = input("Second player name: ").strip()
  14.         if second_player_name != first_player_name and second_player_name:
  15.             break
  16.     while True:
  17.         first_player_sign = input(f"{first_player_name} would you like to play with 'X' or 'O'? ").upper()
  18.         if first_player_sign in ['X', 'O']:
  19.             break
  20.     second_player_sign = 'X' if first_player_sign == 'O' else 'O'
  21.     return Player(first_player_name, first_player_sign), Player(second_player_name, second_player_sign)
  22.  
  23.  
  24. def print_num_state():
  25.     print("""This is the numeration of the board:
  26. |  1  |  2  |  3  |
  27. |  4  |  5  |  6  |
  28. |  7  |  8  |  9  |""")
  29.  
  30.  
  31. def board_init_state(size_):
  32.     board_ = []
  33.     for _ in range(size_):
  34.         board_.append([None] * size)
  35.     return board_
  36.  
  37.  
  38. def print_current_board(board_):
  39.     for row_ in board_:
  40.         print('|  ', end='')
  41.         print('  |  '.join([s if s else ' ' for s in row_]), end='')
  42.         print('  |')
  43.     return board_
  44.  
  45.  
  46. def is_valid_choice(board_, board_mapper_, choice):
  47.     if choice < 1 or choice > 9:
  48.         return False
  49.     elif board_[board_mapper_[choice][0]][board_mapper_[choice][1]]:
  50.         return False
  51.     return True
  52.  
  53.  
  54. def is_win_rows(board_):
  55.     for row_ in board_:
  56.         if len(set(row_)) == 1 and set(row_) != {None}:
  57.             return True
  58.     return False
  59.  
  60.  
  61. def is_win_diagonals(board_, current_player_):
  62.     diagonal_1, diagonal_2 = [], []
  63.     for idx in range(len(board_)):
  64.         diagonal_1.append(board_[idx][idx] == current_player_.sign)
  65.         diagonal_2.append(board[idx][len(board_) - 1 - idx] == current_player_.sign)
  66.     return all(diagonal_1) or all(diagonal_2)
  67.  
  68.  
  69. def is_win_columns(board_, current_player_):
  70.     for col_ in range(len(board_)):
  71.         current_column = []
  72.         for row_ in range(len(board_)):
  73.             current_column.append(board_[row_][col_] == current_player_.sign)
  74.         if all(current_column):
  75.             return True
  76.     return False
  77.  
  78.  
  79. def is_draw(board_):
  80.     for row_ in board_:
  81.         if None in row_:
  82.             return False
  83.     return True
  84.  
  85.  
  86. board_mapper = {1: [0, 0], 2: [0, 1], 3: [0, 2],
  87.                 4: [1, 0], 5: [1, 1], 6: [1, 2],
  88.                 7: [2, 0], 8: [2, 1], 9: [2, 2]}
  89.  
  90. first_player, second_player = read_players()
  91. size = 3
  92. print_num_state()
  93. print(f"{first_player.name} starts first!")
  94.  
  95. board = board_init_state(size)
  96. turn = 1
  97.  
  98. while True:
  99.     current_player = first_player if turn % 2 != 0 else second_player
  100.     while True:
  101.         current_choice = int(input(f'{current_player.name} choose a free position [1-9]: '))
  102.         if is_valid_choice(board, board_mapper, current_choice):
  103.             break
  104.     row, col = board_mapper[current_choice]
  105.     board[row][col] = current_player.sign
  106.     print_current_board(board)
  107.     if any([is_win_rows(board), is_win_columns(board, current_player), is_win_diagonals(board, current_player)]):
  108.         print(f"{current_player.name} won!")
  109.         break
  110.     if is_draw(board):
  111.         print('draw!')
  112.         break
  113.     turn += 1
  114.  
Advertisement
Add Comment
Please, Sign In to add comment