Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://zetcode.com/lang/tcl/io/
- # http://wiki.tcl.tk/1549
- # https://fr.wikipedia.org/wiki/Tool_Command_Language
- # ---------
- # 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
- }
- # set nb_players [ gets stdin ]
- set nb_players 3
- if { $nb_players > [ llength $names ] } {
- set nb_players [ llength $names ]
- }
- for { set i 0 } { $i < $nb_players } { incr i } {
- # puts $names
- 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 ]
- }
- # puts $name
- }
- 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