IPB     Il futuro di Tevac dipende da te!  

Benvenuto Visitatore ( Log In | Registrati )

  · · · · · · · · · · · ·
Reply to this topicStart new topic
> Giochi da carte - creazione di un mazzo di 52 carte
chebfarid
messaggio 3 Mar 2006, 12:06
Messaggio #1


Level 8/11
********

Gruppo: Team Moderatori
Messaggi: 3.791
Iscritto il: 11-July 03
Da: Milano
Utente Nr.: 395



Mi sto divertendo con un programmino in AppleScript per studiare il Bridge (tempo permettendo spero anche di riuscire a creare una interfaccia in AS Studio).
Uno dei risultati è questa procedura per creare un mazzo di 52 carte e distribuire delle mani a 13 carte. Forse tornerà utile a qualcun altro che vuole sperimentare con AppleScript per creare giochi da carte:
CITAZIONE
property theDeck : {}
property myHand : {}

createTheDeck()
newCards()
showCards()

-- creazione del mazzo di 52 carte
on createTheDeck()
     set theDeck to {}
     set theColors to {"Fiori", "Quadri", "Cuori", "Pique"}
     set hundreds to {100, 200, 300, 400}
     set highCards to {"J", "Q", "K", "A"}
     repeat with i from 1 to 4
          set theColor to item i of theColors
          set the100 to item i of hundreds
          repeat with i from 2 to 10
               set end of theDeck to {cNum: (the100 + i), cName:i, cColor:theColor, cValue:0}
          end repeat
          repeat with j from 1 to 4
               set highCard to item j of highCards
               set end of theDeck to {cNum: (the100 + 10 + j), cName:highCard, cColor:theColor, cValue:j}
          end repeat
     end repeat
end createTheDeck

-- Distribuzione di una mano di 13 carte
on newCards()
     set myHand to {}
     repeat 13 times
          set myCard to some item of theDeck
          set end of myHand to myCard
          cleanTheDeck(myCard)
     end repeat
end newCards

-- aggiornamento del mazzo, togliendo le carte distribuite
on cleanTheDeck(myCard)
     set newDeck to {}
     set theDeckRef to a reference to theDeck
     repeat with i from 1 to count of theDeckRef
          if item i of theDeckRef is not myCard then
               set end of newDeck to item i of theDeckRef
          end if
     end repeat
     set theDeck to newDeck
end cleanTheDeck

-- Mostra e valuta le carte della mano
on showCards()
     set myHandValue to 0
     set myCards to {}
     repeat with aCard in myHand
          set end of myCards to (cNum of aCard)
          set myHandValue to myHandValue + (cValue of aCard)
     end repeat
     
     -- Questo handler ordina myCards - grazie a "dant" (www.macscripter.net)
     set {saveDelims, text item delimiters} to {text item delimiters, ASCII character (10)}
     set x to do shell script "echo \"" & (myCards as text) & "\"|sort -rn"
     set text item delimiters to ASCII character (13)
     set myCards to text items of x
     repeat with i from 1 to count myCards
          set item i of myCards to (item i of myCards) as integer
     end repeat
     set text item delimiters to saveDelims
     
     ----- DISTRIBUZIONE / interessa solo per Bridge -----
     set {Piques, Cuori, Quadri, Fiori} to {0, 0, 0, 0}
     repeat with aCard in myCards
          if aCard > 400 then
               set Piques to Piques + 1
          else if aCard > 300 and aCard < 400 then
               set Cuori to Cuori + 1
          else if aCard > 200 and aCard < 300 then
               set Quadri to Quadri + 1
          else
               set Fiori to Fiori + 1
          end if
     end repeat
     set distribuzione to (Piques & "-" & Cuori & "-" & Quadri & "-" & Fiori) as string
     
     -- TEST / interessa solo per Bridge
     set cardDescription to ""
     repeat with k from 1 to count of myCards
          repeat with m from 1 to count of myHand
               if (item k of myCards) = (cNum of item m of myHand) then
                    set cardDescription to (cardDescription & (cName of item m of myHand) & "-" & (cColor of item m of myHand) & ", ") as string
                    exit repeat
               end if
          end repeat
     end repeat
     set cardDescription to (cardDescription & return & "Valore mano: " & myHandValue & return & "Distribuzione: " & distribuzione) as string
     display dialog cardDescription
     
end showCards


-------------------------
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Buon scripting
Farid


--------------------
Abends lustig, morgens triste
das ist Leben von Artiste
Go to the top of the page
 
+Quote Post
poweruser
messaggio 3 Mar 2006, 15:16
Messaggio #2


MacInATopic
Gruppo icone

Gruppo: Team Moderatori
Messaggi: 24.319
Iscritto il: 27-July 03
Da: Maglie (LE)
Utente Nr.: 440



Interessante come gioco.
Sarebbe interessante vedere anche come mescolerai le carte.

Ho notato che usi spesso una sisntassi di questo tipo:
CODICE
repeat with j from 1 to 4

   set highCard to item j of highCards

   set end of theDeck to {cNum: (the100 + 10 + j), cName:highCard, cColor:theColor, cValue:j}

end repeat

C'è un motivo per cui non utilizzi quest'altra sintassi che a me sembra più elegante?
CODICE
repeat with j in highCards

   set end of theDeck to {cNum: (the100 + 10 + j), cName:highCard, cColor:theColor, cValue:j}

end repeat


Ciao
Paolo


--------------------
Paolo Portaluri

guru, n: A computer owner who can read the manual.
Go to the top of the page
 
+Quote Post
chebfarid
messaggio 3 Mar 2006, 15:36
Messaggio #3


Level 8/11
********

Gruppo: Team Moderatori
Messaggi: 3.791
Iscritto il: 11-July 03
Da: Milano
Utente Nr.: 395



Ciao Paolo,
per mescolare le carte utilizzo al momento solo
CODICE
set x to some item of myList
che restituisce ciclamente un elemento random dal mazzo di carte. Per quello che mi interesse in questa fase basta come generatore casuale. Bisognerebbe fare dei test per capire quanto sono veramente casuali gli elementi estratti da una lista. Si potrebbe comunque scrivere ulteriori piccoli "algoritmi" per mescolare il mazzo prima dell'inizio del gioco.

Per quanto riguarda il repeat loop uso la forma
CODICE
repeat with j from 1 thru 4
perché mi serve proprio anche il valore numerico di j per la creazione delle singole carte, cosa che con l'altra forma non è possibile - anche se per il resto funziona ugualmente ed è più breve/elegante.

Buon scripting
Farid


--------------------
Abends lustig, morgens triste
das ist Leben von Artiste
Go to the top of the page
 
+Quote Post
italiano giochi
messaggio 1 Mar 2008, 12:46
Messaggio #4


Level 1/11
*

Gruppo: Forum User
Messaggi: 1
Iscritto il: 29-February 08
Utente Nr.: 9.866



Grazie per la condivisione del codice.


--------------------
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 

Collapse

> 

    Titolo discussione Risposte Autore discussione Visite Ultima azione
No New Posts   7 nuvolare 133 17 November 2008 - 12:39
Ultimo messaggio di: nuvolare
No New Posts   8 Maurizio75 210 31 October 2008 - 22:04
Ultimo messaggio di: Maurizio75
No New Posts   0 TevacPhoto 29 12 September 2008 - 13:01
Ultimo messaggio di: TevacPhoto
No New Posts   0 TevacPhoto 34 8 August 2008 - 13:14
Ultimo messaggio di: TevacPhoto
No New Posts   0 TevacPhoto 27 8 August 2008 - 13:14
Ultimo messaggio di: TevacPhoto
No New Posts   0 TevacPhoto 39 21 July 2008 - 12:39
Ultimo messaggio di: TevacPhoto
No New Posts   2 Stefano Marri 336 18 July 2008 - 01:30
Ultimo messaggio di: idomy
No New Posts   0 TevacPhoto 47 15 July 2008 - 09:52
Ultimo messaggio di: TevacPhoto
No New Posts   7 inloveangel2 268 11 June 2008 - 16:31
Ultimo messaggio di: sem®
No New Posts   0 sem® 144 9 June 2008 - 20:05
Ultimo messaggio di: sem®
No New Posts   1 pieffe60 125 21 May 2008 - 08:23
Ultimo messaggio di: glauco
No New Posts   4 Slex93 258 4 May 2008 - 10:35
Ultimo messaggio di: mauroga

Modalità di visualizzazione: Normale · Passa a: Lineare · Passa a: Outline


RSS Versione Lo-Fi Oggi è il: 5 December 2008 - 10:37
IP.Board Skin Developed By Creative Networks