扫雷器代码C++

Minesweeper Code C++

本文关键字:C++ 代码 扫雷      更新时间:2023-10-16

我在为扫雷舰编写代码时遇到了问题,当你点击空格时,它会向外级联,并导致一个数字环。我有一个数字数组和一个炸弹数组。在数字数组中,零表示那里什么都没有。我首先检查点击的点是否是炸弹,如果不是,然后检查是否有数字,然后else语句必须导致数字的级联和环。基本上,我在点击框的上、下、左、右循环的同时浏览四个,直到找到一个数字。从那里开始,我有八个for循环,按照x和y的递增和递减顺序,沿着每一条线,直到找到一个数字。一旦找到一个数字,我就试图回到原点,但要检查到达那里的路径的边界,将坐标值增加一,将坐标数值减少一。由于某些原因,我无法使代码正常工作。下面是我while循环的一个例子。"checkx"answers"checky"是数组的坐标。"reveaxposition"answers"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;
 }

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

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

这将给你一个循环,显示所有空格和最接近的数字。希望它能帮助你。

)