1 
2 /*
3     xskat - a card game for 1 to 3 players.
4     Copyright (C) 2000  Gunter Gerhardt
5 
6     This program is free software; you can redistribute it freely.
7     Use it at your own risk; there is NO WARRANTY.
8 
9     Redistribution of modified versions is permitted
10     provided that the following conditions are met:
11     1. All copyright & permission notices are preserved.
12     2.a) Only changes required for packaging or porting are made.
13       or
14     2.b) It is clearly stated who last changed the program.
15          The program is renamed or
16          the version number is of the form x.y.z,
17          where x.y is the version of the original program
18          and z is an arbitrary suffix.
19 */
20 
21 #define RAMSCH_C
22 
23 #include "defs.h"
24 #include "skat.h"
25 #include "ramsch.h"
26 
start_ramsch()27 VOID start_ramsch()
28 {
29   vmh=0;
30   spieler=geber;
31   save_skat(1);
32   home_skat();
33   remmark(0);
34   phase=SPIELEN;
35 }
36 
init_ramsch()37 VOID init_ramsch()
38 {
39   int sn;
40 
41   sramschstufe=0;
42   trumpf=5;
43   spieler=geber;
44   reizp=-1;
45   stich=1;
46   handsp=0;
47   vmh=0;
48   ouveang=0;
49   sort2[0]=sort2[1]=sort2[2]=0;
50   prot2.sramsch=playsramsch;
51   save_skat(0);
52   info_reiz();
53   info_spiel();
54   for (sn=0;sn<numsp;sn++) {
55     initscr(sn,1);
56   }
57   for (sn=0;sn<3;sn++) {
58     rstsum[sn]=rstich[sn]=ggdurchm[sn]=0;
59   }
60   if (playsramsch || (ramschspiele && klopfen)) {
61     phase=DRUECKEN;
62     di_schieben();
63   }
64   else {
65     start_ramsch();
66   }
67 }
68 
zweibuben()69 int zweibuben()
70 {
71   int c0,c1,gespb;
72 
73   if (stich!=9 || possc!=2 ||
74       ((c0=cards[possi[0]])&7)!=BUBE ||
75       ((c1=cards[possi[1]])&7)!=BUBE) return 0;
76   gespb=(gespcd[0<<3|BUBE]==2)+(gespcd[1<<3|BUBE]==2)+
77     (gespcd[2<<3|BUBE]==2)+(gespcd[3<<3|BUBE]==2);
78   if (!vmh || (vmh==1 && (stcd[0]&7)!=BUBE)) {
79     if ((c0>>3)==3 || (c1>>3)==3) {
80       if ((c0>>3)==0 || (c1>>3)==0) {
81 	if (gespb) {
82 	  playcd=(c1>>3)==0;
83 	}
84 	else {
85 	  playcd=(c1>>3)==3;
86 	}
87       }
88       else {
89 	playcd=(c0>>3)==3;
90       }
91       return 1;
92     }
93     if ((c0>>3)==2 || (c1>>3)==2) {
94       if ((c0>>3)==0 || (c1>>3)==0) {
95 	if (gespb) {
96 	  playcd=(c1>>3)==0;
97 	}
98 	else {
99 	  playcd=(c1>>3)==2;
100 	}
101       }
102       return 1;
103     }
104     return 1;
105   }
106   if (vmh==1 || (vmh==2 && ((stcd[0]&7)!=BUBE)+((stcd[1]&7)!=BUBE)==1)) {
107     if ((c0>>3)==3 || (c1>>3)==3) {
108       if ((c0>>3)==0 || (c1>>3)==0) {
109 	if (gespb>1) {
110 	  playcd=(c1>>3)==0;
111 	}
112 	else {
113 	  playcd=(c1>>3)==3;
114 	}
115 	return 1;
116       }
117       if ((c0>>3)==1 || (c1>>3)==1) {
118 	if (gespb>1) {
119 	  playcd=(c1>>3)==1;
120 	}
121 	else {
122 	  if (stcd[0]==(2<<3|BUBE) || (vmh==2 && stcd[1]==(2<<3|BUBE))) {
123 	    playcd=(c1>>3)==1;
124 	  }
125 	  else {
126 	    playcd=(c1>>3)==3;
127 	  }
128 	}
129 	return 1;
130       }
131       return 1;
132     }
133     if ((c0>>3)==2 || (c1>>3)==2) {
134       if ((c0>>3)==0 || (c1>>3)==0) {
135 	if (gespb>1) {
136 	  playcd=(c1>>3)==0;
137 	}
138 	else {
139 	  if (stcd[0]==(1<<3|BUBE) || (vmh==2 && stcd[1]==(1<<3|BUBE))) {
140 	    playcd=(c1>>3)==0;
141 	  }
142 	  else {
143 	    playcd=(c1>>3)==2;
144 	  }
145 	}
146       }
147       return 1;
148     }
149     return 1;
150   }
151   if ((stcd[0]&7)!=BUBE && (stcd[1]&7)!=BUBE) {
152     playcd=(c1>>3)==3 || (c1>>3)==2;
153   }
154   else {
155     playcd=(c1>>3)==1 || (c1>>3)==0;
156   }
157   return 1;
158 }
159 
bubeanspielen()160 int bubeanspielen()
161 {
162   int bb,nbb,j;
163 
164   bb=-1;
165   nbb=0;
166   for (j=0;j<possc;j++) {
167     if ((cards[possi[j]]&7)==BUBE) {
168       nbb++;
169       if (cards[possi[j]]>>3) {
170 	bb=j;
171       }
172     }
173   }
174   if (nbb>1 || bb<0) return 0;
175   for (j=0;j<4;j++) {
176     if (gespcd[j<<3|BUBE]==2) {
177       return 0;
178     }
179   }
180   playcd=bb;
181   return 1;
182 }
183 
sicher(fb,pc,le)184 int sicher(fb,pc,le)
185 int fb,*pc,*le;
186 {
187   int i,j,mkz,akz;
188   int mk[7],ak[7],p[7];
189 
190   *le=0;
191   if (hatnfb[left(ausspl+vmh)][fb]==1 && hatnfb[right(ausspl+vmh)][fb]==1) {
192     return 1;
193   }
194   mkz=akz=0;
195   for (i=7;i>=0;i--) {
196     if (i==BUBE) continue;
197     if (gespcd[fb<<3|i]!=2) {
198       for (j=0;j<possc;j++) {
199 	if (cards[possi[j]]==(fb<<3|i)) break;
200       }
201       if (j<possc) {
202 	mk[mkz]=i;
203 	p[mkz]=j;
204 	mkz++;
205       }
206       else ak[akz++]=i;
207     }
208   }
209   for (i=0;i<mkz && i<akz;i++) {
210     if (mk[i]<ak[i]) break;
211   }
212   if (i<mkz && i<akz) {
213     *pc=p[mkz>1?1:0];
214     if ((cards[possi[*pc]]&7)<=ZEHN) {
215       *pc=p[0];
216     }
217     if (mkz==1 && (cards[possi[*pc]]&7)>ZEHN) {
218       *le=1;
219     }
220     return 0;
221   }
222   return 1;
223 }
224 
moeglklein()225 VOID moeglklein()
226 {
227   int pc,fb,fp,mgb,mgp;
228 
229   for (pc=1;pc<possc;pc++) {
230     fb=cards[possi[pc]]>>3;
231     fp=cards[possi[playcd]]>>3;
232     mgb=(vmh ||
233 	 hatnfb[left(ausspl)][fb]!=1 ||
234 	 hatnfb[right(ausspl)][fb]!=1);
235     mgp=(vmh ||
236 	 hatnfb[left(ausspl)][fp]!=1 ||
237 	 hatnfb[right(ausspl)][fp]!=1);
238     if ((cards[possi[playcd]]&7)==BUBE) {
239       if ((cards[possi[pc]]&7)==BUBE) {
240 	if (cards[possi[pc]]>>3>cards[possi[playcd]]>>3) {
241 	  playcd=pc;
242 	}
243       }
244       else if (mgb) {
245 	playcd=pc;
246       }
247     }
248     else {
249       if ((cards[possi[pc]]&7)!=BUBE) {
250 	if ((((cards[possi[pc]]&7)>(cards[possi[playcd]]&7)) &&
251 	     ((cards[possi[pc]]&7)!=ACHT ||
252 	      (cards[possi[playcd]]&7)!=DAME ||
253 	      !vmh ||
254 	      gespcd[(cards[possi[pc]]&~7)|NEUN]==2) &&
255 	     mgb) ||
256 	    !mgp) {
257 	  playcd=pc;
258 	}
259       }
260       else if (!mgp) {
261 	playcd=pc;
262       }
263     }
264   }
265 }
266 
nimm_bube()267 VOID nimm_bube()
268 {
269   int pc;
270 
271   if (stich>=7 ||
272       cardw[stcd[0]&7]+cardw[stcd[1]&7]>4 ||
273       (gespcd[BUBE]!=2 && gespcd[1<<3|BUBE]!=2 &&
274        gespcd[2<<3|BUBE]!=2 && gespcd[3<<3|BUBE]!=2)) return;
275   for (pc=0;pc<possc;pc++) {
276     if (cards[possi[pc]]==(3<<3|BUBE) ||
277 	cards[possi[pc]]==(2<<3|BUBE)) {
278       playcd=pc;
279       return;
280     }
281   }
282 }
283 
moegldrunter(sc)284 VOID moegldrunter(sc)
285 int sc;
286 {
287   int pc,f,fb,pcl,le,fr,w1,w2,wc;
288 
289   fb=cards[possi[0]]>>3;
290   for (pc=1;pc<possc;pc++) {
291     if (cards[possi[pc]]>>3!=fb) break;
292   }
293   fr=pc!=possc;
294   f=0;
295   wc=30;
296   for (pc=0;pc<possc;pc++) {
297     if (higher(sc,cards[possi[pc]])) {
298       if (f) {
299 	if (fr) {
300 	  if ((cards[possi[pc]]&7)==BUBE) {
301 	    w1=30+(cards[possi[pc]]>>3);
302 	  }
303 	  else {
304 	    w1=7-(cards[possi[pc]]&7);
305 	    if (!sicher(cards[possi[pc]]>>3,&pcl,&le)) {
306 	      w1+=10;
307 	    }
308 	  }
309 	  if ((cards[possi[playcd]]&7)==BUBE) {
310 	    w2=30+(cards[possi[playcd]]>>3);
311 	  }
312 	  else {
313 	    w2=7-(cards[possi[playcd]]&7);
314 	    if (!sicher(cards[possi[playcd]]>>3,&pcl,&le)) {
315 	      w2+=10;
316 	    }
317 	  }
318 	}
319 	else {
320 	  w1=7-(cards[possi[pc]]&7);
321 	  w2=7-(cards[possi[playcd]]&7);
322 	}
323 	if (w1>w2) {
324 	  playcd=pc;
325 	  wc=w1;
326 	}
327       }
328       else {
329 	playcd=pc;
330 	f=1;
331       }
332     }
333   }
334   if (!f) {
335     moeglklein();
336   }
337   else if (fr && vmh==2 && wc<10) nimm_bube();
338 }
339 
ggdurchmarsch()340 int ggdurchmarsch()
341 {
342   int i,j,h,bb,sn;
343 
344   if (rstich[0]+rstich[1]+rstich[2]>1 ||
345       (stcd[0]&7)==SIEBEN ||
346       (vmh==2 && stich!=1 && !higher(stcd[0],stcd[1]))) return 0;
347   sn=(ausspl+vmh)%3;
348   for (i=3;i>=0;i--) {
349     if (gespcd[bb=i<<3|BUBE]!=2) {
350       for (i=0;i<10;i++) {
351 	if (cards[sn*10+i]==bb) return 0;
352       }
353       break;
354     }
355   }
356   if (((stcd[0]&7)==BUBE &&
357        ((hatnfb[left(ausspl)][4]==1 &&
358 	 hatnfb[right(ausspl)][4]==1) ||
359 	(gespcd[0<<3|BUBE]==2 && gespcd[1<<3|BUBE]==2 &&
360 	 gespcd[2<<3|BUBE]==2 && gespcd[3<<3|BUBE]==2))) ||
361       (stcd[0]&7)<KOENIG) {
362     ggdurchm[sn]=1;
363   }
364   if (!ggdurchm[sn]) return 0;
365   j=h=0;
366   if (vmh==2 && !higher(stcd[0],stcd[1])) {
367     h=1;
368   }
369   for (i=0;i<possc;i++) {
370     if (!higher(stcd[h],cards[possi[i]])) {
371       if (!j || cardw[cards[possi[i]]&7]<cardw[cards[possi[j-1]]&7]) {
372 	j=i+1;
373       }
374     }
375   }
376   if (!j) {
377     for (i=0;i<possc;i++) {
378       if (!j || ggdmw[cards[possi[i]]&7]<ggdmw[cards[possi[j-1]]&7]) {
379 	j=i+1;
380       }
381     }
382   }
383   playcd=j-1;
384   return 1;
385 }
386 
m_bvr()387 VOID m_bvr()
388 {
389   int fb,pc,le,lef,f;
390 
391   if (zweibuben()) return;
392   if (bubeanspielen()) return;
393   f=lef=0;
394   for (fb=0;fb<4;fb++) {
395     if (!sicher(fb,&pc,&le)) {
396       if (f) {
397 	if (le>lef ||
398 	    (rswert[cards[possi[pc]]&7]>rswert[cards[possi[playcd]]&7] &&
399 	     le>=lef)) {
400 	  playcd=pc;
401 	  lef=le;
402 	}
403       }
404       else {
405 	playcd=pc;
406 	lef=le;
407 	f=1;
408       }
409     }
410   }
411   if (!f || (cards[possi[playcd]]&7)<=ZEHN) {
412     playcd=0;
413     moeglklein();
414   }
415 }
416 
m_bmr()417 VOID m_bmr()
418 {
419   if (ggdurchmarsch()) return;
420   if (zweibuben()) return;
421   moegldrunter(stcd[0]);
422 }
423 
m_bhr()424 VOID m_bhr()
425 {
426   if (ggdurchmarsch()) return;
427   if (zweibuben()) return;
428   moegldrunter(higher(stcd[0],stcd[1])?stcd[0]:stcd[1]);
429 }
430 
m_bramsch()431 VOID m_bramsch()
432 {
433   playcd=0;
434   if (!vmh) m_bvr();
435   else if (vmh==1) m_bmr();
436   else m_bhr();
437 }
438 
unsich_fb(sn,s)439 int unsich_fb(sn,s)
440 int sn,*s;
441 {
442   int fb,pc,le,n;
443 
444   for (possc=0;possc<10;possc++) {
445     possi[possc]=sn*10+possc;
446   }
447   n=0;
448   for (fb=0;fb<4;fb++) {
449     s[fb]=1;
450     if (!sicher(fb,&pc,&le)) {
451       s[fb]=0;
452       n++;
453     }
454   }
455   return n;
456 }
457 
comp_sramsch(sn)458 int comp_sramsch(sn)
459 int sn;
460 {
461   int fb,n,i,j,c,ea,bb,dum;
462   int p[4],t[4],s[4],o[4],b[4];
463 
464   dum=0;
465   n=unsich_fb(sn,s);
466   bb=b[0]=b[1]=b[2]=b[3]=0;
467   for (i=0;i<10;i++) {
468     c=cards[sn*10+i];
469     if ((c&7)==BUBE) bb++,b[c>>3]=1;
470   }
471   if (ramschspiele && klopfen && !playsramsch) {
472     if ((n<=3 && (!n || bb<=1)) ||
473 	(n<=2 && (!n || bb<=2))) {
474       return di_verdoppelt(0,1);
475     }
476   }
477   if (playsramsch) {
478     if (sn==left(ausspl)) {
479       if ((n<=3 && !bb) ||
480 	  (n==1 && bb<=1 && !b[3]) ||
481 	  !n) {
482 	return di_verdoppelt(0,0);
483       }
484     }
485     else if ((n==3 && !bb) ||
486 	     (n==2 && bb<=1 && !b[3]) ||
487 	     (n==1 && bb<=2) ||
488 	     !n) {
489       return di_verdoppelt(0,0);
490     }
491   }
492   if (!playsramsch) return 0;
493   for (fb=0;fb<4;fb++) {
494     for (c=0;c<8;c++) inhand[fb][c]=0;
495     p[fb]=t[fb]=0;
496     o[fb]=fb;
497   }
498   if (((vmh && prot2.verdopp[right(ausspl+vmh)]!=1) ||
499        (vmh==2 && prot2.verdopp[right(ausspl+vmh)]==1 &&
500 	prot2.verdopp[left(ausspl+vmh)]!=1)) &&
501       (((cards[30]&7)>ZEHN && (cards[31]&7)>ZEHN) ||
502        (cards[30]&7)==SIEBEN || (cards[31]&7)==SIEBEN) &&
503       ((cards[30]&7)>=NEUN || (cards[31]&7)>=NEUN)) {
504     ggdurchm[sn]=1;
505   }
506   for (i=0;i<2;i++) {
507     for (j=0;j<10;j++) {
508       if (cardw[cards[10*sn+j]&7]>cardw[cards[30+i]&7]) {
509 	swap(&cards[30+i],&cards[10*sn+j]);
510       }
511     }
512     if ((cards[30+i]&7)==BUBE) {
513       for (j=0;j<10;j++) {
514 	if ((cards[10*sn+j]&7)!=BUBE) {
515 	  swap(&cards[30+i],&cards[10*sn+j]);
516 	  break;
517 	}
518       }
519     }
520   }
521   for (i=0;i<10;i++) spcards[i]=cards[sn*10+i];
522   spcards[10]=cards[30];
523   spcards[11]=cards[31];
524   bb=b[0]=b[1]=b[2]=b[3]=0;
525   for (i=0;i<12;i++) {
526     c=spcards[i];
527     if ((c&7)!=BUBE) {
528       p[c>>3]+=cardw[c&7];
529       t[c>>3]++;
530       inhand[c>>3][c&7]=1;
531     }
532     else bb++,b[c>>3]=1;
533   }
534   for (fb=0;fb<4;fb++) {
535     for (i=fb+1;i<4;i++) {
536       if (p[o[fb]]<p[o[i]]) {
537 	j=o[i];
538 	o[i]=o[fb];
539 	o[fb]=j;
540       }
541     }
542   }
543   gedr=0;
544   ea=0;
545   for (i=0;i<4;i++) {
546     if (t[i]==1 && inhand[i][AS]) ea++;
547   }
548   if (ea<2) {
549     for (i=0;i<4;i++) {
550       if (t[i]==2 && inhand[i][AS] && inhand[i][ZEHN]) {
551 	drueck(i,2,&dum);
552 	break;
553       }
554     }
555   }
556   for (n=1;n<8 && gedr<2;n++) {
557     for (j=0;j<4 && gedr<2;j++) {
558       i=o[j];
559       if (t[i]==n && !s[i]) {
560 	if (n==1) {
561 	  if (!inhand[i][ACHT]) {
562 	    drueck(i,1,&dum);
563 	  }
564 	}
565 	else if (n==2) {
566 	  if (inhand[i][SIEBEN] || inhand[i][ACHT]) drueck(i,1,&dum);
567 	  else drueck(i,2,&dum);
568 	}
569 	else if (n==3) {
570 	  switch (inhand[i][SIEBEN]+inhand[i][ACHT]+inhand[i][NEUN]) {
571 	  case 3:break;
572 	  case 2:drueck(i,1,&dum);break;
573 	  default:drueck(i,2,&dum);break;
574 	  }
575 	}
576 	else {
577 	  drueck(i,2,&dum);
578 	}
579       }
580     }
581   }
582   if (ramschspiele && klopfen && !ggdurchm[sn]) {
583     n=unsich_fb(sn,s);
584     if ((n<=3 && !bb) ||
585 	(n<=2 && (bb<=1 || (bb==2 && !b[3]))) ||
586 	(n<=1 && (bb<=2 || (bb==3 && b[0]))) ||
587 	!n) {
588       return di_verdoppelt(0,1);
589     }
590   }
591   return 0;
592 }
593 
ramsch_stich()594 VOID ramsch_stich()
595 {
596   rstsum[ausspl]+=cardw[stcd[0]&7]+cardw[stcd[1]&7]+cardw[stcd[2]&7];
597   rstich[ausspl]=1;
598   if (stich==10) {
599     rskatsum=cardw[prot2.skat[1][0]&7]+cardw[prot2.skat[1][1]&7];
600     if (!rskatloser) {
601       rstsum[ausspl]+=rskatsum;
602     }
603   }
604   if ((stcd[0]&7)==BUBE && (stcd[1]&7)!=BUBE && (stcd[2]&7)!=BUBE) {
605     ggdurchm[0]=ggdurchm[1]=ggdurchm[2]=1;
606   }
607 }
608 
ramsch_result()609 VOID ramsch_result()
610 {
611   int maxn,i;
612 
613   stsum=rstsum[0];
614   spieler=0;
615   maxn=1;
616   if (rstsum[1]>stsum) {
617     stsum=rstsum[1];
618     spieler=1;
619   }
620   else if (rstsum[1]==stsum) {
621     spieler=2;
622     maxn++;
623   }
624   if (rstsum[2]>stsum) {
625     stsum=rstsum[2];
626     spieler=2;
627     maxn=1;
628   }
629   else if (rstsum[2]==stsum) {
630     spieler=1-spieler;
631     maxn++;
632   }
633   spgew=0;
634   if (maxn==3) {
635     spieler=spwert=stsum=0;
636   }
637   else {
638     spwert=stsum;
639     if (maxn==2) {
640       stsum=120-2*stsum;
641       spgew=1;
642       if (rskatloser) {
643 	spwert+=rskatsum;
644       }
645     }
646     else if (rskatloser) {
647       stsum+=rskatsum;
648       spwert+=rskatsum;
649     }
650   }
651   nspwert=0;
652   switch (rstich[0]+rstich[1]+rstich[2]) {
653   case 1:
654     nspwert=spwert=stsum=120;
655     spgew=1;
656     mes2=1;
657     break;
658   case 2:
659     mes1=1;
660     spwert*=2;
661     break;
662   }
663   for (i=0;i<sramschstufe;i++) spwert*=2;
664   if (bockspiele && !ramschspiele) spwert*=2;
665 }
666 
testgrandhand(sn)667 int testgrandhand(sn)
668 int sn;
669 {
670   int i,bb,as,zehn,b[4];
671 
672   bb=as=zehn=0;
673   for (i=0;i<10;i++) {
674     switch (cards[10*sn+i]&7) {
675     case BUBE:bb++;break;
676     case AS:as++;break;
677     case ZEHN:zehn++;break;
678     }
679   }
680   calc_inhand(sn);
681   for (i=0;i<4;i++) b[i]=inhand[i][BUBE];
682   return ((bb>=3 && as>=2 && as+zehn>=3) ||
683 	  (bb==4 && as>=2) ||
684 	  testgrand(bb,b,sn==hoerer));
685 }
686