martedì 12 dicembre 2017

Gioco con le carte

Quando si cerca di fare un gioco con le carte da gioco, di solito il primo problema da affrontare è come fare il mazzo, mescolarlo ed estrarre le carte. Oggi vi presento una serie di messaggi e funzioni che fanno tutto il necessario:

on createDeck
   put "A,B,C,D" into cardSuits
   repeat for each item tItem in cardSuits
      repeat with i=1 to 13
         put i & tItem & comma after temp #cards are like: 1A,2A, ..., 1B, ...
      end repeat
   end repeat
   delete the last char of temp
   set the deck of this stack to temp
end createDeck

on shuffleDeck
   put the deck of this stack into temp
   put the number of items of temp into temp2
   sort items of temp by random(temp2)
   set the deck of this stack to temp
end shuffleDeck

function PickCard nCards
   put the deck of this stack into temp
   put item 1 to nCards of temp into picked
   delete item 1 to nCards of temp
   set the deck of this stack to temp
   return picked
end PickCard


In pratica il messaggio CreateDeck vi crea il mazzo e lo memorizza nella proprietà deck.
Il messaggio shuffleDeck mescola il mazzo.
La funzione  pickCard vi estrae il numero di carte che volete dal mazzo e poi vengono anche cancellate dall'elenco del mazzo.

Nessun commento:

Posta un commento