Spaces:
Sleeping
Sleeping
File size: 3,412 Bytes
2752e5e ac27494 8ea503c 8fb5384 fcc7d30 ac27494 6a2274b 5f4fc3b 8ef9f1b fcc7d30 fdbcf27 fcc7d30 8ef9f1b fdbcf27 ac27494 6d57342 8fb5384 b07a75e 7e8af86 03da642 7e8af86 8fb5384 7e8af86 af15634 a890b42 3f65a39 1e3d902 ac27494 8fb5384 63525c3 8fb5384 58902ba fdbcf27 63525c3 7e8af86 cb70215 c224d5d 7e8af86 c224d5d 6a2274b c224d5d 74695d2 ac27494 2752e5e fdbcf27 a890b42 fdbcf27 ac27494 8fb5384 40f1b61 | 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 | import streamlit as st
#Globals
if "legal_numbers" not in st.session_state:
st.session_state.legal_numbers = [1,2,3,4,5,6,7,8,9]
if "gridNumbers" not in st.session_state:
st.session_state.gridNumbers = ["...", "...", "...", "...", "...", "...", "...", "...", "..."]
if "Value" not in st.session_state:
st.session_state.Value = "X"
def get_userInput():
input = st.text_input("Enter a number between 1 to 9 to mark " + st.session_state.Value, key="input")
return input
def validate_userInput(user_input):
if user_input.isdigit():
if int(user_input) < 1 or int(user_input) > 9:
return False
else:
if int(user_input) in st.session_state.legal_numbers:
return True
else:
return False
else:
return False
def error_Message(msg):
st.write(msg)
def toggleValue():
if st.session_state.Value == "X":
st.session_state.Value = "O"
else:
st.session_state.Value = "X"
def display_guide():
st.text("How to play: Enter the grid number as shown below to mark 'X' or 'O'. \n"+
"Kindly pick any number shown in Legal Numbers. \n" +
"Number cannot be repeated more than twice.")
col1, col2, col3 = st.columns(3)
col4, col5, col6 = st.columns(3)
col7, col8, col9 = st.columns(3)
with col1:
st.text("1")
with col2:
st.text("2")
with col3:
st.text("3")
with col4:
st.text("4")
with col5:
st.text("5")
with col6:
st.text("6")
with col7:
st.text("7")
with col8:
st.text("8")
with col9:
st.text("9")
def display_board(num):
col1, col2, col3 = st.columns(3)
col4, col5, col6 = st.columns(3)
col7, col8, col9 = st.columns(3)
st.session_state.gridNumbers[int(num)-1]= st.session_state.Value
with col1:
st.text(st.session_state.gridNumbers[0])
with col2:
st.text(st.session_state.gridNumbers[1])
with col3:
st.text(st.session_state.gridNumbers[2])
with col4:
st.text(st.session_state.gridNumbers[3])
with col5:
st.text(st.session_state.gridNumbers[4])
with col6:
st.text(st.session_state.gridNumbers[5])
with col7:
st.text(st.session_state.gridNumbers[6])
with col8:
st.text(st.session_state.gridNumbers[7])
with col9:
st.text(st.session_state.gridNumbers[8])
toggleValue()
def reset_game():
st.session_state.legal_numbers = [1,2,3,4,5,6,7,8,9]
st.session_state.gridNumbers = ["...", "...", "...", "...", "...", "...", "...", "...", "..."]
st.session_state.Value = "X"
def update_legalNumbers(number):
st.session_state.legal_numbers.remove(int(user_input))
st.text("Legal Numbers: " + "".join(str(st.session_state.legal_numbers)))
#UIApp starts here
st.set_page_config(page_title="Python - Tic Tac Toe", page_icon=":python:")
st.header("Python - Tic Tac Toe")
display_guide()
user_input=get_userInput()
display_board()
#UI Buttons
submit=st.button('Submit')
replay=st.button('Replay')
#Button functionality
if submit:
response=validate_userInput(user_input)
if response:
update_legalNumbers(user_input)
display_board(user_input)
else:
error_Message("Please enter a digit between 1 to 9.")
if replay:
reset_game()
|