Forum >> Principianti >> Pseudo codice per prime armi.

Pagina: 1

Mi date qualche consiglio in modo che possa ottimizzare questo codice. Solo pseudo codice per favore, grazie

'''
Abbiamo quattro giocatori che si sfidano a Scarabeo+. In ogni mano
di Scarabeo+, i giocatori, a turno, devono inserire una parola nel
tabellone ed ottengono un punteggio, calcolato in base al valore
delle lettere che compongono la parola inserita.

Ogni giocatore crea la propria parola scegliendola a partire da una
mano di 8 lettere, che vengono rimpiazzate una volta che la parola
è stata giocata, finché non sono esaurite. Il numero totale di
lettere è 130. Il gioco finisce quando un giocatore riesce a
finire tutte le lettere nella sua mano e non ci sono più lettere a
disposizione per rimpiazzare quelle che ha appena giocato (ovvero,
le 130 lettere sono esaurite, perché giocate oppure perché in mano
agli altri giocatori).

Alla fine delle giocate, vince il giocatore che ha accumulato più
punti, considerando che per ogni lettera che rimane non giocata
(ovvero rimane in mano ad un giocatore quando il gioco finisce)
vengono sottratti 3 punti.
I punteggi sono così calcolati:
1 punto: E, A, I, O, N, R, T, L, S, U
2 punti: D, G
3 punti: B, C, M, P
4 punti: F, H, V, W, Y
5 punti: K
8 punti: J, X
10 punti: Q, Z

Progettare una funzione ex1(g1, g2, g3, g4, dim_hand, num_letters) che calcola i
punteggi di una partita di Scarabeo+ svolta fra i 4 giocatori, con
la variante che il numero di lettere iniziali è num_letters, piuttosto che
130 e il numero di lettere a disposizione di ogni giocatore è dim_hand.
g1, g2, g3 e g4 sono liste di stringhe che rappresentano le
giocate dei giocatori g1, g2, g3 e g4, rispettivamente,
in ciascun turno.

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

Notare che all’inizio della partita 5 lettere vengono date ad ognuno dei
giocatori, dunque il contatore num_letters decresce conseguentemente.

dim_hand - num_letters - parola - punti
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
---------------------------------------------
Finale 21 3 23 9

'''
def calcola_valore(parola):
valore = 0
dizionario_punteggi = {
"a":1,"e":1,"i":1,"o":1,"n":1,"r":1,"t":1,"l":1,"s":1,"u":1,
"d":2,"g":2,
"b":3,"c":3,"m":3,"p":3,
"f":4,"h":4,"v":4,"w":4,"y":4,
"k":5,
"j":8,"x":8,
"q":10,"z":10,
}
for lettera in parola:
valore += dizionario_punteggilettera
return valore

def ex1(g1, g2, g3, g4, dim_hand, num_letters):
lung_g1 = 0
lung_g2 = 0
lung_g3 = 0
lung_g4 = 0
lettere_mano_G1 = dim_hand
lettere_mano_G2 = dim_hand
lettere_mano_G3 = dim_hand
lettere_mano_G4 = dim_hand
lista_punteggi = [0,0,0,0]
num_letters -= dim_hand * 4
while lettere_mano_G1 > 0 and lettere_mano_G2 > 0 and lettere_mano_G3 > 0 and lettere_mano_G4 > 0 or num_letters > 0:
if lung_g1 <= len(g1) - 1:
parola_G1 = g1lung g1
punteggio_G1 = calcola_valore(parola_G1)
lista_punteggi0 += punteggio_G1
lung_g1 += 1
#Calcolo i valori di G1 con il ciclo
lettere_mano_G1 -= len(parola_G1)
#Scarico la mano di G1
if num_letters > 0:
if lettere_mano_G1 + num_letters > dim_hand:
lettere_mano_G1 += len(parola_G1)
elif lettere_mano_G1 + num_letters <= dim_hand:
lettere_mano_G1 += num_letters
#carico la mano di G1
num_letters -= len(parola_G1)
#Scarico il mazzo
#turno 1
if lung_g2 <= len(g2) - 1:
parola_G2 = g2lung g2
punteggio_G2 = calcola_valore(parola_G2)
lista_punteggi1 += punteggio_G2
lung_g2 += 1
#Calcolo i valori di G2 con il ciclo
lettere_mano_G2 -= len(parola_G2)
#Scarico la mano di G2
if num_letters > 0:
if lettere_mano_G2 + num_letters > dim_hand:
lettere_mano_G2 += len(parola_G2)
elif lettere_mano_G2 + num_letters <= dim_hand:
lettere_mano_G2 += num_letters
#carico la mano di G2
num_letters -= len(parola_G2)
#Scarico il mazzo
#turno 2
if lung_g3 <= len(g3) - 1:
parola_G3 = g3lung g3
punteggio_G3 = calcola_valore(parola_G3)
lista_punteggi2 += punteggio_G3
lung_g3 += 1
#Calcolo i valori di G3 con il ciclo
lettere_mano_G3 -= len(parola_G3)
#Scarico la mano di G3
if num_letters > 0:
if lettere_mano_G3 + num_letters > dim_hand:
lettere_mano_G3 += len(parola_G3)
elif lettere_mano_G3 + num_letters <= dim_hand:
lettere_mano_G3 += num_letters
#carico la mano di G3
num_letters -= len(parola_G3)
#Scarico il mazzo
#turno 3
if lung_g4 <= len(g4) - 1:
parola_G4 = g4lung g4
punteggio_G4 = calcola_valore(parola_G4)
lista_punteggi3 += punteggio_G4
lung_g4 += 1
#Calcolo i valori di G4 con il ciclo
lettere_mano_G4 -= len(parola_G4)
#Scarico la mano di G2
if num_letters > 0:
if lettere_mano_G4 + num_letters > dim_hand:
lettere_mano_G4 += len(parola_G4)
elif lettere_mano_G4 + num_letters <= dim_hand:
lettere_mano_G4 += num_letters
#carico la mano di G4
num_letters -= len(parola_G4)
#Scarico il mazzo
#turno 4
lista_punteggi0 -= lettere_mano_G1 * 3
lista_punteggi1 -= lettere_mano_G2 * 3
lista_punteggi2 -= lettere_mano_G3 * 3
lista_punteggi3 -= lettere_mano_G4 * 3
return lista_punteggi
pass
Messaggio nascosto da :
Spam
Messaggio nascosto da :
Spam
Messaggio nascosto da :
Spam
Messaggio nascosto da :
Spam, Spam, Spam, Spam... Lovely Spam! Wonderful Spam!
Messaggio nascosto da :
Spam, Spam, Spam, Spam… Lovely Spam! Wonderful Spam!
Spam, Spam, Spam, Spam…Lovely Spam! Wonderful Spam!
Messaggio nascosto da :
Spam, Spam, Spam, Spam… Lovely Spam! Wonderful Spam!
Messaggio nascosto da :
Spam, Spam, Spam, Spam… Lovely Spam! Wonderful Spam!
Give me some advice so I can optimize this code. Just pseudo code please, thanks

"'
We have four players competing in Scrabble+. In every hand
of Scrabble+, players, in turn, must insert a word into the
scoreboard and get a score, calculated based on the value
of the letters that make up the inserted word.

Each player creates their own word by choosing it from a
hand of 8 letters, which are replaced once the word
it was played, until they are sold out. The total number of
letters is 130. The game ends when a player manages to
finish all the letters in his hand and there are no more letters to
provision to replace those he has just played (i.e.,
the 130 letters are sold out, because they are played or because they are in the hand
to other players).

At the end of the plays, the player who has accumulated the most wins
points, considering that for each letter that remains unplayed
(i.e. remains in the hands of a player 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 calculates the
scores from a Scrabble+ game played between the 4 players, with
the variant that the number of initial letters is num_letters, rather than
130 and the number of letters available to each player is dim_hand.
g1, g2, g3 and g4 are lists of strings representing the
plays of players g1, g2, g3 and g4, respectively,
in each shift.

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

Note that at the beginning of the game 5 letters are given to each of the
players, so the num_letters counter decreases accordingly.

dim_hand - num_letters - word - dots
5 5 5 5 20 silk 4 0 0 0
5 5 5 5 16 guilty 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 21 3 23 9

"'
def calculate_value(word):
value = 0
dictionary_scores = {
"a":1,"e":1,"i":1,"o":1,"n":1,"r":1,"t":1,"l":1,"s":1,"u":1,
"d":2,"g":2,
"b":3,"c":3,"m":3,"p":3,
"f":4,"h":4,"v":4,"w":4,"y":4,
"k":5,
"j":8,"x":8,
"q":10,"z":10,
}
for letter in word:
value += dictionary_scoresletter
return value

def ex1(g1, g2, g3, g4, dim_hand, num_letters):
lung_g1 = 0
lung_g2 = 0
lung_g3 = 0
lung_g4 = 0
hand_letters_G1 = dim_hand
letters_hand_G2 = dim_hand
letters_hand_G3 = dim_hand
hand_letters_G4 = dim_hand
score_list = [0,0,0,0]
num_letters -= dim_hand * 4
while letters_hand_G1 > 0 and letters_hand_G2 > 0 and letters_hand_G3 > 0 and letters_hand_G4 > 0 or num_letters > 0:
if lung_g1 <= len(g1) - 1:
word_G1 = g1lung g1
score_G1 = calculate_value(word_G1)
score_list0 += score_G1
lung_g1 += 1
#Calcolo the values of G1 with the cycle
letters_hand_G1 -= len(word_G1)
#Scarico G1's hand
if num_letters > 0:
if letters_hand_G1 + num_letters > dim_hand:
letters_hand_G1 += len(word_G1)
elif letters_hand_G1 + num_letters <= dim_hand:
letters_hand_G1 += num_letters
#carico G1's hand
num_letters -= len(word_G1)
#Scarico the deck
#turno 1
if lung_g2 <= len(g2) - 1:
word_G2 = g2lung g2
score_G2 = calculate_value(word_G2)
score_list1 += score_G2
lung_g2 += 1
#Calcolo the values of G2 with the cycle
letters_hand_G2 -= len(word_G2)
#Scarico G2's hand
if num_letters > 0:
if letters_hand_G2 + num_letters > dim_hand:
letters_hand_G2 += len(word_G2)
elif letters_hand_G2 + num_letters <= dim_hand:
letters_hand_G2 += num_letters
#carico G2's hand
num_letters -= len(word_G2)
#Scarico the deck
#turno 2
if lung_g3 <= len(g3) - 1:
word_G3 = g3lung g3
score_G3 = calculate_value(word_G3)
score_list2 += score_G3
lung_g3 += 1
#Calcolo the values of G3 with the cycle
letters_hand_G3 -= len(word_G3)
#Scarico G3's hand
if num_letters > 0:
if letters_hand_G3 + num_letters > dim_hand:
letters_hand_G3 += len(word_G3)
elif letters_hand_G3 + num_letters <= dim_hand:
letters_hand_G3 += num_letters
#carico G3's hand
num_letters -= len(word_G3)
#Scarico the deck
#turno 3
if lung_g4 <= len(g4) - 1:
word_G4 = g4lung g4
score_G4 = calculate_value(word_G4)
score_list3 += score_G4
lung_g4 += 1
#Calcolo the values of G4 with the cycle
letters_hand_G4 -= len(word_G4)
#Scarico G2's hand
if num_letters > 0:
if letters_hand_G4 + num_letters > dim_hand:
letters_hand_G4 += len(word_G4)
elif letters_hand_G4 + num_letters <= dim_hand:
letters_hand_G4 += num_letters
#carico G4's hand
num_letters -= len(word_G4)
#Scarico the deck
#turno 4
score_list0 -= hand_letters_G1 * 3
score_list1 -= hand_letters_G2 * 3
score_list2 -= hand_letters_G3 * 3
score_list3 -= hand_letters_G4 * 3
return list_scores
pass
Nice breakdown 👍

To optimize this, try treating all players uniformly using arrays/objects, run the game in round-based loops, and move repeated logic (word play, refill, scoring) into small helper functions. This will remove duplication, reduce bugs, and make the game flow much easier to manage and extend.


Pagina: 1



Esegui il login per scrivere una risposta.