1 /*
2  * (c) Copyright 1997, Qun Zhang.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without fee,
6  * provided that the above copyright notice appear in all copies and
7  * that both that copyright notice and this permission notice appear in
8  * supporting documentation, and that the name of Qun Zhang not be used
9  * in advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  Qun Zhang make no
11  * representations about the suitability of this software for any purpose.
12  * It is provided "as is" without express or implied warranty.
13  *
14  * THE ABOVE-NAMED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE ABOVE-NAMED BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  *
22  */
23 
24 #include <iostream>
25 #include <X11/Xatom.h>
26 #include <X11/Intrinsic.h>
27 #include <X11/Xutil.h>
28 #include <X11/Shell.h>
29 
30 #include <Xm/Xm.h>
31 //#include <Xm/DialogS.h>
32 #include <Xm/MessageB.h>
33 #include <Xm/DrawingA.h>
34 #include <Xm/Form.h>
35 #include <Xm/Label.h>
36 #include <Xm/Scale.h>
37 #include <Xm/ScrollBar.h>
38 #include <Xm/ToggleB.h>
39 #include <Xm/PushBG.h>
40 #include <Xm/SeparatoG.h>
41 #include <Xm/Text.h>
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <assert.h>
46 #include "Dealer.h"
47 #include "Player.h"
48 #include "Table.h"
49 
50 #define XtNbetUnit "betUnit"
51 #define XtCbetUnit "betUnit"
52 #define XtNminBet "minBet"
53 #define XtCminBet "minBet"
54 #define XtNbankroll "bankroll"
55 #define XtCbankroll "bankroll"
56 #define XtNjackpot "jackpot"
57 #define XtCjackpot "jackpot"
58 #define XtNminjackpot "minjackpot"
59 #define XtCminjackpot "minjackpot"
60 #define XtNplayers "players"
61 #define XtCplayers "players"
62 
63 typedef struct {
64   int bankroll;
65   int betUnit;
66   int minBet;
67   int jackpot;
68   int minjackpot;
69   int players;
70   } RValues, *RVptr;
71 
72 RValues rv;
73 
74 XtResource resources[] = {
75    { XtNbankroll, XtCbankroll, XtRInt, sizeof(int),
76      XtOffset(RVptr, bankroll), XtRImmediate, (XtPointer)100 },
77    { XtNbetUnit, XtCbetUnit, XtRInt, sizeof(int),
78      XtOffset(RVptr, betUnit), XtRImmediate, (XtPointer)1 },
79    { XtNminBet, XtCminBet, XtRInt, sizeof(int),
80      XtOffset(RVptr, minBet), XtRImmediate, (XtPointer)0 },
81    { XtNjackpot, XtCjackpot, XtRInt, sizeof(int),
82      XtOffset(RVptr, jackpot), XtRImmediate, (XtPointer)10000000 },
83    { XtNminjackpot, XtCminjackpot, XtRInt, sizeof(int),
84      XtOffset(RVptr, minjackpot), XtRImmediate, (XtPointer)1000000 },
85    { XtNplayers, XtCplayers, XtRInt, sizeof(int),
86      XtOffset(RVptr, players), XtRImmediate, (XtPointer)1 }
87 };
88 
89 Widget Table::toplevel = (Widget) NULL;
90 Widget Table::Layout = (Widget) NULL;
91 Widget Table::PlayTable = (Widget) NULL;
92 Widget Table::ControlTable = (Widget) NULL;
93 Widget Table::DealerHand = (Widget) NULL;
94 Widget Table::DealButton = (Widget) NULL;
95 Widget Table::PlayerHand = (Widget) NULL;
96 Widget Table::AnteScroll = (Widget) NULL;
97 Widget Table::BetButton = (Widget) NULL;
98 Widget Table::FoldButton = (Widget) NULL;
99 Widget Table::ProgressiveButton = (Widget) NULL;
100 Widget Table::JackPotLabel = (Widget) NULL;
101 Widget Table::ExceptionDialog = (Widget) NULL;
102 Widget Table::YouHaveLabel[6] ;
103 Widget Table::YouHaveLabelValue[6];
104 Widget Table::YouWinLoseValue[6];
105 Widget Table::DealerTableLabel;
106 
107 Widget Table::HelpRulesDialog;
108 Widget Table::HelpRulesButton;
109 Widget Table::HelpRulesScrolledText;
110 
SetLabel(Widget w,char * label)111 void Table::SetLabel(Widget w, char* label)
112 {
113 #ifdef DEBUG
114   	 cout << "Calling Table::SetLabel: " << label <<endl;
115 #endif
116    int i;
117    Arg args[5];
118    XmString s;
119 
120    assert(w != NULL);
121    s = XmStringCreate(label, XmSTRING_DEFAULT_CHARSET);
122 
123    i = 0;
124    XtSetArg(args[i], XmNlabelString, s);  i++;
125    XtSetValues(w, args, i);
126    XmStringFree(s);
127 }
128 
Initialize()129 void Table::Initialize()
130 {
131 
132 #ifdef DEBUG
133   	 cout << "BankRoll: " <<  _bankroll <<endl;
134     cout << "BetUnit: " << _betUnit <<endl;
135     cout << "Minimum Bet: " << _minBet <<endl;
136     cout << "JackPot: " <<  _jackpot <<endl;
137     cout << "JackPot: " <<  _minjackpot <<endl;
138     cout << "Number Of Players: " <<  _totalSeats <<endl;
139 #endif
140 	 _dealerFace = 0;
141 	 _playerSeats[0] = new DealerSeat(this);
142 	 _dealer = new Dealer(_playerSeats[0]);
143     _dealer->JackPot(_jackpot);
144     _dealer->MinJackPot(_minjackpot);
145 
146     Player * player;
147     for(int i =1; i <= _players; i++)
148 	 {
149 	    _playerSeats[i] = new PlayerSeat(this, (i-1)*100/_players,
150 														    i*100/_players);
151 		 player = new Player(_dealer, _playerSeats[i]);
152 		 _dealer->Add(player);
153 		 player->BankRoll(_bankroll);
154 	 }
155     CurrentSeat(0);
156     Disable( BetButton ) ;
157     Disable( FoldButton) ;
158 }
159 
Table(int argc,char ** argv)160 Table::Table(int argc, char **argv) :  _dealer(0)
161 {
162 
163    try{
164    XtSetLanguageProc ( (XtAppContext) NULL, (XtLanguageProc) NULL, (XtPointer) NULL );
165    XtToolkitInitialize ();
166    app_context = XtCreateApplicationContext ();
167    _display = XtOpenDisplay (app_context, NULL, argv[0], "CaribbeanStud",
168                             NULL, 0, &argc, argv);
169    int scr = DefaultScreen(_display);
170    if (!_display)
171    {
172        printf("%s: can't open display, exiting...\n", argv[0]);
173        exit (-1);
174    }
175 
176 	CreateWidgets ( _display, argv[0], argc, argv);
177 	AddCallbacks();
178 	XtRealizeWidget (toplevel);
179 	Initialize();
180    }
181    catch(const char *msg)
182    {
183       Table::DoExceptionDialog(msg);
184 #ifdef DEBUG
185       cout << msg << endl;
186 #endif
187    }
188 }
189 
~Table()190 Table::~Table()
191 {
192 	for(int i=0; i<_totalSeats; i++)
193 		delete _playerSeats[i];
194 	delete _dealer;
195 }
196 
CreateWidgets(Display * display,char * app_name,int app_argc,char ** app_argv)197 void Table::CreateWidgets(Display *display, char *app_name, int app_argc,
198 								  char **app_argv)
199 {
200 	Widget children[6];      /* Children to manage */
201 	Arg al[64];                    /* Arg List */
202 	register int ac = 0;           /* Arg Count */
203 	char  str[50];    /* temporary storage for XmStrings */
204 
205 	XtSetArg(al[ac], XmNallowShellResize, TRUE); ac++;
206 //   XtSetArg(al[ac], XmNargc, app_argc); ac++;
207 //   XtSetArg(al[ac], XmNargv, app_argv); ac++;
208 
209 
210 	toplevel = XtAppCreateShell ( app_name, "CaribbeanStud", applicationShellWidgetClass, display, al, ac );
211 
212    XtGetApplicationResources(toplevel, (unsigned char *)&rv, resources,
213              XtNumber(resources), NULL, 0);
214 
215     _bankroll = rv.bankroll;
216     _betUnit =  rv.betUnit;
217     _minBet =   rv.minBet;
218     _jackpot =  rv.jackpot;
219     _minjackpot =  rv.minjackpot;
220     _players =  (rv.players > 6 ) ? 6 : rv.players;
221     _totalSeats = _players;
222 
223 	ac = 0;
224 	XtSetArg(al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ac++;
225    ExceptionDialog = XmCreateMessageDialog(toplevel,
226          "ExceptionDialog", al, ac);
227 
228 	ac = 0;
229 	XtSetArg(al[ac], XmNmarginWidth, 0); ac++;
230 	XtSetArg(al[ac], XmNmarginHeight, 0); ac++;
231 	XtSetArg(al[ac], XmNadjustLast, TRUE); ac++;
232 	XtSetArg(al[ac], XmNadjustMargin, TRUE); ac++;
233 	XtSetArg(al[ac], XmNrubberPositioning, TRUE); ac++;
234 	Layout = XmCreateForm ( toplevel, "Layout", al, ac );
235 
236    // control table
237 	ac = 0;
238 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
239 	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
240 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
241 	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
242 	XtSetArg(al[ac], XmNrightPosition, 35); ac++;
243 	XtSetArg(al[ac], XmNrubberPositioning, TRUE); ac++;
244 	ControlTable = XmCreateForm ( Layout, "ControlTable", al, ac );
245 
246    // play Table
247 	ac = 0;
248 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
249 	XtSetArg(al[ac], XmNtopOffset, 20); ac++;
250 	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
251 	XtSetArg(al[ac], XmNbottomOffset, 20); ac++;
252 	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
253 	XtSetArg(al[ac], XmNrightOffset, 20); ac++;
254 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
255 	XtSetArg(al[ac], XmNleftWidget, ControlTable); ac++;
256 	XtSetArg(al[ac], XmNrubberPositioning, TRUE); ac++;
257 	PlayTable = XmCreateForm ( Layout, "PlayTable", al, ac );
258    // For all the contral Items
259 
260    // JackPot
261 
262    ac = 0;
263 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
264 	XtSetArg(al[ac], XmNtopOffset, 20); ac++;
265    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
266    XtSetArg(al[ac], XmNrightOffset, 20); ac++;
267    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
268 	XtSetArg(al[ac], XmNleftOffset, 10); ac++;
269    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
270    XColor      color;
271    Colormap cmap = DefaultColormap(_display, DefaultScreen(_display));
272    color.red = 52428;
273    color.green = color.blue = 0;
274    XAllocColor(display, cmap, &color);
275    XtSetArg(al[ac], XmNforeground, color.pixel);   ac++;
276    JackPotLabel = XmCreateLabel(ControlTable, "JackPotLabel", al, ac);
277 	sprintf(str, "JACKPOT: %d", rv.jackpot);
278    SetLabel(JackPotLabel, str);
279    XtManageChild(JackPotLabel);
280 
281    ac = 0;
282    XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
283    XtSetArg(al[ac], XmNtopWidget, JackPotLabel); ac++;
284    XtSetArg(al[ac], XmNtopOffset, 10); ac++;
285    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
286    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
287    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
288    XtSetArg(al[ac], XmNbottomPosition, 20); ac++;
289    XtSetArg(al[ac], XmNalignment, XmALIGNMENT_CENTER); ac++;
290    char name[50];
291    XmString s;
292    for(int j=0; j < rv.players; j++)
293    {
294       XtSetArg(al[ac], XmNleftPosition, j*100/rv.players);
295       XtSetArg(al[ac+1], XmNrightPosition,(j+1)*100/rv.players);
296       sprintf(name, "YouHaveLabel%i" , j+1);
297       YouHaveLabel[j] = XmCreateLabel(ControlTable, name, al, ac +2);
298       sprintf(name, " Player %i:  " , j+1);
299       SetLabel(YouHaveLabel[j], name);
300    }
301 
302    ac = 0;
303 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
304    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
305    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
306 	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
307 	XtSetArg(al[ac], XmNbottomPosition, 30); ac++;
308    XtSetArg(al[ac], XmNalignment, XmALIGNMENT_CENTER); ac++;
309    for(int j=0; j < rv.players; j++)
310    {
311 		XtSetArg(al[ac], XmNtopWidget, YouHaveLabel[j]);
312       XtSetArg(al[ac+1], XmNleftPosition, j*100/rv.players);
313       XtSetArg(al[ac+2], XmNrightPosition,(j+1)*100/rv.players);
314       sprintf(name, "YouHaveLabelValue%i" , j+1);
315       YouHaveLabelValue[j] = XmCreateLabel(ControlTable, name, al, ac +3);
316       sprintf(str, "$%d", rv.bankroll);
317       SetLabel(YouHaveLabelValue[j], str);
318    }
319 
320    ac = 0;
321    XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
322    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
323    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
324    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
325    XtSetArg(al[ac], XmNbottomPosition, 40); ac++;
326    XtSetArg(al[ac], XmNalignment, XmALIGNMENT_CENTER); ac++;
327    for(int j=0; j < rv.players; j++)
328    {
329       XtSetArg(al[ac], XmNtopWidget, YouHaveLabelValue[j]);
330       XtSetArg(al[ac+1], XmNleftPosition, j*100/rv.players);
331       XtSetArg(al[ac+2], XmNrightPosition,(j+1)*100/rv.players);
332       sprintf(name, "YouWinLoseValue%i" , j+1);
333       YouWinLoseValue[j] = XmCreateLabel(ControlTable, name, al, ac +3);
334       SetLabel(YouWinLoseValue[j], "");
335    }
336 
337    // dealbutton
338    ac = 0;
339 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
340 	XtSetArg(al[ac], XmNtopPosition, 40); ac++;
341 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
342 	XtSetArg(al[ac], XmNleftPosition, 40); ac++;
343    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
344 	XtSetArg(al[ac], XmNrightPosition, 65); ac++;
345    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
346    XtSetArg(al[ac], XmNbottomPosition, 50); ac++;
347 	DealButton = XmCreatePushButtonGadget ( ControlTable, "DealButton", al, ac );
348 
349 
350    // player.progressiveButton
351 	ac = 0;
352 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
353 	XtSetArg(al[ac], XmNtopPosition, 52); ac++;
354 	XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
355 	XtSetArg(al[ac], XmNleftPosition, 15); ac++;
356 	XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
357 	XtSetArg(al[ac], XmNrightPosition, 85); ac++;
358 	XtSetArg(al[ac], XmNmarginTop, 5); ac++;
359 	XtSetArg(al[ac], XmNmarginBottom, 5); ac++;
360    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
361    XtSetArg(al[ac], XmNbottomPosition, 62); ac++;
362 	ProgressiveButton = XmCreateToggleButton ( ControlTable, "ProgressiveButton", al, ac );
363 
364    // playerform.antescroll
365 	ac = 0;
366 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
367 	XtSetArg(al[ac], XmNtopPosition, 64); ac++;
368    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
369 	XtSetArg(al[ac], XmNleftPosition, 15); ac++;
370    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
371    XtSetArg(al[ac], XmNrightPosition, 85); ac++;
372    XtSetArg(al[ac], XmNmaximum, rv.bankroll/3); ac++;
373    if(rv.bankroll/3 >= rv.minBet)
374    	XtSetArg(al[ac], XmNminimum, rv.minBet);
375 	else
376    	XtSetArg(al[ac], XmNminimum, 0);
377    ac++;
378    XtSetArg(al[ac], XmNorientation, XmHORIZONTAL); ac++;
379    XtSetArg(al[ac], XmNshowValue, True); ac++;
380    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
381    XtSetArg(al[ac], XmNbottomPosition, 77); ac++;
382 	AnteScroll = XmCreateScale ( ControlTable, "AnteScroll", al, ac );
383 
384 
385    // player.betbutton
386 	ac = 0;
387 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
388 	XtSetArg(al[ac], XmNtopPosition, 80); ac++;
389    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
390 	XtSetArg(al[ac], XmNleftPosition, 15); ac++;
391    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
392    XtSetArg(al[ac], XmNrightPosition, 45); ac++;
393    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
394    XtSetArg(al[ac], XmNbottomPosition, 90); ac++;
395 	BetButton = XmCreatePushButtonGadget( ControlTable, "BetButton", al, ac );
396 
397    // player.foldbutton
398    ac = 0;
399 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_POSITION); ac++;
400 	XtSetArg(al[ac], XmNtopPosition, 80); ac++;
401    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
402 	XtSetArg(al[ac], XmNleftPosition, 55); ac++;
403    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_POSITION); ac++;
404    XtSetArg(al[ac], XmNrightPosition, 85); ac++;
405    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
406    XtSetArg(al[ac], XmNbottomPosition, 90); ac++;
407    FoldButton = XmCreatePushButtonGadget ( ControlTable, "FoldButton", al, ac );
408 
409    // For PlayTable
410 
411    ac = 0;
412    XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
413    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
414    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_POSITION); ac++;
415    XtSetArg(al[ac], XmNleftPosition, 85); ac++;
416    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
417    XtSetArg(al[ac], XmNbottomPosition, 8);   ac++;
418    HelpRulesButton = XmCreatePushButtonGadget(PlayTable, "HelpRulesButton", al, ac);
419    XtManageChild(HelpRulesButton);
420 
421    ac = 0;
422    XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
423    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_WIDGET); ac++;
424    XtSetArg(al[ac], XmNrightWidget, HelpRulesButton); ac++;
425    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
426    XtSetArg(al[ac], XmNforeground, color.pixel);   ac++;
427    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
428    XtSetArg(al[ac], XmNbottomPosition, 8);   ac++;
429    DealerTableLabel = XmCreateLabel(PlayTable, "DealerLabel", al, ac);
430    SetLabel(DealerTableLabel, "");
431    XtManageChild(DealerTableLabel);
432 
433    // dealer draw area
434    ac = 0;
435 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
436 	XtSetArg(al[ac], XmNtopWidget, DealerTableLabel); ac++;
437 	XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_POSITION); ac++;
438 	XtSetArg(al[ac], XmNbottomPosition, 55); ac++;
439    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
440    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
441 	DealerHand = XmCreateDrawingArea ( PlayTable, "DealerHand", al, ac );
442 
443    // player draw area
444    ac = 0;
445 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
446 	XtSetArg(al[ac], XmNtopWidget, DealerHand); ac++;
447    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
448    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
449    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
450 	PlayerHand = XmCreateDrawingArea ( PlayTable, "PlayerHand", al, ac );
451 
452    // For help on Rules.
453 
454    HelpRulesDialog =  XmCreateFormDialog(toplevel,
455             "HelpRulesDialog", NULL, 0);
456 
457 
458    ac = 0;
459 	XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
460    XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
461    XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
462    XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
463    HelpRulesScrolledText = XmCreateScrolledText(HelpRulesDialog,
464             "HelpRulesScrolledText", al , ac);
465    XtManageChild(HelpRulesScrolledText);
466 
467 
468    for(int j=0; j< rv.players; j++)
469 	{
470 	  ac = 0;
471 	  children[ac++] = YouHaveLabel[j];
472 	  children[ac++] = YouHaveLabelValue[j];
473 	  children[ac++] = YouWinLoseValue[j];
474 	  XtManageChildren(children, ac);
475 	}
476 
477 	ac = 0;
478 	children[ac++] = DealButton;
479 	XtManageChildren(children, ac);
480 
481 	ac = 0;
482 	children[ac++] = AnteScroll;
483 	XtManageChildren(children, ac);
484 	ac = 0;
485 	children[ac++] = BetButton;
486 	children[ac++] = FoldButton;
487 	XtManageChildren(children, ac);
488 	ac = 0;
489 	children[ac++] = ProgressiveButton;
490 	XtManageChildren(children, ac);
491 	ac = 0;
492 	children[ac++] = PlayerHand;
493 	XtManageChildren(children, ac);
494 	ac = 0;
495 	children[ac++] = DealerHand;
496 	XtManageChildren(children, ac);
497    ac = 0;
498    children[ac++] = PlayTable;
499    children[ac++] = ControlTable;
500    XtManageChildren(children, ac);
501 	ac = 0;
502 	XtManageChild ( Layout );
503 }
504 
AddCallbacks()505 void Table::AddCallbacks()
506 {
507    XtAddCallback( DealerHand ,XmNexposeCallback,
508 					 (void(*)(_WidgetRec*, void*, void*))Table::DealerDrawingAreaCB,this);
509    XtAddCallback( DealerHand, XmNresizeCallback,
510 					 (void(*)(_WidgetRec*, void*, void*))Table::DealerDrawingAreaCB, this);
511    XtAddCallback( PlayerHand ,XmNresizeCallback,
512 					 (void(*)(_WidgetRec*, void*, void*))Table::PlayerDrawingAreaCB, this);
513    XtAddCallback( PlayerHand ,XmNexposeCallback,
514 					 (void(*)(_WidgetRec*, void*, void*))Table::PlayerDrawingAreaCB, this);
515    XtAddCallback( DealButton, XmNactivateCallback,
516 					 (void(*)(_WidgetRec*, void*, void*))Table::DealCB, this);
517    XtAddCallback( BetButton,  XmNactivateCallback,
518 					 (void(*)(_WidgetRec*, void*, void*))Table::BetCB,  this);
519    XtAddCallback( FoldButton, XmNactivateCallback,
520 					 (void(*)(_WidgetRec*, void*, void*))Table::FoldCB, this);
521    XtAddCallback( AnteScroll,XmNvalueChangedCallback,
522 					 (void(*)(_WidgetRec*, void*, void*))Table::AnteScrollCB,this);
523    XtAddCallback(HelpRulesButton, XmNactivateCallback,
524             	 (void(*)(_WidgetRec*, void*, void*))ManageHelpRulesDialogCB, this);
525 //   XtAddCallback(ExceptionDialog, XmNfocusCallback,
526 //                 (void(*)(_WidgetRec*, void*, void*))DoNothing, NULL);
527 
528 }
529 
DoExceptionDialog(const char * str)530 void Table::DoExceptionDialog(const char *str)
531 {
532    int i = 0;
533    XmString s;
534    Arg args[5];
535 
536    i = 0;
537    s = XmStringCreate((char*)str, XmSTRING_DEFAULT_CHARSET);
538    XtSetArg(args[i], XmNmessageString, s);   i++;
539    XtSetValues(ExceptionDialog, args, i);
540    XmStringFree(s);
541    XtUnmanageChild(XmMessageBoxGetChild(ExceptionDialog,
542                XmDIALOG_CANCEL_BUTTON));
543    XtUnmanageChild(XmMessageBoxGetChild(ExceptionDialog,
544                XmDIALOG_HELP_BUTTON));
545    XtManageChild(ExceptionDialog);
546    XtPopup(XtParent(ExceptionDialog), XtGrabNone);
547 }
548 
CurrentSeat(int whichone)549 void Table::CurrentSeat(int whichone)
550 {
551 
552    if(_players <= 1) return;
553 
554    Arg args[5];
555    int i = 0;
556    Pixel forg, back;
557    char  label[50];
558 
559 
560    XtSetArg(args[i], XmNforeground, &forg); i++;
561    XtSetArg(args[i], XmNbackground, &back); i++;
562    XtGetValues( YouHaveLabelValue[0], args, i);
563 
564    for(int j=0; j < _players; j++)
565    {
566       sprintf(label, "Player %d: ", j+1);
567       i=0;
568       if(j == whichone){
569         XtSetArg(args[i], XmNbackground, forg);i++;
570         XtSetArg(args[i], XmNforeground, back);i++;
571       }
572       else{
573         XtSetArg(args[i], XmNforeground, forg);i++;
574         XtSetArg(args[i], XmNbackground, back);i++;
575       }
576       XtSetValues(YouHaveLabel[j], args, i);
577       SetLabel(YouHaveLabel[j], label);
578    }
579    if( whichone >= _players)
580    throw(" The index for Table::CurrentSeat is too big. ");
581 }
582 
DealerDrawingAreaCB(Widget w,XtPointer client_data,XmDrawingAreaCallbackStruct * call_data)583 void Table::DealerDrawingAreaCB( Widget w, XtPointer client_data,
584 											XmDrawingAreaCallbackStruct *call_data)
585 {
586   try{
587 #ifdef DEBUG
588    cout << "Calling DealerDrawingAreaCB" << endl;
589 #endif // DEBUG
590    if (call_data->window) {
591      XEvent event;
592      Table * table = (Table*) client_data;
593      // drain Expose events
594      while (XCheckTypedWindowEvent(XtDisplay(table->DealerDrawingArea()),
595             XtWindow(table->DealerDrawingArea()), Expose, &event));
596      XClearWindow(XtDisplay(table->DealerDrawingArea()),
597 						XtWindow(table->DealerDrawingArea()));
598      (table->_dealerFace == 0) ?
599      ((DealerSeat*)table->GetDealerSeat())->ShowCardsBeforeBet(table->GetDealer()) :
600      table->GetDealerSeat()->ShowCards(table->GetDealer());
601    }
602  }
603  catch(const char *msg)
604  {
605     Table::DoExceptionDialog(msg);
606 #ifdef DEBUG
607     cout << msg << endl;
608 #endif
609  }
610 }
611 
PlayerDrawingAreaCB(Widget w,XtPointer client_data,XmDrawingAreaCallbackStruct * call_data)612 void Table::PlayerDrawingAreaCB( Widget w, XtPointer client_data,
613                                  XmDrawingAreaCallbackStruct *call_data)
614 {
615   try{
616 #ifdef DEBUG
617    cout << "Calling PlayerDrawingAreaCB" << endl;
618 #endif // DEBUG
619 
620    if (call_data->window) {
621      XEvent event;
622      Table * table = (Table*) client_data;
623      Dealer* dealer = table->GetDealer() ;
624       // drain Expose events
625      while (XCheckTypedWindowEvent(table->GetDisplay(),
626             XtWindow(table->PlayerDrawingArea()), Expose, &event));
627      XClearWindow(XtDisplay(table->PlayerDrawingArea()), XtWindow(table->PlayerDrawingArea()));
628      int i = dealer->CurrentSpot();
629      do {
630         dealer->CurrentPlayer()->GetSeat()->ShowCards(dealer->CurrentPlayer());
631 		  if(!dealer->NextPlayer())
632 			 dealer->FirstPlayer();
633      } while(dealer->CurrentSpot() != i);
634    }
635  }
636  catch(const char *msg)
637  {
638     Table::DoExceptionDialog(msg);
639 #ifdef DEBUG
640     cout << msg << endl;
641 #endif
642  }
643 
644 }
645 
AnteScrollCB(Widget w,XtPointer client_data,XmScaleCallbackStruct * call_data)646 void Table::AnteScrollCB( Widget w, XtPointer client_data,
647 								  XmScaleCallbackStruct *call_data)
648 {
649 #ifdef DEBUG
650    cout << "Calling AnteScrollCB" << endl;
651 #endif // DEBUG
652    int i;
653    char str[80];
654    Arg  args[5];
655 
656    Table *table = (Table*)client_data;
657    table->_dealerFace = 0;
658    int   betPlaced = call_data->value;
659    Dealer * dealer = table->GetDealer();
660 
661    if(table->_minBet > (dealer->CurrentPlayer()->BankRoll()-1)/3)
662    {
663       char str[80];
664       sprintf(str, "Not enough money left for a bet.\nAdded another $%d!", table->
665 _bankroll);
666       DoExceptionDialog(str);
667       dealer->CurrentPlayer()->BankRoll(table->_bankroll+
668                                         dealer->CurrentPlayer()->BankRoll());
669       sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
670       SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
671    }
672 
673    if(betPlaced > (dealer->CurrentPlayer()->BankRoll()-1)/3)
674    {
675       sprintf(str, "Not enough money either for initial ante or additional bet.");
676       DoExceptionDialog(str);
677    }
678 }
679 
ManageHelpRulesDialogCB(Widget w,XtPointer client_data,caddr_t call_data)680 void Table::ManageHelpRulesDialogCB( Widget w, XtPointer client_data, caddr_t call_data)
681 {
682    XtManageChild(HelpRulesDialog);
683 }
684 
DealCB(Widget w,XtPointer client_data,caddr_t call_data)685 void Table::DealCB( Widget w, XtPointer client_data, caddr_t call_data)
686 {
687 #ifdef DEBUG
688    cout << "Calling DealCB" << endl;
689 #endif // DEBUG
690    Arg  args[5];
691    char str[80];
692 	XmScaleCallbackStruct  data;
693    Table *table = (Table*)client_data;
694 
695    SetLabel(table->DealerTableLabel, "");
696    XtSetArg(args[0], XmNvalue, &(data.value));
697    XtGetValues(table->AnteScroll, args, 1);
698    try{
699    int i;
700 
701    table->_dealerFace = 0;
702    int   betPlaced = data.value;
703    Dealer * dealer = table->GetDealer();
704    i = (int) XmToggleButtonGetState (table->ProgressiveButton);
705 
706    if(table->_minBet > (dealer->CurrentPlayer()->BankRoll()-1)/3)
707    {
708       char str[80];
709       sprintf(str, "Not enough money left for a bet.\nAdded another $%d!", table->
710 _bankroll);
711       DoExceptionDialog(str);
712       dealer->CurrentPlayer()->BankRoll(table->_bankroll+
713                                         dealer->CurrentPlayer()->BankRoll());
714       sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
715       SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
716    }
717 
718    if(i != dealer->CurrentPlayer()->Progressive())
719 	   dealer->CurrentPlayer()->Progressive(i);
720    dealer->CurrentPlayer()->Ante(betPlaced);
721 	sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
722 	SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
723 	if(dealer->NextPlayer())
724 	{
725        i=0;
726 	    XtSetArg(args[i], XmNmaximum, (dealer->CurrentPlayer()->BankRoll()-1)/3);
727 	    betPlaced = (betPlaced > (dealer->CurrentPlayer()->BankRoll()-1)/3) ?
728 						  table->_minBet : betPlaced;
729 	    XtSetArg(args[i], XmNvalue, betPlaced); i++;
730    	 XtSetValues(table->AnteScroll, args, i);
731 		 sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
732 		 SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
733        table->CurrentSeat(dealer->CurrentSpot() -1);
734 	}else{
735 		 Disable( table->DealButton) ;
736        Disable( table->AnteScroll) ;
737    	 dealer->DealCards();
738        XClearWindow(XtDisplay(table->PlayerDrawingArea()), XtWindow(table->PlayerDrawingArea()));
739        XClearWindow(XtDisplay(table->DealerDrawingArea()), XtWindow(table->DealerDrawingArea()));
740 		 ((DealerSeat*)dealer->GetSeat())->ShowCardsBeforeBet(dealer);
741        dealer->FirstPlayer();
742 		 do{
743 			dealer->CurrentPlayer()->Score();
744 			dealer->CurrentPlayer()->GetSeat()->ShowCards(dealer->CurrentPlayer());
745 		 }while(dealer->NextPlayer());
746        Enable( table->BetButton ) ;
747        Enable( table->FoldButton) ;
748        dealer->FirstPlayer();
749        table->CurrentSeat(0);
750    }
751  }
752  catch(const char *msg)
753  {
754     Table::DoExceptionDialog(msg);
755 #ifdef DEBUG
756     cout << msg << endl;
757 #endif
758  }
759 }
760 
BetCB(Widget w,XtPointer client_data,caddr_t call_data)761 void Table::BetCB( Widget w, XtPointer client_data, caddr_t call_data)
762 {
763   try{
764 #ifdef DEBUG
765    cout << "Calling BetCB" << endl;
766 #endif // DEBUG
767    char str[80];
768    Arg  args[5];
769 
770    Table *table = (Table*)client_data;
771 
772    Dealer * dealer = table->GetDealer();
773    dealer->CurrentPlayer()->Bet(dealer->CurrentPlayer()->Ante()*2);
774 	sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
775 	SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
776 	if(!dealer->NextPlayer())
777 		PayTime(table);
778    else
779       table->CurrentSeat(dealer->CurrentSpot() -1);
780  }
781  catch(const char *msg)
782  {
783     Table::DoExceptionDialog(msg);
784 #ifdef DEBUG
785     cout << msg << endl;
786 #endif
787  }
788 }
789 
FoldCB(Widget w,XtPointer client_data,caddr_t call_data)790 void Table::FoldCB( Widget w, XtPointer client_data, caddr_t call_data)
791 {
792   try{
793 #ifdef DEBUG
794    cout << "Calling FoldCB" << endl;
795 #endif // DEBUG
796 
797    char str[80];
798    Arg  args[5];
799 
800    Table *table = (Table*)client_data;
801 
802    Dealer * dealer = table->GetDealer();
803    dealer->CurrentPlayer()->Fold();
804    sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
805    SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
806    if(!dealer->NextPlayer())
807       PayTime(table);
808    else
809       table->CurrentSeat(dealer->CurrentSpot() -1);
810  }
811  catch(const char *msg)
812  {
813     Table::DoExceptionDialog(msg);
814 #ifdef DEBUG
815     cout << msg << endl;
816 #endif
817  }
818 
819 }
820 
PayTime(Table * table)821 void Table::PayTime(Table* table)
822 {
823   try{
824 #ifdef DEBUG
825    cout << "Calling Table::PayTime" << endl;
826 #endif // DEBUG
827 
828    char str[80];
829    Arg  args[5];
830    Dealer *dealer = table->GetDealer();
831 
832    Disable( table->BetButton ) ;
833    Disable( table->FoldButton) ;
834    table->_dealerFace = 1;
835 	dealer->Score();
836    dealer->FirstPlayer();
837    XClearWindow(XtDisplay(table->DealerDrawingArea()), XtWindow(table->DealerDrawingArea()));
838 	dealer->GetSeat()->ShowCards(dealer);
839    if(dealer->Score() < 573496) // Does not have a King/Ace or higher
840    {
841 	   strcpy(str,"Dealer does not have a ACE/KING or higher.");
842       SetLabel(DealerTableLabel, str);
843    }
844 	dealer->CollectBet();
845 	do
846 	{
847 	  sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
848 	  SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
849      if(dealer->CurrentPlayer()->WinLost() >= 0)
850 		  sprintf(str, "Won: $%d", dealer->CurrentPlayer()->WinLost());
851      else
852 		  sprintf(str, "Lost: $%d", -dealer->CurrentPlayer()->WinLost());
853 	  SetLabel(table->YouWinLoseValue[dealer->CurrentSpot() -1], str);
854 	}while(dealer->NextPlayer());
855 
856    // for the first player
857    dealer->FirstPlayer();
858 	int  betPlaced;
859    XtSetArg(args[0], XmNvalue, &betPlaced);
860    XtGetValues(table->AnteScroll, args, 1);
861    sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
862    SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
863    int i=0;
864    if(table->_minBet > (dealer->CurrentPlayer()->BankRoll()-1)/3)
865    {
866       char str[80];
867       sprintf(str, "Not enough money left for a bet.\nAdded another $%d!", table->_bankroll);
868       DoExceptionDialog(str);
869       dealer->CurrentPlayer()->BankRoll(table->_bankroll+
870                                         dealer->CurrentPlayer()->BankRoll());
871    	sprintf(str, "$%d", dealer->CurrentPlayer()->BankRoll());
872    	SetLabel(table->YouHaveLabelValue[dealer->CurrentSpot() -1], str);
873    }
874    XtSetArg(args[i], XmNmaximum, (dealer->CurrentPlayer()->BankRoll()-1)/3);
875 
876 	betPlaced = (betPlaced > (dealer->CurrentPlayer()->BankRoll()-1)/3) ?
877                                      table->_minBet : betPlaced;
878    XtSetArg(args[i], XmNvalue, betPlaced); i++;
879    XtSetValues(table->AnteScroll, args, i);
880 	Enable( table->DealButton) ;
881    Enable( table->AnteScroll) ;
882    dealer->NewGame();
883    table->CurrentSeat(0);
884 	sprintf(str, "JACKPOT: %d", dealer->JackPot());
885    SetLabel(JackPotLabel, str);
886  }
887  catch(const char *msg)
888  {
889     Table::DoExceptionDialog(msg);
890 #ifdef DEBUG
891     cout << msg << endl;
892 #endif
893  }
894 }
895 
Enable(Widget client_data)896 void Table::Enable ( Widget client_data )
897 {
898       XtSetSensitive (client_data, TRUE);
899 }
900 
Disable(Widget client_data)901 void Table::Disable ( Widget client_data)
902 {
903     XtSetSensitive (client_data, FALSE);
904 }
905 
Start()906 void Table::Start()
907 {
908    XtAppMainLoop (app_context);
909 }
910