Scrabble+ Program in Python

the School Programmer

Four players play Scrabble+. For each hand of Scrabble+, every player enters a word in the scoreboard, getting a score that depends on the value of the letters composing the inserted word.

Players create their own words by choosing them from a hand of 8 tiles, which are replaced once the word has been played until they are exhausted. The total number of tiles is 130. The game ends when a player manages to finish all the tiles in their hand and there are no more tiles available to replace those just played: this happens because all the tiles are in the scoreboard and in the hand of the other players.

At the end of the game, the player with the highest score wins, considering that for each tile that remains unplayed (that is, which remains in a player's hand when the game ends) 3 points are subtracted.

The scores are calculated as follows:

 1 point:  E, A, I, O, N, R, T, L, S, U
 2 points: D, G
 3 points: B, C, M, P
 4 points: F, H, V, W, Y
 5 points: K
 8 points: J, X
10 points: Q , Z

Design a function ex1(g1, g2, g3, g4, dim_hand, num_letters) that returns the scores of a game of Scrabble+ played among the four players, with the variant that the number of initial letters is "num_letters" instead of 130, and that the number of letters available to each player is "dim_hand". g1, g2, g3 and g4 are lists of strings that contain the sequence of words inserted by player 1, player 2, player 3 and player 4, respectively, in every round.

Ex.: dim_hand=5, num_letters=40 g1 = ['seta','peeks','deter'] g2 = ['reo','pumas'] g3 = ['xx','xx'] g4 = ['frs','bern']

 Notice that 5 tiles are given to each of the four players already
 at the beginning of the game, thus num_letters decreases accordingly.
dim_hand - num_letters - word - score
5 5 5 5    20            seta    4  0  0  0
5 5 5 5    16            reo     4  3  0  0
5 5 5 5    13            xx      4  3 16  0
5 5 5 5    11            frs     4  3 16  6
5 5 5 5     8            peeks  15  3 16  6
5 5 5 5     3            pumas  15 12 16  6
5 3 5 5     0            xx     15 12 32  6
5 3 3 5     0            bern   15 12 32 12
5 3 3 1     0            deter  21 12 32 12
0 3 3 1     0                     GAME OVER
-------------------------------------------
Final score                     21  3 23  9

The TIMEOUT for each test is 0.5 seconds.

NOTICE: it’s forbidden to:

  • - import other libraries
  • - use global variables
  • - open files

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#function for returning score of each word
def search(word):
    score = {"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}
    word.lower()
    sum=0
    for i in word:
        sum=sum+score[i]
            
    return(sum)

def ex1(g1,g2,g3,g4,dim_hand,num_letters):

    score=[0]*4                         #list to store score
    tile_in_hand=[dim_hand]*4           #list to store letters in hand
    num_letters=num_letters-(dim_hand*4)#to store total letters
    index=0                             #for switching between words of each list 


    maxx=max(len(g1),len(g2),len(g3),len(g4))
    
    
    while (maxx)>0:
        
           #for player 1
        if(index <=len(g1) - 1):
            word=g1[index]
            score[0]=score[0]+search(word)#adding score of each word in list
                
            if(num_letters<len(word)):
                tile_in_hand[0]=tile_in_hand[0]-len(word)+num_letters
                num_letters=0
            else:
                num_letters-=len(word)    
        
            # for player 2
        if (index <=len(g2) - 1):
            word=g2[index]
            score[1]=score[1]+search(word)
            if (num_letters < len(word)):
                tile_in_hand[1] = tile_in_hand[1] - len(word) + num_letters
                num_letters=0
            else:
                num_letters-=len(word) 

            #for player 3
        if (index <=len(g3) - 1):
            word=g3[index]
            score[2]=score[2]+search(word)
            if (num_letters < len(word)):
                tile_in_hand[2] = tile_in_hand[2] - len(word) + num_letters
                num_letters=0
            else:
                num_letters-=len(word)     
       
            # for player 4
        if (index <=len(g4) - 1):
            word=g4[index]
            score[3]=score[3]+search(word)
            if (num_letters < len(word)):
                tile_in_hand[3] = tile_in_hand[3] - len(word) + num_letters
                num_letters=0
            else:
                num_letters-=len(word)        

        index=index+1
        maxx-=1
        
            #final score calculation by deducting score of leftout letters
    for i in range(4):
       score[i]=score[i]-3*tile_in_hand[i]
       
    return(score)



if __name__=="__main__":
    print(ex1([ "flew", "pus", "gce", "bead", "msc", "zed", "pull", "anon", "eigg", "shot", "clod", "flow", "dfc" ],
              [ "burr", "cup", "sess", "keep", "knox", "kph", "bate", "ryas", "aida", "reus", "gala", "nit", "inde" ],
              [ "zuni", "buss", "rids", "wen", "bad", "omne", "boar", "imps", "back", "rase", "pure", "slog" ],
              [ "tugs", "hats", "shat", "soon", "yule", "pawl", "poe", "cups", "kcal", "bobs", "gush", "soap" ]
              ,4,200))

Source: https://github.com/Tanay-Gupta/Scrabble-Python



Join Us

For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..



Found this useful? Share with your friends. Share

Also check:

Comments

You are welcome to share your ideas with us in comments!!!

Full Screen Mode

Archive

Contact Form

Send