通过融合单元格的值来生成迷宫

时间:2018-01-01 01:26:52

标签: c

我必须为学校项目生成一个迷宫,但是我陷入了一个循环,我不明白为什么,而且我的IDE由于某种原因忽略了我的断点......

这是我的代码:

#include <math.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> 



typedef struct _maze_cell {
    int wallAbove, wallLeft, cellValue;
} MazeCell;

/* define maze structure */
typedef struct _maze_t {
    /* maze size = m lines * n columns */
    int row, column;
    /* array of cells */
    MazeCell* *array;
} Maze;



MazeCell* initMazeCell(){
    MazeCell* cell = malloc(sizeof(MazeCell));
    cell->wallAbove = 1;
    cell->wallLeft = 1;
    cell->cellValue = 0;
    return cell;

}

int rand_a_b(int a, int b){    

    return rand()%(b-a) +a;

}

Maze* initMaze(int m, int n)
 {
  Maze* oMaze= malloc(sizeof(Maze));
  int nbCells=m*n;
  printf("%d \n", nbCells);
  oMaze->array = malloc(nbCells*sizeof(MazeCell));

   int counter=1;
   int i;
   for(i=0;i<m*n;i++)
    {
         if(i%m==0)
            {
                printf("\n");
            }
        oMaze->array[i]= initMazeCell();
        oMaze->array[i]->cellValue=counter++;


        printf("%4d", oMaze->array[i]->cellValue);
    }

      printf("\n");
    //------------------------loop debut
    int done=0;
    while(done==0)
    {
        done=1;
        int sizeofTab=m*n-1;
        int nbAl=rand_a_b( 0, sizeofTab);
        int wallChoosen=rand_a_b( 1, 2);

        if(wallChoosen==1)
        {
            if(nbAl>n)
          {
              int b=nbAl-n;
              int oldValue=oMaze->array[b]->cellValue;
              int newValue=oMaze->array[nbAl]->cellValue;

              if(oldValue!=newValue)
                {
                    oMaze->array[nbAl]->wallAbove=0;
                    for(i=0;i<m*n;i++)
                    {
                        if(oMaze->array[i]->cellValue==oldValue)
                        {
                            //printf("old: %d",oMaze->array[i]->cellValue);
                            oMaze->array[i]->cellValue=newValue;
                            // printf("new: %d",oMaze->array[i]->cellValue);
                        }

                    }
                }
            }
        }

    if(wallChoosen==2)
     {
        if(nbAl>0)
         {
            int b=nbAl-1;
            int oldValue=oMaze->array[b]->cellValue;
            int newValue=oMaze->array[nbAl]->cellValue;


            if(oldValue!=newValue)
            {
                oMaze->array[nbAl]->wallLeft=0;
                for(i=0;i<m*n;i++)
                {
                    if(oMaze->array[i]->cellValue==oldValue)
                    {
                        oMaze->array[i]->cellValue=newValue;
                    }

                }
            //oMaze->array[b]->cellValue=oMaze->array[nbAl]->cellValue;

            }
        }
    }
      int a;
      for(i=0;i<m*n;i++)
        {
            if(i==0)
            {
                a=oMaze->array[i]->cellValue;
            }
            if(i!=0 && oMaze->array[i]->cellValue!=a)
            {
                done=0;
                printf("%4d", oMaze->array[i]->cellValue);
            }

        }

    }
    //--------------------------------loop end
    printf("\n The end!!");
    int y;
    for(y=0;y<m*n;y++)
    {
         if(y%m==0)
            {
                printf("\n");
            }
            printf("%4d", oMaze->array[y]->cellValue);
    }
    return oMaze;
 }

int main()
{
    srand(time(NULL));
    Maze* myMaze=initMaze(5,5);
    return 0;
}

老师告诉我们使用这种方法:maze generation(对不起用法语,但图片有助于理解我在我的代码中尝试做的事情)。

有人可以帮我理解我做错了吗?

我添加了一个计数器并在转了200圈后停止了循环,我明白了:

result

这表明它一直有效,我不知道它为什么直到最后都没有工作......

1 个答案:

答案 0 :(得分:1)

我发现我做错了什么:

library(shiny)
library(data.table)
library(DT)

tdata <- data.table(fruit = c(rep("Apple",4),rep( "Ban",4)), 
                    bug1 = c(rep(c("+","+", "-","-"),2)),
                    bug2 = c(rep(c("+","-"),4)),
                    value = c(rep(c(0.25),4), 0.6,0.4,0,0))

ui <- (fluidPage(tagList(
  sidebarLayout(
    sidebarPanel(uiOutput("file_input")),
    mainPanel(dataTableOutput('fruit_table')) 
  ))))

server <- function(input, output) {

  fileData <- reactive(
    return(tdata)
  )

  colname_list <- reactive( 
    colnames(fileData())
  )


  output$file_input <- renderUI ({
    if(is.null(fileData())){
      return()
    }else{
      tagList(
        lapply(1:(length(fileData())-1), function(i){
          choice_list = unique(fileData()[,get(colnames(fileData()[,i, with = FALSE]))])
          checkboxGroupInput(inputId = colnames(fileData()[,i, with = FALSE]),
                             label = colnames(fileData()[,i, with = FALSE]),
                             choices = choice_list,
                             inline = TRUE,
                             selected = fileData()[1, i, with = FALSE])
        })
      )
    }
  })



   fruitFilter <- reactive({

       i1 <- head(seq_along(fileData()), -1)
       l1 <- vector('list', length(i1))
       for(i in i1){

         l1[[i]] <- fileData()[[colname_list()[i]]] %in% input[[colname_list()[i]]]

       }

       Reduce(`&`, l1)
    })

  output$fruit_table <- renderDataTable({
    datatable(fileData()[fruitFilter()])
  })

}

shinyApp(ui = ui, server = server)

我认为它是从1到2,包括2,但不包括2,所以wallChoosen总是等于1.

我将其替换为:

`int wallChoosen=rand_a_b( 1, 2);`