SimulatorGUI.cpp
Go to the documentation of this file.
1 #include "SimulatorGUI.hpp"
2 #include <cstdlib>
3 #include <ctime>
4 #include <iostream>
5 
6 /*"sleep" is defined in POSIX header <unistd.h> but as usual
7  * Windows is a special snowflake and does not include the
8  * POSIX standard headers everyone else supports.
9 */
10 
11 #ifdef _WIN32
12 
13 //Windows
14 #include <windows.h>
15 void sleep(unsigned int seconds) { Sleep(seconds*1000); }
16 
17 #else
18 
19 //All other OSes
20 #include <unistd.h>
21 
22 #endif
23 
24 using std::atoi;
25 using std::cout;
26 using std::endl;
27 using std::srand;
28 using std::time;
29 
31 
33 {
34  const auto& world = current_sim.getWorldGrid();
35  for(const auto& row : world)
36  {
37  for(const auto& cell : row)
38  switch(cell.contents)
39  {
40  case robot_api::BLOCKED: cout << 'X';
41  break;
42  case robot_api::SELF: cout << current_sim.getOccupantPlayer(cell);
43  break;
44  case robot_api::WALL: cout << '+';
45  break;
46  case robot_api::FORT:
47  switch(cell.fort_orientation)
48  {
49  case robot_api::UP: cout << '^';
50  break;
51  case robot_api::DOWN: cout << 'v';
52  break;
53  case robot_api::LEFT: cout << '<';
54  break;
55  case robot_api::RIGHT: cout << '>';
56  break;
57  }
58  break;
59  case robot_api::CAPSULE: cout << 'o';
60  break;
61  case robot_api::EMPTY: cout << '*';
62  break;
63  }
64  cout << endl;
65  }
66  cout << endl;
67 
68  int ret;
69  try
70  {
71  ret = current_sim.executeSingleTimeStep();
72  }
73  catch(RoboSimExecutionException e)
74  {
75  cout << "An error occurred during execution: " << e.msg << endl;
76  ret=-1;
77  }
78 
79  return ret==-1;
80 }
81 
82 int main(int argc, const char** argv)
83 {
84  int x=20, y=20, skill_points=20, bots_per_player=5, obstacles=30, naptime=3;
85  if(argc>=6)
86  {
87  x=atoi(argv[1]);
88  y=atoi(argv[2]);
89  skill_points=atoi(argv[3]);
90  bots_per_player=atoi(argv[4]);
91  obstacles=atoi(argv[5]);
92  }
93  if(argc==7)
94  naptime=atoi(argv[6]);
95  if(argc==2)
96  naptime=atoi(argv[1]);
97 
98  //RNG should be seeded before simulation
99  //srand(time(NULL));
100 
101  SimulatorGUI gui{x,y,skill_points,bots_per_player,obstacles};
102  try
103  {
104  while(gui.do_timestep())
105  sleep(naptime);
106  }
107  catch(RoboSimExecutionException e)
108  {
109  cout << "Failed with Robot Exception: " << e.msg << endl;
110  }
111 
112  return 0;
113 }
RoboSim current_sim
bool do_timestep()
vector< vector< GridCell > > & getWorldGrid()
Definition: RoboSim.hpp:49
int getOccupantPlayer(const GridCell &cell) const
Definition: RoboSim.hpp:54