Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CS50 pset3和x2014;十五局_C_Sorting_Cs50_Linear Search - Fatal编程技术网

CS50 pset3和x2014;十五局

CS50 pset3和x2014;十五局,c,sorting,cs50,linear-search,C,Sorting,Cs50,Linear Search,下面给出的代码是CS50问题集3的答案。 请看一下函数:init, 绘图, 移动, won 并提出一些我可以做的改进。 事实上,我得到了一些错误,我不明白 /** * fifteen.c * * Implements Game of Fifteen (generalized to d x d). * * Usage: fifteen d * * whereby the board's dimensions are to be d x d, * where d must be in

下面给出的代码是CS50问题集3的答案。 请看一下函数:
init
绘图
移动
won
并提出一些我可以做的改进。 事实上,我得到了一些错误,我不明白

/**
 * fifteen.c
 *
 * Implements Game of Fifteen (generalized to d x d).
 *
 * Usage: fifteen d
 *
 * whereby the board's dimensions are to be d x d,
 * where d must be in [DIM_MIN,DIM_MAX]
 *
 * Note that usleep is obsolete, but it offers more granularity than
 * sleep and is simpler to use than nanosleep; `man usleep` for more.
 */

#define _XOPEN_SOURCE 500

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];

// dimensions
int d;

// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: fifteen d\n");
        return 1;
    }

    // ensure valid dimensions
    d = atoi(argv[1]);
    if (d < DIM_MIN || d > DIM_MAX)
    {
        printf("Board must be between %i x %i and %i x %i, inclusive.\n",
             DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
        return 2;
    }

     // open log
    FILE *file = fopen("log.txt", "w");
    if (file == NULL)
    {
         return 3;
    }

    // greet user with instructions
    greet();

    // initialize the board
    init();

    // accept moves until game is won
    while (true)
    {
        // clear the screen
        clear();

        // draw the current state of the board
        draw();

        // log the current state of the board (for testing)
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                fprintf(file, "%i", board[i][j]);
                if (j < d - 1)
                {
                    fprintf(file, "|");
                }
            }
            fprintf(file, "\n");
        }
        fflush(file);

        // check for win
        if (won())
        {
            printf("ftw!\n");
            break;
        }

        // prompt for move
        printf("Tile to move: ");
        int tile = get_int();

       // quit if user inputs 0 (for testing)
       if (tile == 0)
        {
           break;
        }

        // log move (for testing)
        fprintf(file, "%i\n", tile);
        fflush(file);

         // move if possible, else report illegality
        if (!move(tile))
        {
            printf("\nIllegal move.\n");
            usleep(500000);
        }

         // sleep thread for animation's sake
         usleep(500000);
     }

     // close log
     fclose(file);

     // success
     return 0;
}

/**
  * Clears screen using ANSI escape sequences.
 */
void clear(void)
{
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);
}

/**
 * Greets player.
 */
void greet(void)
{
     clear();
     printf("WELCOME TO GAME OF FIFTEEN\n");
     usleep(2000000);
}

/**
  * Initializes the game's board with tiles numbered 1 through d*d - 1
  * (i.e., fills 2D array with values but does not actually print them).  
  */
  void init(void)
  {
     int board[4][4];
     int d;
     do
     {
        printf("enter the size of the board\n");
        scanf("%i",&d);  
     }while(d <= 4);
    printf("enter the values in the grid\n");
    for(int i=0;i<d;i++)
     {
         for(int j=0;j<d;j++)
          {
            scanf("%i\n",&board[i][j]); // set tile's value
          }
    }
      if(d%2 == 0)
     {
         int temp;
         temp = board[3][1];
         board[3][1] = board[3][2];
         board[3][2] = temp;
     }


}

 /**
  * Prints the board in its current state.
  */
 void draw(void)
 {
    int d;

    for(int i=0;i<d-1;i++)
     {
         for(int j=0;j<d;j++)
         {
             printf("%2i",board[i][j]);
         }
         printf("\n");
     }
     do
     {
         for(int j=0;j<d-1;j++)
         {
             printf("%2i",board[int i][int j]);
          }   
      } while (int i=d-1);
      char board[d-1][d-1] = ' ';
      printf("%c \n", board[d-1][d-1]);
 }

  /**
   * If tile borders empty space, moves tile and returns true, else
    * returns false. 
    */
 bool move(int tile)
 {
      int d;
     for(int i=0;i<d;i++)
     {
         for(int j=0;j<d;j++)
         {
            if(board[i][j] == tile)
             {
                 return board[i][j];
             }
         }
     }  
    int temp;
    temp = board[2][2];
    board[2][2] = board[int i][int j];
    board[int i][int j] = temp; 
 }

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    // TODO

    for(i=0;i<d;i++)
     {
         for(j=0;j<d;j++)
         {
            if(a[i][j] < a[i+1][j+1])
            {
                return true;
                break;
             }
             else{
                 return false;
                 break;
                 }
         }
     }
 } 
/**
*十五
*
*实现十五人游戏(推广到d x d)。
*
*用法:十五天
*
*板的尺寸为d x d,
*其中d必须在[最小尺寸,最大尺寸]
*
*请注意,usleep已经过时,但它提供的粒度比
*睡眠和使用比纳米睡眠更简单`我想再多睡一会儿。
*/
#定义_XOPEN_源500
#包括
#包括
#包括
#包括
//常数
#定义尺寸最小值3
#定义尺寸_MAX 9
//董事会
内板[DIM_MAX][DIM_MAX];
//尺寸
int d;
//原型
无效清除(无效);
无效(void);
void init(void);
作废提款(作废);
bool移动(int-tile);
布尔元(无效);
int main(int argc,字符串argv[])
{
//确保正确使用
如果(argc!=2)
{
printf(“用法:十五天”);
返回1;
}
//确保尺寸有效
d=atoi(argv[1]);
如果(dDIM_MAX)
{
printf(“电路板必须介于%i x%i和%i x%i之间,包括这两个值。\n”,
最小尺寸、最小尺寸、最大尺寸、最大尺寸);
返回2;
}
//原木
FILE*FILE=fopen(“log.txt”,“w”);
if(file==NULL)
{
返回3;
}
//用说明问候用户
问候();
//初始化电路板
init();
//接受移动直到游戏获胜
while(true)
{
//清除屏幕
清除();
//绘制电路板的当前状态
draw();
//记录电路板的当前状态(用于测试)
对于(int i=0;iinit()
函数中(d时,您初始化了
board[4][4]
,但是已经使用了所需的自定义大小的板,您需要根据d保持索引,而不使用4或任何其他固定的板大小

另外,在
init()
函数中,您不需要手动输入磁贴的数字。尝试使用变量(整数)并将其分配给所需的磁贴,并在分配给下一个磁贴之前递增该变量。 因此,检查d是偶数还是奇数的条件将发生变化

draw()
函数似乎很好

move()
函数有一些问题。
return board[i][j];
无效,因为该函数具有返回类型bool。逻辑似乎不正确。如果还没有,请检查演练

won()中
函数,不需要在返回后使用
break;
语句。此外,如果仔细检查在if条件中写入的条件,它不会检查每个变量是否为同一行中的下一个或下一行中的下一个,而是检查每个变量的对角下一个变量,即使它不存在


我建议您重新观看讲座视频、短片和演练,以便更好地理解代码和C的一般含义。

关于您的开头段落,堆栈溢出不是唯一的主题问题,因此下面是错误的含义。您应该编辑文章及其标题以反映这一点。请发布a并给出答案完全错误。而且,不是每个人都知道“CS50”是什么,但说实话,它与问题并不相关,所以我建议删除对它的引用。