-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
57 lines (49 loc) · 1.31 KB
/
game.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
import os
class Game:
def __init__(self):
self.game = [
0, 0, 0,
0, 0, 0,
0, 0, 0
]
def render(self):
rendered = ""
for i, cell in enumerate(self.game):
if cell == 1:
rendered+="o"
elif cell == 2:
rendered+="x"
else:
rendered+="_"
if i in (0, 1, 3, 4, 6, 7):
rendered+="|"
if i == 2:
rendered+="\n"
if i == 5:
rendered+="\n"
return rendered
def parseInput(self):
inp = input("\n? ")
if inp == "q":
quit()
if inp == "r":
self.game = [
0, 0, 0,
0, 0, 0,
0, 0, 0
]
try:
pos = int(inp[0])-1
playerType = inp[1]
if self.game[pos] == 0:
if playerType == "o":
self.game[pos] = 1
elif playerType == "x":
self.game[pos] = 2
except:
print("There was an error when handling player input.")
g = Game()
while True:
print(g.render())
g.parseInput()
os.system('cls' if os.name == 'nt' else 'clear')