|
1 | | -def start(): |
2 | | - global board |
3 | | - board = [ |
4 | | - ['','',''], |
5 | | - ['','',''], |
6 | | - ['','',''] |
7 | | - ] |
8 | | - |
9 | | -def print_board(): |
10 | | - print(' ---------') |
11 | | - for row in board: |
12 | | - print(' ',row[0],'|',row[1],'|',row[2]) |
13 | | - print(' ---------') |
14 | | - |
15 | | -def have_empty_room(): |
16 | | - for row in board: |
17 | | - for room in row: |
18 | | - if not room: |
19 | | - return True |
20 | | - return False |
21 | | - |
22 | | -def set_room_state(roomxy,state): |
23 | | - x = int(roomxy[0])-1 |
24 | | - y = int(roomxy[1])-1 |
25 | | - row = board[x] |
26 | | - room = row[y] |
27 | | - if not room: |
28 | | - board[x][y] = state |
29 | | - return True |
30 | | - return False |
31 | | - |
32 | | -def check_xy(xy): |
33 | | - xy = str(xy) |
34 | | - if len(xy) != 2: |
35 | | - return False |
36 | | - if int(xy[0]) > 3 or int(xy[0]) < 1 or int(xy[1]) > 3 or int(xy[1]) < 1: |
37 | | - return False |
38 | | - return True |
39 | | - |
40 | | -def check_for_win(): |
41 | | - if board[0][0] == board[0][1] == board[0][2] != '': |
42 | | - winner = board[0][0] |
43 | | - print(f'{winner} won!') |
44 | | - |
45 | | - elif board[1][0] == board[1][1] == board[1][2] != '': |
46 | | - winner = board[1][0] |
47 | | - print(f'{winner} won!') |
48 | | - |
49 | | - elif board[2][0] == board[2][1] == board[2][2] != '': |
50 | | - winner = board[2][0] |
51 | | - print(f'{winner} won!') |
52 | | - |
53 | | - elif board[0][0] == board[1][0] == board[2][0] != '': |
54 | | - winner = board[0][0] |
55 | | - print(f'{winner} won!') |
56 | | - |
57 | | - elif board[0][1] == board[1][1] == board[2][1] != '': |
58 | | - winner = board[0][1] |
59 | | - print(f'{winner} won!') |
60 | | - |
61 | | - elif board[0][2] == board[1][2] == board[2][2] != '': |
62 | | - winner = board[0][0] |
63 | | - print(f'{winner} won!') |
64 | | - |
65 | | - elif board[0][0] == board[1][1] == board[2][2] != '': |
66 | | - winner = board[0][0] |
67 | | - print(f'{winner} won!') |
68 | | - |
69 | | - elif board[0][2] == board[1][1] == board[2][0] != '': |
70 | | - winner = board[0][2] |
71 | | - print(f'{winner} won!') |
72 | | - |
73 | | - else: |
74 | | - return False |
75 | | - |
76 | | - return True |
77 | | - |
78 | | - |
79 | | -turn = 'o' |
80 | | -start() |
81 | | - |
82 | | -while have_empty_room(): |
83 | | - print_board() |
84 | | - print('\n') |
85 | | - if turn == 'o': |
86 | | - turn = 'x' |
87 | | - else: |
88 | | - turn = 'o' |
89 | | - print(f'{turn}\'s Turn!') |
90 | | - while True: |
91 | | - xy = int(input('enter x and y: ')) |
92 | | - if check_xy(xy): |
93 | | - if set_room_state(str(xy),turn): |
94 | | - break |
95 | | - print('This room is full!') |
96 | | - continue |
97 | | - print('Error!') |
98 | | - continue |
99 | | - if check_for_win(): |
| 1 | +squares = [' ']*9 |
| 2 | +players = 'XO' |
| 3 | +board = ''' |
| 4 | + 0 1 2 |
| 5 | + {0} | {1} | {2} |
| 6 | + ----------- |
| 7 | +3 {3} | {4} | {5} 5 |
| 8 | + ----------- |
| 9 | + {6} | {7} | {8} |
| 10 | + 6 7 8 |
| 11 | +''' |
| 12 | +win_conditions = [ |
| 13 | + (0, 1, 2), (3, 4, 5), (6, 7, 8), # horizontals |
| 14 | + (0, 3, 6), (1, 4, 7), (2, 5, 8), # verticals |
| 15 | + (0, 4, 8), (2, 4, 6) # diagonals |
| 16 | +] |
| 17 | + |
| 18 | +def check_win(player): |
| 19 | + for a, b, c in win_conditions: |
| 20 | + if {squares[a], squares[b], squares[c]} == {player}: |
| 21 | + return True |
| 22 | + |
| 23 | +while True: |
| 24 | + print(board.format(*squares)) |
| 25 | + if check_win(players[1]): |
| 26 | + print(f'{players[1]} is the winner!') |
100 | 27 | break |
101 | | -print_board() |
102 | | -print('Game Over') |
103 | | -input() |
| 28 | + if ' ' not in squares: |
| 29 | + print('Cats game!') |
| 30 | + break |
| 31 | + move = input(f'{players[0]} to move [0-8] > ') |
| 32 | + if not move.isdigit() or not 0 <= int(move) <= 8 or squares[int(move)] != ' ': |
| 33 | + print('Invalid move!') |
| 34 | + continue |
| 35 | + squares[int(move)], players = players[0], players[::-1] |
0 commit comments