-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
387 lines (272 loc) · 14.7 KB
/
app.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
0 1 2
0 |0> |1> |1>
1
2
1. virtual environment
2. qiskit install
"""
#from multiprocessing.spawn import import_main_pat
#from tkinter import Image
#from tkinter.tix import IMAGE
from PIL import Image
import numpy as np
import pandas as pd
import streamlit as st
import math
from game import getRandom, validate
def main():
#to give a menu
menu= ['PLAY','INSTRUCTIONS','ABOUT']
option=st.sidebar.selectbox(" MENU",menu)
#cases
if option==menu[0]: # Play
st.write("Welcome to Quantum Tic Tac Toe...Lets start playing. Before starting if you dont know What is a Q-comp..then go to [ Quantum Computer/Computing](https://en.wikipedia.org/wiki/Quantum_computing)")
st.write("Computer --> |0>")
st.write("User --> |1>")
psi= '|φ>'
if 'board' not in st.session_state:
st.session_state.board=np.array([[psi,psi,psi],[psi,psi,psi],[psi,psi,psi]])
st.session_state.available_moves=[0,1,2,3,4,5,6,7,8,9]
#dropdown
moves = st.selectbox("Make a move !", st.session_state.available_moves)
if moves==1:
if st.session_state.board[0,0]==psi: #for one time initialization-->1 time selection
st.session_state.board[0,0]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
elif moves==2:
if st.session_state.board[0,1]==psi: #for one time initialization-->1 time selection
st.session_state.board[0,1]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
elif moves==3:
if st.session_state.board[0,2]==psi: #for one time initialization-->1 time selection
st.session_state.board[0,2]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col] == psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
#********************************************************
if moves==4:
if st.session_state.board[1,0]==psi: #for one time initialization-->1 time selection
st.session_state.board[1,0]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
elif moves==5:
if st.session_state.board[1,1]==psi: #for one time initialization-->1 time selection
st.session_state.board[1,1]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if (st.session_state.board[row,col]==psi):
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
elif moves==6:
if st.session_state.board[1,2]==psi: #for one time initialization-->1 time selection
st.session_state.board[1,2]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
#********************************************************
if moves==7:
if st.session_state.board[2,0]==psi: #for one time initialization-->1 time selection
st.session_state.board[2,0]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
elif moves==8:
if st.session_state.board[2,1]==psi: #for one time initialization-->1 time selection
st.session_state.board[2,1]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
elif moves==9:
if st.session_state.board[2,2]==psi: #for one time initialization-->1 time selection
st.session_state.board[2,2]=getRandom()
userFlag=validate(st.session_state.board)
if not userFlag:
st.dataframe(st.session_state.board)
st.session_state.available_moves=list() # user won
comp_square=np.random.randint(1,9)
col = (comp_square-1)%3
row = math.floor((comp_square-1)/3)
compVal = getRandom()
if st.session_state.board[row,col]==psi:
st.session_state.board[row,col]=compVal # if that board piece is unoccupied, to give the value in it
#============== validation for the comp side ==================
compFlag=validate(st.session_state.board)
if not compFlag:
return 0
st.write("Computers move :",comp_square)
st.write("Comps value :",compVal)
st.dataframe(st.session_state.board)
else:
st.dataframe(st.session_state.board)
#********************************************************
elif option==menu[1]:
st.subheader("Instructions")
#st.write("The Instructions are here :- ")
psi='|φ>'
board=np.array([[psi,psi,psi],[psi,psi,psi],[psi,psi,psi]])
st.dataframe(board)
instruction1="""
The super position states are the above. The steps to start are as follows:-
1> The User gets to choose first always and between nos -> 1 to 9
2> The Comp then plays its move
Note: Comp can change its normal game symbol from |0> --> |1>
It can also take the user pos and its previous move pos ...If it occurs then then no move is made by comp
3> Then User again and Like that it will continue
4> If you Still dont understand how it will be you can play a demo. Refresh the page and continue with the challenge.
ALL THE BEST !! """
st.write(instruction1)
#for board numbering
b_num=pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])
st.dataframe(b_num)
instruction2="""
The above are the positions of the board and the numberings
These are the original board positions which are as follows :-
1 --> [0,0]
2 --> [0,1] and so on ..
"""
st.write(instruction2)
else:
pic=Image.open("QTTlogo.jpg")
st.image(pic,caption="GAME LOGO")
st.subheader("A quick intro about the game")
about="""
Created by Dripto
Created using Python, Streamlit, Qiskit
We will basically use the [Quantum Superposition](https://en.wikipedia.org/wiki/Quantum_superposition) technique here"""
st.write(about)
if __name__=='__main__':
c = main()
if c == 0:
st.subheader('GAME OVERRR!! Refresh the page to play again :)')