As an exercise for myself, I’m creating a custom component: streamlit-gameboard
The idea is to have an abstract game board so that custom game logic can be implemented in Streamlit to create learning projects like Tic-Tac-Toe, Checkers, Chess, Connect Four, etc…including creating AI players.
I am completely new to React and TypeScript, so I am blindly stumbling through, working from the template provided by Streamlit. I have much documentation and cleanup to do.
The hiccup I have is in updating the component state from a manual manipulation of session state. I’ve got it working with one caveat: if the next action is outside of the component, the last manual change is reverted.
- Place a piece on the board with a click (renders fine)
- Manually remove the piece via an edit to session state (renders fine).
If step 3 is some other click within the component, all is well. However, if step 3 is a click outside of the component, that last manual change to session state will be forgotten/reverted.
Within the gameboard render funciton, I have:
const rows = this.props.args["rows"]
const cols = this.props.args["cols"]
const players: Array<any> = this.props.args["players"]
const board_color = this.props.args["board_color"]
const board_state_ss = this.props.args["board_state"]
const key = this.props.args["key"]
// Check if board_state is being passed back to accomodate manual modification
if (board_state_ss !== null) {
this.state.board_state = board_state_ss
}
Note if anyone goes playing: Board size can’t be dynamically changed at the moment due to the partial implementation of manual state adjustments, but the colors and player names can be freely changed for an instantiated gameboard. And I have another bug to chase down where clicking away from the color picker to commit the change doesn’t work if I click within the custom component; I have to click anywhere outside of my custom component and outside of the color picker to dismiss the color picker window.