CoolFace
Apppublic

ds21/Q-TicTacToe

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
app.py388 linesDownload Raw Back to root
1"""
2    0   1   2
3 0  |0> |1> |1>
4 1
5 2
6
71. virtual environment
82. qiskit install    
9"""
10 
11#from multiprocessing.spawn import import_main_pat
12#from tkinter import Image
13#from tkinter.tix import IMAGE
14from PIL import Image 
15import numpy as np
16import pandas as pd
17import streamlit as st
18import math
19
20from game import getRandom, validate
21
22
23def main():
24    #to give a menu
25    menu= ['PLAY','INSTRUCTIONS','ABOUT']
26    option=st.sidebar.selectbox("  MENU",menu) 
27    
28
29    #cases
30    if option==menu[0]: # Play
31        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)")
32        st.write("Computer --> |0>")
33        st.write("User --> |1>") 
34        psi= '|φ>'
35
36        if 'board' not in st.session_state:
37            st.session_state.board=np.array([[psi,psi,psi],[psi,psi,psi],[psi,psi,psi]])
38            st.session_state.available_moves=[0,1,2,3,4,5,6,7,8,9]
39        
40        #dropdown 
41        moves = st.selectbox("Make a move !", st.session_state.available_moves)
42
43        if moves==1:
44            if st.session_state.board[0,0]==psi: #for one time initialization-->1 time selection
45                
46                st.session_state.board[0,0]=getRandom()
47                
48                userFlag=validate(st.session_state.board)
49
50                if not userFlag:
51                    st.dataframe(st.session_state.board)
52                    st.session_state.available_moves=list() # user won
53                
54                comp_square=np.random.randint(1,9)
55                col = (comp_square-1)%3
56                row = math.floor((comp_square-1)/3)
57                compVal = getRandom()
58
59                if st.session_state.board[row,col]==psi:
60                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
61                
62                #============== validation for the comp side ==================
63                compFlag=validate(st.session_state.board)
64                if not compFlag:
65                    return 0
66
67                st.write("Computers move :",comp_square)
68                st.write("Comps value :",compVal)
69                st.dataframe(st.session_state.board)
70
71            else:
72                st.dataframe(st.session_state.board)
73
74        elif moves==2:
75            if st.session_state.board[0,1]==psi: #for one time initialization-->1 time selection
76                
77                st.session_state.board[0,1]=getRandom()
78                
79                userFlag=validate(st.session_state.board)
80
81                if not userFlag:
82                    st.dataframe(st.session_state.board)
83                    st.session_state.available_moves=list() # user won
84                
85                comp_square=np.random.randint(1,9)
86                col = (comp_square-1)%3
87                row = math.floor((comp_square-1)/3)
88                compVal = getRandom()
89
90                if st.session_state.board[row,col]==psi:
91                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
92                
93                #============== validation for the comp side ==================
94                compFlag=validate(st.session_state.board)
95                if not compFlag:
96                    return 0
97
98                st.write("Computers move :",comp_square)
99                st.write("Comps value :",compVal)
100                st.dataframe(st.session_state.board)
101
102            else:
103                st.dataframe(st.session_state.board)
104
105        elif moves==3:
106            if st.session_state.board[0,2]==psi: #for one time initialization-->1 time selection
107                
108                st.session_state.board[0,2]=getRandom()
109                
110                userFlag=validate(st.session_state.board)
111
112                if not userFlag:
113                    st.dataframe(st.session_state.board)
114                    st.session_state.available_moves=list() # user won
115                
116                comp_square=np.random.randint(1,9)
117                col = (comp_square-1)%3
118                row = math.floor((comp_square-1)/3)
119                compVal = getRandom()
120
121                if st.session_state.board[row,col] == psi:
122                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
123                
124                #============== validation for the comp side ==================
125                compFlag=validate(st.session_state.board)
126                if not compFlag:
127                    return 0
128
129                st.write("Computers move :",comp_square)
130                st.write("Comps value :",compVal)
131                st.dataframe(st.session_state.board)
132
133            else:
134                st.dataframe(st.session_state.board)
135        #********************************************************
136         
137        if moves==4:
138            if st.session_state.board[1,0]==psi: #for one time initialization-->1 time selection
139                
140                st.session_state.board[1,0]=getRandom()
141                
142                userFlag=validate(st.session_state.board)
143
144                if not userFlag:
145                    st.dataframe(st.session_state.board)
146                    st.session_state.available_moves=list() # user won
147                
148                comp_square=np.random.randint(1,9)
149                col = (comp_square-1)%3
150                row = math.floor((comp_square-1)/3)
151                compVal = getRandom()
152
153                if st.session_state.board[row,col]==psi:
154                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
155                
156                #============== validation for the comp side ==================
157                compFlag=validate(st.session_state.board)
158                if not compFlag:
159                    return 0
160
161                st.write("Computers move :",comp_square)
162                st.write("Comps value :",compVal)
163                st.dataframe(st.session_state.board)
164
165            else:
166                st.dataframe(st.session_state.board)
167
168        elif moves==5:
169            if st.session_state.board[1,1]==psi: #for one time initialization-->1 time selection
170                
171                st.session_state.board[1,1]=getRandom()
172                
173                userFlag=validate(st.session_state.board)
174
175                if not userFlag:
176                    st.dataframe(st.session_state.board)
177                    st.session_state.available_moves=list() # user won
178                
179                comp_square=np.random.randint(1,9)
180                col = (comp_square-1)%3
181                row = math.floor((comp_square-1)/3)
182                compVal = getRandom()
183
184                if (st.session_state.board[row,col]==psi):
185                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
186                
187                #============== validation for the comp side ==================
188                compFlag=validate(st.session_state.board)
189                if not compFlag:
190                    return 0
191
192                st.write("Computers move :",comp_square)
193                st.write("Comps value :",compVal)
194                st.dataframe(st.session_state.board)
195
196            else:
197                st.dataframe(st.session_state.board)
198
199        elif moves==6:
200            if st.session_state.board[1,2]==psi: #for one time initialization-->1 time selection
201                
202                st.session_state.board[1,2]=getRandom()
203                
204                userFlag=validate(st.session_state.board)
205
206                if not userFlag:
207                    st.dataframe(st.session_state.board)
208                    st.session_state.available_moves=list() # user won
209                
210                comp_square=np.random.randint(1,9)
211                col = (comp_square-1)%3
212                row = math.floor((comp_square-1)/3)
213                compVal = getRandom()
214
215                if st.session_state.board[row,col]==psi:
216                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
217                
218                #============== validation for the comp side ==================
219                compFlag=validate(st.session_state.board)
220                if not compFlag:
221                    return 0
222
223                st.write("Computers move :",comp_square)
224                st.write("Comps value :",compVal)
225                st.dataframe(st.session_state.board)
226
227            else:
228                st.dataframe(st.session_state.board)
229        #********************************************************
230         
231        if moves==7:
232            if st.session_state.board[2,0]==psi: #for one time initialization-->1 time selection
233                
234                st.session_state.board[2,0]=getRandom()
235                
236                userFlag=validate(st.session_state.board)
237
238                if not userFlag:
239                    st.dataframe(st.session_state.board)
240                    st.session_state.available_moves=list() # user won
241                
242                comp_square=np.random.randint(1,9)
243                col = (comp_square-1)%3
244                row = math.floor((comp_square-1)/3)
245                compVal = getRandom()
246
247                if st.session_state.board[row,col]==psi:
248                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
249                
250                #============== validation for the comp side ==================
251                compFlag=validate(st.session_state.board)
252                if not compFlag:
253                    return 0
254
255                st.write("Computers move :",comp_square)
256                st.write("Comps value :",compVal)
257                st.dataframe(st.session_state.board)
258
259            else:
260                st.dataframe(st.session_state.board)
261
262        elif moves==8:
263            if st.session_state.board[2,1]==psi: #for one time initialization-->1 time selection
264                
265                st.session_state.board[2,1]=getRandom()
266                
267                userFlag=validate(st.session_state.board)
268
269                if not userFlag:
270                    st.dataframe(st.session_state.board)
271                    st.session_state.available_moves=list() # user won
272                
273                comp_square=np.random.randint(1,9)
274                col = (comp_square-1)%3
275                row = math.floor((comp_square-1)/3)
276                compVal = getRandom()
277
278                if st.session_state.board[row,col]==psi:
279                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
280                
281                #============== validation for the comp side ==================
282                compFlag=validate(st.session_state.board)
283                if not compFlag:
284                    return 0
285
286                st.write("Computers move :",comp_square)
287                st.write("Comps value :",compVal)
288                st.dataframe(st.session_state.board)
289
290            else:
291                st.dataframe(st.session_state.board)
292
293        elif moves==9:
294            if st.session_state.board[2,2]==psi: #for one time initialization-->1 time selection
295                
296                st.session_state.board[2,2]=getRandom()
297                
298                userFlag=validate(st.session_state.board)
299
300                if not userFlag:
301                    st.dataframe(st.session_state.board)
302                    st.session_state.available_moves=list() # user won
303                
304                comp_square=np.random.randint(1,9)
305                col = (comp_square-1)%3
306                row = math.floor((comp_square-1)/3)
307                compVal = getRandom()
308
309                if st.session_state.board[row,col]==psi:
310                    st.session_state.board[row,col]=compVal   # if that board piece is unoccupied, to give the value in it
311                
312                #============== validation for the comp side ==================
313                compFlag=validate(st.session_state.board)
314                
315                if not compFlag:
316                    return 0
317
318                st.write("Computers move :",comp_square)
319                st.write("Comps value :",compVal)
320                st.dataframe(st.session_state.board)
321
322            else:
323                st.dataframe(st.session_state.board)
324        #********************************************************
325         
326
327
328
329    elif option==menu[1]:
330        st.subheader("Instructions")
331        #st.write("The Instructions are here :- ")
332        psi='|φ>'
333        board=np.array([[psi,psi,psi],[psi,psi,psi],[psi,psi,psi]])
334        st.dataframe(board)
335        
336        instruction1="""
337        The super position states are the above. The steps to start are as follows:- 
338        
339        
340        1> The User gets to choose first always and between nos -> 1 to 9
341        
342        2> The Comp then plays its move 
343            Note: Comp can change its normal game symbol from |0> --> |1>
344            It can also take the user pos and its previous move pos ...If it occurs then then no move is made by comp 
345        
346        3> Then User again and Like that it will continue  
347        
348        4> If you Still dont understand how it will be you can play a demo. Refresh the page and continue with the challenge.
349            
350            
351        ALL THE BEST !! """
352        st.write(instruction1)
353
354        #for board numbering
355        b_num=pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])
356        st.dataframe(b_num)
357
358        instruction2="""
359        The above are the positions of the board and the numberings
360        
361        These are the original board positions which are as follows :-
362        
363        1 --> [0,0]
364        
365        2 --> [0,1] and so on ..
366        
367        """
368        st.write(instruction2)
369
370
371    else:
372        pic=Image.open("QTTlogo.jpg")
373        st.image(pic,caption="GAME LOGO")
374        st.subheader("A quick intro about the game")
375        about="""
376        Created by Dripto 
377        
378        Created using Python, Streamlit, Qiskit
379        
380        We will basically use the [Quantum Superposition](https://en.wikipedia.org/wiki/Quantum_superposition) technique here"""
381        st.write(about)
382
383    
384if __name__=='__main__':
385    c = main()
386    if c == 0:
387        st.subheader('GAME OVERRR!! Refresh the page to play again :)')
388