void insert(char pl[128],struct cell **B); void dele(char pl[128],struct cell **B); int check(char pl[128],struct cell **B); void deleall(struct cell **B,struct cell **C); struct cell{ char moji[128]; struct cell *next; };
void main(){ int winner,i=0; char player[128],C,start='t'; player[0]=0; cell *p[26],*q[26]; FILE *fp=fopen("test.dat","r"); for(int j=0;j<26;j++){ p[j]=NULL;//PCが記憶している言葉 q[j]=NULL;//すでに出た言葉 } while(*1)!=EOF){//ファイルのを木構造に入れる insert(player,p); } printf("英語でしりとりしてください。\n'ん'の代わりに’x’で終わる言葉は使えません\nPC :start\n"); while(1){//メインループ printf("YOU:"); scanf("%s",player); if(check(player,q)){ printf("すでに出た言葉です\n"); winner=1;//PCの勝ち break; } if(player[0]!=start){//前後の文字が繋がらないとき printf("前後が繋がりません\n"); winner=1;//PCの勝ち break; } if(player[strlen(player)-1]=='x'){//xで終わるとき printf("xで終わっています\n"); winner=1;//PCの勝ち break; } if(p[(player[strlen(player)-1])-'a']==NULL){//その文字から始まる言葉がないなら insert(player,q); winner=0;//PCの負け break; }
dele(player,p); insert(player,q);//player:出た言葉を保存する printf("PC :%s\n",p[(player[strlen(player)-1])-'a']->moji); insert(p[(player[strlen(player)-1])-'a']->moji,q);//PC:出た言葉を保存する start=p[(player[strlen(player)-1])-'a']->moji[strlen(p[(player[strlen(player)-1])-'a']->moji)-1]; dele(p[(player[strlen(player)-1])-'a']->moji,p);//いまだした言葉を削除する //p[(player[strlen(player)-1])-'a']=p[(player[strlen(player)-1])-'a']->next;//次へ進む
} if(winner==0) printf("あなたの勝ちです\n"); else if(winner) printf("PCの勝ちです\n"); deleall(p,q); fclose(fp); //新しい言葉をFILEに加える FILE *gp=fopen("test.dat","w"); for(int j=0;j<26;j++){ while(q[j]!=NULL){ fprintf(gp,"%s\n",q[j]->moji); q[j]=q[j]->next; } } fclose(gp); }
void insert(char pl[128],struct cell **B){//リストを作る struct cell *p; p=(cell *)malloc(sizeof(cell)); strcpy(p->moji,pl); p->next=B[pl[0]-'a']; B[pl[0]-'a']=p; }
void dele(char pl[128],struct cell **B){//リストからはずす struct cell *q,*r; q=B[pl[0]-'a']; r=NULL; while(q!=NULL){ if(strcmp(pl,q->moji)==0){ if(r==NULL) B[pl[0]-'a']=q->next; else { r->next=q->next; } } r=q; q=q->next; } }
int check(char pl[128],struct cell **B){//以前に出ていないかチェック struct cell *r=B[pl[0]-'a'],*q=NULL; while(r!=NULL){ if(strcmp(pl,r->moji)==0) { return 1; } q=r; r=r->next; } return 0; }
void deleall(struct cell **B,struct cell **C){//BのすべてをCに移す struct cell *q,*r; for(int i=0;i<26;i++){ q=B[i]; r=NULL; while(q!=NULL) { insert(q->moji,C); r=q; q=q->next; } } }
*1 C=fscanf(fp,"%s",player