summaryrefslogtreecommitdiffstats
path: root/examples/blackjack/blackjack.scxml
blob: 5362546c32e482e35943f692671d26dfa763785d (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<scxml
      xmlns="http://www.w3.org/2005/07/scxml" 
      initial="root" profile="ecmascript">
    <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 updateDisplay ()
                {
                    cardsLabel.text = "My Cards: " + myDeck + " Dealer Cards: " + dealerCards;
                }

                


                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;
                var pot = 0;
                var points = 1000;

        </script>

    </onentry>
        <invoke type="q-bindings">
            <content>
                [[welcomeLabel,"text","Welcome to Blackjack"]]
            </content>
    </invoke>
    <transition event="q-signal:newGameButton.clicked()" target="newgame" />
    <state id="newgame">
      <onentry>
        <script>
            points = 1000;
            pointsLabel.text = points;
        </script>
           </onentry>
           <transition target="newround" />
    </state>
      <state id="quitdlg">
            <invoke type="q-messagebox">
                <content>
                    {
                      "parent" : gameWidget,
                      "icon" : QMessageBox.Question,
                      "windowTitle" : "Exit Blackjack",
                      "text" : "Are you sure?",
                      "standardButtons" : QMessageBox.Yes|QMessageBox.No
                    }
                </content>
            </invoke>
        <transition event="q-messagebox.finished" target="exit" cond="_event.data[0]==QMessageBox.Yes" />
        <transition event="q-messagebox.finished" target="gamestate" cond="_event.data[0]==QMessageBox.No" />
  </state>
      <state id="game">
        <history type="deep" id="gamestate" />
        <transition event="q-signal:exitButton.clicked()" target="quitdlg" />
      <state id="newround">
        <onentry>
          <script>
                pot = 0;
                availDeck.reset ();
                myDeck.clear ();
                dealerCards.clear();
                dealerCards.drawFrom(availDeck);
                hitMe ();
                hitMe ();
          </script>
        </onentry>
        <transition target="waitForBet" />
      </state>
      <state id="waitForBet">
        <invoke type="q-bindings"><content>
                [
                    [betEdit,"enabled",true],
                    [betButton,"enabled",true],
                    [surrenderButton,"enabled",true],
                    [welcomeLabel,"text","Please place your bet"]
                ]
        </content></invoke>
        <transition event="q-signal:betButton.clicked()" target="testCards" cond="parseInt(betEdit.text) &lt;= points">
            <script>
                pot = betEdit.text;
                points -= pot;
                pointsLabel.text = points;
            </script>
        </transition>
        <transition event="q-signal:betButton.clicked()" target="betTooHigh" cond="parseInt(betEdit.text) &gt; points">
         </transition>
        <transition event="q-signal:surrenderButton.clicked()" target="newround" />
      </state>
        <state id="betTooHigh">
            <invoke type="q-messagebox">
                <content>
                    {
                      "parent" : betEdit,
                      "icon" : QMessageBox.Warning,
                      "windowTitle" : "Bet is Too High",
                      "text" : "Please Place Another Bet",
                      "standardButtons" :     
                                QMessageBox.Ok
                    }
                </content>
            </invoke>
            <transition event="q-messagebox.finished" target="waitForBet"  />
            <transition event="bth-mb-timeout" target="waitForBet" />
        <onentry>
        <send event="bth-mb-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="q-signal:hitButton.clicked()" target="testCards">
            <script>hitMe (); </script>
        </transition>
        <transition event="q-signal:standButton.clicked()" target="getDealerCards" />
         <invoke type="q-bindings"><content>
            [
                [welcomeLabel,"text","Hit/Stand?"],
                [hitButton,"enabled",true],
                [standButton,"enabled",true]
            ]
         </content></invoke>
      </state>

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

      <state id="checkWinner">
        <onentry>
            <assign dataid="diff" expr="myDeck.evalBest() - dealerCards.evalBest()" />
        </onentry>

        <transition cond="_data.diff&gt;0" target="win" />
        <transition cond="_data.diff&lt;0" target="loss" />
        <transition cond="_data.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="q-signal:newRoundButton.clicked()" target="newround" />
           <transition event="timeout" target="newround" />
           <onentry>
            <send event="timeout" delay="3s" />
            </onentry>
             <state id="win">
                <onentry>
                  <script>
                      points = parseInt(points) + parseInt(pot) * 2;
                      pointsLabel.text = points;
                  </script>
                </onentry>
                <invoke type="q-bindings"><content>[[welcomeLabel,"text","You Won!"]]</content></invoke>
              </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>
                      <script>
                        points = parseInt(points) + parseInt(pot);
                        pointsLabel.text = points;
                      </script>
                  </onentry>
                </state>
            </state>
        </state>

    </state>

</scxml>