tick-tack-toe-1 / streamlit_app.py
nonstopiodemo's picture
Upload folder using huggingface_hub
cea0710 verified
import streamlit as st
import numpy as np
def check_winner(board):
for player in ['X', 'O']:
# Check rows, columns, and diagonals
for i in range(3):
if (board[i, :] == [player, player, player]).all() or \
(board[:, i] == [player, player, player]).all():
return player
if (np.diag(board) == [player, player, player]).all() or \
(np.diag(np.fliplr(board)) == [player, player, player]).all():
return player
return None
def main():
st.title('Tic Tac Toe Game')
board = np.array([['', '', ''],
['', '', ''],
['', '', '']])
player = 'X'
winner = None
while not winner and '' in board:
st.write(f"Player {player}'s turn")
row = st.slider("Select Row", 0, 2)
col = st.slider("Select Column", 0, 2)
if board[row, col] == '':
board[row, col] = player
winner = check_winner(board)
player = 'O' if player == 'X' else 'X'
else:
st.warning("Invalid move. Please select an empty cell.")
st.write(board)
if winner:
st.success(f"Player {winner} wins!")
else:
st.warning("It's a draw!")
if __name__ == '__main__':
main()