-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
HumanvsHuman.py
80 lines (69 loc) · 3.24 KB
/
HumanvsHuman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
game_state = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
players = ['X','O']
def play_move(player, block_num):
if game_state[int((block_num-1)/3)][(block_num-1)%3] is ' ':
game_state[int((block_num-1)/3)][(block_num-1)%3] = player
else:
block_num = int(input("Block is not empty, ya blockhead! Choose again: "))
play_move(player, block_num)
def check_current_state(game_state):
# Check if draw
draw_flag = 0
for i in range(3):
for j in range(3):
if game_state[i][j] is ' ':
draw_flag = 1
if draw_flag is 0:
return None, "Draw"
# Check horizontals
if (game_state[0][0] == game_state[0][1] and game_state[0][1] == game_state[0][2] and game_state[0][0] is not ' '):
return game_state[0][0], "Done"
if (game_state[1][0] == game_state[1][1] and game_state[1][1] == game_state[1][2] and game_state[1][0] is not ' '):
return game_state[1][0], "Done"
if (game_state[2][0] == game_state[2][1] and game_state[2][1] == game_state[2][2] and game_state[2][0] is not ' '):
return game_state[2][0], "Done"
# Check verticals
if (game_state[0][0] == game_state[1][0] and game_state[1][0] == game_state[2][0] and game_state[0][0] is not ' '):
return game_state[0][0], "Done"
if (game_state[0][1] == game_state[1][1] and game_state[1][1] == game_state[2][1] and game_state[0][1] is not ' '):
return game_state[0][1], "Done"
if (game_state[0][2] == game_state[1][2] and game_state[1][2] == game_state[2][2] and game_state[0][2] is not ' '):
return game_state[0][2], "Done"
# Check diagonals
if (game_state[0][0] == game_state[1][1] and game_state[1][1] == game_state[2][2] and game_state[0][0] is not ' '):
return game_state[1][1], "Done"
if (game_state[2][0] == game_state[1][1] and game_state[1][1] == game_state[0][2] and game_state[2][0] is not ' '):
return game_state[1][1], "Done"
return None, "Not Done"
def print_board(game_state):
print('----------------')
print('| ' + str(game_state[0][0]) + ' || ' + str(game_state[0][1]) + ' || ' + str(game_state[0][2]) + ' |')
print('----------------')
print('| ' + str(game_state[1][0]) + ' || ' + str(game_state[1][1]) + ' || ' + str(game_state[1][2]) + ' |')
print('----------------')
print('| ' + str(game_state[2][0]) + ' || ' + str(game_state[2][1]) + ' || ' + str(game_state[2][2]) + ' |')
print('----------------')
# PLaying
current_state = "Not Done"
print("New Game!")
print_board(game_state)
player_choice = input("Choose which player goes first - X or O: ")
winner = None
if player_choice == 'X' or player_choice == 'x':
current_player_idx = 0
else:
current_player_idx = 1
while current_state == "Not Done":
block_choice = int(input(str(players[current_player_idx]) + "'s Turn! Choose where to place (1 to 9): "))
play_move(players[current_player_idx], block_choice)
print_board(game_state)
winner, current_state = check_current_state(game_state)
if winner is not None:
print(str(winner) + " won!")
else:
current_player_idx = (current_player_idx + 1)%2
if current_state is "Draw":
print("Draw!")