主题:类似三子连珠的game,用户直接输入回车时的问题, 代码如下
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
void swap(char *player);
void change_ele1(int dev,int mod, char (*r)[12],char *player);
int if_end(char (*row)[12]);
char *prompt();
void main()
{
char quit,map[4][12]={"---+---+---","-1-+-2-+-3-","-4-+-5-+-6-","-7-+-8-+-9-"}; //the original map
char row[3][12]={" | | "," | | "," | | "};
int turn=0,pos,dev,mod,if_num; //pos=postion, dev=devision,
char player[2]="X";
int check_end=0,empty;
char *for_prompt;
while(turn<=6 && check_end==0) // game isn't over and need to add new element
{
turn=turn+1;
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",map[0],row[0],map[1],row[1],map[2],row[2],map[3]);
// print out map
do
{
printf("player %s:",player);
if_num=scanf("%d",&pos);
if(if_num==0) // user input isn't number!
{
for_prompt=prompt();
if(strcmp(for_prompt,"Q")==0) // user input "Q"
{
swap(player);
printf("player %s wins!\n",player);
exit(EXIT_SUCCESS);
}
else if(for_prompt[0]=='\0') //user input '/n'
{
printf("can't skip step!\n");
empty=0;
}
else
{
printf("Invalid input!\n");
empty=0;
}
}
else if(pos>9 || pos<=0)
{
printf("Invalid input!\n");
empty=0;
}
else // input is number
{
dev=pos/3;
mod=pos%3;
if(mod==1) // check the chosed postion is empty or not
{
if(row[dev][1]==' ')
{
empty=1;
}
else
{
empty=0;
}
}
else if(mod==2)
{
if(row[dev][5]==' ')
{
empty=1;
}
else
{
empty=0;
}
}
else
{
if(row[dev-1][9]==' ')
{
empty=1;
}
else
{
empty=0;
}
}
}
}while(empty==0); //repeat cheak for the input!
change_ele1(dev,mod,row,player); // use function to change new element
check_end=if_end(row); // if somebody wins
if(check_end==1)
{
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",map[0],row[0],map[1],row[1],map[2],row[2],map[3]);
printf("1:player %s wins!\n",player);
exit(EXIT_SUCCESS);
}
else //no player wins
{
swap(player);
}
}// first while loop
while(turn>6 && check_end ==0) // turn > 3
{
printf("do nothing!\n");
// move from one to one until a player win~~~
}
} //main loop
void swap(char *player)
{
if(player[0]=='X')
{
player[0]='O';
}
else
{
player[0]='X';
}
}
void change_ele1(int dev,int mod, char (*row)[12],char *player)
{
if(dev==0)
{
if(mod==1)
{
row[0][1]=player[0];
}
else
{
row[0][5]=player[0];
}
}
else if(dev==1)
{
if(mod==1)
{
row[1][1]=player[0];
}
else if(mod==2)
{
row[1][5]=player[0];
}
else
{
row[0][9]=player[0];
}
}
else if(dev==2)
{
if(mod==1)
{
row[2][1]=player[0];
}
else if(mod==2)
{
row[2][5]=player[0];
}
else
{
row[1][9]=player[0];
}
}
else
{
if(mod==0)
{
row[2][9]=player[0];
}
}
}
int if_end(char (*row)[12])
{
if((row[0][1] != ' ')&&row[0][1] == row[0][5] && row[0][5]== row[0][9])
{
return 1;
}
else if((row[1][1] != ' ') && row[1][1] == row[1][5] && row[1][5] == row[1][9])
{
return 1;
}
else if((row[2][1] != ' ') && row[2][1] == row[2][5] && row[2][5] == row[2][9])
{
return 1;
} // compaire every row
else if((row[0][1] != ' ') && row[0][1] == row[1][1] && row[1][1]== row[2][1])
{
return 1;
}
else if((row[0][5] != ' ') && row[0][5] == row[1][5] && row[1][5]== row[2][5])
{
return 1;
}
else if((row[0][9] != ' ') && row[0][9] == row[1][9] && row[1][9] == row[2][9])
{
printf("da f");
return 1;
} //compaire every column
else if((row[0][9] != ' ') && row[0][9] == row[1][5] && row[1][5] == row[2][1])
{
return 1;
}
else if((row[0][1] != ' ') && row[0][1] == row[1][5] && row[1][5] == row[2][9])
{
return 1;
}
else
{
return 0;
}
}
char *prompt()
{
int cur_array_len = 2;
int pos = 0;
char ch;
char *text = NULL;
text = malloc(sizeof(char) * cur_array_len);
do
{
scanf("%c", &ch);
if(ch == '\n')
{
text[pos] = '\0';
}
else
{
text[pos] = ch;
pos++;
if(pos == cur_array_len - 1)
{
// expand array
char *t2 = malloc(sizeof(char) * cur_array_len * 2);
int i;
for(i = 0; i < cur_array_len; i++)
{
t2[i] = text[i];
}
cur_array_len *= 2;
free(text);
text = t2;
}
}
} while(ch != '\n');
return text;
}
#include<string.h>
#include<stdlib.h>
void swap(char *player);
void change_ele1(int dev,int mod, char (*r)[12],char *player);
int if_end(char (*row)[12]);
char *prompt();
void main()
{
char quit,map[4][12]={"---+---+---","-1-+-2-+-3-","-4-+-5-+-6-","-7-+-8-+-9-"}; //the original map
char row[3][12]={" | | "," | | "," | | "};
int turn=0,pos,dev,mod,if_num; //pos=postion, dev=devision,
char player[2]="X";
int check_end=0,empty;
char *for_prompt;
while(turn<=6 && check_end==0) // game isn't over and need to add new element
{
turn=turn+1;
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",map[0],row[0],map[1],row[1],map[2],row[2],map[3]);
// print out map
do
{
printf("player %s:",player);
if_num=scanf("%d",&pos);
if(if_num==0) // user input isn't number!
{
for_prompt=prompt();
if(strcmp(for_prompt,"Q")==0) // user input "Q"
{
swap(player);
printf("player %s wins!\n",player);
exit(EXIT_SUCCESS);
}
else if(for_prompt[0]=='\0') //user input '/n'
{
printf("can't skip step!\n");
empty=0;
}
else
{
printf("Invalid input!\n");
empty=0;
}
}
else if(pos>9 || pos<=0)
{
printf("Invalid input!\n");
empty=0;
}
else // input is number
{
dev=pos/3;
mod=pos%3;
if(mod==1) // check the chosed postion is empty or not
{
if(row[dev][1]==' ')
{
empty=1;
}
else
{
empty=0;
}
}
else if(mod==2)
{
if(row[dev][5]==' ')
{
empty=1;
}
else
{
empty=0;
}
}
else
{
if(row[dev-1][9]==' ')
{
empty=1;
}
else
{
empty=0;
}
}
}
}while(empty==0); //repeat cheak for the input!
change_ele1(dev,mod,row,player); // use function to change new element
check_end=if_end(row); // if somebody wins
if(check_end==1)
{
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",map[0],row[0],map[1],row[1],map[2],row[2],map[3]);
printf("1:player %s wins!\n",player);
exit(EXIT_SUCCESS);
}
else //no player wins
{
swap(player);
}
}// first while loop
while(turn>6 && check_end ==0) // turn > 3
{
printf("do nothing!\n");
// move from one to one until a player win~~~
}
} //main loop
void swap(char *player)
{
if(player[0]=='X')
{
player[0]='O';
}
else
{
player[0]='X';
}
}
void change_ele1(int dev,int mod, char (*row)[12],char *player)
{
if(dev==0)
{
if(mod==1)
{
row[0][1]=player[0];
}
else
{
row[0][5]=player[0];
}
}
else if(dev==1)
{
if(mod==1)
{
row[1][1]=player[0];
}
else if(mod==2)
{
row[1][5]=player[0];
}
else
{
row[0][9]=player[0];
}
}
else if(dev==2)
{
if(mod==1)
{
row[2][1]=player[0];
}
else if(mod==2)
{
row[2][5]=player[0];
}
else
{
row[1][9]=player[0];
}
}
else
{
if(mod==0)
{
row[2][9]=player[0];
}
}
}
int if_end(char (*row)[12])
{
if((row[0][1] != ' ')&&row[0][1] == row[0][5] && row[0][5]== row[0][9])
{
return 1;
}
else if((row[1][1] != ' ') && row[1][1] == row[1][5] && row[1][5] == row[1][9])
{
return 1;
}
else if((row[2][1] != ' ') && row[2][1] == row[2][5] && row[2][5] == row[2][9])
{
return 1;
} // compaire every row
else if((row[0][1] != ' ') && row[0][1] == row[1][1] && row[1][1]== row[2][1])
{
return 1;
}
else if((row[0][5] != ' ') && row[0][5] == row[1][5] && row[1][5]== row[2][5])
{
return 1;
}
else if((row[0][9] != ' ') && row[0][9] == row[1][9] && row[1][9] == row[2][9])
{
printf("da f");
return 1;
} //compaire every column
else if((row[0][9] != ' ') && row[0][9] == row[1][5] && row[1][5] == row[2][1])
{
return 1;
}
else if((row[0][1] != ' ') && row[0][1] == row[1][5] && row[1][5] == row[2][9])
{
return 1;
}
else
{
return 0;
}
}
char *prompt()
{
int cur_array_len = 2;
int pos = 0;
char ch;
char *text = NULL;
text = malloc(sizeof(char) * cur_array_len);
do
{
scanf("%c", &ch);
if(ch == '\n')
{
text[pos] = '\0';
}
else
{
text[pos] = ch;
pos++;
if(pos == cur_array_len - 1)
{
// expand array
char *t2 = malloc(sizeof(char) * cur_array_len * 2);
int i;
for(i = 0; i < cur_array_len; i++)
{
t2[i] = text[i];
}
cur_array_len *= 2;
free(text);
text = t2;
}
}
} while(ch != '\n');
return text;
}
大概就是X,O两人轮流下棋。若先有一方棋子练成线则赢,输入大写Q则对家赢,其它情况提示有误并且让用户重新输入。每方棋子只有三颗,所以前三轮是“新添”棋子,之后要靠“移动,”现在我只是在完成“新添”部分,可是用户在比如printf(“player X: ”)用户直接输入回车的话就怎么也不行,想请教如何解决这个问题!如果用户输入的既有数字也有字符怎么办?