Hexagon Generation

My code loads down in a straight veins. But i am trying to make it looks like this when it loads in

(https://www.google.pt/search?q=hexagon+map&rlz=1C1GGRV_enPT752PT752&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjJ6qrgtvvZAhVHRhQKHa1CDuYQ_AUICigB&biw=1455&bih=699#imgdii=OEI1FXYbIvdYuM:&imgrc=FiWYFCRhaYrC2M: )

float [] PlayerPoints;
float [] XColmns;
float [] YColmns;
float [] ColorR;
float [] ColorG;
float [] ColorB;

int [] TileColors;
int [] TileColorChance;

int AmountOfTiles = 1131;


void setup() {

  size(975, 830);

   XColmns = new float[AmountOfTiles];
   YColmns = new float[AmountOfTiles];
   ColorR = new float[AmountOfTiles];
   ColorG = new float[AmountOfTiles];
   ColorB = new float[AmountOfTiles];

   TileColors = new int[AmountOfTiles];
   TileColorChance = new int[AmountOfTiles];

  for (int c = 0; c < 39; c++) {
    if ((int)c%2==0) {   
    for (int N = 0; N < 29; N++) { 
     XColmns[N+c*29] = 31+c*24;
     YColmns[N+c*29] = 30 + N * 27; 
    } }
   else { 
     for (int N = 0; N < 29; N++) { 
     XColmns[N+c*29] = 31+c*24;
     YColmns[N+c*29] = 43 + N * 27; 

   } } }


  for (int TE = 0; TE < AmountOfTiles; TE++) {
    TileColors[TE] = round(random(0, 3));
    TileColorChance[TE] = round(random(0, 1));
        if (TE > 1) { 
        if (TileColors[TE-1] == 0 && TileColorChance[TE] == 0) { TileColors[TE] = 0; }
        if (TileColors[TE-1] == 1 && TileColorChance[TE] == 0) { TileColors[TE] = 1; }
        if (TileColors[TE-1] == 2 && TileColorChance[TE] == 0) { TileColors[TE] = 2; }
        if (TileColors[TE-1] == 3 && TileColorChance[TE] == 0) { TileColors[TE] = 3; }
        } 
  }

}//void setup end

///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////

void draw() {

 background(226, 185, 108);

//table
  fill(89, 60, 5);  
  ellipse(-260, -330, 1500, 1500);

//black board
  fill(0);
  stroke(108, 67, 4);
  strokeWeight(30);
  rect(0, 0, width, height);

  for (int i = 0; i < AmountOfTiles; i++) {
    Hexagon(XColmns[i], YColmns[i], 14, TileColors[i]);
  }


}//void draw bracket


void Hexagon (float x, float y, float gs, float ColorSelecter) {
  //Ocean
  if (ColorSelecter == 0) { fill(16, 0, 165); }
  //Grassland
  if (ColorSelecter == 1) { fill(81, 175, 14); }
  //Jungle
  if (ColorSelecter == 2) { fill(27, 157, 2); }
  //Pond
  if (ColorSelecter == 3) { fill(1, 116, 147); }
 noStroke();
  beginShape();
  for(int i=0;i<6;i++){
    float angle = i * 2 * PI / 6;
    vertex(x + gs*cos(angle),y + gs*sin(angle));
  }
  endShape(CLOSE);
}
«13

Answers

  • Answer ✓

    Please go back and edit your post

    Select the code and then click the small C button in the command bar

    What exactly doesn’t work...?

  • edited March 2018

    Thanks for the reply did i change it right now.

  • It’s not better

    Edit your post with the gear

    Empty lines before and after the code section

    Select code with mouse

    Hit ctrl-t

  • edited March 2018

    Chrisir i think i did it now. It was a bit hard to understand but i looked at post on how to set up code in processing forum and i figured it out.

  • Well done!

  • code loads down in a straight veins. But i am trying to make it looks like this when it loads

    not sure what you mean, this has straight lines down too :

    422d64508b3a4da613841652207dda31

  • remember to hit ctrl-t in processing to get better indents:

    float [] PlayerPoints;
    float [] XColmns;
    float [] YColmns;
    float [] ColorR;
    float [] ColorG;
    float [] ColorB;
    
    int [] TileColors;
    int [] TileColorChance;
    
    int AmountOfTiles = 1131;
    
    
    void setup() {
    
      size(975, 830);
    
      XColmns = new float[AmountOfTiles];
      YColmns = new float[AmountOfTiles];
      ColorR = new float[AmountOfTiles];
      ColorG = new float[AmountOfTiles];
      ColorB = new float[AmountOfTiles];
    
      TileColors = new int[AmountOfTiles];
      TileColorChance = new int[AmountOfTiles];
    
      for (int c = 0; c < 39; c++) {
        if ((int)c%2==0) {   
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 30 + N * 27;
          }
        } else { 
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 43 + N * 27;
          }
        }
      }
    
    
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        TileColors[TE] = round(random(0, 3));
        TileColorChance[TE] = round(random(0, 1));
        if (TE > 1) { 
          if (TileColors[TE-1] == 0 && TileColorChance[TE] == 0) { 
            TileColors[TE] = 0;
          }
          if (TileColors[TE-1] == 1 && TileColorChance[TE] == 0) { 
            TileColors[TE] = 1;
          }
          if (TileColors[TE-1] == 2 && TileColorChance[TE] == 0) { 
            TileColors[TE] = 2;
          }
          if (TileColors[TE-1] == 3 && TileColorChance[TE] == 0) { 
            TileColors[TE] = 3;
          }
        }
      }
    } // setup end
    
    ///////////////////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////////////////////
    
    void draw() {
    
      background(226, 185, 108);
    
      //table
      fill(89, 60, 5);  
      ellipse(-260, -330, 1500, 1500);
    
      //black board
      fill(0);
      stroke(108, 67, 4);
      strokeWeight(1);
      //rect(0, 0, width, height);
    
      for (int i = 0; i < AmountOfTiles; i++) {
        Hexagon(XColmns[i], YColmns[i], 14, TileColors[i]);
      }
    } // draw bracket
    
    
    void Hexagon (float x, float y, 
      float gs, 
      float ColorSelecter) {
    
      //Ocean
    
      if (ColorSelecter == 0) { 
        fill(16, 0, 165);
      }
    
      //Grassland
      if (ColorSelecter == 1) { 
        fill(81, 175, 14);
      }
    
      //Jungle
      if (ColorSelecter == 2) { 
        fill(27, 157, 2);
      }
    
      //Pond
      if (ColorSelecter == 3) { 
        fill(1, 116, 147);
      }
    
      // ---
    
      noStroke();
      beginShape();
      for (int i=0; i<6; i++) {
        float angle = i * 2 * PI / 6;
        vertex(x + gs*cos(angle), y + gs*sin(angle));
      }
      endShape(CLOSE);
    }//function 
    //
    
  • what i am trying to say is in the image above the forest spawned in a group and the grassland spawned in a group too in random location and in random groups but i do not know how to do that.

  • but you still want random, but group wise?

  • Yes, but they could be larger or smaller groups of hexagons.

  • edited March 2018

    here is a solution.

    float [] PlayerPoints;
    
    float [] XColmns;
    float [] YColmns;
    
    float [] ColorR;
    float [] ColorG;
    float [] ColorB;
    
    int [] TileColors;
    int [] TileColorChance;
    
    int AmountOfTiles = 1131;
    
    
    void setup() {
    
      size(975, 830);
    
      XColmns = new float[AmountOfTiles];
      YColmns = new float[AmountOfTiles];
      ColorR = new float[AmountOfTiles];
      ColorG = new float[AmountOfTiles];
      ColorB = new float[AmountOfTiles];
    
      TileColors = new int[AmountOfTiles];
      TileColorChance = new int[AmountOfTiles];
    
      for (int c = 0; c < 39; c++) {
    
        if ((int)c%2==0) {   
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 30 + N * 27;
          }
        } else { 
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 43 + N * 27;
          }
        }
      }
    
    
      //for (int TE = 0; TE < AmountOfTiles; TE++) {
      //  TileColors[TE] = round(random(0, 3));
      //  TileColorChance[TE] = round(random(0, 1));
      //  if (TE > 1) { 
      //    if (TileColors[TE-1] == 0 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 0;
      //    }
      //    if (TileColors[TE-1] == 1 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 1;
      //    }
      //    if (TileColors[TE-1] == 2 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 2;
      //    }
      //    if (TileColors[TE-1] == 3 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 3;
      //    }
      //  }
      //}
    
    
      // RESET 
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        TileColors[TE] = 0; // OCEAN
      }
    
      // attempts
      for (int i = 0; i < 69; i++) {
    
        // new group of Type startingNewGroupType : loop over types of hexagon :
        for (int startingNewGroupType = 0; startingNewGroupType < 4; startingNewGroupType++) {
    
          int startingNewGroupWhere = int (random(AmountOfTiles));
    
          TileColors[startingNewGroupWhere] = startingNewGroupType;
    
          // going down and up
          int from =  -int(random(8));
          int to   =   int(random(8));  
          for (int i2 = from; i2 < to; i2++) {
            int newFieldIndex= startingNewGroupWhere + i2; 
            if (  newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
              TileColors[newFieldIndex] = startingNewGroupType;
    
            // for each new field going left and right 
            from =  -int(random(8));
            to   =   int(random(8));  
            for (int i3 = from; i3 < to; i3++) {
              if ( newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                TileColors[newFieldIndex + i3*29] = startingNewGroupType;
            }
          }
        }
        //
      }//for 
      //
    } // setup end
    
    ///////////////////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////////////////////
    
    void draw() {
    
      background(226, 185, 108);
    
      //table
      fill(89, 60, 5);  
      ellipse(-260, -330, 1500, 1500);
    
      //black board
      fill(0);
      stroke(108, 67, 4);
      strokeWeight(1);
      //rect(0, 0, width, height);
    
      for (int i = 0; i < AmountOfTiles; i++) {
        Hexagon(XColmns[i], YColmns[i], 14, TileColors[i]);
      }
    } // draw bracket
    
    
    void Hexagon (float x, float y, 
      float gs, 
      float ColorSelecter) {
    
      noFill(); 
    
      //Ocean
      if (ColorSelecter == 0) { 
        fill(16, 0, 165);
      }
    
      //Grassland
      else if (ColorSelecter == 1) { 
        fill(81, 175, 14);
      }
    
      //Jungle
      else if (ColorSelecter == 2) { 
        fill(27, 157, 2);
      }
    
      //Pond
      else if (ColorSelecter == 3) { 
        fill(1, 116, 147);
      } 
    
      // Error 
      else
      {
        println ("Error ColorSelecter "+ColorSelecter);
      }
    
      // ---
    
      noStroke();
      beginShape();
      for (int i=0; i<6; i++) {
        float angle = i * 2 * PI / 6;
        vertex(x + gs*cos(angle), y + gs*sin(angle));
      }
      endShape(CLOSE);
    }//function 
    //
    
  • thank you for your help you solved it and did a great job. For learning wise what did you change to get it to that.

  • edited March 2018

    ;-)

    lines 65 to 99 - read the comments

    also, I made 130 to 158 be an if....else if .... structure with error section (else)

    [EDITED]

  • edited March 2018

    lines 65 to 99 :

    for 69 attempts
    
          for each of the 4 types (startingNewGroupType) 
    
              chose a random field to start from 
    
              move randomly up and down from here (i2) 
    
              for each of the fields you reach in this manner, go randomly left and right i3 
    
                  for each of the fields you reach in this manner, set it to TYPE startingNewGroupType
    
  • gotta go

    Bye.

  • Thank you it has been the most help ever i appreciate your time helping me.

  • You’re welcome!

  • Chrisir i have a quick question where would you put in conditions on spawning. For example the blue tiles only spawn in if they are touching.

  • Please explain

    the blue tiles only spawn in if they are touching.

  • Atm between line 70 and 99 just new sections of blocks (eg. a big forest) are build, overwriting and overlapping previous sections. Old colors of hexagons are not looked at at all.

    You would need to change the concept of those lines a bit.

    Do you need it randomly or could you just use an fixed map of ocean and trees and so on...?

  • Mainly i am just trying to find the best place to put an if statements about hexagons generation. The blue hexagons would only spawn in if ever blue hexagon on the map were touching this was just an example though.

  • So that's were you would put in more if statements for generation in groups like in my example.

  • Difficult at the moment because a lot of overwriting happening at the moment

  • edited March 2018

    Yeah sorry for the confusion i am new to coding. I was just trying to figuring out the best way to implement an if statement on changing the hexagon generation in the code you did. But not sure how i was hoping you could show me how to implement my example into it so i could learn how. Did this help you understand.

  • It’s simple. We just need another concept of doing the distribution

  • How would you explain your if-clause-idea to a friend? What results do you want to achieve?

  • You wrote

    The blue hexagons would only spawn in if ever blue hexagon on the map were touching

    Not sure I understand... touching what??

    You mean you place a new blue tile only next to an existing blue tile? That I already have because we go from a initial seed.

    Also do you need a random effect or can the map look the same every time you run it?

  • edited March 2018

    I mean that all the blue tiles next to existing blue tiles. Random effect would be better. Chrisir were do you think the best place to learn code is i have learn't the basics but i am looking to go into more advanced processing coding any suggestions.

  • edited March 2018

    all the blue tiles next to existing blue tiles

    Hm. when the thing (all tiles except ocean) is an island, blue (=ocean) would only be on the outside

    When blue can also mean a lake or river, it can also be on the inside. Is this what you mean? Like a network but all connected, you mean?

    you could make all forests and make the ocean last and make it so that they are connected.

    this was just an example though.

    But that's only for blue, what other if rules are there for you?

    • advanced processing coding : look at the books and keep on practicing
  • Yes that's what i mean. Okay thanks for the tip.

  • Yes that's what i mean

    ??

    But that's only for blue, what other if rules are there for you?

  • new version with more ocean distribution technology ;-)

    but there is no simple if to achieve those things....

    float [] PlayerPoints;
    
    float [] XColmns;
    float [] YColmns;
    
    float [] ColorR;
    float [] ColorG;
    float [] ColorB;
    
    int [] TileColors;
    int [] TileColorChance;
    
    int AmountOfTiles = 1131;
    
    
    void setup() {
    
      size(975, 830);
    
      XColmns = new float[AmountOfTiles];
      YColmns = new float[AmountOfTiles];
      ColorR = new float[AmountOfTiles];
      ColorG = new float[AmountOfTiles];
      ColorB = new float[AmountOfTiles];
    
      TileColors = new int[AmountOfTiles];
      TileColorChance = new int[AmountOfTiles];
    
      for (int c = 0; c < 39; c++) {
    
        if ((int)c%2==0) {   
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 30 + N * 27;
          }
        } else { 
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 43 + N * 27;
          }
        }
      }
    
    
      //for (int TE = 0; TE < AmountOfTiles; TE++) {
      //  TileColors[TE] = round(random(0, 3));
      //  TileColorChance[TE] = round(random(0, 1));
      //  if (TE > 1) { 
      //    if (TileColors[TE-1] == 0 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 0;
      //    }
      //    if (TileColors[TE-1] == 1 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 1;
      //    }
      //    if (TileColors[TE-1] == 2 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 2;
      //    }
      //    if (TileColors[TE-1] == 3 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 3;
      //    }
      //  }
      //}
    
    
      // RESET 
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        TileColors[TE] = 1; // not OCEAN
      }
    
    
      // attempts
      for (int i = 1; i < 13; i++) {
    
        // new group of Type startingNewGroupType : loop over types of hexagon :
        // starting with 1 = NO ocean 
        for (int startingNewGroupType = 1; startingNewGroupType < 4; startingNewGroupType++) {
    
          int startingNewGroupWhere = int (random(AmountOfTiles));
    
          TileColors[startingNewGroupWhere] = startingNewGroupType;
    
          // going down and up
          int from =  -int(random(4, 12));
          int to   =   int(random(4, 12));  
          for (int i2 = from; i2 < to; i2++) {
            int newFieldIndex= startingNewGroupWhere + i2; 
            if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
              TileColors[newFieldIndex] = startingNewGroupType;
    
            // for each new field going left and right 
            from =  -int(random(4, 12));
            to   =   int(random(4, 12));  
            for (int i3 = from; i3 < to; i3++) {
              if (newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                TileColors[newFieldIndex + i3*29] = startingNewGroupType;
            }
          }
        }
        //
      }//for
    
      // 
    
      //add ocean
      int startingNewGroupType = 0;
    
      float xPrev=-10000;
      float yPrev=-10000;
    
      // attempts
      for (int i4 = 1; i4 < 12; i4++) {
    
        int startingNewGroupWhere = indexOfExistingOcean(); // find old ocean tile
    
        if ( startingNewGroupWhere==-1)
          startingNewGroupWhere= int (random(AmountOfTiles));
    
        TileColors[startingNewGroupWhere] = startingNewGroupType;
    
        if (random(100) > 90) {
    
          // random walker / river 
    
          int newFieldIndex = startingNewGroupWhere  -1; 
          int direction = int(random(4));
    
          // attempts
          for (int i = 1; i < 293; i++) {
    
            if (random(100)>60) 
              direction = int(random(4)); 
    
            switch (direction) {
            case 0: //north
              newFieldIndex = startingNewGroupWhere  -1; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 1: //east 
              newFieldIndex= startingNewGroupWhere + 29; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 2: //south 
              newFieldIndex= startingNewGroupWhere + 1; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 3: //west 
              newFieldIndex= startingNewGroupWhere - 29; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
            }
            startingNewGroupWhere=newFieldIndex;
          }
        }
    
        //---
    
        else 
        {
          // Lake 
    
          // going down and up
          int from =  -int(random(4, 12));
          int to   =   int(random(4, 12));  
          for (int i2 = from; i2 < to; i2++) {
    
            int newFieldIndex= startingNewGroupWhere + i2;
    
            if (newFieldIndex >= 0 && 
              newFieldIndex < AmountOfTiles && 
              dist(xPrev, yPrev, XColmns[newFieldIndex], YColmns[newFieldIndex]) < 66 ) {
    
              TileColors[newFieldIndex] = startingNewGroupType;
    
              // for each new field going left and right 
              from =  -int(random(4, 12));
              to   =   int(random(4, 12));  
              for (int i3 = from; i3 < to; i3++) {
                if (newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                  TileColors[newFieldIndex + i3*29] = startingNewGroupType;
              }
            }
          }//for
    
          xPrev=XColmns[startingNewGroupWhere];
          yPrev=YColmns[startingNewGroupWhere];
        }//else 
        //
      }//for
    
      //
    } // setup end
    
    // ------------------------------------------------------------------------
    
    int indexOfExistingOcean() {
    
      //
      ArrayList<Integer> indices = new ArrayList(); 
    
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        if (TileColors[TE] == 0) { //  OCEAN
          //
          indices.add (TE); 
          //
        }//if
      }//for
    
      if (indices.size()==0) {
        return -1;
      } 
      int randomNumber = int(random(indices.size())); 
      return indices.get(randomNumber); 
      //
    }//func 
    
    ///////////////////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////////////////////
    
    void draw() {
    
      background(226, 185, 108);
    
      //table
      fill(89, 60, 5);  
      // ellipse(-260, -330, 1500, 1500);
    
      //black board
      fill(0);
      stroke(108, 67, 4);
      strokeWeight(1);
      //rect(0, 0, width, height);
    
      for (int i = 0; i < AmountOfTiles; i++) {
        Hexagon(XColmns[i], YColmns[i], 14, TileColors[i]);
      }
    } // draw bracket
    
    
    void Hexagon (float x, float y, 
      float gs, 
      float ColorSelecter) {
    
      noFill(); 
    
      //Ocean
      if (ColorSelecter == 0) { 
        fill(16, 0, 165);
      }
    
      //Grassland
      else if (ColorSelecter == 1) { 
        fill(81, 175, 14);
      }
    
      //Jungle
      else if (ColorSelecter == 2) { 
        fill(27, 157, 2);
      }
    
      //Pond
      else if (ColorSelecter == 3) { 
        fill(1, 116, 147);
      } 
    
      // Error 
      else
      {
        println ("Error ColorSelecter "+ColorSelecter);
      }
    
      // ---
    
      noStroke();
      beginShape();
      for (int i=0; i<6; i++) {
        float angle = i * 2 * PI / 6;
        vertex(x + gs*cos(angle), y + gs*sin(angle));
      }
      endShape(CLOSE);
    }//function 
    //
    
  • O sorry forgot that. All the other colors except blue have to be in a group larger than 5 all have to be touching their others.

  • edited March 2018

    all have to be touching their others

    Wow. Good luck with that.

  • Good luck with that.

    I can't help you with that.

    It would have been helpful to have that information initially before I put so much work in it.

  • edited March 2018

    Sorry about that but i do appreciate your time helping me on this it has been great.

  • I gave enough ideas - random walker and growing to all sights

    I hope you can build on that

    Problem I see is that when you make one complex shape like the ocean and then the next, you would overwrite the previous. So it has to stop when he finds a defined field instead of overwriting it. But then the first will be very complex where the others are simple blobs.

    Maybe you are not fully clear what yo want

    all have to be touching their others

    That means there are 3 or so massive blocks, they can’t be networks, they would cut each other and separate parts from their main block.

    Even if you have 3 massive block and overlay the ocean at the end, it’s only allowed to go along the borders of the blocks because otherwise they would cut again fields off from their main blocks.

    Youu see, it’s complicated.

  • edited March 2018

    Yeah i understand it takes time to do these things no problem though i had a blast talking with you. Its great to be able to talk to someone who understands me when i say things about code thank you.

  • Technically it would be more comfortable to have the data of the grid in a 2D array

    Because at the moment when you go from one field near the top up and reach the border you just come out at the bottom. But then we have two separate blocks (one at the top, one at the bottom) of one color what we don’t want

    With a 2D array we could just stop more easily

  • edited March 2018

    I have never seen a 2D array before how would it work in are code.

  • See tutorial section on processing.org

    Tutorial two dimensional arrays

  • Okay will do. I just saw this code i never saw before yesterday it was randomSeed() its quite cool how it works here example in code.

    float [] PlayerPoints;
    
    float [] XColmns;
    float [] YColmns;
    
    float [] ColorR;
    float [] ColorG;
    float [] ColorB;
    
    int [] TileColors;
    int [] TileColorChance;
    
    int AmountOfTiles = 1131;
    int Seed = round(random(1000000000));
    
    
    void setup() {
    
      size(975, 830);
    
      println("Seed:  "+Seed);
      randomSeed(Seed);
    
      XColmns = new float[AmountOfTiles];
      YColmns = new float[AmountOfTiles];
      ColorR = new float[AmountOfTiles];
      ColorG = new float[AmountOfTiles];
      ColorB = new float[AmountOfTiles];
    
      TileColors = new int[AmountOfTiles];
      TileColorChance = new int[AmountOfTiles];
    
      for (int c = 0; c < 39; c++) {
    
        if ((int)c%2==0) {   
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 30 + N * 27;
          }
        } else { 
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 43 + N * 27;
          }
        }
      }
    
    
      //for (int TE = 0; TE < AmountOfTiles; TE++) {
      //  TileColors[TE] = round(random(0, 3));
      //  TileColorChance[TE] = round(random(0, 1));
      //  if (TE > 1) { 
      //    if (TileColors[TE-1] == 0 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 0;
      //    }
      //    if (TileColors[TE-1] == 1 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 1;
      //    }
      //    if (TileColors[TE-1] == 2 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 2;
      //    }
      //    if (TileColors[TE-1] == 3 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 3;
      //    }
      //  }
      //}
    
    
      // RESET 
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        TileColors[TE] = 1; // not OCEAN
      }
    
    
      // attempts
      for (int i = 1; i < 13; i++) {
    
        // new group of Type startingNewGroupType : loop over types of hexagon :
        // starting with 1 = NO ocean 
        for (int startingNewGroupType = 1; startingNewGroupType < 4; startingNewGroupType++) {
    
          int startingNewGroupWhere = int (random(AmountOfTiles));
    
          TileColors[startingNewGroupWhere] = startingNewGroupType;
    
          // going down and up
          int from =  -int(random(4, 12));
          int to   =   int(random(4, 12));  
          for (int i2 = from; i2 < to; i2++) {
            int newFieldIndex= startingNewGroupWhere + i2; 
            if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
              TileColors[newFieldIndex] = startingNewGroupType;
    
            // for each new field going left and right 
            from =  -int(random(4, 12));
            to   =   int(random(4, 12));  
            for (int i3 = from; i3 < to; i3++) {
              if (newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                TileColors[newFieldIndex + i3*29] = startingNewGroupType;
            }
          }
        }
        //
      }//for
    
      // 
    
      //add ocean
      int startingNewGroupType = 0;
    
      float xPrev=-10000;
      float yPrev=-10000;
    
      // attempts
      for (int i4 = 1; i4 < 12; i4++) {
    
        int startingNewGroupWhere = indexOfExistingOcean(); // find old ocean tile
    
        if ( startingNewGroupWhere==-1)
          startingNewGroupWhere= int (random(AmountOfTiles));
    
        TileColors[startingNewGroupWhere] = startingNewGroupType;
    
        if (random(100) > 90) {
    
          // random walker / river 
    
          int newFieldIndex = startingNewGroupWhere  -1; 
          int direction = int(random(4));
    
          // attempts
          for (int i = 1; i < 293; i++) {
    
            if (random(100)>60) 
              direction = int(random(4)); 
    
            switch (direction) {
            case 0: //north
              newFieldIndex = startingNewGroupWhere  -1; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 1: //east 
              newFieldIndex= startingNewGroupWhere + 29; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 2: //south 
              newFieldIndex= startingNewGroupWhere + 1; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 3: //west 
              newFieldIndex= startingNewGroupWhere - 29; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
            }
            startingNewGroupWhere=newFieldIndex;
          }
        }
    
        //---
    
        else
        {
          // Lake 
    
          // going down and up
          int from =  -int(random(4, 12));
          int to   =   int(random(4, 12));  
          for (int i2 = from; i2 < to; i2++) {
    
            int newFieldIndex= startingNewGroupWhere + i2;
    
            if (newFieldIndex >= 0 && 
              newFieldIndex < AmountOfTiles && 
              dist(xPrev, yPrev, XColmns[newFieldIndex], YColmns[newFieldIndex]) < 66 ) {
    
              TileColors[newFieldIndex] = startingNewGroupType;
    
              // for each new field going left and right 
              from =  -int(random(4, 12));
              to   =   int(random(4, 12));  
              for (int i3 = from; i3 < to; i3++) {
                if (newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                  TileColors[newFieldIndex + i3*29] = startingNewGroupType;
              }
            }
          }//for
    
          xPrev=XColmns[startingNewGroupWhere];
          yPrev=YColmns[startingNewGroupWhere];
        }//else 
        //
      }//for
    
      //
    } // setup end
    
    // ------------------------------------------------------------------------
    
    int indexOfExistingOcean() {
    
      //
      ArrayList<Integer> indices = new ArrayList(); 
    
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        if (TileColors[TE] == 0) { //  OCEAN
          //
          indices.add (TE); 
          //
        }//if
      }//for
    
      if (indices.size()==0) {
        return -1;
      } 
      int randomNumber = int(random(indices.size())); 
      return indices.get(randomNumber); 
      //
    }//func 
    
    ///////////////////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////////////////////
    
    void draw() {
    
      background(226, 185, 108);
    
      //table
      fill(89, 60, 5);  
      // ellipse(-260, -330, 1500, 1500);
    
      //black board
      fill(0);
      stroke(108, 67, 4);
      strokeWeight(1);
      //rect(0, 0, width, height);
    
      for (int i = 0; i < AmountOfTiles; i++) {
        Hexagon(XColmns[i], YColmns[i], 14, TileColors[i]);
      }
    } // draw bracket
    
    
    void Hexagon (float x, float y, 
      float gs, 
      float ColorSelecter) {
    
      noFill(); 
    
      //Ocean
      if (ColorSelecter == 0) { 
        fill(16, 0, 165);
      }
    
      //Grassland
      else if (ColorSelecter == 1) { 
        fill(81, 175, 14);
      }
    
      //Jungle
      else if (ColorSelecter == 2) { 
        fill(27, 157, 2);
      }
    
      //Pond
      else if (ColorSelecter == 3) { 
        fill(1, 116, 147);
      } 
    
      // Error 
      else
      {
        println ("Error ColorSelecter "+ColorSelecter);
      }
    
      // ---
    
      noStroke();
      beginShape();
      for (int i=0; i<6; i++) {
        float angle = i * 2 * PI / 6;
        vertex(x + gs*cos(angle), y + gs*sin(angle));
      }
      endShape(CLOSE);
    }//function 
    //
    
  • I don’t understand, what did you change? Only the line with randomSeed?

    What are the effects? Better randomness...?

  • edited March 2018

    if you do not understand the picture here's the link to a video exampling it.

    youtube.com/watch?v=yGgEi_40fD4

    This video helped me understand it.

    No, better randomization.

  • edited March 2018

    Chrisir i wanted to see were i could add if statements. So that way i could add boxes in at random if the statements. But how could you make it were the boxes only show to if they are 8 hexagons away from another one if there is on e spawned in.

    float [] box;
    
    float [] XColmns;
    float [] YColmns;
    
    float [] ColorR;
    float [] ColorG;
    float [] ColorB;
    
    int [] TileColors;
    int [] TileColorChance;
    
    int AmountOfTiles = 1131;
    int Seed = round(random(1000000000));
    
    
    void setup() {
    
      size(975, 830);
    
      println("Seed:  "+Seed);
      randomSeed(Seed);
    
      XColmns = new float[AmountOfTiles];
      YColmns = new float[AmountOfTiles];
      ColorR = new float[AmountOfTiles];
      ColorG = new float[AmountOfTiles];
      ColorB = new float[AmountOfTiles];
      box = new float[AmountOfTiles*10];
      TileColors = new int[AmountOfTiles];
      TileColorChance = new int[AmountOfTiles];
    
    
      for (int c = 0; c < 39; c++) {
    
        if ((int)c%2==0) {   
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 30 + N * 27;
            box[N+c*29] = round(random(0, 15));
          }
        } else { 
          for (int N = 0; N < 29; N++) { 
            XColmns[N+c*29] = 31+c*24;
            YColmns[N+c*29] = 43 + N * 27;
            box[N+c*29] = round(random(0, 15));
          }
        }
      }
    
    
      //for (int TE = 0; TE < AmountOfTiles; TE++) {
      //  TileColors[TE] = round(random(0, 3));
      //  TileColorChance[TE] = round(random(0, 1));
      //  if (TE > 1) { 
      //    if (TileColors[TE-1] == 0 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 0;
      //    }
      //    if (TileColors[TE-1] == 1 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 1;
      //    }
      //    if (TileColors[TE-1] == 2 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 2;
      //    }
      //    if (TileColors[TE-1] == 3 && TileColorChance[TE] == 0) { 
      //      TileColors[TE] = 3;
      //    }
      //  }
      //}
    
    
      // RESET 
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        TileColors[TE] = 1; // not OCEAN
      }
    
    
      // attempts
      for (int i = 1; i < 13; i++) {
    
        // new group of Type startingNewGroupType : loop over types of hexagon :
        // starting with 1 = NO ocean 
        for (int startingNewGroupType = 1; startingNewGroupType < 4; startingNewGroupType++) {
    
          int startingNewGroupWhere = int (random(AmountOfTiles));
    
          TileColors[startingNewGroupWhere] = startingNewGroupType;
    
          // going down and up
          int from =  -int(random(4, 12));
          int to   =   int(random(4, 12));  
          for (int i2 = from; i2 < to; i2++) {
            int newFieldIndex= startingNewGroupWhere + i2; 
            if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
              TileColors[newFieldIndex] = startingNewGroupType;
    
            // for each new field going left and right 
            from =  -int(random(4, 12));
            to   =   int(random(4, 12));  
            for (int i3 = from; i3 < to; i3++) {
              if (newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                TileColors[newFieldIndex + i3*29] = startingNewGroupType;
            }
          }
        }
        //
      }//for
    
      // 
    
      //add ocean
      int startingNewGroupType = 0;
    
      float xPrev=-10000;
      float yPrev=-10000;
    
      // attempts
      for (int i4 = 1; i4 < 12; i4++) {
    
        int startingNewGroupWhere = indexOfExistingOcean(); // find old ocean tile
    
        if ( startingNewGroupWhere==-1)
          startingNewGroupWhere= int (random(AmountOfTiles));
    
        TileColors[startingNewGroupWhere] = startingNewGroupType;
    
        if (random(100) > 90) {
    
          // random walker / river 
    
          int newFieldIndex = startingNewGroupWhere  -1; 
          int direction = int(random(4));
    
          // attempts
          for (int i = 1; i < 293; i++) {
    
            if (random(100)>60) 
              direction = int(random(4)); 
    
            switch (direction) {
            case 0: //north
              newFieldIndex = startingNewGroupWhere  -1; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 1: //east 
              newFieldIndex= startingNewGroupWhere + 29; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 2: //south 
              newFieldIndex= startingNewGroupWhere + 1; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
    
            case 3: //west 
              newFieldIndex= startingNewGroupWhere - 29; 
              if (newFieldIndex >= 0 && newFieldIndex < AmountOfTiles) 
                TileColors[newFieldIndex] = startingNewGroupType;
              break;
            }
            startingNewGroupWhere=newFieldIndex;
          }
        }
    
        //---
    
        else
        {
          // Lake 
    
          // going down and up
          int from =  -int(random(4, 12));
          int to   =   int(random(4, 12));  
          for (int i2 = from; i2 < to; i2++) {
    
            int newFieldIndex= startingNewGroupWhere + i2;
    
            if (newFieldIndex >= 0 && 
              newFieldIndex < AmountOfTiles && 
              dist(xPrev, yPrev, XColmns[newFieldIndex], YColmns[newFieldIndex]) < 66 ) {
    
              TileColors[newFieldIndex] = startingNewGroupType;
    
              // for each new field going left and right 
              from =  -int(random(4, 12));
              to   =   int(random(4, 12));  
              for (int i3 = from; i3 < to; i3++) {
                if (newFieldIndex + i3*29 >= 0 && newFieldIndex + i3*29 < AmountOfTiles) 
                  TileColors[newFieldIndex + i3*29] = startingNewGroupType;
              }
            }
          }//for
    
          xPrev=XColmns[startingNewGroupWhere];
          yPrev=YColmns[startingNewGroupWhere];
        }//else 
        //
      }//for
    
      //
    } // setup end
    
    // ------------------------------------------------------------------------
    
    int indexOfExistingOcean() {
    
      //
      ArrayList<Integer> indices = new ArrayList(); 
    
      for (int TE = 0; TE < AmountOfTiles; TE++) {
        if (TileColors[TE] == 0) { //  OCEAN
          //
          indices.add (TE); 
          //
        }//if
      }//for
    
      if (indices.size()==0) {
        return -1;
      } 
      int randomNumber = int(random(indices.size())); 
      return indices.get(randomNumber); 
      //
    }//func 
    
    void draw() {
    
      background(226, 185, 108);
    
      //table
      fill(89, 60, 5);  
      // ellipse(-260, -330, 1500, 1500);
    
      //black board
      fill(0);
      stroke(108, 67, 4);
      strokeWeight(1);
      //rect(0, 0, width, height);
    
      for (int i = 0; i < AmountOfTiles; i++) {
        Hexagon(XColmns[i], YColmns[i], 14, TileColors[i], box[i]);
      }
    } // draw bracket
    
    void box(float x, float y) {
    fill(0);  
    rect(x-2, y-2, 5, 5);
    }
    
    void Hexagon (float x, float y, 
      float gs, 
      float ColorSelecter,
      float box) {
    
      noFill(); 
    
      //Ocean
      if (ColorSelecter == 0) { 
        fill(16, 0, 165);
      }
    
      //Grassland
      else if (ColorSelecter == 1) { 
        fill(81, 175, 14);
      }
    
      //Jungle
      else if (ColorSelecter == 2) { 
        fill(27, 157, 2);
      }
    
      //Pond
      else if (ColorSelecter == 3) { 
        fill(1, 116, 147);
      } 
    
      // Error 
      else
      {
        println ("Error ColorSelecter "+ColorSelecter);
      }
    
      // ---
    
      noStroke();
      beginShape();
      for (int i=0; i<6; i++) {
        float angle = i * 2 * PI / 6;
        vertex(x + gs*cos(angle), y + gs*sin(angle));
      }
      endShape(CLOSE);
      if (ColorSelecter ==0) {  }
      if (ColorSelecter ==1) { if (box == 0) { box(x, y); } }
      if (ColorSelecter ==2) {  }
      if (ColorSelecter ==3) {  }
    
    }//function 
    
  • please post a runnable version without errors

    only show to if they are 8 hexagons away from another one

    why do you bring in new rules again? Are there more rules still to come?

    gotta go.

  • edited March 2018

    I updated the coded above. Not sure i just want to check with you that this is the best way to do the code. K.

  • What are the relevant line numbers that are new please?

Sign In or Register to comment.