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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
/***************************************************************************
lskatdoc.cpp - description
-------------------
begin : Tue May 2 15:47:11 CEST 2000
copyright : (C) 2000 by Martin Heni
email : martin@heni-online.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// include files for TQt
#include <tqdir.h>
#include <tqfileinfo.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
// include files for KDE
#include <klocale.h>
// application specific includes
#include "lskatdoc.h"
#include "lskat.h"
#include "lskatview.h"
#include <kcarddialog.h>
TQPtrList<LSkatView> *LSkatDoc::pViewList = 0L;
LSkatDoc::LSkatDoc(TQWidget *tqparent, const char *name) : TQObject(tqparent, name)
{
int i;
if(!pViewList)
{
pViewList = new TQPtrList<LSkatView>();
}
pViewList->setAutoDelete(true);
isrunning=0;
wasgame=false;
initrandom();
// Allow translation of playernames
/*
names[0]=i18n("Alice");
names[1]=i18n("Bob");
*/
for (i=0;i<14;i++) cardvalues[i]=0;
cardvalues[(int)Ace]=11;
cardvalues[(int)Ten]=10;
cardvalues[(int)King]=4;
cardvalues[(int)Queen]=3;
cardvalues[(int)Jack]=2;
/*
computerlevel=2;
playedby[1]=KG_INPUTTYPE_INTERACTIVE;
playedby[0]=KG_INPUTTYPE_PROCESS;
// playedby[1]=KG_INPUTTYPE_INTERACTIVE;
*/
isintro=1;
server=false;
port=7432;
host="";
Name="";
remoteswitch=false;
ClearStats();
}
LSkatDoc::~LSkatDoc()
{
}
void LSkatDoc::addView(LSkatView *view)
{
pViewList->append(view);
}
void LSkatDoc::removeView(LSkatView *view)
{
pViewList->remove(view);
}
void LSkatDoc::setAbsFilePath(const TQString &filename)
{
absFilePath=filename;
}
const TQString &LSkatDoc::getAbsFilePath() const
{
return absFilePath;
}
void LSkatDoc::setTitle(const TQString &_t)
{
title=_t;
}
const TQString &LSkatDoc::getTitle() const
{
return title;
}
void LSkatDoc::InitMove(LSkatView *sender,int player,int x,int y)
{
LSkatView *w;
if(pViewList)
{
for(w=pViewList->first(); w!=0; w=pViewList->next())
{
if(w!=sender) w->InitMove(player,x,y);
}
}
}
void LSkatDoc::slotUpdateAllViews(LSkatView *sender)
{
LSkatView *w;
if(pViewList)
{
for(w=pViewList->first(); w!=0; w=pViewList->next())
{
if(w!=sender)
w->tqrepaint();
}
}
}
void LSkatDoc::UpdateViews(int mode)
{
LSkatView *w;
if (IsIntro()) return ;
if(pViewList)
{
for(w=pViewList->first(); w!=0; w=pViewList->next())
{
if (mode & UPDATE_STATUS) w->updatetqStatus();
}
}
}
bool LSkatDoc::saveModified()
{
return true;
}
void LSkatDoc::closeDocument()
{
deleteContents();
}
bool LSkatDoc::newDocument(KConfig * /*config*/,TQString path)
{
int res;
modified=false;
absFilePath=TQDir::homeDirPath();
title=i18n("Untitled");
if (global_debug>1) printf("path=%s\n",path.latin1());
res=LoadBitmap(path);
if (res==0) return false;
return true;
}
bool LSkatDoc::LoadGrafix(TQString path)
{
int res;
res=LoadCards(cardPath);
if (res==0) return false;
res=LoadDeck(deckPath);
if (res==0) return false;
return true;
}
bool LSkatDoc::SetCardDeckPath(TQString deck,TQString card)
{
bool update=false;
if (!deck.isNull() && deck!=deckPath)
{
update=true;
deckPath=deck;
LoadDeck(deckPath);
}
if (!card.isNull() && card!=cardPath)
{
update=true;
cardPath=card;
LoadCards(cardPath);
}
return update;
}
bool LSkatDoc::openDocument(const TQString &filename, const char * /*format*/ /*=0*/)
{
TQFileInfo fileInfo(filename);
title=fileInfo.fileName();
absFilePath=fileInfo.absFilePath();
/////////////////////////////////////////////////
// TODO: Add your document opening code here
/////////////////////////////////////////////////
modified=false;
return true;
}
bool LSkatDoc::saveDocument(const TQString & /*filename*/, const char * /*format*/ /*=0*/)
{
/////////////////////////////////////////////////
// TODO: Add your document saving code here
/////////////////////////////////////////////////
modified=false;
return true;
}
void LSkatDoc::deleteContents()
{
/////////////////////////////////////////////////
// TODO: Add implementation to delete the document contents
/////////////////////////////////////////////////
}
// Called after game ends..give points to players
void LSkatDoc::EvaluateGame()
{
if (score[0]+score[1]!=120)
{
printf("Warning: Score does not end up to 120\n");
}
stat_games[0]++;
stat_games[1]++;
if (score[0]==score[1]) // drawn
{
stat_points[0]++;
stat_points[1]++;
}
else if (score[0]>score[1])
{
stat_won[0]++;
stat_points[0]+=2;
if (score[0]>=90) stat_points[0]++; // Schneider
if (score[0]>=120) stat_points[0]++; // SChwarz
}
else
{
stat_won[1]++;
stat_points[1]+=2;
if (score[1]>=90) stat_points[1]++; // Schneider
if (score[1]>=120) stat_points[1]++; // SChwarz
}
}
void LSkatDoc::EndGame(bool aborted)
{
if (aborted)
{
stat_brk[0]++;
stat_brk[1]++;
}
else
{
startplayer=1-began_game;
}
isrunning=0;
wasgame=true;
}
void LSkatDoc::NewGame()
{
int i,r;
int num[NO_OF_CARDS];
isintro=0;
began_game=startplayer;
for (i=0;i<NO_OF_TILES;i++)
{
cardheight[i]=2;
}
trump=(CCOLOUR)(random(4));
if (global_debug>5) printf("Trump=%d\n",trump);
if (random(8)==0) trump=Grand; // grand
// Fast drawing of random cards
for (i=0;i<NO_OF_CARDS;i++)
{
num[i]=i;
}
for (i=0;i<NO_OF_CARDS;i++)
{
r=random(NO_OF_CARDS-i); // cards available 32-i (+1)
card[i]=num[r];
num[r]=num[NO_OF_CARDS-i-1]; // shift numbers
}
isrunning=1;
movestatus=-1;
currentplayer=startplayer;
laststartplayer=-1;
moveno=0;
computerscore=0;
lock=false;
for (i=0;i<2;i++)
{
score[i]=0;
curmove[i]=-1;
}
}
int LSkatDoc::GetMove(int no) {return curmove[no];}
CCOLOUR LSkatDoc::GetTrump()
{
return trump;
}
void LSkatDoc::SetTrump(CCOLOUR i)
{
trump=i;
}
// pos=0..7, height=2..1..(0 no card left), player=0..1
int LSkatDoc::GetCard(int player, int pos,int height)
{
int i;
if (height==0) return -1;
height=2-height;
i=NO_OF_TILES*player+8*height+pos;
return card[i];
}
// pos=0..7, player=0..1
int LSkatDoc::GetHeight(int player, int pos)
{
int i;
i=8*player+pos;
return cardheight[i];
}
// pos=0..7, player=0..1
void LSkatDoc::SetHeight(int player, int pos,int h)
{
int i;
i=8*player+pos;
cardheight[i]=h;
}
int LSkatDoc::random(int max)
{
double value;
int r;
r=rand();
value=(double)((double)r*(double)max)/(double)RAND_MAX;
return (int)value;
}
void LSkatDoc::initrandom()
{
srand( (unsigned)time( NULL ) ); // randomize
}
bool LSkatDoc::IsRunning() {return isrunning;}
bool LSkatDoc::LegalMove(int p1, int p2)
{
CCOLOUR col1,col2,col3;
CCARD card1,card2,card3;
card1=(CCARD)((p1)/4);
col1=(CCOLOUR)((p1)%4);
card2=(CCARD)((p2)/4);
col2=(CCOLOUR)((p2)%4);
// force trump colour
if (card1==Jack) col1=trump;
if (card2==Jack) col2=trump;
// same colour always ok
if (col1==col2) return true;
// Search for same colour
bool flag=true;
for (int i=0;i<8;i++)
{
int h,c;
h=GetHeight(1-startplayer,i);
if (h==0) continue;
c=GetCard(1-startplayer,i,h);
card3=(CCARD)((c)/4);
col3=(CCOLOUR)((c)%4);
if (card3==Jack) col3=trump;
if (col3==col1)
{
flag=false;
break;
}
}
if (flag) return true;
return false;
}
int LSkatDoc::PrepareMove(int player,int pos)
{
int h,card;
h=GetHeight(player,pos);
if (h==0) return -1; // not possible
if (player!=currentplayer) return -2; // wrong player
card=GetCard(player,pos,h);
// New round
if (currentplayer==startplayer)
{
curmove[0]=-1;
curmove[1]=-1;
}
else
{
if (!LegalMove(curmove[startplayer],card))
{
printf("Illegal move\n");
return -3;
}
}
lock=true;
movestatus=card;
SetHeight(player,pos,h-1);
return 1;
}
int LSkatDoc::MakeMove()
{
lock=false;
curmove[currentplayer]=movestatus;
if (currentplayer==startplayer)
{
moveno++;
currentplayer=1-startplayer;
}
else
{
laststartplayer=startplayer;
if (WonMove(curmove[startplayer],curmove[1-startplayer]))
{
// switch startplayer
startplayer=1-startplayer;
}
else
{
}
currentplayer=startplayer;
score[startplayer]+=CardValue(curmove[0]);
score[startplayer]+=CardValue(curmove[1]);
if (moveno==NO_OF_TILES)
{
EndGame(false);
return 2;
}
}
movestatus=-1;
return 1;
}
int LSkatDoc::CardValue(int card)
{
int card1;
card1=card/4;
return cardvalues[card1];
}
int LSkatDoc::WonMove(int c1,int c2)
{
CCOLOUR col1,col2;
CCARD card1,card2;
card1=(CCARD)((c1)/4);
col1=(CCOLOUR)((c1)%4);
card2=(CCARD)((c2)/4);
col2=(CCOLOUR)((c2)%4);
// Two jacks
if (card1==Jack && card2==Jack)
{
if (col1<col2) return 0;
else return 1;
}
// One Jack wins always
if (card1==Jack) return 0;
if (card2==Jack) return 1;
// higher one wins if same colour
if (col1==col2)
{
if (card1==Ten)
{
if (card2==Ace) return 1;
else return 0;
}
if (card2==Ten)
{
if (card1==Ace) return 0;
return 1;
}
if ((int)card1<(int)card2) return 0;
return 1;
}
// trump wins
if (col1==trump) return 0;
if (col2==trump) return 1;
// first one wins
return 0;
}
int LSkatDoc::GetMovetqStatus() {return movestatus;}
void LSkatDoc::SetMovetqStatus(int i) { movestatus=i; }
int LSkatDoc::GetCurrentPlayer() {return currentplayer;}
void LSkatDoc::SetCurrentPlayer(int i) {currentplayer=i;}
int LSkatDoc::GetStartPlayer() {return startplayer;}
void LSkatDoc::SetStartPlayer(int i) {startplayer=i;}
void LSkatDoc::SetName(int no, TQString n) { names[no]=n; }
TQString LSkatDoc::GetName(int no) {return names[no];}
int LSkatDoc::GetScore(int no) {return score[no];}
int LSkatDoc::GetMoveNo() {return moveno;}
/*
void LSkatDoc::SetDeckNo(int no) {deckno=no;}
int LSkatDoc::GetDeckNo() {return deckno;}
*/
int LSkatDoc::GetLastStartPlayer() {return laststartplayer;}
KG_INPUTTYPE LSkatDoc::GetPlayedBy(int no) {return playedby[no];}
void LSkatDoc::SetPlayedBy(int no,KG_INPUTTYPE type) {playedby[no]=type;}
int LSkatDoc::GetComputerLevel() {return computerlevel;}
void LSkatDoc::SetComputerLevel(int lev) {computerlevel=lev;}
void LSkatDoc::SetComputerScore(int sc) {computerscore=sc;}
int LSkatDoc::GetComputerScore() {return computerscore;}
void LSkatDoc::SetLock() {lock=true;}
void LSkatDoc::ReleaseLock() {lock=false;}
bool LSkatDoc::IsLocked() {return lock;}
bool LSkatDoc::IsIntro() {return isintro;}
bool LSkatDoc::WasRunning() {return wasgame;}
void LSkatDoc::SetIntro(bool b) {isintro=b;}
int LSkatDoc::LoadBitmap(TQString path)
{
int i;
TQString buf;
if (global_debug>5) printf("Loading bitmaps\n");
for (i=0;i<NO_OF_TRUMPS;i++)
{
buf.sprintf("%st%d.png",path.latin1(),i+1);
if(!mPixTrump[i].load(buf))
{
printf("Fatal error: bitmap %s not found \n",buf.latin1());
}
}
for (i=0;i<3;i++)
{
buf.sprintf("%stype%d.png",path.latin1(),i+1);
if (!mPixType[i].load(buf))
{
printf("Fatal error: bitmap %s not found \n",buf.latin1());
}
}
buf.sprintf("%sbackground.png",path.latin1());
mPixBackground.load(buf);
if (!mPixBackground.load(buf))
{
printf("Fatal error: bitmap %s not found \n",buf.latin1());
}
for (i=0;i<NO_OF_ANIM;i++)
{
buf.sprintf("%s4000%02d.png",path.latin1(),i);
if(!mPixAnim[i].load(buf))
{
printf("Fatal error: bitmap %s not found \n",buf.latin1());
}
}
return 1;
}
int LSkatDoc::LoadCards(TQString path)
{
TQString buf;
for (int i=0;i<NO_OF_CARDS;i++)
{
buf.sprintf("%s%d.png",path.latin1(),i+1);
if(!mPixCard[i].load(buf))
{
printf("Fatal error: bitmap %s not found \n",buf.latin1());
return 0;
}
}
cardsize=mPixCard[0].size();
return 1;
}
int LSkatDoc::LoadDeck(TQString path)
{
if (!mPixDeck.load(path))return 0;
return 1;
}
void LSkatDoc::SetInputHandler(KEInput *i)
{
inputHandler=i;
}
KEInput *LSkatDoc::QueryInputHandler()
{
return inputHandler;
}
int LSkatDoc::SwitchStartPlayer()
{
startplayer=1-startplayer;
return startplayer;
}
bool LSkatDoc::IsServer()
{
return server;
}
void LSkatDoc::SetServer(bool b)
{
server=b;
}
void LSkatDoc::SetHost(TQString h)
{
host=h;
}
void LSkatDoc::SetName(const TQString& n)
{
Name=n;
}
void LSkatDoc::SetPort(short p)
{
port=p;
}
TQString LSkatDoc::QueryHost()
{
return host;
}
short LSkatDoc::QueryPort()
{
return port;
}
TQString LSkatDoc::QueryName() const
{
return Name;
}
int *LSkatDoc::GetCardP()
{
return card;
}
int *LSkatDoc::GetCardHeightP()
{
return cardheight;
}
void LSkatDoc::SetCard(int no,int c)
{
card[no]=c;
}
void LSkatDoc::SetRemoteSwitch(bool b)
{
remoteswitch=b;
}
bool LSkatDoc::IsRemoteSwitch()
{
return remoteswitch;
}
int LSkatDoc::GetStatWon(int no)
{
return stat_won[no];
}
int LSkatDoc::GetStatGames(int no)
{
return stat_games[no];
}
int LSkatDoc::GetStatAborted(int no)
{
return stat_brk[no];
}
int LSkatDoc::GetStatPoints(int no)
{
return stat_points[no];
}
TQString LSkatDoc::GetProcess()
{
return procfile;
}
void LSkatDoc::ClearStats()
{
for (int j=0;j<2;j++)
{
stat_won[j]=0;
stat_points[j]=0;
stat_games[j]=0;
stat_brk[j]=0;
}
}
void LSkatDoc::ReadConfig(KConfig *config)
{
KConfig emailCfg( "emaildefaults", true );
emailCfg.setGroup( "UserInfo" );
TQString name = emailCfg.readEntry( "FullName" );
if ( name.isEmpty() )
{
struct passwd *pw = getpwuid( getuid() );
if ( pw )
name = TQString::fromLocal8Bit( pw->pw_gecos );
}
config->setGroup("Parameter");
host=config->readEntry("host");
port=(unsigned short)config->readNumEntry("port",7432);
procfile=config->readEntry("process",TQCString("lskatproc"));
Name=config->readEntry("gamename");
names[0]=config->readEntry("Name1",i18n("Alice"));
// names[1]=config->readEntry("Name2",i18n("Bob"));
names[1]=config->readEntry("Name2", name.isEmpty() ? i18n("Bob") : name);
// This is for debug and testing as you can run it from the CVS without
// installing the carddecks !
// For the release version you can remove the aruments to the following two
// functions !!!!
cardPath=config->readPathEntry("cardpath", KCardDialog::getDefaultCardDir());
deckPath=config->readPathEntry("deckpath", KCardDialog::getDefaultDeck());
// Debug only
if (global_debug>3)
printf("cardPath=%s\ndeckPath=%s\n",cardPath.latin1(),deckPath.latin1());
startplayer=config->readNumEntry("Startplayer",0);
if (startplayer>1 || startplayer<0) startplayer=0;
began_game=startplayer;
computerlevel=config->readNumEntry("Level",2);
playedby[0]=(KG_INPUTTYPE)config->readNumEntry("Player1",
(int)KG_INPUTTYPE_PROCESS);
playedby[1]=(KG_INPUTTYPE)config->readNumEntry("Player2",
(int)KG_INPUTTYPE_INTERACTIVE);
stat_won[0]=config->readNumEntry("Stat1W",0);
stat_won[1]=config->readNumEntry("Stat2W",0);
stat_brk[0]=config->readNumEntry("Stat1B",0);
stat_brk[1]=config->readNumEntry("Stat2B",0);
stat_points[0]=config->readNumEntry("Stat1P",0);
stat_points[1]=config->readNumEntry("Stat2P",0);
stat_games[0]=config->readNumEntry("Stat1G",0);
stat_games[1]=config->readNumEntry("Stat2G",0);
}
/** write config file */
void LSkatDoc::WriteConfig(KConfig *config)
{
config->setGroup("Parameter");
config->writeEntry("host",host);
config->writeEntry("port",port);
config->writeEntry("process",procfile);
config->writePathEntry("tmppath",picpath);
config->writeEntry("delpath",delpath);
config->writeEntry("Name1",names[0]);
config->writeEntry("Name2",names[1]);
config->writeEntry("gamename",Name);
config->writeEntry("Startplayer",startplayer);
config->writeEntry("Level",computerlevel);
config->writeEntry("Player1",int(playedby[0]));
config->writeEntry("Player2",int(playedby[1]));
config->writeEntry("Stat1W",stat_won[0]);
config->writeEntry("Stat2W",stat_won[1]);
config->writeEntry("Stat1B",stat_brk[0]);
config->writeEntry("Stat2B",stat_brk[1]);
config->writeEntry("Stat1P",stat_points[0]);
config->writeEntry("Stat2P",stat_points[1]);
config->writeEntry("Stat1G",stat_games[0]);
config->writeEntry("Stat2G",stat_games[1]);
config->writePathEntry("cardpath",cardPath);
config->writePathEntry("deckpath",deckPath);
config->sync();
}
#include "lskatdoc.moc"
|