40張撲克牌供一位莊家三位閒家遊玩。
每一波每家抽2張.一次用8張.共可以玩5波。
對子最大>>依序是10>9>8>7>6 A對最小。
紅黑不成對>>>例如紅磚7+紅心7[7仔對] 紅磚7+黑桃7[只有4點]
點數9點最大
9>8>7>6>5>4>3>2>1>0最小[目賊拉.媽殺摳]
點數相同時.則比手牌中最大點數者為贏
依序大小為10+9>8+1>7+2>6+3>5+4
若牌與莊家強碰[2副牌都一樣].則算莊家贏
例如莊家10+9.玩家10+9 算莊家贏
0點[目賊仔]一律算莊家贏
---
顯示結果
---
/*code*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 40
typedef struct
{
int c1;
int c2;
int sum;
int win;
} Player;
typedef struct
{
int color;
int number;
} Card;
void shuffle();
void battle();
int numsave[8];//移到全域變數 才解決亂數問題
int colsave[8];
int main()
{
/*
花色:S=0,H=1,D=2,C=3
數字:1~10
*/
srand(time(NULL));
int cards[N];
Player p1,p2,p3,bank;
int i = 0, now = N, dice = -1, pick = 0,set = 1;
for(i=0; i<N; i++)
{
cards[i] = i+1;
}
shuffle(cards);//洗牌
while(now>0)
{
printf("★★★★★ 局 %d ★★★★★\n",set);
dice = rand()%4;//丟骰子決定誰先抽牌
switch(dice)
{
case 0:
bank.c1 = cards[pick];
bank.c2 = cards[pick+1];
p1.c1 = cards[pick+2];
p1.c2 = cards[pick+3];
p2.c1 = cards[pick+4];
p2.c2 = cards[pick+5];
p3.c1 = cards[pick+6];
p3.c2 = cards[pick+7];
pick +=8;
break;
case 1:
bank.c1 = cards[pick+6];
bank.c2 = cards[pick+7];
p1.c1 = cards[pick];
p1.c2 = cards[pick+1];
p2.c1 = cards[pick+2];
p2.c2 = cards[pick+3];
p3.c1 = cards[pick+4];
p3.c2 = cards[pick+5];
pick +=8;
break;
case 2:
bank.c1 = cards[pick+4];
bank.c2 = cards[pick+5];
p1.c1 = cards[pick+6];
p1.c2 = cards[pick+7];
p2.c1 = cards[pick];
p2.c2 = cards[pick+1];
p3.c1 = cards[pick+2];
p3.c2 = cards[pick+3];
pick +=8;
break;
case 3:
bank.c1 = cards[pick+2];
bank.c2 = cards[pick+3];
p1.c1 = cards[pick+4];
p1.c2 = cards[pick+5];
p2.c1 = cards[pick+6];
p2.c2 = cards[pick+7];
p3.c1 = cards[pick];
p3.c2 = cards[pick+1];
pick +=8;
break;
}
battle(bank,p1,p2,p3);//比大小
now-=8;
set+=1;
}
return 0;
}
void shuffle(int* a)
{
int j = 0, r = -1, tmp = -1;
for(j=0; j<N; j++)
{
r = rand()%N;
tmp = a[j];
a[j] = a[r];
a[r] = tmp;
}
}
void battle(Player bank,Player p1,Player p2,Player p3)
{
Card crd[8];
char suit[4][2];
int k = 0, m = 0, tmax=0, p = 0;
crd[0].number = (bank.c1 % 10)==0?10:(bank.c1 % 10);
crd[1].number = (bank.c2 % 10)==0?10:(bank.c2 % 10);
crd[2].number = (p1.c1 % 10)==0?10:(p1.c1 % 10);
crd[3].number = (p1.c2 % 10)==0?10:(p1.c2 % 10);
crd[4].number = (p2.c1 % 10)==0?10:(p2.c1 % 10);
crd[5].number = (p2.c2 % 10)==0?10:(p2.c2 % 10);
crd[6].number = (p3.c1 % 10)==0?10:(p3.c1 % 10);
crd[7].number = (p3.c2 % 10)==0?10:(p3.c2 % 10);
crd[0].color = (bank.c1-1)/10;
crd[1].color = (bank.c2-1)/10;
crd[2].color = (p1.c1-1)/10;
crd[3].color = (p1.c2-1)/10;
crd[4].color = (p2.c1-1)/10;
crd[5].color = (p2.c2-1)/10;
crd[6].color = (p3.c1-1)/10;
crd[7].color = (p3.c2-1)/10;
for(k=0; k<8; k++) //暫存八張數字
{
numsave[k] = crd[k].number;
colsave[k] = crd[k].color;
}
k=0;
/*點數和*/
while(k<8)
{
/*判斷是否為對子,紅黑不成對*/
if(crd[k].number==crd[k+1].number) //數字同
{
if((crd[k].color==0&&crd[k+1].color==3)||(crd[k].color==3&&crd[k+1].color==0)||(crd[k].color==1&&crd[k+1].color==2)||(crd[k].color==2&&crd[k+1].color==1))
{
//同花色之對子
switch(k)
{
case 0:
bank.sum = crd[k].number + crd[k+1].number ;
break;
case 2:
p1.sum = crd[k].number + crd[k+1].number ;
break;
case 4:
p2.sum = crd[k].number + crd[k+1].number ;
break;
case 6:
p3.sum = crd[k].number + crd[k+1].number ;
break;
}
}
else
{
//不同花色之對子,一樣取餘數
switch(k)
{
case 0:
bank.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
case 2:
p1.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
case 4:
p2.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
case 6:
p3.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
}
}
}
else
{
//兩張數字不同
switch(k)
{
case 0:
bank.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
case 2:
p1.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
case 4:
p2.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
case 6:
p3.sum = (crd[k].number + crd[k+1].number)%10 ;
break;
}
}
k+=2;
}
//printf("banker cards : %d\n",crd[0].number);
/*p1,p2,p3跟莊家比大小*/
/*先比點數和*/
if(p1.sum>bank.sum) p1.win = 1;
else p1.win = 0;
if(p2.sum>bank.sum) p2.win = 1;
else p2.win = 0;
if(p3.sum>bank.sum) p3.win = 1;
else p3.win = 0;
/*總數和相同判斷是否目賊,若非則比最大那張大小或同牌組*/
if(p1.sum==bank.sum)
{
if(p1.sum==0) p1.win = 0;//目賊
tmax = (crd[2].number > crd[3].number)?crd[2].number : crd[3].number;
if(tmax > crd[0].number && tmax > crd[1].number)
{
//最大的那張比莊家任一張都大
p1.win = 1;
}
else
{
//包含了 牌組相同之情形 (EX:9+10,10+9)
p1.win = 0;
}
}
if(p2.sum==bank.sum)
{
if(p2.sum==0) p2.win = 0;//目賊
tmax = (crd[4].number > crd[5].number)?crd[4].number : crd[5].number;
if(tmax > crd[0].number && tmax > crd[1].number)
{
//最大的那張比莊家任一張都大
p2.win = 1;
}
else
{
p2.win = 0;
}
}
if(p3.sum==bank.sum)
{
if(p3.sum==0) p3.win = 0;//目賊
tmax = (crd[6].number > crd[7].number)?crd[6].number : crd[7].number;
if(tmax > crd[0].number && tmax > crd[1].number)
{
//最大的那張比莊家任一張都大
p3.win = 1;
}
else
{
p3.win = 0;
}
}
/*先印出牌組*/
k=0,m=0;
while(k<8)
{
//printf("k= %d m= %d\n",k,m);
switch(colsave[2*k+m])
{
case 0:
suit[k][m] = 'S';
//printf(" %d ",k);
break;
case 1:
suit[k][m] = 'H';
//printf(" %d ",k);
break;
case 2:
suit[k][m] = 'D';
//printf(" %d ",k);
break;
case 3:
suit[k][m] = 'C';
//printf(" %d ",k);
break;
}
m=(m+1)%2;
if(m==0) k +=1;
}
printf("莊家牌組: %c_%d %c_%d\n",suit[0][0],numsave[0],suit[0][1],numsave[1]);
printf("閒一牌組: %c_%d %c_%d\n",suit[1][0],numsave[2],suit[1][1],numsave[3]);
printf("閒二牌組: %c_%d %c_%d\n",suit[2][0],numsave[4],suit[2][1],numsave[5]);
printf("閒三牌組: %c_%d %c_%d\n",suit[3][0],numsave[6],suit[3][1],numsave[7]);
printf("莊家點數和 : %d\n",bank.sum);
printf("閒一點數和 : %d\n",p1.sum);
printf("閒二點數和 : %d\n",p2.sum);
printf("閒三點數和 : %d\n",p3.sum);
printf("==========勝負==========\n");
if(p1.win) printf("閒一勝\n");
else printf("閒一敗_QQ\n");
if(p2.win) printf("閒二勝\n");
else printf("閒二敗_QQ\n");
if(p3.win) printf("閒三勝\n");
else printf("閒三敗_QQ\n");
printf("\n");
}
---
沒有留言:
張貼留言