| import streamlit as st |
| from PIL import Image |
| import random, operator, ast, sqlite3, datetime, json, math |
| from itertools import product, permutations |
| from utils import * |
| from sqlite3 import Connection |
| import pandas as pd |
|
|
| @st.cache_data() |
| def static_variables(): |
| suits = ["hearts","spades","diamonds","clubs"]; jo = "_of_"; path = "static/" |
| return suits, jo, path |
|
|
| @st.cache_data(ttl=3600*4) |
| def static_layout_top(): |
| st.header("24 Game!") |
| st.markdown(f"""Welcome! Today is {st.session_state.date}""") |
|
|
| st.markdown(""" |
| #### How to play: |
| |
| - Using +, -, *, / , and () on 4 cards to get 24; |
| - Each card represent a number as shown, where J=11, Q=12, K=13, and A=1 |
| - You will have 3 operations, and you have to express the first and second |
| operation explicitly, e.g. even though you have 6, 6, 6, 6 and you know that |
| 6 + 6 + 6 + 6 = 24, you need to type your expression clearly as ((6+6)+6)+6 or |
| (6+6)+(6+6) to specify your first two operations. |
| - The algorithm encourage thinking of associativity, so, (12/6)*(11+1) and |
| (12/6)*(1+11) would be counted as two different and correct answers. |
| |
| """) |
| c0, c1, c2, c3 = st.columns([0.25,0.25,0.25,0.25]); cs = [c0,c1,c2,c3] |
| for idx, x in enumerate(cs): |
| with x: |
| im = Image.open(path+str(st.session_state.nums[idx])+jo+st.session_state.su[idx]+".png") |
| st.image(im) |
| |
| @st.cache_resource(ttl=3600*4) |
| def get_cards(): |
| i = True |
| while i: |
| nums = random.sample(range(1,14),4) |
| suit = random.sample(suits,4) |
| sols = solve24(nums) |
| i = (len(sols) == 0) |
| conn = get_connection("data24.db"); date = str(datetime.date.today()) |
| value = (date, str(nums), str(suit), str(sols), len(sols)) |
| insert_value_games(conn, value) |
| return nums, suit, sols, date, conn |
|
|
| def check_ans_default(inp): |
| if inp in st.session_state.sols: |
| if st.session_state.get('ans') == None: |
| st.session_state['ans'] = {inp} |
| |
| else: |
| s1 = st.session_state['ans'] |
| s1.add(inp) |
| st.session_state['ans'] = s1 |
| a1, a2 = st.columns([0.5,0.5]) |
| with a1: |
| st.markdown(f"Got one! :green[${inp}=24$]") |
| with a2: |
| st.markdown(f"There are {len(st.session_state.sols)} expressions in total. :chart_with_upwards_trend:") |
| else: |
| st.write("Oop! Incorrect.....") |
| return |
|
|
| def check_ans_user(inp): |
| id = int(st.session_state.u_id); date = st.session_state.date; |
| nums = str(st.session_state.nums); suit = str(st.session_state.su) |
| df = get_plays_answer(st.session_state.conn, id, date, nums) |
| if len(df) > 0: |
| st.session_state['ans'] = set(ast.literal_eval(df.values[0][0])) |
| else: |
| st.session_state['ans'] = set() |
| if inp in st.session_state.sols: |
| s1 = st.session_state['ans'] |
| if len(s1) > 0: |
| s1.add(inp) |
| else: |
| s1 = {inp} |
| st.session_state['ans'] = s1 |
| if st.session_state.u_id is not None: |
| if not check_plays_user(st.session_state.conn, id, date, nums): |
| value = (id, date, nums, suit, json.dumps(s1,default=tuple)) |
| insert_value_plays(st.session_state.conn, value) |
| else: |
| update_value_plays(st.session_state.conn, id, date, nums, json.dumps(s1,default=tuple)) |
| else: |
| st.write("Oop! Incorrect.....") |
|
|
| |
| |
| suits, jo, path = static_variables() |
| st.session_state['nums'], st.session_state['su'], st.session_state['sols'], st.session_state['date'], st.session_state['conn'] = get_cards() |
|
|
|
|
| |
| |
| |
| |
| |
|
|
| static_layout_top() |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| inp = st.text_input("Enter your expression here: ").replace(" ", "") |
| if st.button("Try your luck!"): |
| |
| |
| |
| check_ans_default(inp) |
| |
| |
| |
| if st.session_state.get('ans') == None: |
| pass |
| else: |
| st.markdown("### Your correct answers: ") |
| col = 4; n = len(st.session_state.ans) |
| row = math.ceil(n / col) |
| for i in range(row): |
| cols = st.columns(col) |
| for m in range(min(4, n-4*i)): |
| cols[m].latex(list(st.session_state.ans)[m+i*4].replace("/","\div")) |
| st.markdown("##") |
|
|
| |
|
|