-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt.py
105 lines (86 loc) · 3.22 KB
/
ttt.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
'A game of tic-tac-toe'
import streamlit as st
import numpy as np
import gameboard as sg
st.title('Tic Tac Toe')
def reset_game():
st.session_state.victory = 0
st.session_state.game = sg.DEFAULT(3,3)
st.button('Reset game', on_click=reset_game)
def check_win(board):
# Check rows
for i in range(3):
if all(board[i][j]['player'] == 1 for j in range(3)):
return 1
if all(board[i][j]['player'] == 2 for j in range(3)):
return 2
# Check columns
for j in range(3):
if all(board[i][j]['player'] == 1 for i in range(3)):
return 1
if all(board[i][j]['player'] == 2 for i in range(3)):
return 2
# Check diagonals
if all(board[i][i]['player'] == 1 for i in range(3)):
return 1
if all(board[i][i]['player'] == 2 for i in range(3)):
return 2
if all(board[i][2-i]['player'] == 1 for i in range(3)):
return 1
if all(board[i][2-i]['player'] == 2 for i in range(3)):
return 2
if all(board[i][j]['player'] != 0 for i in range(3) for j in range(3)):
return -1
return 0
def initialize():
if 'color1' not in st.session_state:
st.session_state.color1 = '#3A5683'
st.session_state.alpha1 = 255
if 'color2' not in st.session_state:
st.session_state.color2 = '#73956F'
st.session_state.alpha2 = 255
if 'game' not in st.session_state:
st.session_state.player1 = 'Player 1'
st.session_state.player2 = 'Player 2'
st.session_state.game = sg.DEFAULT(3,3)
st.session_state.victory = 0
initialize()
if st.session_state.player1 == '':
st.session_state.player1 = 'Player 1'
if st.session_state.player2 == '':
st.session_state.player2 = 'Player 2'
color1 = st.session_state.color1
color2 = st.session_state.color2
with st.sidebar:
st.header("Player Settings")
def reset_colors():
del st.session_state.color1
del st.session_state.color2
del st.session_state.alpha1
del st.session_state.alpha2
st.button('reset colors',on_click = reset_colors)
# Set player info
player1 = st.text_input('Player 1', key='player1')
color1 = st.color_picker('❄️ Player 1 Color', key='color1')
alpha1 = st.slider("Player 1 Alpha", 30, 255, key='alpha1')
st.write('---')
player2 = st.text_input('Player 2', key='player2')
color2 = st.color_picker('🎈 Player 2 Color', key='color2')
alpha2 = st.slider("Player 2 Alpha", 30, 255, key='alpha2')
color1 = color1+f'{alpha1:02x}'
color1 = color1.upper()
color2 = color2+f'{alpha2:02x}'
color2 = color2.upper()
players = {player1:color1,player2:color2}
st.session_state.victory = check_win(st.session_state.game)
if st.session_state.victory != 0:
for i in range(3):
for j in range(3):
st.session_state.game[i][j]['enabled'] = False
if st.session_state.victory == 1:
st.snow()
if st.session_state.victory == 2:
st.balloons()
if st.session_state.victory == -1:
st.success('It\'s a tie! Please click reset to play again.')
sg.gameboard(3,3, players, board_state = st.session_state.game, key='game')