#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <cctype>
using namespace std;

bool isGoal(int);
bool prompt();
void timeOutPrompt();
bool isCollision();
const int fieldx = 100;
const int fieldy = 100;
const int num_players = 3;

class robot {
public:
  int x;
  int y;
  
  /*
   *  Prints robot coordinates
   */
  void print() {
    cout << "pos:" << " (" << x << "," << y << ")\n";
    return;
  } // end print()
  
  /*
   *  Initializes robot coordinates to random x and y
   */
  void initCoords() {
    x = rand()%fieldx;
    y = rand()%fieldy;
    return;
  } // end initCoords()
  
  /*
   *  Moves robot randomly in 1 of 8 directions
   */
  void move() {
    // increment or decrement x
    if (rand()%2) {
      if (x <= fieldx-1)
        x += rand()%2;
    } else {
      if (x >= 0)
        x -= rand()%2;
    }
    // increment or decrement y
    if (rand()%2) {
      if (y <= fieldy-1)
        y += rand()%2;
    } else {
      if (y >= 0)
        y -= rand()%2;
    }
    return;
  } // end move()
} player[num_players];

/*
 * User interface to start game 
 */
bool prompt() {
  char start;
  cout << "Welcome to 3 blind robots, press ENTER to continue" << endl;
  cout << "Press S to start Q to quit" << endl;
  while (1) {
    cin >> start;
    toupper(start);
    switch (start) {
      case 'S':
        return 1;
      case 'Q':
        return 0;
      default:
        cout << "Please enter S to start, Q to quit" << endl;
        break;
    }
  }
  return 0;
}

/*
 * Gives user the option to quit after a collision has occurred 
 */
void timeOutPrompt() {
  char resume;
  cout << "A collision has occured, press R to resume" << endl;
  cout << "press Q to exit program" << endl;
  while (1) {
    cin >> resume;
    toupper(resume);
    switch (resume) {
      case 'R':
        return;
      case 'Q':
        exit(0);
      default:
        cout << "Please enter R to resume, Q to quit" << endl;
        break;
    }
  }
  return;
}

/*
 * Checks to see if a robot has scored a goal
 */
bool isGoal(int rnum) {
  if (player[rnum].x >= fieldx-10 
      && player[rnum].y > fieldy-40 && player[rnum].y < fieldy-10)
    return true;
  
  return false;
}

/*
 * Checks to see if a robot has collided with another
 */
bool isCollision() {
  for (int i = 0; i < num_players - 1; i++) {
    for (int j = i+1; j < num_players; j++) {
      if (player[i].x == player[j].x && player[i].y == player[j].y)
        return 1;
    }
  }

  return 0;
}

/*
 * Main thread
 */
int main() {
  srand(time(NULL));
  bool goal = 0;  
  int rnum;
  int winner;
  
  if (prompt()) {
    for (int i = 0; i < num_players; i++)
      player[i].initCoords();
    
    while (!goal) {
      for (int rnum = 0; rnum < num_players; rnum++) {
        player[rnum].move();
        cout << "player #" << rnum << " ";
        player[rnum].print();
        goal = isGoal(rnum);
        if (goal)
          winner = rnum;
      }
      if (isCollision())
        timeOutPrompt();
    }
    cout << "GOAL" << endl;
    cout << "robot " << winner << " is the winner!" << endl;
  }
  cout << "Goodbye!" <<endl;
  
  return 0;
}