我正在做一个学校的项目,我不明白为什么switch语句没有在main中打印。我正在尝试做一个菜单选择,我在另一个项目中使用了相同的代码,它工作得很完美。
char square[] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int choices, def;
int checkWin();
struct Player
{
char init[3]; //initials for player
char xo; //x or o
int wins; //player wins
int losses; //player losses
int ties; //player ties
int turn; //players turn
};
int checkWin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9]) {
return 1;
}
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
square[4] != '4' && square[5] != '5' && square[6] != '6' &&
square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else {
return - 1;
}
}
void displayBoard()
{
printf("\n\n\tTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
//board display
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2], square[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[4], square[5], square[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[7], square[8], square[9]);
printf(" | | \n\n");
}
int main() {
int player = 1, i, choice;
char mark;
switch(choices) {
case 1:
printf("\t\t\t1. PLAY\n");
printf("\t\t\t2. INSTRUCTIONS\n");
printf("\t\t\t3. STATISTICS\n");
printf("\t\t\t4. EXIT\n\n");
printf("\t\t\tENTER A NUMBER (1-4): ");
scanf("%d", &def);
if (def == 1) {
void displayBoard();
}
if (def == 2) {
printf("----INSTRUCTIONS----\n\n");
printf("To begin the game, select play in the menu.\n");
printf("This is your board:\n\n");
printf("----------\n");
printf("1 | 2 | 3\n");
printf("----------\n");
printf("4 | 5 | 6\n");
printf("----------\n");
printf("7 | 8 | 9\n");
printf("----------\n\n");
printf("Players will take turns inputting their X or O, ('X' or 'O') by entering the number on the board.\n");
printf("The first player to get three of their letter in a row wins.\n\n");
}
if (def == 3) {
struct Player;
}
if (def == 4) {
fclose;
} else {
return 0;
}
do {
displayBoard();
player = (player % 2) ? 1 : 2;
printf("Player %d, enter a number: ", player);
scanf("%d", &choice);
mark = (player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1') {
square[1] = mark;
} else if (choice == 2 && square[2] == '2') {
square[2] = mark;
} else if (choice == 3 && square[3] == '3') {
square[3] = mark;
} else if (choice == 4 && square[4] == '4') {
square[4] = mark;
} else if (choice == 5 && square[5] == '5') {
square[5] = mark;
} else if (choice == 6 && square[6] == '6') {
square[6] = mark;
} else if (choice == 7 && square[7] == '7') {
square[7] = mark;
} else if (choice == 8 && square[8] == '8') {
square[8] = mark;
} else if (choice == 9 && square[9] == '9') {
square[9] = mark;
} else {
printf("Invalid move ");
player--;
getch();
}
i = checkWin();
player++;
} while (i == -1);
{
displayBoard();
}
if (i == 1) {
printf("==>\aPlayer %d win ", --player);
} else {
printf("==>\aGame draw");
}
getch();
int selection();
}
}
Switch语句没有打印出来,我试过移动代码,把它放在它自己的函数里面,所有的一切,我默认把它放在main里面,希望它能打印出来,但是它默认打印井字游戏板,完全跳过了switch语句。
1条答案
按热度按时间kcwpcxri1#
来自评论:
全局变量
choices
没有显式初始值,默认值为0
,不执行case 1
,没有其他情况,不执行。