我目前正在编写一个简单的抽搐脚趾游戏,这证明不是那么简单(至少对我来说,我是个初学者)。我在编写display_board函数时遇到了困难,这个函数应该打印如下所示:
_|_|_
_|_|_
| |当玩家将棋盘标记到特定位置时,添加X或O。我打算做的是把所有这些都放在一个字符串中,但这有点让人困惑,特别是我想要添加的新行,这样板才能正常运行。新的行运算符是否算作字符串中的一个或两个字符?如果你想看我的游戏代码,下面是:
#include <stdio.h>
#include <stdbool.h>
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[];
while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", &move);
if(valid_location(move)) {
mark_location(user1, move);
display_board();
else if(won_the_game(user1) {
printf("Congratulations User-1, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
printf("User-2, please enter your move:");
scanf("%s", &move);
if(valid_location(move)) {
mark_location(user2, move);
display_board();
else if(won_the_game(user2) {
printf("Congratulations User-2, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
}
bool valid_location(char str[]) {
if (str[] == "upperLeft" || str[] == "up" || str[] == "upperRight" || str[] == "left" || str[] == "center" || str[] == "right" || str[] == "lowerLeft" || str[] == "down" || str[] == "lowerRight") {
return true;
}
}
void mark_location(int userU, char str[]) {
if (str[] == "upperLeft") {
board[0][0] = userU;
else if (str[] == "up") {
board[0][1] = userU;
else if (str[] == "upperRight") {
board[0][2] = userU;
else if (str[] == "left") {
board[1][0] = userU;
else if (str[] == "center") {
board[1][1] = userU;
else if (str[] == "right") {
board[1][2] = userU;
else if (str[] == "lowerLeft") {
board[2][0] = userU;
else if (str[] == "down") {
board[2][1] = userU;
else if (str[] == "lowerRight") {
board [2][2] = userU;
}
}有点乱,我说过我也是新来的。如果您有任何建议来清理它,请随时帮助我。
发布于 2017-08-08 13:21:00
使用strcmp函数比较C中的字符串
str[] == "upperLeft"不是有效的C表达式。
这一定义还包括:
char move[];不是有效的数组定义,它遗漏了[]之间的元素数。
此外,
scanf("%s", &move);%s转换规范将指向char的指针作为参数。&move的值是指向数组的指针,而不是指向字符的指针。以这种方式调用函数:
scanf("%s", move);https://stackoverflow.com/questions/8857161
复制相似问题