awacke1 commited on
Commit
b99447d
·
1 Parent(s): 0c8d337

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import numpy as np
3
+ import streamlit as st
4
+
5
+ # Define game constants
6
+ 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}
7
+ 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}
8
+
9
+ def get_tile():
10
+ """Get a random tile from the bag."""
11
+ tiles = []
12
+ for tile, count in TILE_DISTRIBUTION.items():
13
+ tiles.extend([tile] * count)
14
+ return np.random.choice(tiles)
15
+
16
+ def get_word_score(word, letter_values):
17
+ """Calculate the score for a given word."""
18
+ score = 0
19
+ for letter in word:
20
+ score += letter_values[letter]
21
+ return score
22
+
23
+ def display_board(board):
24
+ """Display the game board."""
25
+ for i in range(len(board)):
26
+ for j in range(len(board[i])):
27
+ st.write(board[i][j], end=' ')
28
+ st.write('\n')
29
+
30
+ # Create the game board
31
+ board = np.zeros((15, 15), dtype=int)
32
+
33
+ # Create a rack of tiles for the user to choose from
34
+ rack = []
35
+ for i in range(7):
36
+ rack.append(get_tile())
37
+
38
+ # Display the board and rack
39
+ st.write('Scrabble Game')
40
+ display_board(board)
41
+ st.write('Rack:', rack)
42
+
43
+ # Allow the user to select a tile and place it on the board
44
+ 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', ' ']
45
+ tile = st.selectbox('Select a tile:', letters)
46
+
47
+ expander = st.expander('Select a position on the board')
48
+ with expander:
49
+ row = st.number_input('Row:', min_value=1, max_value=15, value=1)
50
+ col = st.number_input('Column:', min_value=1, max_value=15, value=1)
51
+
52
+ if st.button('Place tile'):
53
+ # Check if the selected tile is in the rack
54
+ if tile in rack:
55
+ # Place the tile on the board
56
+ row -= 1
57
+ col -= 1
58
+ board[row][col] = tile
59
+ rack.remove(tile)
60
+
61
+ # Calculate the score for the word
62
+ word = ''
63
+ for i in range(len(board)):
64
+ for j in range(len(board[i])):
65
+ if board[i][j] != 0:
66
+ word += chr(board[i][j])
67
+ if len(word) > 1:
68
+ score = get_word_score(word, LETTER_VALUES)
69
+ st.write(word, ':', score, 'points')
70
+ word = ''
71
+
72
+ # Refill the rack with new tiles
73
+ while len(rack) < 7 and len(TILE_DISTRIBUTION) > 0:
74
+ tile = get_tile()
75
+ rack.append(tile)
76
+ TILE_DISTRIBUTION[tile] -= 1
77
+ if TILE_DISTRIBUTION[tile] == 0:
78
+ del TILE_DISTRIBUTION[tile]
79
+
80
+ # Display the updated board and rack
81
+ display_board(board)
82
+ st.write('Rack:', rack)
83
+ else:
84
+ st.write('Tile not in rack.')
85
+
86
+
87
+ st.markdown("""
88
+ Create a top ten game list for word games that you can write in computer code.
89
+ Sure, here's a list of the top ten word games that can be implemented in computer code:
90
+
91
+ 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.
92
+
93
+ 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.
94
+
95
+ Crosswords: A game where players must fill in a grid of white and black squares with words that fit a series of clues.
96
+
97
+ 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.
98
+
99
+ 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.
100
+
101
+ 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.
102
+
103
+ 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.
104
+
105
+ Anagrams: A game where players must create as many words as possible using the letters from a given word or phrase.
106
+
107
+ Word association: A game where players take turns saying a word that is associated with the previous word, creating a chain of related words.
108
+
109
+ 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.
110
+ """)