【问题标题】:ArrayGame. Why doesn't it update the print?阵列游戏。为什么不更新打印?
【发布时间】:2014-07-03 08:25:11
【问题描述】:

这是我的 C 编程作业。我们需要构建一个使用数组的简单游戏。我们的游戏就像流行的扫雷游戏。首先,我们初始化 20*50 的数组区域。然后我们在地图中随机放置一些炸弹。在游戏中,玩家需要从起点走到终点才能赢得比赛。当玩家移动时,移动会使数组隐藏,以便用户知道他从哪里开始。但是,就我而言,系统不会在玩家移动后更新并使数组为空。任何人都可以帮我处理我的's'代码吗?怎么了?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define iMAX 20
#define jMAX 50
char array[20][50];
int i; //row
int j; //column
int z; //bomb
int n; //steps counter
int o; //x
int p; //y
o = 0;
p = 0;
int level;
int bomb;
char move;
int steps;

int main() {

printf("Welcome to the BombArray Game!\n");
printf("\nLevel 1 Begineer     : 50 bombs\nLevel 2 Intermediate : 100 bombs\nLevel 3 Advance      : 200 bombs\n");
printf("\nI want to challenge level ");
scanf_s("%d", &level);
printf("\n");

srand(time(NULL));

for (i = 0; i < 20; i++) {
    for (j = 0; j < 50; j++) {
        array[i][j] = '*';
    }
}

array[0][0] = 'S';
array[19][49] = 'E';

if (level == 1) {
    bomb = 50;
}

else if (level == 2) {
    bomb = 100;
}

else if (level == 3) {
    bomb = 200;
}

for (z = 0; z < bomb; z++) {
    i = rand() % 20;
    j = rand() % 50;
    array[i][j] = '1';
}

do {
system("cls");

for (i = 0; i < iMAX; i++) {
    for (j = 0; j < jMAX; j++) {
        if (array[i][j] == 'S') {
            printf("S");
        }
        else if (array[i][j] == '*') {
            printf("*");
        }
        else if (array[i][j] == '1') {
            printf("*");
        }
        else if (array[i][j] == 'E') {
            printf("E");
        }
        else if (array[i][j] == '2') {
            printf(" ");
        }
    }
    printf("\n");
}

printf("\n\nMoving direction (w:up s:down a:left d:right  e:exit): ");
scanf_s(" %c", &move);
printf("Steps? ");
scanf_s("%d", &steps);

if (move == 's') {
    for (n = 0; n < steps; n++) {
        i = o;
        j = p;
        i++;
        array[i][j] = '2';
        o = i;
        p = j;
    }
}


} while (array[19][49] != 2);

return 0;

}

【问题讨论】:

  • 为什么要用这么多全局变量?
  • 让它们在本地解决我的问题吗?
  • 你可以试试,但我不这么认为。 o = 0;p = 0; 在 main 之外给我一个编译器错误。
  • 我将它们设为本地,但问题仍然存在。
  • 我没有收到任何编译器错误..

标签: c arrays logic printf


【解决方案1】:
if (move == 's') {
  array[o][p] = '2';  
  for (n = 0; n < steps; n++) {   
    i = o;
    j = p;
    i++;
    array[i][j] = '2';
    o = i;
    p = j;
  }
  array[o][p] = 'S';
}

移动的时候要把Start位置的S删掉,写在end位置

一些额外的东西:你不需要那么多变量。您可以删除ij(或op)。 如果您在关卡中输入 1-3 以外的值,您将获得未定义数量的炸弹(如果您将 bomb 变量声明为局部变量),因此您应该使用默认情况。

你永远不会看你是否击中了炸弹,你只是覆盖array[i][j]而不证明是否有炸弹。

更好:

if (move == 's') {
  array[i][j] = '2';  
  for (n = 0; n < steps; n++) {
    i++;
    if (array[i][j] == '1') {
      printf("bomb\n");
      return 0;
    }
    array[i][j] = '2';
  }
  array[i][j] = 'S';
}

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2021-01-12
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多