简体   繁体   English

扫雷程序,意外 output

[英]Minesweeper Program, unexpected output

I was doing one problem(Minesweeper) from Steven Skiena(Programming Challenges).我正在做一个来自 Steven Skiena(编程挑战)的问题(扫雷)。 Where you have to output the number of mines adjacent to that square.在那里你必须 output 与那个广场相邻的地雷数量。

#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>m>>n;
char a[m][n];
for(int i=0;i<m;i++)
{
    for (int j = 0; j < n; j++)
    {
        cin>>a[i][j];
    }
}

for (int i = 0; i < m; i++)
{
    for (int j = 0; j < n; j++)
    {
        if(a[i][j]!='*')
        {
            int no=0;
            if(a[i-1][j-1]=='*')
            no++;
            else if(a[i][j-1]=='*')
            no++;
            else if(a[i+1][j-1]=='*')
            no++;
            else if(a[i+1][j]=='*')
            no++;
            else if(a[i+1][j+1]=='*')
            no++;
            else if(a[i][j+1]=='*')
            no++;
            else if(a[i-1][j+1]=='*')
            no++;
            else if(a[i-1][j]=='*')
            no++;
            a[i][j]=(char)no;
        }
    }

}


for(int i=0;i<m;i++)
{
    for (int j=0; j<n; j++)
    {
        cout<<a[i][j];
    }
    cout<<endl;
}


return 0;
}

When I try to run this program, the array where the mines isn't placed are blank.当我尝试运行这个程序时,没有放置地雷的数组是空白的。 Is it something related to the casting of integer to character?是不是跟integer选角色有关?

One mistake is this:一个错误是这样的:

a[i][j]=(char)no;

This is not the way you convert a one-digit integer into the char representation.这不是将一位数 integer 转换为char表示的方式。

The correct way is to add '0' to the integer:正确的方法是在 integer 上加'0'

a[i][j] = no + '0';

The reason why this works is that the character representation of the number is different than the actual digit value.这样做的原因是数字的字符表示与实际数字值不同。 For example, the ASCII collating sequence uses 48 as the character '0' , 49 for '1' , 50 for '2' , etc.例如,ASCII 整理序列使用 48 作为字符'0' ,49 作为'1' ,50 作为'2' ,等等。

Thus adding '0' , ie 48, will give you the character representation of the number.因此添加'0' ,即 48,将为您提供数字的字符表示。

You could change your program for example like this:你可以像这样改变你的程序:

I changed your array to an int-array and all mines are a -1 .我将您的数组更改为 int 数组,所有地雷都是-1 So you don't have to mix int-values and char-values in one array.因此,您不必在一个数组中混合使用 int 值和 char 值。 With this you have to handle this in the output with an if-condition.有了这个,你必须用 if 条件在 output 中处理这个。

#include<iostream>
using namespace std;

int main()
{
    int n,m;
    cin>>m>>n;
    char a[m][n];

    for(int i=0;i<m;i++)
    {
        for (int j = 0; j < n; j++)
        {
            char in;
            cin>>in;
            if(in == '*') {
                a[i][j] = -1;
            } else {
                a[i][j] = 0;
            }
        }
    }

    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if(a[i][j]!=-1)
            {
                int no=0;
                if(a[i-1][j-1]==-1)
                no++;
                else if(a[i][j-1]==-1)
                no++;
                else if(a[i+1][j-1]==-1)
                no++;
                else if(a[i+1][j]==-1)
                no++;
                else if(a[i+1][j+1]==-1)
                no++;
                else if(a[i][j+1]==-1)
                no++;
                else if(a[i-1][j+1]==-1)
                no++;
                else if(a[i-1][j]==-1)
                no++;
                a[i][j]=no;
            }
        }

    }


    for(int i=0;i<m;i++)
    {
        for (int j=0; j<n; j++)
        {
            if(a[i][j] == -1) {
                cout<<" * ";
            } else {
                cout<<" "<<a[i][j]<<" ";
            }
        }
        cout<<endl;
    }

    return 0;
}

Since it looks like people are giving men fish, i don't see a reason not to.既然看起来人们在给男人送鱼,我看不出有什么理由不这样做。 Change the line of how no starts for a nice solution computationally.在计算上改变一个很好的解决方案的 no 启动方式。

    char no='0';

I see a few separate issues here that, collectively, are leading to the issue you're running into.我在这里看到一些单独的问题,它们共同导致了您遇到的问题。

First, as pointed out above, once you've counted the number of mines adjacent to each mine, you're currently storing the result in a way that won't print the way you expect it to.首先,如上所述,一旦您计算了与每个地雷相邻的地雷数量,您当前存储结果的方式将不会按照您期望的方式打印。 The other answers here have suggested several different ways that you can do this.这里的其他答案建议了几种不同的方法可以做到这一点。

Second, your way of counting adjacent mines is (slightly) incorrect.其次,您计算相邻地雷的方法(稍微)不正确。 Let's look at this code:让我们看看这段代码:

if(a[i-1][j-1]==-1)
   no++;
else if(a[i][j-1]==-1)
   no++;
else if(a[i+1][j-1]==-1)
   no++;
else if(a[i+1][j]==-1)
   no++;
else if(a[i+1][j+1]==-1)
   no++;
else if(a[i][j+1]==-1)
   no++;
else if(a[i-1][j+1]==-1)
   no++;
else if(a[i-1][j]==-1)
   no++;

The pattern here you're using of if / else if / else if /... means that at most one of these branches will execute.您在此处使用的模式if / else if / else if /... 意味着最多将执行其中一个分支。 That is, as soon as this code finds a mine, it stops looking at the other locations around the cell for other mines.也就是说,一旦此代码找到地雷,它就会停止查看单元格周围的其他位置以查找其他地雷。 One way to fix this is to make this not a chain of if / else if , but just a regular chain of if s:解决此问题的一种方法是使它不是if / else if的链,而只是if的常规链:

if(a[i-1][j-1]==-1)
   no++;
if(a[i][j-1]==-1)
   no++;
if(a[i+1][j-1]==-1)
   no++;
if(a[i+1][j]==-1)
   no++;
if(a[i+1][j+1]==-1)
   no++;
if(a[i][j+1]==-1)
   no++;
if(a[i-1][j+1]==-1)
   no++;
if(a[i-1][j]==-1)
   no++;

You may even want to consider changing how you've structured this so that, instead of having a long list of if statements, you use a doubly-nested for loop to enumerate all positions one step away from you.您甚至可能想考虑更改您的结构,这样您就可以使用双重嵌套的 for 循环来枚举距离您一步之遥的所有位置,而不是使用一长串if语句。 that will condense the code and make it more clearly match the pattern of "for each adjacent location, if it's a mine, count it as such."这将压缩代码并使其更清楚地匹配“对于每个相邻位置,如果它是地雷,则将其算作地雷”的模式。

Hope this helps!希望这可以帮助!

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

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