forked from KBroloes/python-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
81 lines (61 loc) · 2.28 KB
/
game.py
File metadata and controls
81 lines (61 loc) · 2.28 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
import sys
import os
from tictacpy import board
def setup_board():
# Load from file if a file is passed from commandline
file = parse_filename()
if file is not None:
return board.Board.load(file)
print("File not found, starting new game...")
return board.Board()
def parse_filename():
""" Parses the commandline arguments for whether a filename was supplied """
# You can do this better by using getopt for more advanced commandline parsing.
# But this is fine for our purposes
if len(sys.argv) == 2 and os.path.isfile(sys.argv[1]):
return sys.argv[1]
return None
def handle_save_and_exit():
print("\nSaving game. Please choose a filename (or ctrl+c to skip):")
try:
file = input()
game.save(file)
except Exception as err:
print(err)
finally:
sys.exit(0)
# We've executed this file from the commandline
if __name__ == "__main__":
game = setup_board()
# The game loop
while True:
try:
print("\n===========================")
print("To save & exit type: exit\n")
game.display()
print(f"The current player is: {game.current_move}")
print(f"available locations: {game.available_locations()}")
print("\nMake a move:")
# Read input
move = input()
# If the selected move is invalid, try again (unless exit command)
while move not in game.available_locations():
if move == "exit":
handle_save_and_exit()
print("Sorry, move invalid. Please try again:")
move = input()
# The move is valid, set it.
game.set_piece(move)
# If the game is over, display and exit
if game.check_win_condition() == True:
print(f"Congratulations player {game.current_move}!")
game.display()
break
if game.all_filled() == True:
print("The only winning move is not to play. Try again?")
break
# Otherwise, pass turn
game.pass_turn()
except KeyboardInterrupt:
# Exit the game if we ctrl+c
handle_save_and_exit()