PSET3游戏十五输出不正确 [英] PSET3 game of fifteen output incorrect

查看:90
本文介绍了PSET3游戏十五输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

To understand the code's goal, please go to:

<a href="http://cdn.cs50.net/2016/x/psets/3/pset3/pset3.html#the_game_begins">Problem Set 3: Game of Fifteen</a> 

My code doesn't show any error. My output, however, is wrong in the last part ( switching the 1 and 2 if the d dimension is even). The 1 and 2 won't switch.




<pre>Some example outputs:

<pre>~/workspace/pset3/fifteen/ $ ./fifteen 4
WELCOME TO GAME OF FIFTEEN
  15  14  13  12
  11  10   9   8
   7   6   5   4
   3   2   1   _
Tile to move: ^C

~/workspace/pset3/fifteen/ $ ./fifteen 3
WELCOME TO GAME OF FIFTEEN
   8   7   6
   5   4   3
   2   1   _
Tile to move: 





详细说明:



如果d - 维度是奇数,那么最后2个数字的顺序应该是:1 2.

如果d是偶数,最后2个数字的顺序应该是:2 1.



输出中的错误是当这两个数字以错误的顺序打印时d为偶数。在上面的示例中,我的输出为d为4,最后2个数字为2 1而不是1 2.





PS:我试过gdb。我还没有得出任何明确的错误(通过断点和打印值)。



非常感谢,



我尝试过:





Detailed Expl.:

If d - dimension is odd, then the order of the last 2 numbers should be: 1 2.
If d is even, the order of the last 2 numbers should be: 2 1.

The mistake in my output is when those 2 numbers get printed in the wrong order with d as even. In the above example, my output for d as 4 gives the last 2 numbers as 2 1 instead of 1 2.


PS: I tried gdb. I couldn't conclude any clear error yet (by breakpoints and printing values along).

Thanks a lot,

What I have tried:

void init(void)
{
    int c = 1;
    for (int a = 0; a < d; a++)
    {
        for (int b = 0; b < d; b++)
        {
            
            board[a][b] = d*d - c;
            c++;
            if (c == d*d -2)
            {
                if (d % 2 == 0)
                {
                    board[d-1][d-3] = 1;
                    board[d-1][d-2] = 2;
                    board[d-1][d-1] = 0;
                }
                
                else if (d % 2 != 0)
                {
                    board[d-1][d-3] = 2;
                    board[d-1][d-2] = 1;
                    board[d-1][d-1] = 0;
                }
                
             }
            
            
        }
    }
    
     
     
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
   for (int e = 0; e < d; e++)
   {
       for (int f = 0; f < d; f++)
       {
           if (!( ( e == d-1) && ( f == d-1) ))
             {
                printf("%c %2d",' ', board[e][f]);
              }

            else
               {
                   printf("%2s","   _");
               }
            
       }
       printf("\n");
   }
    
}

推荐答案

./ fifteen 4
欢迎来到游戏FIFTEEN
15 14 13 12
11 10 9 8
7 6 5 4
3 2 1 _
移动瓷砖:^ C

~ / workspace / pset3 / fifteen /
./fifteen 4 WELCOME TO GAME OF FIFTEEN 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 _ Tile to move: ^C ~/workspace/pset3/fifteen/


./ fifteen 3
欢迎来到FIFTEEN游戏
8 7 6
5 4 3
2 1 _
移动瓷砖:
./fifteen 3 WELCOME TO GAME OF FIFTEEN 8 7 6 5 4 3 2 1 _ Tile to move:





详细说明:



如果d - 维度为奇数,则最后2个数字的顺序应为:1。

如果d为偶数,则最后2个数字的顺序应为:2 1.



输出中的错误是当这两个数字以错误的顺序打印时d为偶数。在上面的示例中,我的输出为d为4,最后2个数字为2 1而不是1 2.





PS:我试过gdb。我还没有得出任何明确的错误(通过断点和打印值)。



非常感谢,



我尝试过:





Detailed Expl.:

If d - dimension is odd, then the order of the last 2 numbers should be: 1 2.
If d is even, the order of the last 2 numbers should be: 2 1.

The mistake in my output is when those 2 numbers get printed in the wrong order with d as even. In the above example, my output for d as 4 gives the last 2 numbers as 2 1 instead of 1 2.


PS: I tried gdb. I couldn't conclude any clear error yet (by breakpoints and printing values along).

Thanks a lot,

What I have tried:

void init(void)
{
    int c = 1;
    for (int a = 0; a < d; a++)
    {
        for (int b = 0; b < d; b++)
        {
            
            board[a][b] = d*d - c;
            c++;
            if (c == d*d -2)
            {
                if (d % 2 == 0)
                {
                    board[d-1][d-3] = 1;
                    board[d-1][d-2] = 2;
                    board[d-1][d-1] = 0;
                }
                
                else if (d % 2 != 0)
                {
                    board[d-1][d-3] = 2;
                    board[d-1][d-2] = 1;
                    board[d-1][d-1] = 0;
                }
                
             }
            
            
        }
    }
    
     
     
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
   for (int e = 0; e < d; e++)
   {
       for (int f = 0; f < d; f++)
       {
           if (!( ( e == d-1) && ( f == d-1) ))
             {
                printf("%c %2d",' ', board[e][f]);
              }

            else
               {
                   printf("%2s","   _");
               }
            
       }
       printf("\n");
   }
    
}


Quote:

我的代码没有显示任何错误。然而,我的输出在最后一部分是错误的

My code doesn't show any error. My output, however, is wrong in the last part



你真的需要注意并理解一些事情。

- 当编译器发现错误时,它只是意味着你的程序不符合语言语法并且有一些拼写错误。

- 没有编译器错误,并不意味着程序正在做它应该做的事情。 猫在飞是正确的英语,但也是胡说八道。




You really need to pay attention and understand a few things.
- When the compiler find errors, it just means that your program do not conform to the language syntax and have some "spelling" errors.
- No compiler error, does not imply that the program is doing what it is supposed to do. "The cat is flying" is correct English, but also nonsense.

引用:

我试过gdb。我无法得出任何明确的错误

I tried gdb. I couldn't conclude any clear error yet



调试器在程序中找不到错误,只是告诉你程序正在做什么,你的任务是与什么比较应该做并发现不匹配。


The debugger don't find errors in your program, it just show you what the program is doing, it is your task to compare with what is should do and spot mismatches.


这篇关于PSET3游戏十五输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆