#include #include #include "common.h" extern double drand48(); inline bool isOdd(int x); inline int square (int x); // Returns true if x is odd, false if it is even inline bool isOdd(int x) { return x & 0x01; } // Returns the square of x inline int square (int x) { return x * x; } // Returns what the change in the x-coordinate will be of an object // moving in direction dir int xChange (int dir) { switch (dir) { case N: case S: return 0; case W: return -1; case E: return 1; default: cerr << "Direction " << dir << " passed to xChange" << endl; return 0; } } // Returns what the change in the y-coordinate will be of an object // moving in direction dir int yChange (int dir) { switch (dir) { case N: return -1; case S: return 1; case W: case E: return 0; default: cerr << "Direction " << dir << " passed to yChange" << endl; doNothing(); return 1; } } // Returns a random direction int randomDirection () { return int(drand48() * NUM_DIRECTIONS); } // Returns the next direction (to the right) of dir int nextDir (int dir) { return (dir + 1) % NUM_DIRECTIONS; } // Returns the opposite direction of dir int backDir (int dir) { return (dir + 2) % NUM_DIRECTIONS; } // Returns the previous direction before dir int prevDir (int dir) { return (dir + 3) % NUM_DIRECTIONS; } // Returns 10^p int expTen (int p) { int val = 1; while (p > 0) { if (isOdd(p)) { p--; val *= 10; } else { val >>= 1; val = square(val); } }; return val; } // Returns the numerical value of a character digit int digitVal (char c) { return c - 0x30; } template void Swap (T& a, T& b) { T temp; temp = a; a = b; b = temp; } // Randomly returns true or false bool coinFlip () { return drand48() * 2; } // Randoms a random integer between low and high, inclusive int randomBetween (int low, int high) { return low + int(drand48() * (high - low + 1)); }