【问题标题】:Nested if statements, while also removing options嵌套 if 语句,同时删除选项
【发布时间】:2016-10-17 11:57:41
【问题描述】:

我目前正在尝试在 C++ 中执行“Wa-Tor 模拟”作业。它还处于课程的早期,所以不是作业的图形部分,而是鱼和鲨鱼的运动和功能。如果您不了解 Wa-Tor 模拟,我认为这对我的问题并不重要。

我想要完成的是:

我有一个由空、鱼和鲨鱼组成的二维数组。

鲨鱼随机向上、向下、向左或向右移动。如果鲨鱼在最左边的点移动到数组的左边,它将出现在右侧(以及上、下和右)。如果鲨鱼移动到没有鱼的位置,它将超越该位置,但如果遇到另一条鲨鱼,它会尝试移动到相对于其第一个位置的另一个位置。

如果鲨鱼被其他鲨鱼包围,它不会移动。

到目前为止,我所做的是创建一个临时数组来移动鲨鱼,以便在运行 for 循环时,鲨鱼不会移动两次。

因此我的代码如下所示:

 for (int i = 0; i < MAX_X; i++) {
    for (int j = 0; j < MAX_Y; j++) {

        if (myOcean[i][j] == SHARK) {
            int randNum = (rand() % 4 + 1);

            switch (randNum) {
            case 1:
 //This is where I don't know how to properly code to make my function work, so bare with me while I make fake code..
 //if(myOcean[i+1][j] == SHARK){ GO BACK AND FIND A NEW RANDOM NUMBER, BUT THIS TIME REMOVE CASE 1. THUS NOT ALLOWING AN ATTEMPT TO MOVE [i+1]
                if ((i + 1) == MAX_X) { tempArray[0][j] = SHARK; }
                else { tempArray[i + 1][j] = SHARK; }
                tempArray[i][j] = EMPTY;
                break;
            case 2:
 // Same as above. Remove Case 2 if it doesnt work.
                if ((i - 1) < 0) { tempArray[MAX_X - 1][j] = SHARK; }
                else { tempArray[i - 1][j] = SHARK; }
                tempArray[i][j] = EMPTY;
                break;

            case 3:
 // Same as above. Remove Case 3 if it doesnt work.
                if ((j + 1) == MAX_Y) { tempArray[i][0] = SHARK; }
                else { tempArray[i][j + 1] = SHARK; }
                tempArray[i][j] = EMPTY;
                break;
            case 4:
 // Same as above. Remove Case 4 if it doesnt work.
                if ((j - 1) < 0) { tempArray[i][MAX_Y - 1] = SHARK; }
                else { tempArray[i][j - 1] = SHARK; }
                tempArray[i][j] = EMPTY;
                break;
            default:
  // No cases worked, thus the shark remains still.
                tempArray[i][j] = myOcean[i][j];

            }
        }
    }
}

这样我的问题就结束了。我不知道如何返回并删除特定案例。希望我能很好地解释我的问题,因为我对编码还是很陌生。

【问题讨论】:

    标签: c++ arrays if-statement nested switch-statement


    【解决方案1】:

    这样设计您的代码。有一个额外的 remove_me 变量来跟踪要删除的案例。并继续生成随机值,直到找到另一个有用的值。然后将 remove_me 变量设为空。同样,当您遇到此类情况时,请将此变量指定为 case no。此外,您必须将 j 减 1 才能在循环的下一次迭代中进行相同的运行。 第一种情况的代码如下所示,其余您可以使用相同的逻辑进行设计:

    int remove_me = 0
    for (int i = 0; i < MAX_X; i++) {
        for (int j = 0; j < MAX_Y; j++) {
    
            if (myOcean[i][j] == SHARK) {
                int randNum = (rand() % 4 + 1);
                while (randNum == remove_me)
                    randNum = (rand() % 4 + 1);
                remove_me = 0
                switch (randNum) {
                    case 1:
                        if(myOcean[i+1][j] == SHARK){
                            remove_me = 1;
                            j--;
                        }
                        if ((i + 1) == MAX_X){
                            tempArray[0][j] = SHARK;
                        }
                        else{
                            tempArray[i + 1][j] = SHARK;
                        }
                        tempArray[i][j] = EMPTY;
                        break;
    

    PS : 请考虑代码的缩进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多