共有回帖数 0 个
-
◣我重发~~~◥:人机对战扑克游戏,详情请进
/**********************13cards.c开始******************/
/*将以下代码保存为 13cards.c*/
#include "showcrds.c"
#include "pokerAI.c"
#include "menu.c"
#include graphics.h
#include stdlib.h
typedef struct{
unsigned char num;
unsigned char style;
}Poker;
typedef struct{
unsigned char num;
unsigned char style;
unsigned char status;
}PokerPL;
Poker allPoker[53]={{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},
{10,1},{11,1},{12,1},{13,1},{14,1},{15,1},
{3,2},{4,2},{5,2},{6,2},{7,2},{8,2},{9,2},
{10,2},{11,2},{12,2},{13,2},{14,2},{15,2},
{3,3},{4,3},{5,3},{6,3},{7,3},{8,3},{9,3},
{10,3},{11,3},{12,3},{13,3},{14,3},{15,3},
{3,4},{4,4},{5,4},{6,4},{7,4},{8,4},{9,4},
{10,4},{11,4},{12,4},{13,4},{14,4},{15,4},{0,0}};
Poker deskTopPoker[53];
Poker cpuPoker[3][14];
PokerPL playerPoker[14];
Poker currentPokers[14];
void pokerToStr(Poker *p,unsigned char *str);
void drawCurrent(int player);
void washPokers(int flag);
void devide4(void);
void initGrphErrExit(void);
void updateCpus(int index);
void updatePlayer(int flag,int current);
void updateCrntPoker(void);
void updateDsktpPk(void);
void addTodeskTop(Poker p);
int game(int player);
void drawCurrent(int player);
int turnToCpus(int index,int *flag);
int getPeriod(int playerIndex);
int turnToPlayer(int *flag);
void pokerToStr(Poker p[],unsigned char *str);
int getCountP(Poker p[]);
int getCountPL(void);
void msg(int winner);
void main(void);
void washPokers(int flag){
Poker washTmp;
int i;
int m,n;
if(flag==0) randomize();
for(i=0;i5000;i++){
m=random(52);
n=random(52);
washTmp=allPoker[m];
allPoker[m]=allPoker[n];
allPoker[n]=washTmp;
}
currentPokers[0].num=0;
for(i=0;i52;i++)
deskTopPoker.num=0;
cpuPoker[2][0].num=0;
cpuPoker[1][0].num=0;
cpuPoker[0][0].num=0;
playerPoker[0].num=0;
}
void devide4(void){
int i,tmp,j;
PokerPL swapTmp;
int pa=0,pb[3]={0,0,0};
for(i=0;i52;i++){
if(i%4==0){
playerPoker[pa].num=allPoker.num;
playerPoker[pa].style=allPoker.style;
playerPoker[pa].status=0;
pa++;
}
else{
tmp=(i%4)-1;
cpuPoker[tmp][pb[tmp]]=allPoker;
pb[tmp]++;
}
}
playerPoker[13].num=0;
playerPoker[13].status=0;
cpuPoker[0][13].num=0;
cpuPoker[1][13].num=0;
cpuPoker[2][13].num=0;
currentPokers[0].num=0;
deskTopPoker[0].num=0;
for(j=0;j13;j++){
for(i=0;i12-j;i++){
if(playerPoker.numplayerPoker[i+1].num){
swapTmp=playerPoker;
playerPoker=playerPoker[i+1];
playerPoker[i+1]=swapTmp;
}
}
}
}
void initGrphErrExit(void){
int gd=VGA,gm=VGAHI,errorcode;
/*registerbgidriver(EGAVGA_driver);*/
initgraph(&gd,&gm,"");
errorcode=graphresult();
if (errorcode != grOk)
{
printf("nGraphics error: %sn",grapherrormsg(errorcode));
printf("nI am sorry that an error occurred(in initgraph).nn");
printf("ttPress any key to exit...");
getch();
exit(0);
}
}
void updateCpus(int index){
int i;
switch(index){
case 0:
setfillstyle(SOLID_FILL,GREEN);
bar(560,50,636,412);
setcolor(LIGHTBLUE);
outtextxy(565,50,"Bill");
for(i=0;cpuPoker[index].num != 0;i++)
showCard(563,i*21+60,0,0);
break;
case 1:
setfillstyle(SOLID_FILL,GREEN);
bar(140,0 ,482,100);
setcolor(RED);
outtextxy(55,4,"Wen Xichang");
for(i=0;cpuPoker[index].num != 0;i++)
showCard(i*22+144,2,0,0);
break;
case 2:
setfillstyle(SOLID_FILL,GREEN);
bar(2,50,76,412);
setcolor(YELLOW);
outtextxy(5,50,"Computer");
for(i=0;cpuPoker[index].num != 0;i++)
showCard(5,i*21+60,0,0);
break;
}
}
void updatePlayer(int flag,int current){
static old=20;
int i;
int x,y;
if(old==20){
setfillstyle(SOLID_FILL,GREEN);
for(i=0;i13;i++) bar(149+i*22,462,167+i*22,477);
}
else{
setfillstyle(SOLID_FILL,GREEN);
bar(149+old*22,462,167+old*22,477);
}
logo(149+current*22,462,48,COPY_PUT);
old=current;
if(flag != 0){
setfillstyle(SOLID_FILL,GREEN);
bar(147,342,481,363);
bar(147,439,481,460);
for(i=0;i13;i++){
if(playerPoker.num != 0){
if(playerPoker.status==0)
showCard(147+i*22,364,playerPoker.num,playerPoker.style);
else
showCard(147+i*22,342,playerPoker.num,playerPoker.style);
}
else {
setfillstyle(SOLID_FILL,GREEN);
bar(196+i*22,342,481,460);
break;
}
}
}
}
void updateCrntPoker(void){
int i;
setfillstyle(SOLID_FILL,GREEN);
bar(226,231,512,330);
for(i=0;currentPokers.num != 0;i++)
showCard(229+i*15,232,currentPokers.num,currentPokers.style);
}
void updateDsktpPk(void){
int i;
setfillstyle(SOLID_FILL,GREEN);
bar(140,102,512,230);
for(i=0;deskTopPoker.num != 0;i++){
if(i26) showCard(141+i*12,103,deskTopPoker.num,deskTopPoker.style);
else showCard(141+(i-26)*12,133,deskTopPoker.num,deskTopPoker.style);
}
}
void addTodeskTop(Poker p){
int i=0;
for(;;){
if(deskTopPoker.num==0) break;
i++;
}
deskTopPoker=p;
if(i26) showCard(141+i*12,103,deskTopPoker.num,deskTopPoker.style);
else showCard(141+(i-26)*12,133,deskTopPoker.num,deskTopPoker.style);
}
int game(int player){
int tmp=0,i;
int playerTurn;
unsigned char tmp1[14];
unsigned char tmp2[14];
int jump;
setfillstyle(SOLID_FILL,GREEN);
bar(0,0,639,479);
washPokers(1);
devide4();
updateCpus(0);
updateCpus(1);
updateCpus(2);
updatePlayer(1,0);
playerTurn=player;
jump=player;
for(;;){
switch(jump){
case 3:
AAAA:
drawCurrent(3);
tmp=turnToPlayer(&playerTurn);
switch(tmp){
case 20:
switch(inMenu(3)){
case 2:
return 4;
case 1:
case 0:
updateDsktpPk();
updateCrntPoker();
goto AAAA;
case 3:
closegraph();
exit(0);
}
break;
case 10:
return 3;
}
if(tmp != 0){
sleep(1);
for(i=0;currentPokers.num != 0;i++)
addTodeskTop(currentPokers);
}
jump=0;
break;
case 0:
drawCurrent(0);
tmp=turnToCpus(0,&playerTurn);
if(tmp != 0){
sleep(1);
for(i=0;currentPokers.num != 0;i++)
addTodeskTop(currentPokers);
}
if(tmp==10)
return 0;
jump++;
break;
case 1:
drawCurrent(1);
tmp=turnToCpus(1,&playerTurn);
if(tmp != 0){
sleep(1);
for(i=0;currentPokers.num != 0;i++)
addTodeskTop(currentPokers);
}
if(tmp==10)
return 1;
jump++;
break;
case 2:
drawCurrent(2);
tmp=turnToCpus(2,&playerTurn);
if(tmp != 0){
sleep(1);
for(i=0;currentPokers.num != 0;i++)
addTodeskTop(currentPokers);
}
if(tmp==10)
return 2;
jump++;
break;
}
}
}
void drawCurrent(int player){
setcolor(GREEN);
line(20,23,52,23);
line(20,23,36,44);
line(36,44,52,23);
line(112,34,133,50);
line(112,34,112,66);
line(133,50,112,66);
line(578,19,610,19);
line(578,19,594,40);
line(610,19,594,40);
line(107,398,107,430);
line(107,398,128,414);
line(107,430,128,414);
line(144,101,478,101);
line(559,55,559,403);
line(79,55,79,403);
setcolor(YELLOW);
switch(player){
case 0:
line(578,19,610,19);
line(578,19,594,40);
line(610,19,594,40);
line(559,55,559,403);
break;
case 1:
line(112,34,133,50);
line(112,34,112,66);
line(133,50,112,66);
line(144,101,478,101);
break;
case 2:
line(20,23,52,23);
line(20,23,36,44);
line(36,44,52,23);
line(79,55,79,403);
break;
case 3:
line(107,398,107,430);
line(107,398,128,414);
line(107,430,128,414);
}
}
int turnToCpus(int index,int *flag){
unsigned char tmp1[14],ind[14],tmp2[14];
int i,j;
if(*flag==index){
pokerToStr(cpuPoker[index],tmp1);
AIout(tmp1,ind,getPeriod(index));
for(i=0;i14;i++){
if(ind==255) break;
currentPokers=cpuPoker[index][ind];
cpuPoker[index][ind].num=0;
cpuPoker[index][ind].style=0;
}
currentPokers.num=0;
*flag=index;
for(i=0;i14;i++){
if(cpuPoker[index].num==0){
for(j=i+1;j14;j++){
if(cpuPoker[index][j].num != 0){
cpuPoker[index]=cpuPoker[index][j];
cpuPoker[index][j].num=0;
cpuPoker[index][j].style=0;
break;
}
}
}
}
updateCpus(index);
updateCrntPoker();
if(getCountP(cpuPoker[index])==0) return 10;
return 1;
}
else{
pokerToStr(cpuPoker[index],tmp1);
pokerToStr(currentPokers,tmp2);
if(getType(tmp2)==0)
printf("!!!!");
if(AIeat(tmp1,tmp2,ind,getPeriod(index)) != 0){
for(i=0; i14;i++){
if(ind==255) break;
currentPokers=cpuPoker[index][ind];
cpuPoker[index][ind].num=0;
cpuPoker[index][ind].style=0;
}
currentPokers.num=0;
*flag=index;
for(i=0;i14;i++){
if(cpuPoker[index].num==0){
for(j=i+1;j14;j++){
if(cpuPoker[index][j].num != 0){
cpuPoker[index]=cpuPoker[index][j];
cpuPoker[index][j].num=0;
cpuPoker[index][j].style=0;
break;
}
}
}
}
updateCpus(index);
updateCrntPoker();
if(getCountP(cpuPoker[index])==0) return 10;
return 1;
}
return 0;
}
}
int getPeriod(int playerIndex){
int num[4],sum;
num[0]=getCountP(cpuPoker[0]);
num[1]=getCountP(cpuPoker[1]);
num[2]=getCountP(cpuPoker[2]);
num[3]=getCountPL();
if(num[playerIndex+1]2||num[playerIndex]3) return 4;
sum=num[0]+num[1]+num[2]+num[3];
if(sum40) return 1;
if(sum26) return 2;
return 3;
}
int turnToPlayer(int *flag){
int key=0;
int i,j,t;
static int hand=0;
unsigned char tmp1[14],tmp2[14],tmp3[14],tmp4[14];
Poker tmpP[14],tmpQ[14];
for(;;){
if (bioskey(1))
key=bioskey(0);
else
key=0;
switch(key){
case LEFT:
hand=((hand1)?(getCountPL()-1): (hand-1));
updatePlayer(0,hand);
break;
case RIGHT:
hand=((hand = (getCountPL()-1))?0 : (hand+1));
updatePlayer(0,hand);
break;
case UP:
if(playerPoker[hand].num2&&playerPoker[hand].num16){
playerPoker[hand].status=1;
updatePlayer(1,hand);
}
break;
case DOWN:
playerPoker[hand].status=0;
updatePlayer(1,hand);
break;
case ENTER:
case SPACE:
i=0;
for(j=0;j14;j++){
if(playerPoker[j].status==1){
tmpP.num=playerPoker[j].num;
tmpP.style=playerPoker[j].style;
playerPoker[j].status=0;
i++;
}
}
tmpP.num=0;
pokerToStr(tmpP,tmp1);
if(*flag != 3){
pokerToStr(currentPokers,tmp2);
for(i=0;i14;i++)
tmpQ.num=playerPoker.num;
pokerToStr(tmpQ,tmp3);
if(AIeat(tmp3,tmp2,tmp4,1)==0) return 0;
t=getType(tmp1);
if(t!=4){
if((getType(tmp2) != t)||(t==0)||
(getSmall(tmp2)=getSmall(tmp1))){
updatePlayer(1,hand);
break;
}
}
else{
if((getType(tmp2) == 4)&&
(getSmall(tmp2)=getSmall(tmp1))){
updatePlayer(1,hand);
break;
}
}
}
else{
if(getType(tmp1)==0){
updatePlayer(1,hand);
break;
}
}
for(i=0;tmpP.num != 0;i++){
for(j=0;j14;j++){
if(tmpP.num==playerPoker[j].num&&
tmpP.style==playerPoker[j].style){
playerPoker[j].num=0;
playerPoker[j].style=0;
currentPokers=tmpP;
}
}
}
currentPokers.num=0;
for(i=0;i14;i++){
if(playerPoker.num==0){
for(j=i+1;j14;j++){
if(playerPoker[j].num != 0){
playerPoker=playerPoker[j];
playerPoker[j].num=0;
playerPoker[j].style=0;
break;
}
}
}
}
hand=0;
updatePlayer(1,hand);
updateCrntPoker();
*flag=3;
if(getCountPL()==0) return 10;
return 1;
case ESC:
return 20;
}
}
}
void pokerToStr(Poker p[],unsigned char *str){
int i,j=0;
for(i=0;i14;i++){
if(p.num != 0){
*(str+j)=(char)(p.num);
j++;
}
else break;
}
*(str+j)=0;
}
int getCountP(Poker p[]){
int i=0;
for(;;){
if(p.num==0) break;
i++;
}
return i;
}
int getCountPL(void){
int i=0;
for(;;){
if(playerPoker.num==0) break;
i++;
}
return i;
}
void msg(int winner){
setfillstyle(SOLID_FILL,9);
bar(204,152,427,285);
dBorder(204,152,427,285);
paintLogo(4);
setcolor(WHITE);
outtextxy(246,189,"WINNER:");
setcolor(BLUE);
settextstyle(0,0,2);
switch(winner){
case 0:
outtextxy(230,211,"Bill Gates");
break;
case 1:
outtextxy(230,211,"Wen Xichang");
break;
case 2:
outtextxy(230,211,"Computer");
break;
default:
outtextxy(230,211,"YOU !!!");
}
settextstyle(0,0,0);
setcolor(GREEN);
outtextxy(260,257,"Press ENTER ...");
while(getch() != 13);
setfillstyle(SOLID_FILL,GREEN);
bar(204,152,427,285);
}
void main(void){
int key;
int winner=3;
initGrphErrExit();
inMenu(1);
washPokers(0);
for(;;){
winner=game(winner);
if(winner==4){
inMenu(1);
winner=3;
}
else
msg(winner);
}
}
/**********************13cards.c结束******************/
楼主 2016-03-16 17:45 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知