Amazing-Python-Scripts

Форк
0
116 строк · 3.4 Кб
1
import random
2

3

4
def display_board(board):
5
    print('-------------')
6
    print('| ' + board[7] + ' | ' + board[8] + ' | ' + board[9] + ' |')
7
    print('-------------')
8
    print('| ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' |')
9
    print('-------------')
10
    print('| ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' |')
11
    print('-------------')
12

13

14
def player_input():
15
    marker = ''
16
    while marker != 'X' and marker != 'O':
17
        marker = input('Player 1, choose X or O: ').upper()
18
    player1 = marker
19
    player2 = 'O' if player1 == 'X' else 'X'
20
    return player1, player2
21

22

23
def place_marker(board, marker, position):
24
    board[position] = marker
25

26

27
def win_check(board, mark):
28
    winning_combinations = [
29
        [1, 2, 3], [4, 5, 6], [7, 8, 9],  # rows
30
        [1, 4, 7], [2, 5, 8], [3, 6, 9],  # columns
31
        [1, 5, 9], [3, 5, 7]  # diagonals
32
    ]
33
    return any(all(board[i] == mark for i in combination) for combination in winning_combinations)
34

35

36
def choose_first():
37
    return random.choice(['Player 1', 'Player 2'])
38

39

40
def space_check(board, position):
41
    return board[position] == ' '
42

43

44
def full_board_check(board):
45
    return all(board[i] != ' ' for i in range(1, 10))
46

47

48
def player_choice(board):
49
    position = 0
50
    while position not in range(1, 10) or not space_check(board, position):
51
        position = int(input('Choose a position (1-9): '))
52
    return position
53

54

55
def replay():
56
    choice = input('Do you want to play again? Enter Yes or No: ')
57
    return choice.lower() == 'yes'
58

59

60
def play_tic_tac_toe():
61
    print('Welcome to Tic Tac Toe!')
62
    while True:
63
        the_board = [' '] * 10
64
        player1_marker, player2_marker = player_input()
65
        turn = choose_first()
66
        print(turn + ' will go first.')
67
        play_game = input('Are you ready to play? Enter y or n: ')
68
        if play_game.lower() == 'y':
69
            game_on = True
70
        else:
71
            game_on = False
72

73
        while game_on:
74
            if turn == 'Player 1':
75
                display_board(the_board)
76
                position = player_choice(the_board)
77
                place_marker(the_board, player1_marker, position)
78

79
                if win_check(the_board, player1_marker):
80
                    display_board(the_board)
81
                    print('Player 1 has won!')
82
                    game_on = False
83
                else:
84
                    if full_board_check(the_board):
85
                        display_board(the_board)
86
                        print('TIE GAME!')
87
                        game_on = False
88
                    else:
89
                        turn = 'Player 2'
90
            else:
91
                display_board(the_board)
92
                position = player_choice(the_board)
93
                place_marker(the_board, player2_marker, position)
94

95
                if win_check(the_board, player2_marker):
96
                    display_board(the_board)
97
                    print('Player 2 has won!')
98
                    game_on = False
99
                else:
100
                    if full_board_check(the_board):
101
                        display_board(the_board)
102
                        print('TIE GAME!')
103
                        game_on = False
104
                    else:
105
                        turn = 'Player 1'
106

107
        if not replay():
108
            if play_game.lower() == 'n':
109
                print('BYE! Have a good day.')
110
            else:
111
                print('Thank you for playing.')
112
            break
113

114

115
# Start the game
116
play_tic_tac_toe()
117

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.