Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

C++ Hexagonal Grid Number Cascading

$
0
0
Hey everyone, I could use some help with my partially completed function. Heres the part of the code I need help with.

  int numberCells(int illudiumY, int illudiumX, int r, int c) {
  Queue q;
  q.enqueue(map[illudiumY][illudiumX]);            //Enqueue location of Illudium Phosdex
  map[illudiumY][illudiumX] = 0;                   //Cell of IP = 0, Lava -2, all Other Squares -1
  
  while (!q.isEmpty()) {                           //While the queue is not empty
    q.dequeue();                                   //Dequeue a location
    
    for (int i=0;i<5;i++) {                        //For each neighbor of the location
        dC[i];                                     //Hexagonal grid movements for columns
        dR1[i];                                    //Even rows
        dR2[i];                                    //Odd rows
    if (map[r][c] == -1) {                         //If neighbors cell is -1
    map[r][c] = i+1;                               //Neighbors cell = Location's cell +1
    q.enqueue(i);                                  //Enqueue the neighbor
      }
    }
  }

  for (r=0; r<ROW; r++) {
  for (c=0; c<COL; c++)
        cout << map[r][c];
        cout << "\n";
}
}


And heres the values in the 3 arrays.

int dC[] = {0,1,1,0,-1,-1};
int dR1[] = {-1,0,1,1,1,0};
int dR2[] = {-1,-1,0,1,0,-1};


All this function is suppose to do is cascade numbers around its 6 neighbors on a hexagonal grid by +1 every time until it reaches the end of the grid. It starts at 0 and flows out from there. Here's a picture of what I mean.

http://i.imgur.com/chYI4.png

Now ill simply post the rest of my code as a reference.

Queue.cc

#include "queue.h"
#include <iostream>

using namespace std;

const int ROW = 22;
const int COL = 22;
int numberCells(int illudiumY, int illudiumX, int r, int c);

int map[ROW][COL];
int dC[] = {0,1,1,0,-1,-1};
int dR1[] = {-1,0,1,1,1,0};
int dR2[] = {-1,-1,0,1,0,-1};

int main(void) {

  int duckX, duckY, marvinX, marvinY, illudiumX, illudiumY, lavaX, lavaY;
  int r,c,nLava;
  bool visited = false;

  for (r=0; r<ROW; r++)
    for (c=0; c<COL; c++)
        map[r][c] = -1;

  for (r=0;r<ROW;r++)
    map[0][r] = map[r][0] = map[ROW-1][r] = map[r][ROW-1] = -2;

  cout << "Please enter Duck Dodger's Y coordinate: ";
  cin >> duckY;
  cout << "Please enter Duck Dodger's X coordinate: ";
  cin >> duckX;
  cout << "\nPlease enter Marvin the Martian's Y coordinate: ";
  cin >> marvinY;
  cout << "Please enter Marvin the Martian's X coordinate: ";
  cin >> marvinX;
  cout << "\nPlease enter the Illudium Phosdex's Y coordinate: ";
  cin >> illudiumY;
  cout << "Please enter the Illudium Phosdex's X coordinate: ";
  cin >> illudiumX;

  illudiumX++;
  illudiumY++;
  duckX++;
  duckY++;
  marvinX++;
  marvinY++;
  // check for validity here if you want

  cout << "Enter number of lava locations: ";
  cin >> nLava;
  for (int i=0;i<nLava;i++) {
    cout << "Enter lava Y location: ";
    cin >> lavaY;
    cout << "Enter lava X location: ";
    cin >> lavaX;
    lavaX++;
    lavaY++;
    // mark lava location
    map[lavaY][lavaX] = -2;
  }

  map[illudiumY][illudiumX] = 0;

  for (r=0; r<ROW; r++) {
  for (c=0; c<COL; c++)
        cout << map[r][c];
        cout << "\n";
  }

cout << "\n\n";

  numberCells(illudiumY, illudiumX, r, c);

}

// next... find a path for Duck Dodgers and Marvin Martian
//  void findPath() {
//       Queue q;
//  Start at one's location, enqueue it. Look at the surrounding neighbors and for any that are (Current Path -1), enqueue them.

  }

  int numberCells(int illudiumY, int illudiumX, int r, int c) {
  Queue q;
  q.enqueue(map[illudiumY][illudiumX]);            //Enqueue location of Illudium Phosdex
  map[illudiumY][illudiumX] = 0;                   //Cell of IP = 0, Lava -2, all Other Squares -1
  
  while (!q.isEmpty()) {                           //While the queue is not empty
    q.dequeue();                                   //Dequeue a location
    
    for (int i=0;i<5;i++) {                        //For each neighbor of the location
        dC[i];                                     //Hexagonal grid movements for columns
        dR1[i];                                    //Even rows
        dR2[i];                                    //Odd rows
    if (map[r][c] == -1) {                         //If neighbors cell is -1
    map[r][c] = i+1;                               //Neighbors cell = Location's cell +1
    q.enqueue(i);                                  //Enqueue the neighbor
      }
    }
  }

  for (r=0; r<ROW; r++) {
  for (c=0; c<COL; c++)
        cout << map[r][c];
        cout << "\n";
}
}


                
//With every cell numbered (-1) except illudium phosdex (0) and lava (-2), use map[illudiumY][illudiumX] to +1 every cell around it to the edge.



Queue.h

#ifndef _QUEUE_H
#define _QUEUE_H

const int MAX_QUEUE_SIZE = 20;

enum QueueExceptions {
  E_QUEUE_EMPTY,
  E_QUEUE_FULL,
  E_ARRAY_OUT_OF_BOUNDS
};

class Queue {
 public:
  Queue(void) {
    head = 0;
    tail = 0;
    count = 0; 
  }
  ~Queue(void) { }

  void clear(void) { 
    head = 0;
    tail = 0;
    count = 0;
  }
  bool isEmpty(void) {
    return !count;
  }
  int size(void) {
    return count;
  }

void Queue::enqueue(int d) throw (int) {

  if (count == MAX_QUEUE_SIZE)
    throw E_QUEUE_FULL;

  data[tail] = d;
  tail = (tail + 1) % MAX_QUEUE_SIZE;
  count++;
}

int Queue::dequeue(void) throw(int) {
  int d;

  if (count == 0)
    throw E_QUEUE_EMPTY;

  d = data[head];
  head = (head + 1) % MAX_QUEUE_SIZE;
  count--;

  return d;
}

//  void enqueue(int) throw (int);
//  int dequeue(void) throw (int);
  int numberCells(int r, int c);

 private:
  int data[MAX_QUEUE_SIZE];
  int head,tail,count;
};

#endif



If you have any follow up questions to make the question easier to understand, please feel free to ask.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>