-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtic-tac-toe.py
More file actions
executable file
·120 lines (108 loc) · 3.34 KB
/
tic-tac-toe.py
File metadata and controls
executable file
·120 lines (108 loc) · 3.34 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
import shelve
board = {
"top-L": " ",
"top-C": " ",
"top-R": " ",
"mid-L": " ",
"mid-C": " ",
"mid-R": " ",
"bot-L": " ",
"bot-C": " ",
"bot-R": " ",
}
def print_board():
print("%s|%s|%s" % (board["top-L"], board["top-C"], board["top-R"]))
print("-+-+-")
print("%s|%s|%s" % (board["mid-L"], board["mid-C"], board["mid-R"]))
print("-+-+-")
print("%s|%s|%s" % (board["bot-L"], board["bot-C"], board["bot-R"]))
def check_valid_move(move):
valid_move = True
if(move not in board):
valid_move = False
if(valid_move and board[move] != " "):
valid_move = False
return(valid_move)
def save_game():
global board, last_move, current_move
shelfFile = shelve.open("savegame.ttt")
shelfFile['board'] = board
shelfFile['last_move'] = last_move
shelfFile['current_move'] = current_move
shelfFile.close()
def do_load_game():
global board, last_move, current_move
shelfFile = shelve.open("savegame.ttt")
board = shelfFile['board']
last_move = shelfFile['last_move']
current_move = shelfFile['current_move']
shelfFile.close()
def check_save_game(move):
if("save" == move):
save_game()
exit()
def ask_move():
global current_move, last_move
turn_over = False
while(not turn_over):
print(current_move.upper() + "'s move. Specify {top,mid,bot}-{L,C,R}")
move = input()
check_save_game(move)
valid_move = check_valid_move(move)
if(valid_move):
board[move] = current_move
turn_over = True
else:
print("Invalid move. Choose another space.")
current_move, last_move = last_move, current_move
def check_win():
win = False
# check top row
if(board['top-L'] == board['top-C'] == board['top-R'] and board['top-L'] != " "):
win = True
# check middle row
if(board['mid-L'] == board['mid-C'] == board['mid-R'] and board['mid-L'] != " "):
win = True
# check bottom row
if(board['bot-L'] == board['bot-C'] == board['bot-R'] and board['bot-L'] != " "):
win = True
# check left column
if(board['top-L'] == board['mid-L'] == board['bot-L'] and board['top-L'] != " "):
win = True
# check center column
if(board['top-C'] == board['mid-C'] == board['bot-C'] and board['top-C'] != " "):
win = True
# check right column
if(board['top-R'] == board['mid-R'] == board['bot-R'] and board['top-R'] != " "):
win = True
# check \ diagonal
if(board['top-L'] == board['mid-C'] == board['bot-R'] and board['top-L'] != " "):
win = True
# check / diagonal
if(board['bot-L'] == board['mid-C'] == board['top-R'] and board['bot-L'] != " "):
win = True
return(win)
def check_over():
full = (" " not in board.values())
if(full):
print("No more moves. Stalemate. Game over.")
win = check_win()
if(win):
print("Game over. "+current_move+" wins.")
return(not(full) and not(win))
def ask_load_game():
print("Load saved game? (Y = yes)")
load_game = input()
if("Y" == load_game):
do_load_game()
last_move = "x"
current_move = "o"
game_not_over = True
ask_load_game()
while(game_not_over):
print_board()
ask_move()
game_not_over = check_over()
# Print board one last time
print_board()