2015-07-12 1 views
-2

Je travaille donc sur mes devoirs multi-dimensionnels pour ma classe C++ et honnêtement, je suis complètement perdu sur la façon dont cela fonctionne. C'est la marche aléatoire et j'ai trouvé plein d'exemples mais je ne comprends toujours pas. Je comprends ce que le programme est censé produire mais c'est la façon dont cela m'a bloqué, logiquement je ne comprends tout simplement pas. Si quelqu'un peut m'expliquer ce que fait le code et comment je l'apprécierais vraiment. Je peux le coder à partir de leur c'est juste essayer de le comprendre qui me donne des coups de pied.Multidimensional Arrays for Random Walk Program

Exigences et constantes pour l'affectation:

  • Bibliothèques: iostream, cstdlib (pour rand stand &), ctime (pour le temps)
  • const int SIZE = 10; (10 ne peut jamais apparaître dans le programme doit appeler la variable)
  • typedef char Grille [TAILLE] [TAILLE];
  • Fonction Prototypes qui doivent être utilisés comme illustré, mais peuvent ajouter des fonctions.

La maquette que nous donnons du programme principal ressemblait à ceci:

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
using namespace std; 

/*** Function Prototypes ***/ 
bool can_move_up(int i, int j, Grid walk); 
bool can_move_up(int i, int j, Grid walk); 
bool can_move_up(int i, int j, Grid walk); 
bool can_move_up(int i, int j, Grid walk); 
void init_array(Grid walk); 
void generate_random_walk(Grid walk); 
void print_array(Grid walk); 
/***************************/ 



int main(void) 
{ 
const int SIZE = 10; 
typedef char Grid[SIZE][SIZE]; 

Grid walk; // the grid in which the random walk occurs 

srand(static_cast<unsigned>(time(NULL))); 

init_array(walk); 
generate_random_walk(walk); 
print_array(walk); 

return 0; 

} 

Voici le code que je suis venu avec jusqu'à présent. Je n'ai pas réussi à le faire courir mais je pense que je suis sur la bonne voie. Je suis juste confus sur l'endroit où aller à partir d'ici logiquement. Je souligne que je veux COMPRENDRE LOGICALLY comment cela fonctionne et ne pas avoir quelqu'un le faire pour moi. Je ne suis pas CS ou CE mais je trouve ça intéressant.

(J'utilise des classes et ai séparé en fichiers .cpp & .hpp mais j'ai placé la partie de classe au début du code pour que vous voyiez tous comment mes classes sont configurées et leurs fonctions sont définies sur travail.)

Merci pour l'aide!

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
using namespace std; 


// References these two variables to the other classes through public domain with in class 
class BaseClass 
{ 
public: 
    const static int SIZE = 10; 
    typedef char Grid[SIZE][SIZE]; 
private: 

}; 

// Contains the initialization, generation, and printing functions for the program 
class RandomWalkClass : public BaseClass 
{ 
public: 
    // Initializes the Grid filling all 100 spaces with '.' and starting poin "0,0" with 'A' 
    void init_array(Grid walk) 
    { 
     for (int i = 0; i < SIZE; i++) 
     { 
      for (int j = 0; j < SIZE; j++) 
      { 
       Grid[i][j] = '.'; // Xcode gives me error for '=' "Expected unqualified-id" 
       Grid[0][0] = 'A'; // Xcode gives me error for '=' "Expected unqualified-id" 
      } 
     } 
    } 
    // I am honestly not sure what to do with this funciton or what should be included in it's body 
    void generate_random_walk(Grid walk) 
    { 

    } 
    // Will print the random walk to the grid 
    void print_array(Grid walk) 
    { 
     for (int i = 0; i < SIZE; i++) 
     { 
      for (int j = 0; j < SIZE; j++) 
      { 
       cout << Grid[i][j]; // Xcode error "Unexpected type name 'Grid'" 
      } 
      cout << endl; 
     } 
    } 
private: 

}; 

// Contains the move functions for the program 
class MoveDirectionClass : public BaseClass 
{ 
public: 
    bool can_move_up(int i, int j, Grid walk) 
    { 
     if (i > 0 && walk[i - 1][j] == '.') 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    bool can_move_down(int i, int j, Grid walk) 
    { 
     if (i < 9 && walk[i + 1][j] == '.') 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    bool can_move_left(int i, int j, Grid walk) 
    { 
     if (j > 0 && walk[i][j - 1] == '.') 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    bool can_move_right(int i, int j, Grid walk) 
    { 
     if (j < 9 && walk[i][j + 1] == '.') 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
private: 

}; 

#include "program6.hpp" 

int main() 
{ 
int i; 
int j; 
int walk; 
int letter; 
int move; 

// Unsure of where this should go and what it's purpose to the program is (we've never discussed srand in class) 
srand(static_cast<unsigned>(time(NULL))); 

// Calls initialization of program 
RandomWalkClass initialize; 
initialize.init_array(<#char (*walk)[10]#>); 

// randomly chooses 0,1,2,3 
move = rand() % 4; 

// Runs through alphabet with each move of the switch program 
for (letter = 1; letter < 26; letter++) 
{ 
    switch (move) 
    { 
     case 0: 
      MoveDirectionClass up; 
      up.can_move_up(<#int i#>, <#int j#>, <#char (*walk)[10]#>); 
      break; 
     case 1: 
      MoveDirectionClass down; 
      down.can_move_down(<#int i#>, <#int j#>, <#char (*walk)[10]#>); 
      break; 
     case 2: 
      MoveDirectionClass left; 
      left.can_move_left(<#int i#>, <#int j#>, <#char (*walk)[10]#>); 
      break; 
     case 3: 
      MoveDirectionClass right; 
      right.can_move_right(<#int i#>, <#int j#>, <#char (*walk)[10]#>); 
      break; 
     default: break; 
    } 

} 

// Calls the printing of the grid with the random walk 
RandomWalkClass generate; 
generate.print_array(<#char (*walk)[10]#>); 


return 0; 
} 
+1

'Grille [i] [j] = '.';' Est faux - vous avez défini 'Grid' comme un type, pas une variable. –

+0

Bienvenue dans Stack Overflow. Lorsque vous écrivez du code, vous devriez commencer par quelque chose de petit et simple qui fonctionne parfaitement, puis ajouter de la complexité un peu à la fois. * N'ajoutez jamais de code qui ne fonctionne pas. * Avez-vous essayé de coder une marche aléatoire unidimensionnelle? Si vous voulez vraiment de l'aide pour un problème spécifique, posez votre question sur ce problème; un peu de contexte ou de contexte est bon, mais ne postez pas tout ce que vous avez et posez une douzaine de questions spécifiques à ce sujet. – Beta

Répondre

0

Quand il est venu au nom du type inattendu que vous avez fait typedef Grid et ne l'a pas mis dans votre classe. Eh bien, juste pour passer d'une pièce à l'autre, si vous voulez des objets dans cette pièce, je ferais quelque chose comme ça.

#include <iostream> 
#include <string> 
using namespace std; 
struct Room{ 
//insert variables here like 
int object; 
} 
typedef struct Room room[5][5]; 
int main() 
{ 
string direction; 
int x; 
int y; 
room space; //This is the variable to access both the array and the struct 
space[0][0].object = 0; //This variable represents what room you start off in and the 0 could represent the type of object or the fact that there is no object. 
//Now create rooms [0][0] through [4][4] which is a 5 by 5 area. After that create this. 
cout << "Enter W to go north, S for South, D for East, and A for West." << endl; 
cin >> direction; 
if (direction == "W") 
y++; 
if (direction == "S") 
y--; 
if (direction == "D") 
x++; 
if (direction == "A") 
x--; 
//Then repeat this code and access the different rooms using space[x][y].object or whatever the variable you want to access from the struct 
} 

Veuillez me faire savoir si vous avez des questions ou des préoccupations. J'ai aussi un jeu avec un exemple de déplacement entre les chambres. Mon courriel est [email protected]

0

Au lieu de Grid[i][j] = '.' et Grid[i][j] = 'A', utilisez

walk[i][j] = '.'; 
walk[0][0] = 'A'; 
+0

Oh mec, je me sens vraiment stupide sur celui-là! Je savais que c'était une erreur simple que je viens de regarder et de trop penser déjà trop. Merci mec! –