1
+ from random import choice , randint
2
+ def draw_board_lite (board ):
3
+ print (' ' + board [7 ] + ' | ' + board [8 ] + ' | ' + board [9 ])
4
+ print ('-----------' )
5
+ print (' ' + board [4 ] + ' | ' + board [5 ] + ' | ' + board [6 ])
6
+ print ('-----------' )
7
+ print (' ' + board [1 ] + ' | ' + board [2 ] + ' | ' + board [3 ])
8
+ def player_input_my ():
9
+ inp = input ('Выберите сторону "Х" или "0" или нажмите Enter для случайного выбора : ' ) or choice (['x' , 'o' ])
10
+ while inp != '' :
11
+ if inp in 'ХхЧчXx{[' :
12
+ return ('X' ,'O' )
13
+ elif inp in 'ОоЩщOoJj0' :
14
+ return ('O' , 'X' )
15
+ else :
16
+ inp = input ('Вы ввели не корректное значение, выберите "Х" или "0": ' ) or choice (['x' , 'o' ])
17
+
18
+ def place_marker (board , marker , position ):
19
+ board [position ] = marker
20
+
21
+ def win_check (board , mark ):
22
+ return ((board [7 ] == mark and board [8 ] == mark and board [9 ] == mark ) or
23
+ (board [4 ] == mark and board [5 ] == mark and board [6 ] == mark ) or
24
+ (board [1 ] == mark and board [2 ] == mark and board [3 ] == mark ) or
25
+ (board [7 ] == mark and board [4 ] == mark and board [1 ] == mark ) or
26
+ (board [8 ] == mark and board [5 ] == mark and board [2 ] == mark ) or
27
+ (board [9 ] == mark and board [6 ] == mark and board [3 ] == mark ) or
28
+ (board [1 ] == mark and board [5 ] == mark and board [9 ] == mark ) or
29
+ (board [7 ] == mark and board [5 ] == mark and board [3 ] == mark ))
30
+
31
+ def space_check (board , position ):
32
+ return board [position ] == ' '
33
+
34
+ def full_board_check (board ):
35
+ for i in range (1 , 10 ):
36
+ if space_check (board , i ):
37
+ return False
38
+ return True
39
+ def player_choose (board ):
40
+ position = ' '
41
+ while position not in '1 2 3 4 5 6 7 8 9' .split () or not space_check (board , int (position )):
42
+ position = input ('Выберите позицию клетку: ' )
43
+ return int (position )
44
+ def replace ():
45
+ return input ('Сыграть еще ? введите: "YES" или "NO" :' ).upper () in 'YES Y НУЫ Н ' .split ()
46
+
47
+ print ('Игра крестики нолики' )
48
+ while True :
49
+ theBoard = [' ' ] * 10
50
+ player1_marker , player2_marker = player_input_my ()
51
+ print (player1_marker + ' ходит первым' )
52
+ game_on = True
53
+ while game_on :
54
+ draw_board_lite (theBoard )
55
+ position = player_choose (theBoard )
56
+ place_marker (theBoard , player1_marker , position )
57
+ if win_check (theBoard , player1_marker ):
58
+ draw_board_lite (theBoard )
59
+ print ('Победа за ' + player1_marker )
60
+ game_on = False
61
+ else :
62
+ if full_board_check (theBoard ):
63
+ draw_board_lite (theBoard )
64
+ print ('Ничья' )
65
+ break
66
+ else :
67
+ draw_board_lite (theBoard )
68
+ position = player_choose (theBoard )
69
+ place_marker (theBoard , player2_marker , position )
70
+ if win_check (theBoard , player2_marker ):
71
+ draw_board_lite (theBoard )
72
+ print ('Победа за ' + player2_marker )
73
+ game_on = False
74
+
75
+ if not replace ():
76
+ break
0 commit comments