我正在制作井字游戏,我需要用户输入1到9之间的任意位置,然后输入X或O。我需要包括的一些条件是我想限制用户输入更大的数字小于9,并且请勿输入'X'或'O'以外的任何字符。我遇到的问题是它没有遵循条件语句。任何帮助将不胜感激。
void CreateBoard(int m, int n, char board[][n])
{
int i, j, position;
char one;
char two;
char temp;
char xORo;
int count = 0;
int end;
do {
printf("Enter the number of the cell you want to insert X or O or enter -1 to exit: \n");
scanf("%d", &position);
printf("Type X or O: \n");
scanf(" %c", &xORo);
if(position < 0){
break;
}
if((position > 9) && xORo != ('X') && ('O'))
{
continue;
}
else if((position > 0 || position < 9) && xORo == ('X') && ('O'))
{
switch(position)
{
case 1: temp = xORo;
xORo = board[0][0];
board[0][0] = temp;
break;
case 2: temp = xORo;
xORo = board[0][1];
board[0][1] = temp;
break;
case 3: temp = xORo;
xORo = board[0][2];
board[0][2] = temp;
break;
case 4: temp = xORo;
xORo = board[1][0];
board[1][0] = temp;
break;
case 5: temp = xORo;
xORo = board[1][1];
board[1][1] = temp;
break;
case 6: temp = xORo;
xORo = board[1][2];
board[1][2] = temp;
break;
case 7: temp = xORo;
xORo = board[2][0];
board[2][0] = temp;
break;
case 8: temp = xORo;
xORo = board[2][1];
board[2][1] = temp;
break;
case 9: temp = xORo;
xORo = board[2][2];
board[2][2] = temp;
break;
}
PrintBoard(3, 3, board);
}
}while(position != -1);
}
答案 0 :(得分:2)
您可以通过一点数学来简化。给定索引i
,您可以使用以下命令获取行/列:
row = i % 3;
col = i / 3;
请注意,i
是0-8
,而不是1-9
。
您可以使用辅助函数将索引转换为行/列:
void get_row_col(int index, int num_cols, int *row, int *col)
{
*row = index % num_cols;
*col = index / num_cols;
}
然后您的功能可以简化为:
void CreateBoard(int m, int n, char board[][n])
{
int position, row, col;
char xORo;
while (1) {
printf("Enter the number of the cell you want to insert X or O or enter -1 to exit: \n");
scanf("%d", &position);
// TODO: Always check the return value from scanf
if (position == -1) break;
printf("Type X or O: \n");
scanf(" %c", &xORo);
if (position < 1 || position > 9 || (xORo != 'X' && xORo != 'Y')) {
continue;
}
get_row_col(position - 1, 3, &row, &col);
board[row][col] = xORo;
PrintBoard(3, 3, board);
}
}
答案 1 :(得分:0)
if((position > 9) && (xORo !='X') && (xORo !='O'))
{
continue;
}
这应该是第二个条件。
else if((position > 0)&&(position < 9)&&(xORo =='X') && (xORo=='O'))
这应该是达到您期望的动机的最终条件。此外,无需在条件(position!=-1)时使用do-while循环,因为在第一个条件中已经实现了这一点。可以使用代码
while(1) {
printf("Enter the number of the cell you want to insert X or O or enter -1 to exit: \n");
scanf("%d", &position);
printf("Type X or O: \n");
scanf(" %c", &xORo);
if(position < 0){
break;
}
if((position > 9) && (xORo !='X') && (xORo !='O'))
{
continue;
}
else if((position > 0)&&(position < 9)&&(xORo =='X') && (xORo=='O'))
{
switch(position)
{
case 1: temp = xORo;
xORo = board[0][0];
board[0][0] = temp;
break;
case 2: temp = xORo;
xORo = board[0][1];
board[0][1] = temp;
break;
case 3: temp = xORo;
xORo = board[0][2];
board[0][2] = temp;
break;
case 4: temp = xORo;
xORo = board[1][0];
board[1][0] = temp;
break;
case 5: temp = xORo;
xORo = board[1][1];
board[1][1] = temp;
break;
case 6: temp = xORo;
xORo = board[1][2];
board[1][2] = temp;
break;
case 7: temp = xORo;
xORo = board[2][0];
board[2][0] = temp;
break;
case 8: temp = xORo;
xORo = board[2][1];
board[2][1] = temp;
break;
case 9: temp = xORo;
xORo = board[2][2];
board[2][2] = temp;
break;
}
PrintBoard(3, 3, board);
}
}
}