Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---------
- # Fonctions
- # ---------
- # Récupérer une valeur au hasard dans une liste
- proc random_pick { a_list } {
- lindex $a_list [ expr { int(rand() * [ llength $a_list ]) } ]
- }
- # Donner la valeur d'une carte
- proc card_value { card } {
- set value [ lindex [ regexp -all -inline "(.+) de (.+)" $card ] 1 ]
- switch $value {
- valet - dame - roi { return 10 }
- as { return 11 }
- default { return $value }
- }
- }
- # Récupérer une carte dans le jeu
- proc pick_card { game i } {
- upvar $game a_game
- set card [ lreplace $a_game $i $i ]
- set a_game $card
- return [ lindex $card $i ]
- }
- # Récupérer une carte dans le jeu
- proc pick_name { names i } {
- upvar $names a_names
- set name [ lreplace $a_names $i $i ]
- set a_names $name
- return [ lindex $name $i ]
- }
- # ---
- # Jeu
- # ---
- # Noms
- set names {
- Yannick Aurelie Guillaume Benoit Hugo Aymeric
- George Jeremy Matthieu Romain Elisa Corentin
- }
- # Cartes
- set cards { 2 3 4 5 6 7 8 9 10 valet dame roi as }
- # Types
- set types { coeur pique trefle carreau }
- # Création du jeu de cartes
- set game [ list ]
- foreach card $cards {
- foreach type $types {
- lappend game "$card de $type"
- }
- }
- set players [ list ]
- for { set i 0 } { $i < 3 } { incr i } {
- random_pick $names
- }
- puts "Nombre de joueurs (min : 1 ; max : [ llength $names ]) : "
- set nb_players [ gets stdin ]
- if { $nb_players < 1 } {
- set nb_players 1
- }
- if { $nb_players >= [ llength $names ] } {
- set nb_players [ expr { [ llength $names ] - 1 } ]
- }
- for { set i 0 } { $i < $nb_players } { incr i } {
- set name [ random_pick $names ]
- pick_name names [ lsearch $names $name ]
- lappend players $name
- set player($name) [ list ]
- set score($name) 0
- for { set j 0 } { $j < 3 } { incr j } {
- set card [ random_pick $game ]
- lappend player($name) $card
- set score($name) [ expr { $score($name) + [ card_value $card ] } ]
- pick_card game [ lsearch $game $card ]
- }
- }
- parray player
- puts {}
- set nb_victories 0
- foreach name $players {
- if { $score($name) == 21 } {
- incr nb_victories
- }
- }
- foreach name $players {
- if { $score($name) == 21 } {
- if { $nb_victories == 1 } {
- set victory victoire
- } else {
- set victory egalite
- }
- } else {
- set victory defaite
- }
- puts "Score de $name : $score($name) - $victory"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement