aboutsummaryrefslogtreecommitdiffstats
path: root/YetiHack/src/yeti.cpp
diff options
context:
space:
mode:
authorRay Hogenson <rayhogenson@gmail.com>2012-01-10 15:24:39 -0900
committerRay Hogenson <rayhogenson@gmail.com>2012-01-10 15:24:39 -0900
commitc1d1a4cfb55e87fb4db68980b3af9c31fde363a9 (patch)
tree2d0f92a791c1151a9683788b808dd6eaf4857c2f /YetiHack/src/yeti.cpp
parentae0551652fa132bb512638433189c1290933164e (diff)
parent4e87a290412cea63f548760de6f4a91b1d1ba352 (diff)
downloadyetihack-c1d1a4cfb55e87fb4db68980b3af9c31fde363a9.tar.zst
Merge branch 'master' of github.com:rayhogenson/YetiHack
Diffstat (limited to 'YetiHack/src/yeti.cpp')
-rw-r--r--YetiHack/src/yeti.cpp281
1 files changed, 281 insertions, 0 deletions
diff --git a/YetiHack/src/yeti.cpp b/YetiHack/src/yeti.cpp
new file mode 100644
index 0000000..eda9d2c
--- /dev/null
+++ b/YetiHack/src/yeti.cpp
@@ -0,0 +1,281 @@
+#include "yeti.h"
+#include <cstdlib>
+#include <ctime>
+#include <vector>
+using namespace std;
+void yeti::moveleft()
+{
+ --x;
+}
+void yeti::moveright()
+{
+ ++x;
+}
+void yeti::moveup()
+{
+ --y;
+}
+void yeti::movedown()
+{
+ ++y;
+}
+yeti::yeti(int x1, int y1, int sizex1, int sizey1)
+{
+ sizex = sizex1;
+ sizey = sizey1;
+ x = x1;
+ y = y1;
+}
+void yeti::movetowards(int a, int b, vector<yeti> yetis, int i)
+{
+ int random = rand() % 2;
+ int left;
+ int right;
+ int real;
+ if (random == 0)
+ {
+ left = (x) + (sizex - a);
+ right = a + (sizex - x);
+ real = absolute(x - a);
+ if (left < right && left < real)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int anew = x - 1;
+ if (anew > sizex)
+ {
+ anew = 0;
+ }
+ if (anew < 1)
+ {
+ anew = sizex;
+ }
+ if (i != j && anew == yetis[j].xpos() && yetis[j].ypos() == y)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ moveleft();
+ }
+ }
+ else if (right < left && right < real)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int anew = x + 1;
+ if (anew > sizex)
+ {
+ anew = 0;
+ }
+ if (anew < 1)
+ {
+ anew = sizex;
+ }
+ if (i != j && anew == yetis[j].xpos() && yetis[j].ypos() == y)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ moveright();
+ }
+ }
+ else
+ {
+ if (x - a < 0)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int anew = x + 1;
+ if (anew > sizex)
+ {
+ anew = 0;
+ }
+ if (anew < 1)
+ {
+ anew = sizex;
+ }
+ if (i != j && anew == yetis[j].xpos() && yetis[j].ypos() == y)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ moveright();
+ }
+ }
+ else if (x - a > 0)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int anew = x - 1;
+ if (anew > sizex)
+ {
+ anew = 0;
+ }
+ if (anew < 1)
+ {
+ anew = sizex;
+ }
+ if (i != j && anew == yetis[j].xpos() && yetis[j].ypos() == y)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ moveleft();
+ }
+ }
+ }
+ }
+ // now for b
+ else
+ {
+ left = (y) + (sizey - b);
+ right = b + (sizey - y);
+ real = absolute(y - b);
+ if (left < right && left < real)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int bnew = y - 1;
+ if (bnew > sizey)
+ {
+ bnew = 0;
+ }
+ if (bnew < 1)
+ {
+ bnew = sizey;
+ }
+ if (i != j && x == yetis[j].xpos() && yetis[j].ypos() == bnew)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ moveup();
+ }
+ }
+ else if (right < left && right < real)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int bnew = y + 1;
+ if (bnew > sizey)
+ {
+ bnew = 0;
+ }
+ if (bnew < 1)
+ {
+ bnew = sizey;
+ }
+ if (i != j && x == yetis[j].xpos() && yetis[j].ypos() == bnew)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ movedown();
+ }
+ }
+ else
+ {
+ if (y - b < 0)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int bnew = y + 1;
+ if (bnew > sizey)
+ {
+ bnew = 0;
+ }
+ if (bnew < 1)
+ {
+ bnew = sizey;
+ }
+ if (i != j && x == yetis[j].xpos() && yetis[j].ypos() == bnew)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ movedown();
+ }
+ }
+ else if (y - b > 0)
+ {
+ bool empty = true;
+ for (int j = 0; j < yetis.size(); ++j)
+ {
+ int bnew = y - 1;
+ if (bnew > sizey)
+ {
+ bnew = 0;
+ }
+ if (bnew < 1)
+ {
+ bnew = sizey;
+ }
+ if (i != j && x == yetis[j].xpos() && yetis[j].ypos() == bnew)
+ {
+ empty = false;
+ }
+ }
+ if (empty)
+ {
+ moveup();
+ }
+ }
+ }
+ }
+ // deal with going off the side
+ if (x < 1)
+ {
+ x = sizex;
+ }
+ if (x > sizex)
+ {
+ x = 1;
+ }
+ if (y < 1)
+ {
+ y = sizey;
+ }
+ if (y > sizey)
+ {
+ y = 1;
+ }
+}
+
+int yeti::xpos()
+{
+ return x;
+}
+int yeti::ypos()
+{
+ return y;
+}
+int yeti::absolute(int value)
+{
+ if (value < 0)
+ {
+ value = 0 - value;
+ }
+ return value;
+}
+