Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- field = [['.', ' ', '.'], [' ', ',', ' '], ['.', ' ', '.']]
- available_pos = [0, 1, 2, 3, 4, 5, 6, 7, 8]
- def start_game():
- player1 = input("Введите имя первого игрока: ")
- player2 = input("Введите имя второго игрока: ")
- print("Два игрока. Тот, кто ходит первым - играет за 'крестики'. Очередность хода выбирается случайным образом.")
- print("У каждой ячейки поля есть свой индекс, от 0 до 8. При запросе вносите одно число из этого интервала.")
- tmp = int(input("Нажмите 1, чтобы начать игру: "))
- if tmp == 1:
- step = random.randint(1, 2)
- if step == 1:
- print(f'Игру начинает {player1}')
- else:
- print(f'Игру начинает {player2}. {player2} ходит "крестиками"')
- print(available_pos)
- return step, player1, player2
- step, player1, player2 = start_game()
- def check_game(cur_field):
- 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]:
- print('Игра окончена')
- return 5
- for k in range(0, len(field)):
- if cur_field[k][0] == cur_field[k][1] == cur_field[k][2]:
- print('Игра окончена')
- return 5
- elif cur_field[0][k] == cur_field[1][k] == cur_field[2][k]:
- print('Игра окончена')
- return 5
- def steps(check, player1, player2):
- game = True
- while game:
- if check == 1:
- check += 1
- current_step = int(input(f'Ходит {player1} - крестик. Введите одно число из {available_pos}: '))
- if available_pos.count(current_step):
- available_pos.remove(current_step)
- if current_step < 3:
- field[0][current_step] = 'X'
- elif 3 <= current_step < 6:
- field[1][current_step-3] = 'X'
- else:
- field[2][current_step-6] = 'X'
- for m in range(0, len(field)):
- for n in range(0, len(field[m])):
- print(field[m][n], end=' ')
- print('| ', end='')
- print('\n')
- print('_' * 11)
- if check_game(field) == 5:
- check = 10
- game = False
- elif check == 2:
- check -= 1
- current_step = int(input(f'Ходит {player2} - нолик. Введите одно число из {available_pos}: '))
- if available_pos.count(current_step):
- available_pos.remove(current_step)
- if current_step < 3:
- field[0][current_step] = 'O'
- elif 3 <= current_step < 6:
- field[1][current_step - 3] = 'O'
- else:
- field[2][current_step - 6] = 'O'
- for q in range(0, len(field)):
- for p in range(0, len(field[q])):
- print(field[q][p], end=' ')
- print('| ', end='')
- print('\n')
- print('_' * 11)
- if check_game(field) == 5:
- check = 10
- game = False
- steps(step, player1, player2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement