awacke1's picture
Update app.py
9110820
raw
history blame
4.69 kB
import numpy as np
import streamlit as st
# Define game constants
LETTER_VALUES = {'A': 1, 'B': 3, 'C': 3, 'D': 2, 'E': 1, 'F': 4, 'G': 2, 'H': 4, 'I': 1, 'J': 8, 'K': 5, 'L': 1, 'M': 3, 'N': 1, 'O': 1, 'P': 3, 'Q': 10, 'R': 1, 'S': 1, 'T': 1, 'U': 1, 'V': 4, 'W': 4, 'X': 8, 'Y': 4, 'Z': 10, ' ': 0}
TILE_DISTRIBUTION = {'A': 9, 'B': 2, 'C': 2, 'D': 4, 'E': 12, 'F': 2, 'G': 3, 'H': 2, 'I': 9, 'J': 1, 'K': 1, 'L': 4, 'M': 2, 'N': 6, 'O': 8, 'P': 2, 'Q': 1, 'R': 6, 'S': 4, 'T': 6, 'U': 4, 'V': 2, 'W': 2, 'X': 1, 'Y': 2, 'Z': 1, ' ': 2}
def get_tile():
"""Get a random tile from the bag."""
tiles = []
for tile, count in TILE_DISTRIBUTION.items():
tiles.extend([tile] * count)
return np.random.choice(tiles)
def get_word_score(word, letter_values):
"""Calculate the score for a given word."""
score = 0
for letter in word:
score += letter_values[letter]
return score
def display_board(board):
"""Display the game board."""
headers = [''] + [str(i+1) for i in range(len(board))]
data = []
for i in range(len(board)):
row = [chr(i+65)]
for j in range(len(board[i])):
row.append(chr(board[i][j]))
data.append(row)
st.table(data=data, headers=headers)
# Create the game board
board = np.zeros((15, 15), dtype=int)
# Create a rack of tiles for the user to choose from
rack = []
for i in range(7):
rack.append(get_tile())
# Display the board and rack
st.write('Scrabble Game')
display_board(board)
st.write('Rack:', rack)
# Allow the user to select a tile and place it on the board
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ']
tile = st.selectbox('Select a tile:', letters)
expander = st.expander('Select a position on the board')
with expander:
row = st.number_input('Row:', min_value=1, max_value=15, value=1)
col = st.number_input('Column:', min_value=1, max_value=15, value=1)
if st.button('Place tile'):
# Check if the selected tile is in the rack
if tile in rack:
# Place the tile on the board
row -= 1
col -= 1
board[row][col] = tile
rack.remove(tile)
# Calculate the score for the word
word = ''
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] != 0:
word += chr(board[i][j])
if len(word) > 1:
score = get_word_score(word, LETTER_VALUES)
st.write(word, ':', score, 'points')
word = ''
# Refill the rack with new tiles
while len(rack) < 7 and len(TILE_DISTRIBUTION) > 0:
tile = get_tile()
rack.append(tile)
TILE_DISTRIBUTION[tile] -= 1
if TILE_DISTRIBUTION[tile] == 0:
del TILE_DISTRIBUTION[tile]
# Display the updated board and rack
display_board(board)
st.write('Rack:', rack)
else:
st.write('Tile not in rack.')
st.markdown("""
Create a top ten game list for word games that you can write in computer code.
Sure, here's a list of the top ten word games that can be implemented in computer code:
Scrabble: A classic game where players take turns creating words on a board using letter tiles and scoring points based on the letters used and placement on the board.
Boggle: In this game, players must find as many words as possible by connecting adjacent letters on a grid of letter cubes within a fixed time limit.
Crosswords: A game where players must fill in a grid of white and black squares with words that fit a series of clues.
Hangman: A game where players must guess a hidden word by suggesting letters, with each incorrect guess resulting in the drawing of a part of a stick figure on a noose.
Word jumble: In this game, players are presented with a scrambled word or phrase and must unscramble it to reveal the correct order of letters.
Word search: A puzzle where players must find a list of hidden words within a grid of letters, often arranged in a pattern or theme.
Typing games: These games test players' typing speed and accuracy by presenting them with a series of words or phrases to type within a certain time limit.
Anagrams: A game where players must create as many words as possible using the letters from a given word or phrase.
Word association: A game where players take turns saying a word that is associated with the previous word, creating a chain of related words.
Ghost: A game where players take turns saying letters, attempting to form a word. The catch is that each player must add a letter to the word, but cannot form a complete word themselves or they lose the round.
""")