Advertisement
GeorgiLukanov87

tic tac toe by Atanas Atananos

Oct 20th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. # Tic-tac-toe Console_Game by Atanas Atananos
  2.  
  3. class InvalidPositionError(Exception):
  4.     pass
  5.  
  6.  
  7. def read_first_player_sign(first_player_name):
  8.     while True:
  9.         first_player_sign = input(f'{first_player_name} would you like to play with X or O? ').upper()
  10.         if first_player_sign in ['X', 'O']:
  11.             return first_player_sign
  12.  
  13.  
  14. def read_players_info():
  15.     first_player_name = input('Player one name: ')
  16.     second_player_name = input('Player two name: ')
  17.  
  18.     first_player_sign = read_first_player_sign(first_player_name)
  19.     second_player_sign = 'O' if first_player_sign == 'X' else 'X'
  20.     return [first_player_name, first_player_sign], [second_player_name, second_player_sign]
  21.  
  22.  
  23. def print_board_numeration():
  24.     print('This is the numeration of the board:')
  25.     print('|  1  |  2  |  3  |')
  26.     print('|  4  |  5  |  6  |')
  27.     print('|  7  |  8  |  9  |')
  28.  
  29.  
  30. def get_coords_by_pos(position):
  31.     if position == 1:
  32.         return 0, 0
  33.     if position == 2:
  34.         return 0, 1
  35.     if position == 3:
  36.         return 0, 2
  37.     if position == 4:
  38.         return 1, 0
  39.     if position == 5:
  40.         return 1, 1
  41.     if position == 6:
  42.         return 1, 2
  43.     if position == 7:
  44.         return 2, 0
  45.     if position == 8:
  46.         return 2, 1
  47.     if position == 9:
  48.         return 2, 2
  49.     raise InvalidPositionError(f'{position} is invalid position. Please provide a valid position in range [1-9]!')
  50.  
  51.  
  52. def display_board(matrix):
  53.     for row in matrix:
  54.         print('|  ', end='')
  55.         print('  |  '.join(row), end='')
  56.         print('  |')
  57.  
  58.  
  59. def is_board_full(matrix):
  60.     for row in matrix:
  61.         is_row_full = all([el != ' ' for el in row])
  62.         if not is_row_full:
  63.             return False
  64.     return True
  65.  
  66.  
  67. def has_player_won(matrix, player_sign):
  68.     # horizontal
  69.     for row in matrix:
  70.         if all([el == player_sign for el in row]):
  71.             return True
  72.  
  73.     # vertical
  74.     size = len(matrix)
  75.     for col in range(size):
  76.         won = True
  77.         for row in range(size):
  78.             if matrix[row][col] != player_sign:
  79.                 won = False
  80.                 break
  81.         if won:
  82.             return True
  83.  
  84.     # primary diagonal
  85.     won = True
  86.     for idx in range(size):
  87.         if matrix[idx][idx] != player_sign:
  88.             won = False
  89.             break
  90.     if won:
  91.         return True
  92.  
  93.     # secondary diagonal
  94.     won = True
  95.     for idx in range(size):
  96.         if matrix[idx][size - 1 - idx] != player_sign:
  97.             won = False
  98.             break
  99.     return won
  100.  
  101.  
  102. def play(matrix, p1, p2):
  103.     turns = 1
  104.     # p1: 1, 3, 5, 7, 9... +2
  105.     # p2: 2, 4, 6, 8, 19... +2
  106.     players_turns = {0: p2, 1: p1}
  107.     while True:
  108.         name, sign = players_turns[turns % 2]
  109.  
  110.         try:
  111.             position = int(input(f'{name} choose a free position [1-9]: '))
  112.             row, col = get_coords_by_pos(position)
  113.  
  114.             if matrix[row][col] != ' ':
  115.                 raise InvalidPositionError(f'{position} is already taken! Please choose a free position!')
  116.  
  117.             matrix[row][col] = sign
  118.             turns += 1
  119.  
  120.             display_board(matrix)
  121.  
  122.             if has_player_won(matrix, sign):
  123.                 print(f'{name} has won!')
  124.                 break
  125.  
  126.             if is_board_full(matrix):
  127.                 print('draw!')
  128.                 break
  129.         except ValueError:
  130.             print('Please provide a valid position in range [1-9]!')
  131.         except InvalidPositionError as error:
  132.             print(error)
  133.  
  134.  
  135. player1, player2 = read_players_info()
  136.  
  137. print_board_numeration()
  138.  
  139. print(f'{player1[0]} starts first!')
  140.  
  141. board = [
  142.     [' ', ' ', ' '],
  143.     [' ', ' ', ' '],
  144.     [' ', ' ', ' ']
  145. ]
  146.  
  147. play(board, player1, player2)
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement