【问题标题】:Minesweeper Code C++扫雷代码 C++
【发布时间】:2015-12-11 14:06:19
【问题描述】:

我在编写扫雷器代码时遇到问题,当您单击空白区域时,它会向外级联并产生一圈数字。我为数字设置了一个数组,为炸弹设置了一个数组。在 numbers 数组中,零表示那里什么都没有。我首先检查单击的点是否是炸弹,如果不是则是否有数字,然后 else 语句必须导致数字的级联和环。基本上,我在单击的框上、下、左和右进行四个 while 循环,直到找到一个数字。从那里我有八个 for 循环,沿着每条线以 x 和 y 的递增和递减顺序进行,直到找到一个数字。一旦找到一个数字,我就会尝试返回原点,但是通过检查我到达那里的路径的边界,将坐标值增加 1 并将坐标值减少 1。由于某种原因,我无法让我的代码正常工作。这是我的while循环的一个例子。 “checkx”和“checky”是数组的坐标。 "revealxposition" 和 "revealyposition" 是图形窗口中的坐标。

int i=0;

         while(i == 0 && checkx < length && checky < length)//right
         {
         if(numbers[checkx][checky] != 0)
         {
             revealxposition = (checkx*20) + 5;
             revealyposition = (checky*20) + 3;
             showNumber(revealxposition, revealyposition, numbers[checkx][checky]);
             xmax = checkx;
             i = 1;
         }
         else
         {
             if(checkx == (length - 1))
             {
                 i = 1;
                 xmax = length;
                 a = checkx*20;
                 b = checky*20;
                 c = a+20;
                 d = b+20;
                 line(a,b,c,d);
                 line(a,d,c,b);
             }
             else
             {
                 a = checkx*20;
                 b = checky*20;
                 c = a+20;
                 d = b+20;
                 line(a,b,c,d);
                 line(a,d,c,b);

                 checkx++;             
              }
         }
         }

这是我的一个 for 循环的示例:

 for(int xx = checkx; xx < xmax; xx++)
 {
         int z = 0;
         int xxx = xx;
         while(z == 0 && checky < length)
         {
         if(numbers[xx][checky] != 0)
         {
             revealxposition = (xx*20) + 5;
             revealyposition = (checky*20) + 3;

             int originalxposition = revealxposition;
             int originalyposition = revealyposition;

             showNumber(revealxposition, revealyposition, numbers[xx][checky]);

             //reveal diagonals and back
             while(bombs[xx][checky] != 1 && checky >= originaly)
             {
               counter++;
               revealxposition = originalxposition - 20;
               xx = xxx - 1;
                 showNumber(revealxposition, revealyposition, numbers[xx][checky]);
                 checky--;
                 revealyposition -= 20;
             }
             xx = xxx;
             checky = originaly;
             revealxposition = originalxposition;
             revealyposition = originalyposition;
             counter = 0;

             while(bombs[xx][checky] != 1 && checky >= originaly)
             {
               counter++;
               revealxposition = originalxposition + 20;
               xx = xxx + 1;
                 showNumber(revealxposition, revealyposition, numbers[xx][checky]);
                 checky--;
                 revealyposition -= 20;
             }
             xx = xxx;
             z = 1;
             counter = 0;
         }    
         else
         {
             a = xx*20;
             b = checky*20;
             c = a+20;
             d = b+20;
             line(a,b,c,d);
             line(a,d,c,b);
             checky++;
         }
         } 
         checky = originaly;
 }

【问题讨论】:

    标签: c++ arrays graphics minesweeper


    【解决方案1】:

    您的代码真的很难阅读,我不知道 xx 或 xxx 是什么意思或a=xx*20; 我做过一次扫雷,我很确定有一种更简单的方法来检查空白区域。 objectif 是使用递归。让我给你看一个例子。

    void checkNumber(x,y) {
      revealNumber(x,y); // write the number
     if(Number[x,y] == 0){ // if it is blank space, check all 8 square around
           if(x>0) 
               checkNumber(x-1,y); // check left 
           if(x<maxX)
                checkNumber(x+1,y); // check right
           // repeat for up,down and diag
            }
       } 
    

    这将为您提供一个循环,该循环将显示所有空格和最接近的数字。希望对你有帮助。

    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2021-06-10
      • 2014-08-27
      • 1970-01-01
      相关资源
      最近更新 更多