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