-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic_tac_sense_hat.py
112 lines (94 loc) · 3 KB
/
Tic_tac_sense_hat.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
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
"""
**Name:** While loop calculator
**Author:** Christoffer Thorske Johnsen
**Created:** 04.01.2019
"""
import time
from sense_hat import SenseHat
sense = SenseHat()
def tic_tac():
X = [255, 0, 0] # Red
O = [0, 0, 255] # Blue
B = [0, 0, 0] #Black
W = [255, 255, 255] #White
brett = [
W, W, B, W, W, B, W, W,
W, W, B, W, W, B, W, W,
B, B, B, B, B, B, B, B,
W, W, B, W, W, B, W, W,
W, W, B, W, W, B, W, W,
B, B, B, B, B, B, B, B,
W, W, B, W, W, B, W, W,
W, W, B, W, W, B, W, W
]
opp_del = ([6, 7, 14, 15], [3, 4, 11, 12], [0, 1, 8, 9],
[30, 31, 38, 39], [27, 28, 35, 36], [24, 25, 32, 33],
[54, 55, 62, 63], [51, 52, 59, 60], [48, 49, 56, 57])
win_pos = ((0, 3, 6), (24, 27, 30), (48, 51, 54), (0, 3, 6), (3, 27, 51),
(6, 30, 54), (0, 27, 54), (6, 27, 48))
end = False
def draw_brett():
sense.set_pixels(brett)
def pl_1():
n = choose_numb()
for i in opp_del[n]:
if brett[i] == [255, 0, 0] or brett[i] == [0, 0, 255]:
print('The place is not free!')
pl_1()
else:
brett[i] = [255, 0, 0]
def pl_2():
n = choose_numb()
for i in opp_del[n]:
if brett[i] == [255, 0, 0] or brett[i] == [0, 0, 255]:
print('The place is not free!')
pl_2()
else:
brett[i] = [0, 0, 255]
def choose_numb():
while True:
try:
a = int(input('\n Choose a number: '))
if a in range(1,10):
a -= 1
return a
else:
print('Not on the board')
except ValueError:
print('Not a number')
def win():
for a, b, c in win_pos:
if brett[a] == [0, 0, 255] and brett[b] == [0, 0, 255] and brett[c] == [0, 0, 255] or brett[a] == [255, 0, 0] and brett[b] == [255, 0, 0] and brett[c] == [255, 0, 0]:
print('Congratulations!\n')
return True
if 36 == sum((pos == [0, 0, 255] or pos == [255, 0, 0]) for pos in brett): #Need to change
print("The game ends in a tie\n")
sense.set_rotation(180)
sense.show_message("Tie")
return True
while not end:
draw_brett()
end = win()
if end == True:
print('Player 2 won!!')
sense.set_rotation(180)
sense.show_letter("2")
time.sleep(1)
sense.clear()
break
print("Player 1 choose where to place red marke")
pl_1()
print()
draw_brett()
end = win()
if end == True:
print('Player 1 won!!')
sense.set_rotation(180)
sense.show_letter("1")
time.sleep(3)
sense.clear()
break
print("Player 2 choose where to place blue marke")
pl_2()
print()
tic_tac()