void MapFactory::addSpawn_(const ptree::value_type &v) { int id = v.second.get<int>("id"); if(id < 0) throw MapException("Wrong ID!"); if(vertices.find(id) == vertices.end()) { double x = v.second.get<double>("x"); double y = v.second.get<double>("y"); int next = v.second.get<int>("next"); // dodawanie drogi if(next < id){ if(vertices.find(next) != vertices.end()) { Vertex ver = boost::add_vertex(*g); vertices[id] = ver; ((*g)[ver]).id = id; PMapElement s(new Spawn(id, x, y, verticesElements[next]->getPosition())); mapElements.push_back(s); verticesElements[id] = s; addEdge_(id, next); } else throw MapException("No element with such ID."); } } else throw MapException("Not unique MapElement id"); }
void Map::setCase(int x, int y, m_type Type) { if (x >= this->_height || x < 0) throw MapException("Wrong Height"); if (y >= this->_width || y < 0) throw MapException("Wrong width"); this->_map[x][y] = Type; }
char Map::getCase(int x, int y) const { if (x >= this->_height || x < 0) throw MapException("Wrong Height"); if (y >= this->_width || y < 0) throw MapException("Wrong width"); return (this->_map[x][y]); }
void Map::produceFood(Snake const *snake) { int ntry = 0; bool state; int x; int y; srand(time(0)); state = false; while (state == false && ntry < 1000) { x = rand() % (this->_height - 1) + 1; y = rand() % (this->_width - 1) + 1; state = true; if (getCase(x, y) != tEmpty) state = false; if (snake) { for (std::list<SnakeElement *>::const_iterator it = snake->getList().begin(); it != snake->getList().end(); ++it) { if (x == (*it)->getPos()[0] && y == (*it)->getPos()[1]) state = false; } } ntry++; } if (ntry == 1000) throw MapException("Impossible to produce food."); setCase(x, y, tFood); }
void MapFactory::addCross_(const ptree::value_type &v) { int id = v.second.get<int>("id"); if(id < 0) throw MapException("Wrong ID"); if(vertices.find(id) == vertices.end()) { double x = v.second.get<double>("x"); double y = v.second.get<double>("y"); map<Direction, int> neighbors; neighbors[N] = v.second.get<int>("N"); neighbors[E] = v.second.get<int>("E"); neighbors[S] = v.second.get<int>("S"); neighbors[W] = v.second.get<int>("W"); // tworzenie PriorityRules w zaleznosci od typu PriorityRules *p; if(v.second.get<string>("crossType") == "rules") { string s = v.second.get<string>("subordinate"); vector<char> subordinate; unsigned int i = 0; while(i < s.length()) { subordinate.push_back(s.at(i)); i++; } p = new CrossRules(subordinate, neighbors); } else throw MapException("Wrong rules type!"); // dostep do wlasnosci wierzcholka Vertex ver = boost::add_vertex(*g); ((*g)[ver]).id = id; vertices[id] = ver; PMapElement c(new Cross(id, x, y, p, neighbors)); mapElements.push_back(c); verticesElements[id] = c; addEdges_(id, neighbors); } else throw MapException("Not unique MapElement id"); }
MapFactory::MapFactory(const string& path) { if(!path.empty()) { try { boost::property_tree::read_json(path, propertyTree); } catch (boost::property_tree::json_parser::json_parser_error& err){ std::ostringstream msg; msg << err.message() << ": " << err.filename() << std::endl; throw MapException(msg.str()); } } }
void MapFactory::addEdges_(int id_, map<Direction, int> neighbors_) { map<Direction, int>::iterator it; for(it = neighbors_.begin(); it != neighbors_.end(); ++it) // jeżeli jest dolaczony do jakiegos innego wierzcholka // i jeżeli jego id_ jest wieksze, to wtedy na pewno tamten stworzony if((*it).second != -1 && id_ > (*it).second) { if(vertices.find((*it).second) != vertices.end()) addEdge_(id_, (*it).second); else throw MapException("No element with such ID."); } }