Advertisement
malixds_

tic_tac_toe

Oct 3rd, 2022
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. import random
  2. field = [['.', ' ', '.'], [' ', ',', ' '], ['.', ' ', '.']]
  3. available_pos = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  4.  
  5.  
  6. def start_game():
  7.     player1 = input("Введите имя первого игрока: ")
  8.     player2 = input("Введите имя второго игрока: ")
  9.     print("Два игрока. Тот, кто ходит первым - играет за 'крестики'. Очередность хода выбирается случайным образом.")
  10.     print("У каждой ячейки поля есть свой индекс, от 0 до 8. При запросе вносите одно число из этого интервала.")
  11.     tmp = int(input("Нажмите 1, чтобы начать игру: "))
  12.     if tmp == 1:
  13.         step = random.randint(1, 2)
  14.         if step == 1:
  15.             print(f'Игру начинает {player1}')
  16.         else:
  17.             print(f'Игру начинает {player2}. {player2} ходит "крестиками"')
  18.     print(available_pos)
  19.     return step, player1, player2
  20.  
  21.  
  22. step, player1, player2 = start_game()
  23.  
  24.  
  25. def check_game(cur_field):
  26.     if cur_field[0][0] == cur_field[1][1] == cur_field[2][2] or cur_field[2][0] == cur_field[1][1] == cur_field[0][2]:
  27.         print('Игра окончена')
  28.         return 5
  29.     for k in range(0, len(field)):
  30.         if cur_field[k][0] == cur_field[k][1] == cur_field[k][2]:
  31.             print('Игра окончена')
  32.             return 5
  33.         elif cur_field[0][k] == cur_field[1][k] == cur_field[2][k]:
  34.             print('Игра окончена')
  35.             return 5
  36.  
  37.  
  38. def steps(check, player1, player2):
  39.     game = True
  40.     while game:
  41.         if check == 1:
  42.             check += 1
  43.             current_step = int(input(f'Ходит {player1} - крестик. Введите одно число из {available_pos}: '))
  44.             if available_pos.count(current_step):
  45.                 available_pos.remove(current_step)
  46.                 if current_step < 3:
  47.                     field[0][current_step] = 'X'
  48.                 elif 3 <= current_step < 6:
  49.                     field[1][current_step-3] = 'X'
  50.                 else:
  51.                     field[2][current_step-6] = 'X'
  52.                 for m in range(0, len(field)):
  53.                     for n in range(0, len(field[m])):
  54.                         print(field[m][n], end=' ')
  55.                         print('| ', end='')
  56.                     print('\n')
  57.                     print('_' * 11)
  58.                 if check_game(field) == 5:
  59.                     check = 10
  60.                     game = False
  61.         elif check == 2:
  62.             check -= 1
  63.             current_step = int(input(f'Ходит {player2} - нолик. Введите одно число из {available_pos}: '))
  64.             if available_pos.count(current_step):
  65.                 available_pos.remove(current_step)
  66.                 if current_step < 3:
  67.                     field[0][current_step] = 'O'
  68.                 elif 3 <= current_step < 6:
  69.                     field[1][current_step - 3] = 'O'
  70.                 else:
  71.                     field[2][current_step - 6] = 'O'
  72.                 for q in range(0, len(field)):
  73.                     for p in range(0, len(field[q])):
  74.                         print(field[q][p], end=' ')
  75.                         print('| ', end='')
  76.                     print('\n')
  77.                     print('_' * 11)
  78.                 if check_game(field) == 5:
  79.                     check = 10
  80.                     game = False
  81.  
  82.  
  83. steps(step, player1, player2)
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement