// // OnlineMatch.cc // LeJoueur // // Created by Jean-Noël Vittaut on 20/04/13. // Copyright (c) 2013 LIASD. All rights reserved. // #include "Logger.h" #include #include "GdlAtom.h" #include "GdlTerm.h" #include "OnlineMatch.h" #include "Circuit.h" #include #include #include using namespace std; namespace lejr { OnlineMatch::OnlineMatch() {} string OnlineMatch::start(string const& s) { GDLList to_do = factory_.parse(s).front()->args(); matchid = to_do[0]->name()->str(); GDLTerm role = to_do[1]; GDLTerm rules = to_do[2]; starttime = std::atoi(to_do[3]->name()->str().c_str()); playtime = std::atoi(to_do[4]->name()->str().c_str()); LOG_INFO << "MatchId: " << matchid; LOG_INFO << "I am player <" << *role << ">"; LOG_INFO << "Starttime: " << starttime; LOG_INFO << "Playtime: " << playtime; c_ = new Circuit(rules->args(), factory_); c_->init(); c_->execute(); srand(time(NULL)); // boost::posix_time::ptime until = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(std::max(0, starttime * 1000 - 3000)); return "ready"; } static string gdltermToString(GDLTerm term) { stringstream ss; ss << *term; return ss.str(); } string OnlineMatch::play(string const& s) { // boost::posix_time::ptime until = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(std::max(0, playtime * 1000 - 3000)); LOG_INFO << "*** Step " << step << " ***"; ++step; GDLList to_do = factory_.parse(s).front()->args(); GDLTerm mvs = to_do[1]; GDLList moves; if(mvs->name()->str().empty()) moves = mvs->args(); else { moves.push_back(factory_.make(mvs->name())); moves.insert(moves.end(), mvs->args().begin(), mvs->args().end()); } for (GDLTerm m : moves) { cout << "\nMOVES SIZE : " << moves.size() << endl; string tstr = gdltermToString(m); if (tstr == "nil") { cout << "\nNIL DONC ON NE FAIT RIEN" << endl; break; } transform(tstr.begin(), tstr.end(), tstr.begin(), ::tolower); cout << tstr << endl; lejr::GDLAtom legalatom = factory_.make(std::string("legal")); // tomahawk GDLTerm lm = factory_.make(legalatom, factory_.parse(string("robot " + tstr))); cout << "\nON APPLIQUE : " << *lm << endl; c_->does(lm); c_->execute(); c_->next(); c_->execute(); } vector leg = c_->legal(); cout << "\nLEGAL SIZE : " << leg.size() << endl; int move_number = rand() % leg.size(); /* // cout << "ON APPLIQUE : " << *leg[move_number] << endl; c_->does(leg[move_number]); c_->execute(); c_->next(); c_->execute(); */ cout << *leg[move_number] << endl; // cout << "\nON RENVOIE :" << *leg[move_number] << endl; return gdltermToString(leg[move_number]->args()[1]); // return "noop"; } }