About checking fields around in 2 dimensional array [C++] -


i know might sound stupid, got ask.

it's easy thing.

you have 2 dimensional array, elements inside or empty, location(x , y) , have draw 1 field free fields around.

i know how it, doesn't seems ... elegant or nice. way doing checking if i'm on max left, max right, top, bottom, etc. if there in fields around, , rand().

it's long , seems unpleasent. don't know if there's shorter way? thanks.

and sorry english, doing best.

there might performance issues. critic situation when have point [x, y] in corner, rand() can possibly select invalid elements multiple times, have rand() , check again.

the way i'd it, check available neighbours , push each valid neighbour std::vector. after that, there's 1 random number generation chooses 1 element within vector:

std::vector<coordinate> validneighbours; // coordinate struct x , y integers, can use std::pair<int, int> or pointers elements  if(/* has neighbour left*/)     validneighbours.push_back(coordinate(x - 1, y));  // check in other directions  coordinate c = validneighbours[std::rand() % validneighbours.size()]; 

you might want check whether validneighbours not empty before performing modulo, in case have 1x1 array (validneighbours.size() 0).


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -