forked from wjw12/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.py
59 lines (47 loc) · 1.14 KB
/
tic-tac-toe.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
"""
---------- Tic-Tac-Toe ----------
Exercises
1. Give the X and O a different color and width.
2. What happens when someone taps a taken spot?
3. How would you detect when someone has won?
4. How could you create a computer player?
"""
from turtle import *
from freegames import line
def grid():
"Draw tic-tac-toe grid."
line(-67, 200, -67, -200)
line(67, 200, 67, -200)
line(-200, -67, 200, -67)
line(-200, 67, 200, 67)
def drawx(x, y):
"Draw X player."
line(x, y, x + 133, y + 133)
line(x, y + 133, x + 133, y)
def drawo(x, y):
"Draw O player."
up()
goto(x + 67, y + 5)
down()
circle(62)
def floor(value):
"Round value down to grid with square size 133."
return ((value + 200) // 133) * 133 - 200
state = {'player': 0}
players = [drawx, drawo]
def tap(x, y):
"Draw X or O in tapped square."
x = floor(x)
y = floor(y)
player = state['player']
draw = players[player]
draw(x, y)
update()
state['player'] = not player
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
grid()
update()
onscreenclick(tap)
done()