本帖最後由 qwert 於 2009/1/23 18:45 編輯
改良版的game, but有時仍會死機 :唉: :哭著跑:
高手指點下...
# include <stdlib.h>
# include <time.h>
# include <math.h>
# include <stdio.h>
main()
{
int size,qw;
srand(time(NULL));
printf("Enter the number of matches: ");
scanf("%d",&size);
printf("Enter the matches you take once: ");
scanf("%d",&qw);
int match[size],i,p,q,no,count=0,end,score,temp,r,s,j,k,ace;
printf("----------------------------------------\n");
printf("****************MATCHES*****************\n");
printf("Game rule:\n");
printf("1. There are %d matches, the player who takes the last match will lose\n",size);
printf("2. You should take the max %d consecutive matches\n",qw);
printf("-----------------------------------------\n");
for (i=0; i<size; i++)
match= 1;
printf("The matches are shown below:\n");
for (k=0; k<size; k++){
printf("%d",match[k]);
printf(" ");}
printf("\n");
do {
if (count%2 == 0 ){
printf("Player 1: Input the first and last matches you want to take(e.g. 3,5): ");
scanf("%d,%d",&p,&q);
ace=0;
if (p>q){
temp=p;
p=q;
q=temp;}
if ((p&&q>=1) && (p&&q<=size)){
if (q-p>=0-qw+1 && q-p<=qw-1) {
for (i=p; i<=q; i++)
ace+=match[i-1];
if (ace==q-p+1){
if (q>p)
for (no=p; no<=q; no++)
match [no-1]=0;
if (p==q)
match [p-1]=0;
}
else
continue;}else continue;}
else
continue;}
else{
do{
r=0; s=0;
ace=0; score=1;
r=rand()%size;
s=rand()%size;
if (r>s){
temp=r;
r=s;
s=temp;}
if ((r&&s>=1) && (r&&s<=size)){
if (s-r>=0-qw+1 && s-r<=qw-1){
if (match[r-1]==1 && match[s-1]==1){
for (i=r; i<=s; i++)
ace+=match[i-1];
if (ace==q-p+1){
printf("computer: Input the first and last matches you want to take(e.g. 3 5):%d %d\n",r,s);
score=0;}
}
}
} }while (score==1);
if (s>r){
for (no=r; no<=s; no++)
match [no-1]=0;}
if (r==s)
match [r-1]=0;
}
count++;
printf("\n");
for (k=0; k<size; k++){
printf("%d",match[k]);
printf(" ");}
printf("\n");
for(j=0;j<=size; j+=5){
printf("%d",j+1);
printf("\t "); }
printf("\n");
end=0;
for (i=0; i<size; i++){
end+=match;}
printf("Number of matches left: %d\n", end);
if (end==0)
end=-1;
} while (end>0);
printf("----------------------------------\n");
printf("Game over.\n");
if (count%2==0)
printf("Player 1 wins!\n");
else
printf("computer wins!\n");
system("pause");
} |