Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb731cf

Browse files
Merge pull request avinashkranjan#2175 from nashira26/1580-scrabble
Created a scrabble game
2 parents a0d919d + 4f38ca5 commit cb731cf

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

‎Scrabble/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# SCRABBLE
2+
3+
## Definition
4+
This project is the Scrabble game which can be played between many players.
5+
6+
### Project structure :
7+
8+
- scrabble.py
9+
- requirements.txt
10+
- README.md
11+
12+
## Libraries
13+
### PyDictionary:
14+
Returns meaning, synonyms, antonyms for a word (scraping dictionary.com) limit the no of results also get meaning, synonyms and antonyms in different color..[seemore](https://pypi.org/project/Py-Dictionary/)
15+
16+
## Installing Libraries
17+
The requirements.txt file that has the library used which simply can be used to install the libraries through the package manager [pip](https://pip.pypa.io/en/stable/).
18+
19+
```bash
20+
pip install -r requirements.txt
21+
```
22+
## Usage
23+
```bash
24+
python scrabble.py
25+
```
26+
The players can exit the game by typing Y or else continue the game by Enter.
27+
```python
28+
**********Welcome to the Scrabble game**********
29+
Let's start playing!!
30+
31+
How many players? 2
32+
Player 1: A
33+
Player 2: B
34+
****************************************
35+
36+
A | Type a word: hat
37+
B | Type a word: pot
38+
If exit, type Y:
39+
```
40+
41+
## Functioning
42+
The project.py contains 6 functions including the main function.
43+
Main function prints out the user interface of the game.
44+
45+
### valid(word) function :
46+
This function takes a word and check its validity by looking for its availability in the PyDictionary. If it is available, returns True else it returns False
47+
48+
### compute_score(word) function :
49+
This function computes and outputs the score of a word. If the word is not alphabetic or if the word is not valid, exceptions are arisen.
50+
51+
### player_count() function :
52+
This function prompt the user for an input of number of players. Until a valid integer is typed, the function is called. Finally the count is returned.
53+
54+
### get_input(score_board) function :
55+
This function takes an empty scoreboard dictionary, prompts the players to type their words in number of rounds until the exit term Y is typed and the scores are added simultaneously. Whenever a player types an invalid format of word, the player will be requested again. The function returns a dictionary scoreboard of the players with their score
56+
57+
### winner(score_board) function :
58+
This function takes the scoreboard and finds the winner or winners, then output the result announcement accordingly.
59+
60+
## Author: Nashira

‎Scrabble/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
git+https://github.com/yeahwhat-mc/goslate#egg=goslate
2+
Py-Dictionary

‎Scrabble/scrabble.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
from PyDictionary import PyDictionary
2+
3+
4+
def main():
5+
"""Main entry point of the Scrabble game."""
6+
print('*' * 10 + "Welcome to the Scrabble game" + '*' * 10)
7+
print("Let's start playing!!\n")
8+
score_board = {}
9+
for i in range(player_count()):
10+
player = input(f"Player {i+1}: ")
11+
score_board[player] = 0
12+
print('*' * 40)
13+
print(winner(get_input(score_board)).center(40, " "))
14+
print('*' * 40)
15+
print("Thank you for your time. Have a Nice day!")
16+
17+
18+
def valid(word):
19+
"""
20+
Checks if a word is valid by checking its meaning using PyDictionary.
21+
Args:
22+
word (str): The word to be validated.
23+
Returns:
24+
bool: True if the word is valid, False otherwise.
25+
"""
26+
dictionary = PyDictionary()
27+
return bool(dictionary.meaning(word))
28+
29+
30+
def compute_score(word):
31+
"""
32+
Computes the score for a given word based on a score list.
33+
Args:
34+
word (str): The word for which the score needs to be computed.
35+
Returns:
36+
int: The computed score for the word.
37+
Raises:
38+
ValueError: If the word is invalid or contains non-alphabetic characters.
39+
"""
40+
score_list = {
41+
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2,
42+
'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1,
43+
'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1,
44+
'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}
45+
score = 0
46+
if word.isalpha():
47+
if valid(word):
48+
for char in word:
49+
score += score_list[char.lower()]
50+
return score
51+
raise ValueError("Invalid word")
52+
raise ValueError("Word should only contain alphabetic characters")
53+
54+
55+
def player_count():
56+
"""
57+
Prompts the user to input the number of players for the game.
58+
Returns:
59+
int: The number of players.
60+
Raises:
61+
ValueError: If the user inputs a non-positive number.
62+
"""
63+
while True:
64+
try:
65+
count = int(input("How many players? "))
66+
if count < 1:
67+
raise ValueError("Please enter a positive number")
68+
return count
69+
except ValueError as e:
70+
print(str(e))
71+
72+
73+
def get_input(score_board):
74+
"""
75+
Retrieves the word input from each player and updates their scores.
76+
Args:
77+
score_board (dict): The dictionary storing the scores of each player.
78+
Returns:
79+
dict: The updated score board.
80+
"""
81+
while True:
82+
for player in score_board:
83+
while True:
84+
try:
85+
word = input(f"{player} | Type a word: ")
86+
score_board[player] += compute_score(word)
87+
except ValueError as e:
88+
print(str(e))
89+
else:
90+
break
91+
92+
if input("If exit, type Y: ").lower() == "y":
93+
print('*' * 40)
94+
break
95+
continue
96+
return score_board
97+
98+
99+
def winner(score_board):
100+
"""
101+
Args:
102+
score_board (dict): The dictionary storing the scores of each player.
103+
Returns:
104+
str: The winner(s) message.
105+
Raises:
106+
IndexError: If there are no players in the score board.
107+
"""
108+
sorted_scores = sorted(score_board.items(), key=lambda x: x[1], reverse=True)
109+
if len(sorted_scores) > 0:
110+
max_score = sorted_scores[0][1]
111+
winners = [player for player, score in sorted_scores if score == max_score]
112+
if len(winners) > 1:
113+
return f"It's a tie. The winners are {', '.join(winners)}!!"
114+
return f"The winner is {winners[0]}!"
115+
return "No players found. Game over!"
116+
117+
118+
if __name__ == "__main__":
119+
main()

‎src/goslate

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit d6d511a9c001fe0b5c1cf947fd650770e9794f1d

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /