summaryrefslogtreecommitdiffstats
path: root/examples/qml/blackjack/blackjack.scxml
blob: ab1b9cda150ac28e6ea60cbe5784ed748f18c2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<scxml
      xmlns="http://www.w3.org/2005/07/scxml" 
      initial="root" profile="ecmascript">
      <datamodel>
        <data id="points" expr="1000" />
        <data id="pot" expr="0" />
        <data id="pointsToBet" expr="0" />
      </datamodel>
    <script><![CDATA[
                var Suits = "CDHS";
                var Ranks = "-A23456789TJQK";

                function Card (r,s)
                {
                    this.rank = r;
                    this.suit = s;
                    this.minValue = Math.min(r,10);
                    this.toString = function() {
                        return "" + Ranks[this.rank] + Suits[this.suit];
                    };
;
                }

                function Deck()
                {
                    this.draw = function()
                    {
                        return this.cards.pop();
                    };
                    this.cards = new Array();
                    this.reset = function()
                   {
                        this.clear ();
                        for (var i=1; i <= 13; ++i)
                            for (var j = 0; j < 4; ++j)
                                this.cards.push(new Card(i,j));
                                    this.cards.sort(function (a,b)
                                    {
                                            return Math.random() * 3 - 1;
                                    });
                    };
                    
                    this.clear = function()
                    {
                        this.cards = new Array;
                    };
                    this.evalMin = function ()
                   {
                        var minVal = 0;
                        var cardCount = this.cards.length;
                        for (c in this.cards) {
                            minVal += this.cards[c].minValue;
                        }
                        if (cardCount > 4 && minVal < 22)
                            minVal = 21;
                        return minVal;
                    };

                    this.evalBest = function() 
                    {
                        var bestVal = this.evalMin();
                        if (bestVal > 21)
                            return 0;
                        else if (bestVal == 21)
                            return bestVal;

                        for (i in this.cards) {
                            if (this.cards[i].rank == 1)
                            {
                                var v = bestVal + 10;
                                if (v <= 21)
                                    bestVal = v;
                            }
                        }
                        return bestVal;

                    };
                    this.toString = function()                 
                    {
                        var s = "";
                        for (i in this.cards)
                            s += this.cards[i].toString() + ":";

                        return s;
                    };

                    this.drawFrom = function(d)
                    {
                        var c = d.draw ();
                        this.cards.push(c);
//                        updateDisplay ();
                    };
                }


                function hitMe ()
                {
                    myDeck.drawFrom (availDeck);
                }
    ]]></script>
    <final id="exit" />
    <state id="root" initial="newgame">
    <onentry>
        <script>
                var myDeck = new Deck;
                var dealerCards = new Deck;
                var availDeck = new Deck;
        </script>

    </onentry>
    <transition event="NEWGAME" target="newgame" />
    <state id="newgame">
        <transition target="newround" />
    </state>
      <state id="quitdlg">
        <transition event="YES" target="exit" />
        <transition event="NO" target="gamestate" />
  </state>
      <state id="game">
        <history type="deep" id="gamestate" />
        <transition event="EXIT" target="quitdlg" />
      <state id="newround">
        <onentry>
            <assign dataid="pot" expr="0" />
          <script>
                availDeck.reset ();
                myDeck.clear ();
                dealerCards.clear();
                dealerCards.drawFrom(availDeck);
                hitMe ();
                hitMe ();
          </script>
        </onentry>
        <transition target="waitForBet" />
      </state>
      <state id="waitForBet">
        <transition event="BET" target="testCards" cond="parseInt(_data.pointsToBet) &lt;= _data.points">
            <assign dataid="pot" expr="_data.pointsToBet" />
            <assign dataid="points" expr="_data.points-_data.pot" />
        </transition>
        <transition event="BET" target="betTooHigh" cond="parseInt(_data.pointsToBet) &gt; _data.points">
         </transition>
        <transition event="SURRENDER" target="newround" />
      </state>
        <state id="betTooHigh">
            <transition event="OK" target="waitForBet"  />
            <transition event="TIMEOUT" target="waitForBet" />
        <onentry>
        <send event="TIMEOUT" delay="1500ms" />
        </onentry>
      </state>
      <state id="testCards">
        <transition target="loss" cond="myDeck.evalBest() == 0" />
        <transition target="getDealerCards" cond="myDeck.evalBest() == 21" />
        <transition target="waitForAction" cond="myDeck.evalBest() %21 != 0" />
      </state>

      <state id="waitForAction">
        <transition event="HIT" target="testCards">
            <script>hitMe (); </script>
        </transition>
        <transition event="STAND" target="getDealerCards" />
      </state>

      <state id="getDealerCards">
         <onentry>
            <script><![CDATA[
                while (dealerCards.evalBest() > 0 && dealerCards.evalBest() < 17)
                    dealerCards.drawFrom(availDeck);
            ]]></script>
            <raise event="DONE" />
         </onentry>
         <transition target="checkWinner" />
      </state>

      <state id="checkWinner">

        <onentry>
            <assign location="_data.diff" expr="myDeck.evalBest() - dealerCards.evalBest()" />
        </onentry>

        <transition cond="diff&gt;0" target="win" />
        <transition cond="diff&lt;0" target="loss" />
        <transition cond="diff==0" target="draw" />
      </state>
      <state id="endGame">
        <invoke type="q-bindings"><content>[[welcomeLabel,"text","Game Over"]]</content></invoke>
        <transition event="TIMEOUT" target="newgame" />
        <onentry>
            <send event="TIMEOUT" delay="3s" />
        </onentry>
      </state>
        <state id="endRound">
           <invoke type="q-bindings"><content>[[newRoundButton,"enabled",true]]</content></invoke>
           <transition event="NEWROUND" target="newround" />
           <transition event="TIMEOUT" target="newround" />
           <onentry>
            <send event="TIMEOUT" delay="3s" />
            </onentry>

             <state id="win">
                <onentry>
                      <assign dataid="points" expr="Math.floor(_data.points) + Math.floor(_data.pot) * 2" />
                </onentry>
              </state>
              <state id="loss">
                <invoke type="q-bindings"><content>[[welcomeLabel,"text","You Lost..."]]</content></invoke>
                <transition cond="points == 0" target="endGame" />
                </state>
                <state id="draw">
                <invoke type="q-bindings"><content>[[welcomeLabel,"text","You It's a draw."]]</content></invoke>
                  <onentry>
                      <assign dataid="points" expr="Math.floor(_data.points) + Math.floor(pot)" />
                  </onentry>
                </state>
            </state>
        </state>

    </state>

</scxml>