c++ - Comparing coordinates of two array indexes -
i creating basic c++ game involves user trying escape zombies.
the zombies stored in array called zeds
. there traps laid out throughout game supposed reset zombies initial position, stored in array named holes
.
i trying figure out how can compare coordinates of zombies coordinates of holes, , if match, reset zombies.
i don't know how i'd begin comparing coordinates of zeds coordinates of holes. have tried writing this, doesn't work.
for (int = 0; < maxzeds; ++i) { (int j = 0; < maxholes; ++j) { if (zeds[i].x == holes[j].x && zeds[i].y == holes[j].y) { //reset cords code here } } }
appreciate help,
thanks guys!
you have several typos in code , supposed j in thirs line. anyway, can inhence readabilty of code using oop practices. let's have point class holds x , y (which suppose have)
srtuct point{ int x,y; };
first lets overload ==
operator in order comapare points
srtuct point{ int x,y; bool operator == (const point& rhs){ return x==rhs.x && y == rhs.y; } };
now lets use range-semantics instead of indexes , c++11 features
for (auto& zombiecoord : zeds){ (auto& holecoord : holes){ if (zombiecoord == holecoord ){ //do somthing } } }
isn't smaller, cleaner , more clear ?
things might want :
continued using x , y public members did, suggest making them private , make getters (and if needed) setters.
might want call them in convention, m_x , m_y, signifies them members.
also, developer may not understand zeds
may mean. why not give clear name "ongoingzombies"?
Comments
Post a Comment