-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe_quantum.py
169 lines (157 loc) · 5.47 KB
/
tic_tac_toe_quantum.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def quantum_calculation():
"""
Quantum simulator that returns binary array
"""
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator
simulator = QasmSimulator()
k = []
for x in range(4):
circut = QuantumCircuit(1,1)
circut.h(0)
circut.measure([0],[0])
compile = transpile(circut,simulator)
job = simulator.run(compile,shots=1)
res = job.result()
count = res.get_counts(compile)
k.append(count)
k1 = ""
for x in range(len(k)):
k1 += str(k[x])
j = 2
new = []
for x in range(len(k)):
new.append(int(k1[j]))
j += 8
return new
def szybkie_wyliczanie(new):
"""
Converting from binary to decimal
"""
op = 0
for x in range(len(new)):
if(new[x] == 1):
op+=2**x
return op
def quick_repair(result):
if(result > 8):
result = int(result/2)
return result
def SI():
result1 = quantum_calculation()
result2=szybkie_wyliczanie(result1)
result2 = quick_repair(result2)
return result2
def draw(table):
"""
Draw checking
"""
if((table[0] != '0') and (table[1] != '1') and (table[2] != '2') and (table[3] != '3') and (table[4] != '4') and (table[5] != '5') and (table[6] != '6') and (table[7] != '7') and (table[8] != '8')):
print("Draw")
return True
else:
return False
def main_tic():
"""
main function
"""
a1 = 48
player1,player2,mode0 = 0,0,0
table = []
for i in range(0,9):
table.append(chr(a1))
a1 += 1
print("Choose game mode")
mode0 = int(input("1 player(1)\n: "))
if(mode0 == 1):
end = False
print("-------")
print(f"|{table[0]}|{table[1]}|{table[2]}|")
print(f"|{table[3]}|{table[4]}|{table[5]}|")
print(f"|{table[6]}|{table[7]}|{table[8]}|")
print("-------")
while(end == False):
#player1
try0 = False
while((try0 == False) and (end == False)):
player1 = int(input("Your move\n: "))
for x in range(0,9):
if(player1 == x):
if(table[x] == 'X' or table[x] == 'O'):
print("Place is taken")
if(draw(table) == True):
exit()
else:
table[x] = 'X'
try0 = True
break
if((table[0] == 'X') and (table[3] == 'X') and (table[6] == 'X')):
print("WIN")
end = True
elif((table[0] == 'X') and (table[1] == 'X') and (table[2] == 'X')):
print("WIN")
end = True
elif((table[3] == 'X') and (table[4] == 'X') and (table[5] == 'X')):
print("WIN")
end = True
elif((table[6] == 'X') and (table[7] == 'X') and (table[8] == 'X')):
print("WIN")
end = True
elif((table[1] == 'X') and (table[4] == 'X') and (table[7] == 'X')):
print("WIN")
end = True
elif((table[2] == 'X') and (table[5] == 'X') and (table[8] == 'X')):
print("WIN")
end = True
elif((table[6] == 'X') and (table[4] == 'X') and (table[2] == 'X')):
print("WIN")
end = True
elif((table[0] == 'X') and (table[4] == 'X') and (table[8] == 'X')):
print("WIN")
end = True
#player2
try1 = False
while((try1 == False) and (end == False)):
player2 = SI()
for x in range(0,9):
if(player2 == x):
if(table[x] == 'X' or table[x] == 'O'):
if(draw(table) == True):
exit()
else:
table[x] = 'O'
try1 = True
break
if((table[0] == 'O') and (table[3] == 'O') and (table[6] == 'O')):
print("LOSE")
end = True
elif((table[0] == 'O') and (table[1] == 'O') and (table[2] == 'O')):
print("LOSE")
end = True
elif((table[3] == 'O') and (table[4] == 'O') and (table[5] == 'O')):
print("LOSE")
end = True
elif((table[6] == 'O') and (table[7] == 'O') and (table[8] == 'O')):
print("LOSE")
end = True
elif((table[1] == 'O') and (table[4] == 'O') and (table[7] == 'O')):
print("LOSE")
end = True
elif((table[2] == 'O') and (table[5] == 'O') and (table[8] == 'O')):
print("LOSE")
end = True
elif((table[6] == 'O') and (table[4] == 'O') and (table[2] == 'O')):
print("LOSE")
end = True
elif((table[0] == 'O') and (table[4] == 'O') and (table[8] == 'O')):
print("LOSE")
end = True
print("-------")
print(f"|{table[0]}|{table[1]}|{table[2]}|")
print(f"|{table[3]}|{table[4]}|{table[5]}|")
print(f"|{table[6]}|{table[7]}|{table[8]}|")
print("-------")
else:
print("Wrong value")
if(__name__=="__main__"):
main_tic()