Ir ao conteúdo
  • Cadastre-se

Conixs

Membro Júnior
  • Posts

    3
  • Cadastrado em

  • Última visita

Reputação

4
  1. Boa tarde, @giu_d! Eu estava tentando gerar um random entre 0 e 1, mas pelo que vi não funciona mesmo, corrigi assim: 0 + (rand() % (1 - 0 + 1)) Agora preciso repensar a lógica da função addInnerWalls.
  2. @devair1010, no seu código quando você chama a função addInnerWalls o último parametro está errado: addInnerWalls(camara, 1, 1, sizeof(camara)/sizeof(camara[0]) - 1, sizeof(camara[0])/sizeof(camara[0]) - 1); Deveria ser: addInnerWalls(camara, 1, 1, sizeof(camara)/sizeof(camara[0]) - 1, sizeof(camara[0])/sizeof(camara[0][0]) - 1); Com essa mudança, o código roda, mas não imprimi nada! Obrigado.
  3. Boa noite, estava tentando implementar um programa que seja capaz de gerar de forma recursiva um labirinto, como descrito pelo Recursive Division Method. No entanto, quando chamo a recursividade está me dando FLOATING POINT EXCEPTION. Poderiam me dar uma dica nesse problema? Aqui está o meu código: #include <stdio.h> #define HORIZONTAL 1 #define VERTICAL 2 const int ROWS = 5; const int COLS = 5; int main() { int chamber[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { chamber[i][j] = 0; } } addOuterWalls(chamber, sizeof(chamber)/sizeof(chamber[0]), sizeof(chamber[0])/sizeof(chamber[0][0])); addEntrance(chamber, sizeof(chamber)/sizeof(chamber[0])); addExit(chamber, sizeof(chamber)/sizeof(chamber[0])); addInnerWalls(chamber, 1, 1, sizeof(chamber)/sizeof(chamber[0]) - 1, sizeof(chamber[0])/sizeof(chamber[0][0]) - 1); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { printf("%d ", chamber[i][j]); } printf("\n"); } return 0; } void addOuterWalls(int chamber[ROWS][COLS], int rows, int cols) { for (int i = 0; i < rows; i++) { if (i == 0 || i == (rows - 1)) { for (int j = 0; j < cols; j++) { chamber[i][j] = 1; } } else { chamber[i][0] = 1; chamber[i][cols - 1] = 1; } } } void addEntrance(int chamber[ROWS][COLS], int rows) { int random = 0 + (rand() % ((rows - 1) - 0) + 1); chamber[0][random] = 2; } void addExit(int chamber[ROWS][COLS], int rows) { int random = 0 + (rand() % ((rows - 1) - 0) + 1); chamber[rows - 1][random] = 2; } void addInnerWalls(int chamber[ROWS][COLS], int startRow, int startCol, int endRow, int endCol) { int orientation, randR, randC, i, j, randPR, randPC; if (endRow < 2 || endCol < 2) { return; } if (endCol < endRow) { orientation = HORIZONTAL; } else if (endCol > endRow) { orientation = VERTICAL; } else { orientation = 0 + (rand() % (1 - 0) + 1) == 0 ? HORIZONTAL : VERTICAL; } randR = startRow + (rand() % ((endRow - 1) - startRow) + 1); randC = startCol + (rand() % ((endCol - 1) - startCol) + 1); if (orientation == HORIZONTAL) { for(i = startRow; i < endRow; i++) { if(i == randR) { randPR = startRow + (rand() % ((endRow - 1) - startRow) + 1); for(j = startCol; j < endCol; j++) { if(j == randPR) { chamber[i][j] = 8; } else { chamber[i][j] = 1; } } } } addInnerWalls(chamber, startRow, startCol, randR - 1, endCol); addInnerWalls(chamber, randR + 1, startCol, endRow, endCol); } else { for(j = startCol; j < endCol; j++) { if(j == randC) { randPC = startCol + (rand() % ((endCol - 1) - startCol) + 1); for(i = startRow; i < endRow; i++) { if(i == randPC) { chamber[i][j] = 8; } else { chamber[i][j] = 1; } } } } addInnerWalls(chamber, startRow, startCol, randC - 1, randC - 1); addInnerWalls(chamber, randC + 1, randC + 1, endRow, endCol); } } Grato!

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...