View difference between Paste ID: Qmj0iBSZ and zCbG5w6v
SHOW: | | - or go back to the newest paste.
1
import flash.text.TextField
2
import flash.events.*
3
4
class BlackJack extends TextField {
5
	new() {
6
		super()
7
		@pts = new TextField()
8
		@cartes_tirees = []
9
		@scroll = 30
10
11
		text = "Cliquez sur moi pour tirer une carte"
12
		width = 300
13
		flash.Lib.current.addChild(@)
14
15
		@pts.x = 200
16
		flash.Lib.current.addChild(@pts)
17
	}
18
19
	write(Str txt) {
20
		tf = new TextField()
21
		tf.text = txt
22
		tf.y = @scroll
23
		tf.width = 1000
24
		flash.Lib.current.addChild(tf)
25
		@scroll += 10
26
	}
27
28
	rand_carte() {
29
		carte = ""
30
		val = Math.rand(1, 14)
31
32
		carte = switch val {
33
			when 11 { "Valet" }
34
			when 12 { "Dame"  }
35
			when 13 { "Roi"   }
36
			else    { val.to_str() }
37
		}
38
39
		couls = [ "Trefle", "Carreau", "Coeur", "Pique" ]
40
		out = carte + " de " + couls.rand_val()
41
42
		# WHAT ?
43
		if Lambda.indexOf(@cartes_tirees, out) == -1 {
44
			@cartes_tirees.push(out)
45
			return out
46
		}
47
		# /WHAT ?
48
49
		return rand_carte()
50
	}
51
52
	rand_jeu() {
53
		persons = [ "Yannick", "Aymeric", "Guillaume", "Ben#1", "Ben#2", "Matthieu", "Aurélie" ]
54
		p = persons.rand_val()
55
		jeu = []
56
57
		do {
58
			jeu.push(rand_carte())
59
		} while eval_game(jeu) <= 15
60
61
		io.write(p + jeu.to_str() + " (" + eval_game(jeu) + " pts)")
62
63
		return eval_game(jeu)
64
	}
65
66
	carte_val(Str str) {
67
		val = str.to_i()
68
		if val != nil { return val }
69
		return 11
70
	}
71
72
	setPts(Num _pts) {
73
		@pts.text = "Pts : " + _pts
74
	}
75
76
	eval_game(List cartes) {
77
		asc = 0
78
		val = 0
79
		v = 0
80
81
		for carte in cartes {
82
			v = carte_val(carte)
83
			if v == 1 { asc++ }
84
			val += v
85
		}
86
87
		if val > 21 { return val }
88
		if asc > 0 {
89
			if val + 10 <= 21 {
90
				val += 10
91
			}
92
		}
93
94
		return val
95
	}
96
97
}
98
99-
nb = new BlackJack()
99+
bj = new BlackJack()
100
jeu = []
101
pts = 0
102
103
scores = [ bj.rand_jeu() loop 3 ]
104
105
bj.scroll += 10
106
107
bj.addEventListener('click', (e) {
108
	carte = bj.rand_carte()
109
	jeu.push(carte)
110
	pts = eval_game(jeu)
111
	bj.write(carte)
112
	bj.setPts(pts)
113
114
	if pts > 21 {
115
		bj.write("Perdu!")
116
	} else {
117
		for i in scores {
118
			if pts < i and i <= 21 {
119
				return
120
			} 
121
		}
122
		bj.write("Gagné!")
123
	}
124
})