Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flash.text.TextField
- import flash.events.*
- class BlackJack extends TextField {
- new() {
- super()
- @pts = new TextField()
- @cartes_tirees = []
- @scroll = 30
- text = "Cliquez sur moi pour tirer une carte"
- width = 300
- flash.Lib.current.addChild(@)
- @pts.x = 200
- flash.Lib.current.addChild(@pts)
- }
- write(Str txt) {
- tf = new TextField()
- tf.text = txt
- tf.y = @scroll
- tf.width = 1000
- flash.Lib.current.addChild(tf)
- @scroll += 10
- }
- rand_carte() {
- carte = ""
- val = Math.rand(1, 14)
- carte = switch val {
- when 11 { "Valet" }
- when 12 { "Dame" }
- when 13 { "Roi" }
- else { val.to_str() }
- }
- couls = [ "Trefle", "Carreau", "Coeur", "Pique" ]
- out = carte + " de " + couls.rand_val()
- # WHAT ?
- if Lambda.indexOf(@cartes_tirees, out) == -1 {
- @cartes_tirees.push(out)
- return out
- }
- # /WHAT ?
- return rand_carte()
- }
- rand_jeu() {
- persons = [ "Yannick", "Aymeric", "Guillaume", "Ben#1", "Ben#2", "Matthieu", "Aurélie" ]
- p = persons.rand_val()
- jeu = []
- do {
- jeu.push(rand_carte())
- } while eval_game(jeu) <= 15
- io.write(p + jeu.to_str() + " (" + eval_game(jeu) + " pts)")
- return eval_game(jeu)
- }
- carte_val(Str str) {
- val = str.to_i()
- if val != nil { return val }
- return 11
- }
- setPts(Num _pts) {
- @pts.text = "Pts : " + _pts
- }
- eval_game(List cartes) {
- asc = 0
- val = 0
- v = 0
- for carte in cartes {
- v = carte_val(carte)
- if v == 1 { asc++ }
- val += v
- }
- if val > 21 { return val }
- if asc > 0 {
- if val + 10 <= 21 {
- val += 10
- }
- }
- return val
- }
- }
- bj = new BlackJack()
- jeu = []
- pts = 0
- scores = [ bj.rand_jeu() loop 3 ]
- bj.scroll += 10
- bj.addEventListener('click', (e) {
- carte = bj.rand_carte()
- jeu.push(carte)
- pts = eval_game(jeu)
- bj.write(carte)
- bj.setPts(pts)
- if pts > 21 {
- bj.write("Perdu!")
- } else {
- for i in scores {
- if pts < i and i <= 21 {
- return
- }
- }
- bj.write("Gagné!")
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement