Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scrabble Score Keeper
#1
Hi everyone.... first time poster here and complete newbie to Python.

I started reading up on Python about a week ago so very early days of learning it.

I was on a plane on Saturday morning and had an idea to make a little app to keep score for scrabble games using the things I had leant already. My wife always wins, so I might need to add a little 'feature' that always makes player 'Rich' win :-)

This is as far as I have got so far..... there are 'elif' for 3 and 4 players too just not included in the below as I wanted to get 2 playuer scoring working first.

#Seems I have to define player*scores otherwise I get errors.
player1score = int(0)
player2score = int(0)
player3score = int(0)
player4score = int(0)

p1go = 0
p2go = 0
p3go = 0
p4go = 0


number_of_players = input('How many players?: ')

if number_of_players == '2':
    player1 = input('Enter name for player 1: ')
    player2 = input('Enter name for player 2: ')
    print('%s, %s.... lets play scrabble!!!' %(player1, player2))
    print(' ')

# I don't like this line below here.... 
    while p1go != 1000:

#I don't like this f'{} coding... must be a nice way to do that. I would like to use the %s method but I does not seem to work.

        p1go = int(input(f'{player1} Score?'))
        p2go = int(input(f'{player2} Score?'))
        player1score = (player1score + p1go)
        player2score = (player2score + p2go)
        print(" ")
        print(player1, ':', player1score)
        print(player2, ':', player2score)
        print('RUNNING TOTAL')
        print(' ')
Is there any better ways to code the above?

I would like to add the following once I know how...
  1. A way to return a "invalid input" if bad information is inputed by the user.
  2. A way to end the game and print out final scores
  3. A way to allow for deducting remaining tiles at the end of the game... it is possible to just enter a negative number but I feel there must be a better way.
  4. Much further down the line it would be nice to add a simple GUI to it.


Thanks for any input you can give me.

Rich
Reply
#2
(Mar-18-2019, 06:15 PM)thebraketester Wrote: Is there any better ways to code the above?
I think a list of players would be better than individual player1/player2/playerETC variables. As an added bonus, you no longer need to do different if/elif for how many players there are, as it would no longer matter.

For example:
>>> players = []
>>> while True:
...   player = input("Player's name [leave blank when done]: ")
...   if not player:
...     break
...   players.append({ "name": player, "score": 0 })
...
Player's name [leave blank when done]: Rich
Player's name [leave blank when done]: Deb
Player's name [leave blank when done]: Craig
Player's name [leave blank when done]:
>>> print(players)
[{'name': 'Rich', 'score': 0}, {'name': 'Deb', 'score': 0}, {'name': 'Craig', 'score': 0}]
Then, when getting scores, you just loop over the players repeatedly. That way it doesn't matter how many people are playing. Sort of like...
>>> game_in_progress = True
>>> while game_in_progress:
...   for ndx, player in enumerate(players):
...     print("It's {0}'s turn, who currently has {1} points.".format(player["name"], player["score"]))
...     valid_input = False
...     while not valid_input:
...       try:
...         points = int(input("How many points did they get? [blank to end the game] "))
...         valid_input = True
...       except ValueError:
...         print("That's not a valid number, please try again.")
...     if not points:
...       game_in_progress = False
...       break
...     players[ndx]["score"] += points
...
It's Rich's turn, who currently has 0 points.
How many points did they get? [blank to end the game] 12
It's Deb's turn, who currently has 0 points.
How many points did they get? [blank to end the game] 21
It's Craig's turn, who currently has 0 points.
How many points did they get? [blank to end the game] 8
It's Rich's turn, who currently has 12 points.
How many points did they get? [blank to end the game] 9
It's Deb's turn, who currently has 21 points.
How many points did they get? [blank to end the game] 36
It's Craig's turn, who currently has 8 points.
How many points did they get? [blank to end the game] 19
It's Rich's turn, who currently has 21 points.
How many points did they get? [blank to end the game] fish
That's not a valid number, please try again.
How many points did they get? [blank to end the game] 8
It's Deb's turn, who currently has 57 points.
How many points did they get? [blank to end the game]
That's not a valid number, please try again.
How many points did they get? [blank to end the game] 0
There's a few minor issues with that, like how it says "leave blank to end the game" but that doesn't work. Should be enough to help move you in the right direction, though.
Reply
#3
You could even have them enter the word and calculate the score for the word. A dictionary of scores per letter, and symbols for double/tripple letter (*) and word (!) scores (like 'kn*ight!!').
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thanks for your help so far. I have been thinking about using classes to define the player... but cannot seem to get the thing to work. Maybe this is not a good solution anyway.


class player:

    def __init__(self, name, score):
        self.name = name
        self.score = score

p1 = input('name?')
s1 = input('score?')

player1 = player(p1, s1)

print(player1)
Reply
#5
That's totally a fine way to do it, and is probably the "best" way. I just didn't include that in my example in case it'd be "too much" all at once.

If you want to print an instance of a class out, you'll get mostly garbage ("<player object at 0x000023024>"). If you want it to be meaningful instead, define a __str__ method:
class player:
    def __init__(self, name, score):
        # as you already have it

    def __str__(self):
        # here, we create a string to "show" what's inside this class
        return "{0} => {1}".format(self.name, self.score)

p1 = player("fred", "2")
print(p1) # fred => 2
Reply
#6
I would give the score parameter to the player __init__ method a default of 0. Generally you are going to want to create the players at the beginning of the game, when they have no score.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with keeping score W3BL3Y 2 6,201 Apr-29-2018, 01:12 PM
Last Post: W3BL3Y

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020