Code to run the microcontrollers on the R5 competition bot

Dependencies:   LineSensors mbed

Committer:
Hypna
Date:
Sat Apr 18 02:54:55 2015 +0000
Revision:
15:150ec9448efc
experiment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hypna 15:150ec9448efc 1 #include "r5map.h"
Hypna 15:150ec9448efc 2 #include <iostream>
Hypna 15:150ec9448efc 3 #include <cstdlib>
Hypna 15:150ec9448efc 4
Hypna 15:150ec9448efc 5 R5Map::R5Map(int conSize) : size(conSize)
Hypna 15:150ec9448efc 6 {
Hypna 15:150ec9448efc 7 int cellCount = 1;
Hypna 15:150ec9448efc 8
Hypna 15:150ec9448efc 9 for(int i = 6; i >= 0; i--)
Hypna 15:150ec9448efc 10 {
Hypna 15:150ec9448efc 11 for(int j = 0; j < 7; j++)
Hypna 15:150ec9448efc 12 {
Hypna 15:150ec9448efc 13 map[j][i].number = cellCount++;
Hypna 15:150ec9448efc 14 }
Hypna 15:150ec9448efc 15 }
Hypna 15:150ec9448efc 16
Hypna 15:150ec9448efc 17 switch(size)
Hypna 15:150ec9448efc 18 {
Hypna 15:150ec9448efc 19 case 5 :
Hypna 15:150ec9448efc 20 start.x = 5;
Hypna 15:150ec9448efc 21 start.y = 0;
Hypna 15:150ec9448efc 22 fin.x = 0;
Hypna 15:150ec9448efc 23 fin.y = 5;
Hypna 15:150ec9448efc 24 pos = start;
Hypna 15:150ec9448efc 25 break;
Hypna 15:150ec9448efc 26 case 6 :
Hypna 15:150ec9448efc 27 start.x = 5;
Hypna 15:150ec9448efc 28 start.y = 0;
Hypna 15:150ec9448efc 29 fin.x = 0;
Hypna 15:150ec9448efc 30 fin.y = 6;
Hypna 15:150ec9448efc 31 pos = start;
Hypna 15:150ec9448efc 32 break;
Hypna 15:150ec9448efc 33 case 7 :
Hypna 15:150ec9448efc 34 start.x = 6;
Hypna 15:150ec9448efc 35 start.y = 0;
Hypna 15:150ec9448efc 36 fin.x = 0;
Hypna 15:150ec9448efc 37 fin.y = 5;
Hypna 15:150ec9448efc 38 pos = start;
Hypna 15:150ec9448efc 39 break;
Hypna 15:150ec9448efc 40 default :
Hypna 15:150ec9448efc 41 cerr<<"ERROR: Invalid R5Map size. Exiting."<<endl;
Hypna 15:150ec9448efc 42 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 43 }
Hypna 15:150ec9448efc 44 }
Hypna 15:150ec9448efc 45
Hypna 15:150ec9448efc 46 void R5Map::setPosition(position spot)
Hypna 15:150ec9448efc 47 {
Hypna 15:150ec9448efc 48 if(spot.x >= 0 && spot.y >= 0 && spot.x <= 6 && spot.y <= 6)
Hypna 15:150ec9448efc 49 {
Hypna 15:150ec9448efc 50 pos = spot;
Hypna 15:150ec9448efc 51 map[pos.x][pos.y].visited = true;
Hypna 15:150ec9448efc 52 }
Hypna 15:150ec9448efc 53 else
Hypna 15:150ec9448efc 54 {
Hypna 15:150ec9448efc 55 cerr<<"ERROR: Position assignment with R5Map::setPosition(position) outside map bounds!"<<endl;
Hypna 15:150ec9448efc 56 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 57 }
Hypna 15:150ec9448efc 58 }
Hypna 15:150ec9448efc 59
Hypna 15:150ec9448efc 60 void R5Map::setStart(position newStart)
Hypna 15:150ec9448efc 61 {
Hypna 15:150ec9448efc 62 if(newStart.x >= 0 && newStart.y >= 0 && newStart.x <= 6 && newStart.y <= 6)
Hypna 15:150ec9448efc 63 start = newStart;
Hypna 15:150ec9448efc 64 else
Hypna 15:150ec9448efc 65 {
Hypna 15:150ec9448efc 66 cerr<<"ERROR: Start assignment with R5Map::setStart(position) outside map Bounds!"<<endl;
Hypna 15:150ec9448efc 67 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 68 }
Hypna 15:150ec9448efc 69 }
Hypna 15:150ec9448efc 70
Hypna 15:150ec9448efc 71 void R5Map::setFinish(position newFin)
Hypna 15:150ec9448efc 72 {
Hypna 15:150ec9448efc 73 if(newFin.x>= 0 && newFin.y >= 0 && newFin.x <= 6 && newFin.y <= 6)
Hypna 15:150ec9448efc 74 fin = newFin;
Hypna 15:150ec9448efc 75 else
Hypna 15:150ec9448efc 76 {
Hypna 15:150ec9448efc 77 cerr<<"ERROR: Finsih assignment with R5Map::setFinish(position) outside map Bounds!"<<endl;
Hypna 15:150ec9448efc 78 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 79 }
Hypna 15:150ec9448efc 80 }
Hypna 15:150ec9448efc 81
Hypna 15:150ec9448efc 82 void R5Map::setWall(position cell, direction dir, wallState state)
Hypna 15:150ec9448efc 83 {
Hypna 15:150ec9448efc 84 if(cell.x >= 0 && cell.y >= 0 && cell.x <= 6 && cell.y <= 6)
Hypna 15:150ec9448efc 85 {
Hypna 15:150ec9448efc 86 switch(dir)
Hypna 15:150ec9448efc 87 {
Hypna 15:150ec9448efc 88 case NORTH : map[cell.x][cell.y].north = state;
Hypna 15:150ec9448efc 89 break;
Hypna 15:150ec9448efc 90 case SOUTH : map[cell.x][cell.y].south = state;
Hypna 15:150ec9448efc 91 break;
Hypna 15:150ec9448efc 92 case EAST : map[cell.x][cell.y].east = state;
Hypna 15:150ec9448efc 93 break;
Hypna 15:150ec9448efc 94 case WEST : map[cell.x][cell.y].west = state;
Hypna 15:150ec9448efc 95 }
Hypna 15:150ec9448efc 96 }
Hypna 15:150ec9448efc 97 else
Hypna 15:150ec9448efc 98 {
Hypna 15:150ec9448efc 99 cerr<<"ERROR: Position selection in R5Map::setWall(position, cell, direction) outside map bounds!"<<endl;
Hypna 15:150ec9448efc 100 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 101 }
Hypna 15:150ec9448efc 102 }
Hypna 15:150ec9448efc 103
Hypna 15:150ec9448efc 104 void R5Map::setCurWall(direction dir, wallState state)
Hypna 15:150ec9448efc 105 {
Hypna 15:150ec9448efc 106 switch(dir)
Hypna 15:150ec9448efc 107 {
Hypna 15:150ec9448efc 108 case NORTH : map[pos.x][pos.y].north = state;
Hypna 15:150ec9448efc 109 break;
Hypna 15:150ec9448efc 110 case SOUTH : map[pos.x][pos.y].south = state;
Hypna 15:150ec9448efc 111 break;
Hypna 15:150ec9448efc 112 case EAST : map[pos.x][pos.y].east = state;
Hypna 15:150ec9448efc 113 break;
Hypna 15:150ec9448efc 114 case WEST : map[pos.x][pos.y].west = state;
Hypna 15:150ec9448efc 115 }
Hypna 15:150ec9448efc 116 }
Hypna 15:150ec9448efc 117
Hypna 15:150ec9448efc 118 void R5Map::setVisited(position cell)
Hypna 15:150ec9448efc 119 {
Hypna 15:150ec9448efc 120 if(cell.x >= 0 && cell.y >= 0 && cell.x <= 6 && cell.y <= 6)
Hypna 15:150ec9448efc 121 map[cell.x][cell.y].visited = true;
Hypna 15:150ec9448efc 122 else
Hypna 15:150ec9448efc 123 {
Hypna 15:150ec9448efc 124 cerr<<"ERROR: Position selection in R5Map::setVisited(position) outside of map bounds!"<<endl;
Hypna 15:150ec9448efc 125 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 126 }
Hypna 15:150ec9448efc 127 }
Hypna 15:150ec9448efc 128
Hypna 15:150ec9448efc 129 void R5Map::setCurVisited() { map[pos.x][pos.y].visited = true; }
Hypna 15:150ec9448efc 130
Hypna 15:150ec9448efc 131 void R5Map::addSymbol(position cell, char symbol)
Hypna 15:150ec9448efc 132 {
Hypna 15:150ec9448efc 133 if(cell.x >= 0 && cell.y >= 0 && cell.x <= 6 && cell.y <= 6)
Hypna 15:150ec9448efc 134 {
Hypna 15:150ec9448efc 135 vector<char>::iterator iter;
Hypna 15:150ec9448efc 136
Hypna 15:150ec9448efc 137 iter = map[cell.x][cell.y].symbols.begin();
Hypna 15:150ec9448efc 138 map[cell.x][cell.y].symbols.insert(iter,symbol);
Hypna 15:150ec9448efc 139 }
Hypna 15:150ec9448efc 140 else
Hypna 15:150ec9448efc 141 {
Hypna 15:150ec9448efc 142 cerr<<"ERROR: Position selection in R5Map::addSymbol(position, char) outside of map bounds!"<<endl;
Hypna 15:150ec9448efc 143 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 144 }
Hypna 15:150ec9448efc 145 }
Hypna 15:150ec9448efc 146
Hypna 15:150ec9448efc 147 void R5Map::addCurSymbol(char symbol)
Hypna 15:150ec9448efc 148 {
Hypna 15:150ec9448efc 149 vector<char>::iterator iter;
Hypna 15:150ec9448efc 150
Hypna 15:150ec9448efc 151 iter = map[pos.x][pos.y].symbols.begin();
Hypna 15:150ec9448efc 152 map[pos.x][pos.y].symbols.insert(iter,symbol);
Hypna 15:150ec9448efc 153 }
Hypna 15:150ec9448efc 154
Hypna 15:150ec9448efc 155 position R5Map::getPosition() const { return pos; }
Hypna 15:150ec9448efc 156
Hypna 15:150ec9448efc 157 position R5Map::getStart() const { return start; }
Hypna 15:150ec9448efc 158
Hypna 15:150ec9448efc 159 position R5Map::getFinish() const { return fin; }
Hypna 15:150ec9448efc 160
Hypna 15:150ec9448efc 161 wallState R5Map::getWall(position cell, direction dir) const
Hypna 15:150ec9448efc 162 {
Hypna 15:150ec9448efc 163 if(cell.x >= 0 && cell.y >= 0 && cell.x <= 6 && cell.y <= 6)
Hypna 15:150ec9448efc 164 {
Hypna 15:150ec9448efc 165 switch(dir)
Hypna 15:150ec9448efc 166 {
Hypna 15:150ec9448efc 167 case NORTH : return map[cell.x][cell.y].north;
Hypna 15:150ec9448efc 168 case SOUTH : return map[cell.x][cell.y].south;
Hypna 15:150ec9448efc 169 case EAST : return map[cell.x][cell.y].east;
Hypna 15:150ec9448efc 170 case WEST : return map[cell.x][cell.y].west;
Hypna 15:150ec9448efc 171 }
Hypna 15:150ec9448efc 172 }
Hypna 15:150ec9448efc 173 else
Hypna 15:150ec9448efc 174 {
Hypna 15:150ec9448efc 175 cerr<<"ERROR: Position selection in R5Map::getWall(position, direction) outside of map bounds!"<<endl;
Hypna 15:150ec9448efc 176 exit(EXIT_FAILURE);
Hypna 15:150ec9448efc 177 }
Hypna 15:150ec9448efc 178 }
Hypna 15:150ec9448efc 179
Hypna 15:150ec9448efc 180 wallState R5Map::getCurWall(direction dir) const
Hypna 15:150ec9448efc 181 {
Hypna 15:150ec9448efc 182 switch(dir)
Hypna 15:150ec9448efc 183 {
Hypna 15:150ec9448efc 184 case NORTH : return map[pos.x][pos.y].north;
Hypna 15:150ec9448efc 185 case SOUTH : return map[pos.x][pos.y].south;
Hypna 15:150ec9448efc 186 case EAST : return map[pos.x][pos.y].east;
Hypna 15:150ec9448efc 187 case WEST : return map[pos.x][pos.y].west;
Hypna 15:150ec9448efc 188 }
Hypna 15:150ec9448efc 189 }
Hypna 15:150ec9448efc 190
Hypna 15:150ec9448efc 191 bool R5Map::getVisited(position cell) const
Hypna 15:150ec9448efc 192 {
Hypna 15:150ec9448efc 193 if(cell.x >= 0 && cell.y >= 0 && cell.x <= 6 && cell.y <= 6)
Hypna 15:150ec9448efc 194 return map[cell.x][cell.y].visited;
Hypna 15:150ec9448efc 195 else
Hypna 15:150ec9448efc 196 {
Hypna 15:150ec9448efc 197 cerr<<"ERROR: Position selection in R5Map::getVisited(position) outside of map bounds!"<<endl;
Hypna 15:150ec9448efc 198 return false;
Hypna 15:150ec9448efc 199 }
Hypna 15:150ec9448efc 200 }
Hypna 15:150ec9448efc 201
Hypna 15:150ec9448efc 202 bool R5Map::getCurVisited() const { return map[pos.x][pos.y].visited; }
Hypna 15:150ec9448efc 203
Hypna 15:150ec9448efc 204 vector<char> R5Map::getSymbols(position cell) const
Hypna 15:150ec9448efc 205 {
Hypna 15:150ec9448efc 206 if(cell.x >= 0 && cell.y >= 0 && cell.x <= 6 && cell.y <= 6)
Hypna 15:150ec9448efc 207 return map[cell.x][cell.y].symbols;
Hypna 15:150ec9448efc 208 else
Hypna 15:150ec9448efc 209 {
Hypna 15:150ec9448efc 210 cerr<<"ERROR: Position selection in R5Map::getSymbols(position) outside of map bounds!"<<endl;
Hypna 15:150ec9448efc 211 vector<char> empty;
Hypna 15:150ec9448efc 212 return empty;
Hypna 15:150ec9448efc 213 }
Hypna 15:150ec9448efc 214 }
Hypna 15:150ec9448efc 215
Hypna 15:150ec9448efc 216 vector<char> R5Map::getCurSymbols() const { return map[pos.x][pos.y].symbols; }
Hypna 15:150ec9448efc 217
Hypna 15:150ec9448efc 218 int R5Map::getSize() const { return size; }
Hypna 15:150ec9448efc 219
Hypna 15:150ec9448efc 220 void R5Map::printMap() const
Hypna 15:150ec9448efc 221 {
Hypna 15:150ec9448efc 222 for(int i = 0; i < size; i++)
Hypna 15:150ec9448efc 223 {
Hypna 15:150ec9448efc 224 for(int k = 0; k < size; k++)
Hypna 15:150ec9448efc 225 {
Hypna 15:150ec9448efc 226 cout<<"cell : "<<i+1<<','<<k+1<<endl;
Hypna 15:150ec9448efc 227 cout<<" North : "<<map[i][k].north<<endl;
Hypna 15:150ec9448efc 228 cout<<" South : "<<map[i][k].south<<endl;
Hypna 15:150ec9448efc 229 cout<<" East : "<<map[i][k].east<<endl;
Hypna 15:150ec9448efc 230 cout<<" West : "<<map[i][k].west<<endl;
Hypna 15:150ec9448efc 231 cout<<" Symbols : ";
Hypna 15:150ec9448efc 232
Hypna 15:150ec9448efc 233 for(int j = 0; j < map[i][k].symbols.size(); j++)
Hypna 15:150ec9448efc 234 {
Hypna 15:150ec9448efc 235 cout<<map[i][k].symbols.at(j)<<" ";
Hypna 15:150ec9448efc 236 }
Hypna 15:150ec9448efc 237 cout<<endl<<"----------------------------"<<endl<<endl;
Hypna 15:150ec9448efc 238 }
Hypna 15:150ec9448efc 239 }
Hypna 15:150ec9448efc 240 }
Hypna 15:150ec9448efc 241
Hypna 15:150ec9448efc 242 R5Map::~R5Map() { }