Allegro.cc Forums » Programming Questions » Generating random combination of obstacles:Problem

 This thread is locked; no one can reply to it.
 Generating random combination of obstacles:Problem
 CodeOne Member #15,442 December 2013 Guys I have successfully implemented my first basic level Allegro5 c++ game.It has a player that keeps moving continuously in right direction. From the right edge of the screen player faces obstacles like triangles and squares. These obstacles come alive at right edge of screen and die at left edge of screen.I randomized the occurrence of both square and triangle individually.player jumps over them, if hits triangle loses life, if falls on squares rolls over it.But then, I thought a lot about how to make them randomly appear in combinations.For example: If suppose X represents triangleO represents squaresand . (Fullstop) represents empty space..........x.....xo.....oxxo............oo.....x....oxx......xo...and similarly they should appear in many randomized patterns.But I after having tried a lot, I still am empty-handed (No solution).Apart from rendering I have used below code:#SelectExpand 1 if(rand() % 200 == 0) // Random triangles such that they dont overlap 2 { 3 Triangle *t = new Triangle(800 + Cameraposition[0],325); 4 objects.push_back(t); 5 t->SetAlive(true); 6 7 if( (triangle->GetX() - 12 < t->GetX() + 12 && triangle->GetX() + 12 > t->GetX() - 12) || 8 (square->GetX() - 40 < t->GetX() + 12 && square->GetX() + 40 > t->GetX() - 12) ) 9 {} 10 11 else if( triangle->GetAlive()==false ) 12 { 13 triangle->SetAlive(true); 14 triangle->SetX(t->GetX()); 15 triangle->SetY(t->GetY()); 16 } 17 18 } 19 20 if(rand() % 200 == 1)//Random squares such that they dont overlap 21 { 22 Square *s = new Square(800 + Cameraposition[0],325); 23 objects.push_back(s); 24 s->SetAlive(true); 25 26 if( (square->GetX() - 40 < s->GetX() + 40 && square->GetX() + 40 > s->GetX() - 40) || 27 (triangle->GetX() - 12 < s->GetX() + 40 && triangle->GetX() + 12 > s->GetX() - 40)) 28 {} 29 30 31 else if( triangle->GetAlive()==false ) 32 { 33 square->SetAlive(true); 34 square->SetX(s->GetX()); 35 square->SetY(s->GetY()); 36 } 37 38 } [Note: once in a while, like rarely, triangle and square DO overlap.Dunno how!]I request you all to guide me until I solve the problem.{"name":"Capture.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/a\/ea61732842592cbc05dfedb8fccbeca9.jpg","w":1279,"h":524,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/a\/ea61732842592cbc05dfedb8fccbeca9"}
 pkrcel Member #14,001 February 2012 I have only skimmed the code briefly, but what exactly is not working?The randomeness? Tri and Squares not actually appearing? Did you seed (with srand() ) the random function? It is unlikely that Google shares your distaste for capitalism. - DerezoIf one had the eternity of time, one would do things later. - Johan Halmén
 Dizzy Egg Member #10,824 March 2009 if(rand()%200 == 1)That means if a random number between 0 and 199 is 1 !!! Not much of a range!!! ----------------------------------------------------Please check out my songs:https://soundcloud.com/dont-rob-the-machina
 Lupuss.Umbrae Member #8,387 March 2007 If I understand you correctly, you want to generate something like.........x.....xo.....oxxo......but your code only generates something like...x...o...o...x...x...o......xsometimes with a triangle and a square sharing the same coordinate?First off: They are ovelapping, because you're checking if a square or triangle should be generated individually instead of checking if an obstacle is to be generated in general and then deciding, whether the obstacle should be a square or a triange. In other words: If the first rand()%200 generates a zero, you create a triangle, but the second rand()%200 can generate a 1 right after the zero, in which case you create a new square at the same coordinates as the riangle.If you want to avoid overlapping, rewrite your code to ```if(rand()%200 == 0) //create obstacle { if(rand()%2 == 0) //obstacle is a triangle { //put triangle creainon code here } else //obstacle is a square { //put square creainon code here } } ``` If you want your code to genarate a random group of obstacles like..xooxoxxoxxxo..you could use do something like this: #SelectExpand 1if(rand()%200 == 0) //create obstacle 2{ 3 if(rand()%200 > 0) //create single obstacle 4 { 5 if(rand()%2 == 0) //obstacle is a triangle 6 { 7 //put triangle creainon code here 8 } 9 else //obstacle is a square 10 { 11 //put square creainon code here 12 } 13 } 14 else //create group of obstacles 15 { 16 int obstacle_count = rand()%15+2; // 17 for(int i = 0; i < obstacle_count; i++) 18 { 19 if(rand()%2 == 0) //obstacle is a triangle 20 { 21 Triangle *t = new Triangle(800 + Cameraposition[0]+i*width,325); 22 //put rest of triangle creainon code here 23 } 24 else //obstacle is a square 25 { 26 Square *s = new Square(800 + Cameraposition[0]+i*width,325); 27 //put rest of square creainon code here 28 } 29 } 30 } 31} This code would generate a line from 2 to 17 obstacles off screen.Take note that the code also eventually will generate overlapping object in the line, as it doesn't check if there already is an object at 800 + Cameraposition[0]Never mind, apparently you're checking for overlaps already, though I don't understand what your logic is behind that. What exactly are the square and triangle object and how and where are they defined? --------------------------------------------------I'm not a signature, I'm just cleaning here...
 pkrcel Member #14,001 February 2012 Lupuss.Umbrae said: What exactly are the square and triangle object and how and where are they defined? That is mainly why I asked what exactly does not work.Besides not entirely grasping this logic (due to too little code shown I guess?) to check overlapping, the rest seems to be plausible to generate a random pattern of sqaure+triangles. It is unlikely that Google shares your distaste for capitalism. - DerezoIf one had the eternity of time, one would do things later. - Johan Halmén
 Audric Member #907 January 2001 When you add an obstacle (or series), you should compute the width of what you've created. Don't create anything more until the screen has scrolled by this width + the minimum distance you want between two series.
 killocan Member #1,556 October 2001 I would suggest you to create some simple patterns, like: {x} {xo} {xoox} {oxxo} {oxx}... And some variations(random maybe) then you proceed to randomize that. I think your map will be a little more organized.
 CodeOne Member #15,442 December 2013 LUPUSS caught my problem. And KILLOCAN what you said is perfectly what i want to do. I am going back on computer to bang my head against code wall. Lets see if i make it to the other side of problem. But KILLOCAN how do i make those combinations , because to render my gameobjects i used a list. So what you suggested is all blurry. But I will give it many tries too. UPDATED:------------------------------------------------------------Guys look I tried this thing to get the combination of obstacles like KILLOCAN said, using what Lupuss suggested.#SelectExpand 1void createObstacles(int numObstacles) 2{ 3 for(int i = 0; i < numObstacles; i++) 4 { 5 if(rand() % 4 ==0) 6 { 7 if(rand() % 2 == 0) 8 { 9 Triangle *t = new Triangle(800 +40+ Cameraposition[0],319); 10 objects.push_back(t); 11 t->SetAlive(true); 12 13 if(!(triangle->GetX() - 15 < t->GetX() + 15 && triangle->GetX() + 15 > t->GetX() - 15) && 14 !(square->GetX() - 40 < t->GetX() + 15 && square->GetX() + 40 > t->GetX() - 15) ) 15 continue; 16 17 18 else if( triangle->GetAlive()==false ) 19 { 20 triangle->SetAlive(true); 21 triangle->SetX(t->GetX()); 22 triangle->SetY(t->GetY()); 23 } 24 } 25 else 26 { 27 Square *s = new Square(800 +40+ Cameraposition[0],310); 28 objects.push_back(s); 29 s->SetAlive(true); 30 31 if( !(square->GetX() - 40 < s->GetX() + 40 && square->GetX() + 40 > s->GetX() - 40) && 32 !(triangle->GetX() - 15 < s->GetX() + 40 && triangle->GetX() + 15 > s->GetX() - 40)) 33 continue; 34 35 36 else if( triangle->GetAlive()==false ) 37 { 38 square->SetAlive(true); 39 square->SetX(s->GetX()); 40 square->SetY(s->GetY()); 41 } 42 } 43 } 44 } 45} 46 47 48// I have this calling in main function as below: 49if(rand() % 200 == 0) 50{ 51 // Create anywhere from 1 to maxToCreate (which is 4) obstacles at a time. 52 const int maxToCreate = 4; 53 int numToCreate = (rand() % maxToCreate) + 1; 54 55 // Call the function to make some obstacles. 56 createObstacles(numToCreate); 57} Now also i am facing a problem.:-[I think there is some conflict between creation of triangles and that of squares. No triangle overlaps other triangle. No Square overlaps other square. But either triangle overlap a square which is already drawn, OR a square overlaps an already drawn triangle. Can you see any error in my triangle/square creation code?When they overlap, somehow both get drawn on same place such that their x & y coordinates (or Their centre points) are same.
 Dizzy Egg Member #10,824 March 2009 *(also thanks to Dizzy for making my rand()% range smaller ) ----------------------------------------------------Please check out my songs:https://soundcloud.com/dont-rob-the-machina
 Lupuss.Umbrae Member #8,387 March 2007 CodeOne said: I think there is some conflict between creation of triangles and that of squares. No triangle overlaps other triangle. No Square overlaps other square. But either triangle overlap a square which is already drawn, OR a square overlaps an already drawn triangle. Can you see any error in my triangle/square creation code?When they overlap, somehow both get drawn on same place such that their x & y coordinates (or Their centre points) are same. Lupuss.Umbrae said: What exactly are the square and triangle object and how and where are they defined? I think you should getrid of ``` if(!(triangle->GetX() - 15 < t->GetX() + 15 && triangle->GetX() + 15 > t->GetX() - 15) && !(square->GetX() - 40 < t->GetX() + 15 && square->GetX() + 40 > t->GetX() - 15) ) continue; ``` altoether and do what Audric said Audric said: When you add an obstacle (or series), you should compute the width of what you've created. Don't create anything more until the screen has scrolled by this width + the minimum distance you want between two series. P.S.: CodeOne said: Guys look I tried this thing to get the combination of obstacles like KILLOCAN said, using what Lupuss suggested. Sorry, but I recognize neither his, nor my approach... --------------------------------------------------I'm not a signature, I'm just cleaning here...
 CodeOne Member #15,442 December 2013 #SelectExpand 1Square::Square(float x,float y) 2{ 3 GameObject::Init(x,y,0,0,0,40,15); 4 SetID(SQUARE); 5 SetAlive(false); 6} 7 8//Render 9al_draw_filled_rectangle(x-40 , y+15 , x + 40, y - 15 , al_map_rgb(0,255,255)); 10 11Triangle::Triangle(float x,float y) 12{ 13 GameObject::Init(x,y,0,0,0,12,6); //(float x,float y,float velX,int dirX, int dirY, int boundX, int boundY) 14 SetID(ENEMY); 15 SetAlive(false); 16} 17 18//Render 19al_draw_filled_triangle(x-15 , y+6 , x + 15, y +6,x ,y - 34, al_map_rgb(255,0,0)); Lupuss if you didnt notice I tried to club the idea of creating a combination of random size (Between 1-4) by improvising on my previous code where you implemented rand()%2 ==0 inside rand()%200==0 ... I just implemented those rand() statements seperately, one in void createObstacles() and another inside the main function where void createobstacles() is being called.Dizzy, haha...#oops Thanks to you too.But hey guyz, hello! I am still facing the issue. I am still working on it,meanwhile if you get any idea , I'll be waiting.
 Edgar Reynaldo Major Reynaldo May 2007 You need to implement code for each combination of obstacle you want to do.#SelectExpand 1switch (rand()%4) { 2 // x is triangle and o is square 3 case 0 : 4 // xx - make two triangles together 5 6 break; 7 case 1 : 8 // xy - make a triangle and square together 9 break; 10 case 2 : 11 // yx - make a square and a triangle together 12 13 break; 14 case 3 : 15 // yy - make two squares together 16 17 break; 18 case default : 19 printf("Combination not identified.\n"); 20 break; 21} Or make an array of rand()%3 (empty / triangle / square) to determine what is coming up next and then add those objects to your game.
 CodeOne Member #15,442 December 2013 Edgar, Thanx man...I was making things complex for no reason.Classic methods always work :Lesson learnedThanx everyone for looking into my confusing codes UPDATE: Now where did this 'give credits section' disappear along with that 'Problem Solved' tab.
 Edgar Reynaldo Major Reynaldo May 2007 Actually I would go with the second method. Use an array / list / vector to store a sequence of randomly generated objects with possible empty spaces between them. The first method means largely writing more code than you should, since its easier to work on a whole array instead of all the specific use cases.The 'Give Credits' option is only available if you make the new thread classification 'a question with a specific answer'.
 CodeOne Member #15,442 December 2013 Edgar, I am newbie. I dont have exposure with things. The first method was easy to go for since I have been doing it in ever since i started coding.But second method, I never used an array to randomize things. And, if i have to store sequences in array how does it decrease the amount of code that i have to right.I would like to learn this method too. If you have a little time, can you give me a quick brief about second method?
 killocan Member #1,556 October 2001 What i suggest can be expressed(in pseudo correct code hehehe) as something like this:#SelectExpand 1 const char *objs[] = {"x", "xoox", "oo", "xxx", "xx", "xoxo"}; 2 ... //somecode... 3 4 //randomize your creation i.e: (rand()%200 == 0) 5 int index = rand()%6; 6 const char * cur_seq = objs[index]; 7 for (int i = 0; i < strlen(cur_seq); ++i) 8 { 9 switch(cur_seq[i]) 10 { 11 case 'x': 12 ...//Create something (quad) 13 break; 14 case 'o': 15 ...//Create anotherthing (tri) 16 break; 17 } 18 } You can make it an std::vector(if you like) (instead of array) and load it with random shapes during the stage setup, then use it like i did.
 Edgar Reynaldo Major Reynaldo May 2007 First , determine a random size for the number of obstacles in this object.Then determine which obstacles is which - ie which is a triangle / square / empty space.For each obstacle, push it back onto the vector of objects.
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads