SHOW:
|
|
- or go back to the newest paste.
1 | - | # http://zetcode.com/lang/tcl/io/ |
1 | + | |
2 | - | # http://wiki.tcl.tk/1549 |
2 | + | |
3 | - | # https://fr.wikipedia.org/wiki/Tool_Command_Language |
3 | + | |
4 | # Récupérer une valeur au hasard dans une liste | |
5 | proc random_pick { a_list } { | |
6 | lindex $a_list [ expr { int(rand() * [ llength $a_list ]) } ] | |
7 | } | |
8 | ||
9 | # Donner la valeur d'une carte | |
10 | proc card_value { card } { | |
11 | set value [ lindex [ regexp -all -inline "(.+) de (.+)" $card ] 1 ] | |
12 | ||
13 | switch $value { | |
14 | valet - dame - roi { return 10 } | |
15 | as { return 11 } | |
16 | default { return $value } | |
17 | } | |
18 | } | |
19 | ||
20 | # Récupérer une carte dans le jeu | |
21 | proc pick_card { game i } { | |
22 | upvar $game a_game | |
23 | set card [ lreplace $a_game $i $i ] | |
24 | set a_game $card | |
25 | return [ lindex $card $i ] | |
26 | } | |
27 | ||
28 | # Récupérer une carte dans le jeu | |
29 | proc pick_name { names i } { | |
30 | upvar $names a_names | |
31 | set name [ lreplace $a_names $i $i ] | |
32 | set a_names $name | |
33 | return [ lindex $name $i ] | |
34 | } | |
35 | ||
36 | # --- | |
37 | # Jeu | |
38 | # --- | |
39 | # Noms | |
40 | set names { | |
41 | Yannick Aurelie Guillaume Benoit Hugo Aymeric | |
42 | George Jeremy Matthieu Romain Elisa Corentin | |
43 | } | |
44 | ||
45 | # Cartes | |
46 | set cards { 2 3 4 5 6 7 8 9 10 valet dame roi as } | |
47 | ||
48 | # Types | |
49 | set types { coeur pique trefle carreau } | |
50 | ||
51 | # Création du jeu de cartes | |
52 | set game [ list ] | |
53 | ||
54 | foreach card $cards { | |
55 | foreach type $types { | |
56 | lappend game "$card de $type" | |
57 | } | |
58 | } | |
59 | ||
60 | set players [ list ] | |
61 | ||
62 | for { set i 0 } { $i < 3 } { incr i } { | |
63 | random_pick $names | |
64 | } | |
65 | ||
66 | puts "Nombre de joueurs (min : 1 ; max : [ llength $names ]) : " | |
67 | set nb_players [ gets stdin ] | |
68 | ||
69 | if { $nb_players < 1 } { | |
70 | - | # set nb_players [ gets stdin ] |
70 | + | set nb_players 1 |
71 | - | set nb_players 3 |
71 | + | |
72 | ||
73 | - | if { $nb_players > [ llength $names ] } { |
73 | + | if { $nb_players >= [ llength $names ] } { |
74 | - | set nb_players [ llength $names ] |
74 | + | set nb_players [ expr { [ llength $names ] - 1 } ] |
75 | } | |
76 | ||
77 | for { set i 0 } { $i < $nb_players } { incr i } { | |
78 | - | # puts $names |
78 | + | |
79 | pick_name names [ lsearch $names $name ] | |
80 | lappend players $name | |
81 | ||
82 | set player($name) [ list ] | |
83 | set score($name) 0 | |
84 | ||
85 | for { set j 0 } { $j < 3 } { incr j } { | |
86 | set card [ random_pick $game ] | |
87 | lappend player($name) $card | |
88 | set score($name) [ expr { $score($name) + [ card_value $card ] } ] | |
89 | pick_card game [ lsearch $game $card ] | |
90 | } | |
91 | } | |
92 | ||
93 | parray player | |
94 | - | # puts $name |
94 | + | |
95 | puts {} | |
96 | ||
97 | set nb_victories 0 | |
98 | ||
99 | foreach name $players { | |
100 | if { $score($name) == 21 } { | |
101 | incr nb_victories | |
102 | } | |
103 | } | |
104 | ||
105 | foreach name $players { | |
106 | if { $score($name) == 21 } { | |
107 | if { $nb_victories == 1 } { | |
108 | set victory victoire | |
109 | } else { | |
110 | set victory egalite | |
111 | } | |
112 | } else { | |
113 | set victory defaite | |
114 | } | |
115 | ||
116 | puts "Score de $name : $score($name) - $victory" | |
117 | } |