/*
該遊戲規則如下: 操作者先輸入一個奇數 N(<200)表示N個石
子. 設電腦為 A 方,操作者為 B 方, 雙方輪流取石子,每次取1-3個. 最後取
到石子總數為奇數的一方獲勝. 編製程式使電腦有較多的獲勝機會,
*/
#include "stdio.h"
#include "conio.h"
main()
{
int total;
int p=0,c=0,pAdd,cAdd;
printf("total=");
scanf("%d",&total);
printf("\n****begin****\n\n");
do
{
if(c%2==0)
{
if(total>=3)
{
cAdd=3;
}
else if(total>=1)
{
cAdd=1;
}
}
else
{
if(total>=2)
{
cAdd=2;
}
else
{
cAdd=1;
}
}
total-=cAdd;
c+=cAdd;
printf("computer:%d\n",cAdd);
if(total==0) break;
printf(" %d leave.\n",total);
printf(" person:");
scanf("%d",&pAdd);
while(pAdd>3 || pAdd<1 || pAdd>total)
{
printf(" error! %d leave.again:",total);
scanf("%d",&pAdd);
}
total-=pAdd;
p+=pAdd;
printf("person:%d\n",pAdd);
}while(total!=0);
printf("\n****end****\n\n");
outputResult(p,c);
getch();
}
outputResult(_pNum,_cNum)
{
int pTag,cTag;
printf("----result----\n");
printf("person:%d\n",_pNum);
printf("computer:%d\n",_cNum);
pTag=(_pNum%2!=0?1:0);
cTag=(_cNum%2!=0?1:0);
printf("--------\n");
if((pTag||cTag) && !(pTag&&cTag))
{
if(pTag)
{
printf("person win!");
}
else
{
printf("computer win!");
}
}
else
{
printf("no one win!");
}
}