繁体   English   中英

ArrayGame。 为什么不更新打印?

[英]ArrayGame. Why doesn't it update the print?

这是我的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;

}

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';
}

移动时,必须在开始位置删除S并在结束位置写入S

其他一些事项:您不需要那么多的变量。 您可以删除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';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM