root/ggpa/GameWorld.h
| Revision 1, 13.9 kB (checked in by pantley2, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | * GameWorld.h |
| 3 | * Header file for GameWorld class |
| 4 | */ |
| 5 | |
| 6 | #ifndef GAME_WORLD_H |
| 7 | #define GAME_WORLD_H |
| 8 | |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | #include <map> |
| 12 | #include <utility> |
| 13 | using namespace std; |
| 14 | |
| 15 | #include "SentenceStructure.h" |
| 16 | #include "Sentence.h" |
| 17 | #include "State.h" |
| 18 | #include "Utils.h" |
| 19 | #include "Action.h" |
| 20 | |
| 21 | /* |
| 22 | * Class that represents the rules and states of a game |
| 23 | */ |
| 24 | class GameWorld { |
| 25 | |
| 26 | private: |
| 27 | |
| 28 | string matchID; // unique identifier for this match |
| 29 | string role; // our role in the game |
| 30 | double startClock; // time we have to learn in minutes |
| 31 | double playClock; // time we have to make each move |
| 32 | |
| 33 | // a set of the roles in the game |
| 34 | vector<string> roles; |
| 35 | |
| 36 | // A map from the names of sentences to their SentenceStructure |
| 37 | map<string, SentenceStructure*> sentenceStructures; |
| 38 | |
| 39 | // initial state of the game, as a set of literals |
| 40 | // No variables (?) |
| 41 | vector<Sentence*> initialSentences; |
| 42 | State* initialState; |
| 43 | |
| 44 | // a dependency graph for the rules |
| 45 | // The nodes are literals |
| 46 | // An edge from literal a to literal b represents there is a rule with |
| 47 | // b in the head and r2 in the body, i.e. (<= a b) |
| 48 | vector<Sentence*> dependencyGraph; |
| 49 | |
| 50 | // a collection of the current literals (sentences and negated sentences) |
| 51 | // This is the same as the set of literals l1 ... ln such that (true l1) |
| 52 | // for all i |
| 53 | State* currentState; |
| 54 | |
| 55 | // a collection of the goals in the game (for all players). This will |
| 56 | // be a collection of pointers to nodes in the graph. |
| 57 | vector<Sentence*> goals; |
| 58 | |
| 59 | // a collection of the rules for updating the current state |
| 60 | vector<Sentence*> updateRules; |
| 61 | |
| 62 | // a collection of rules for determining legal moves |
| 63 | // the pair is used to store the parameters corrensponding to a role |
| 64 | //vector<pair<string, Sentence*> > legalMoves; |
| 65 | vector<Action*> legalMoveRules; |
| 66 | |
| 67 | // a colections of rules that will determine the value of a property |
| 68 | // These should not depend on the previous state. |
| 69 | vector<Sentence*> definitions; |
| 70 | |
| 71 | // a collection of legal moves for the current state |
| 72 | vector<Action*> legalMoves; |
| 73 | |
| 74 | // a collection of the actions within the game |
| 75 | //vector<SentenceStructure*> actions; |
| 76 | |
| 77 | public: |
| 78 | |
| 79 | /* |
| 80 | * Constructor |
| 81 | * Parameters: rules - string to define rules to create a new GameWorld |
| 82 | * No return type |
| 83 | * Uses the rules to initialize the GameWorld |
| 84 | */ |
| 85 | GameWorld(const string& rules); |
| 86 | |
| 87 | /* |
| 88 | * Copy Constructor |
| 89 | * Initializes this to be a copy of the original GameWorld. |
| 90 | */ |
| 91 | GameWorld(const GameWorld& oldWorld); |
| 92 | |
| 93 | /* |
| 94 | * operator= |
| 95 | * Sets this to be a copy of the original GameWorld |
| 96 | */ |
| 97 | GameWorld& operator=(const GameWorld& oldWorld); |
| 98 | |
| 99 | /* |
| 100 | * Deconstructor |
| 101 | * Frees up the memory. |
| 102 | */ |
| 103 | ~GameWorld(); |
| 104 | |
| 105 | /* |
| 106 | * parse |
| 107 | * Parameters: rules - string containing the rules |
| 108 | * Return value: none |
| 109 | * Parses rules to create a GameWorld that we can use |
| 110 | */ |
| 111 | void parse(const string& rules); |
| 112 | |
| 113 | /* |
| 114 | * getMatchID |
| 115 | * No parameters |
| 116 | * Return type: string |
| 117 | * Returns the match id for this game |
| 118 | */ |
| 119 | string getMatchID(); |
| 120 | |
| 121 | /* |
| 122 | * getRole |
| 123 | * No parameters |
| 124 | * Return type: string |
| 125 | * Returns the role of this program in the game |
| 126 | */ |
| 127 | string getRole(); |
| 128 | |
| 129 | /* |
| 130 | * getRoles |
| 131 | * Parameters: none |
| 132 | * Return type: a vector of strings |
| 133 | * Returns all roles in this game. |
| 134 | */ |
| 135 | const vector<string>& getRoles() const; |
| 136 | |
| 137 | /* |
| 138 | * getStartClock |
| 139 | * No parameters |
| 140 | * Return type: double |
| 141 | * Returns the time available for learning before the match starts. |
| 142 | * This time will be in *minutes* |
| 143 | */ |
| 144 | double getStartClock(); |
| 145 | |
| 146 | /* |
| 147 | * getPlayClock |
| 148 | * No parameters |
| 149 | * Return type: double |
| 150 | * Returns the time available to make each move during the match. This |
| 151 | * time will be in *seconds* |
| 152 | */ |
| 153 | double getPlayClock(); |
| 154 | |
| 155 | /* |
| 156 | * tokenize |
| 157 | * Parameters: line - a string that may contain multiple works or arguments |
| 158 | * Return type: a vector of strings |
| 159 | * Breaks the line into a series of arguments. These arguments will be |
| 160 | * first level, meaning that if you have nested arguments, you will |
| 161 | * just get back an unparsed copy of that argument. CHANGE: The first |
| 162 | * parenthesis will be ignored, assuming that the end parenthesis is at |
| 163 | * the end of the string. |
| 164 | */ |
| 165 | vector<string> tokenize(string line) const; |
| 166 | |
| 167 | /* |
| 168 | * function that prints out everything, for "testing" |
| 169 | */ |
| 170 | void output() const; |
| 171 | |
| 172 | /* |
| 173 | * update |
| 174 | * Parameters: actions - a string containing a list of the actions |
| 175 | * taken by the other players |
| 176 | * No Return type |
| 177 | * Updates the game state for a new turn. |
| 178 | */ |
| 179 | void update(string actions); |
| 180 | |
| 181 | /* |
| 182 | * update |
| 183 | * Parameters: currentActions - a vector of actions containing the actions |
| 184 | * taken by players previously. |
| 185 | * No return type |
| 186 | * Updates the game state based on the actions taken, and the current |
| 187 | * state. |
| 188 | */ |
| 189 | void update(vector<Action*> currentActions); |
| 190 | |
| 191 | /* |
| 192 | * getLegalMoves |
| 193 | * Parameters: none |
| 194 | * Return type: a vector of Action pointers |
| 195 | * Returns a set of legal moves for the default player. |
| 196 | */ |
| 197 | vector<Action*> getLegalMoves() const; |
| 198 | |
| 199 | /* |
| 200 | * getLegalMoves |
| 201 | * Parameters: player - the player to get the actions for |
| 202 | * Return type: a vector of Action pointers |
| 203 | * Returns a set of legal moves for the player. If the player does |
| 204 | * not exist in the game, it will return an empty vector |
| 205 | */ |
| 206 | vector<Action*> getLegalMoves(string player) const; |
| 207 | |
| 208 | /* |
| 209 | * getState |
| 210 | * No parameters. |
| 211 | * Returns the current state for this GameWorld |
| 212 | */ |
| 213 | State* getState() const; |
| 214 | |
| 215 | /* |
| 216 | * isDone |
| 217 | * Parameters: none |
| 218 | * Return type: bool |
| 219 | * Returns true if the game has reached a terminal state, and false |
| 220 | * otherwise. |
| 221 | */ |
| 222 | bool isDone(); |
| 223 | |
| 224 | /* |
| 225 | * startGame |
| 226 | * No parameters or return type |
| 227 | * Initializes the GameWorld for a new game |
| 228 | */ |
| 229 | void startGame(); |
| 230 | |
| 231 | /* |
| 232 | * getReward |
| 233 | * Parameters: curRole - the role we are interested in |
| 234 | * Return type: int |
| 235 | * Returns the reward for the current state. No goal is satisfied, |
| 236 | * it returns the default value. |
| 237 | */ |
| 238 | int getReward(string curRole) const; |
| 239 | |
| 240 | |
| 241 | /*************************************************************** |
| 242 | * Private functions |
| 243 | ***************************************************************/ |
| 244 | |
| 245 | private: |
| 246 | |
| 247 | /* |
| 248 | * parseRules |
| 249 | * Parameters: rules - string containing the rules of the game |
| 250 | * start - place to start parsing the rules |
| 251 | * Return value: int - the index of the last letter in the rules |
| 252 | * Parses the rules from the string and puts them into the class fields, |
| 253 | * returning the last index used |
| 254 | */ |
| 255 | int parseRules(const string& rules); |
| 256 | |
| 257 | /* |
| 258 | * parseRule |
| 259 | * Parameters: rule - a string containing a single rule to be parsed |
| 260 | * Return value: none |
| 261 | * Parses the rule into the GameWorld |
| 262 | */ |
| 263 | void parseRule(const string& rule); |
| 264 | |
| 265 | |
| 266 | /* |
| 267 | * getFirstWord |
| 268 | * Parameters: rule - a string containing the rule we are interested in |
| 269 | * Return type: string |
| 270 | * Extracts the first word from the rule, and returns it. NOTE: The first |
| 271 | * parenthesis in the rule should be removed before calling this |
| 272 | */ |
| 273 | string getFirstWord(string rule) const; |
| 274 | |
| 275 | /* |
| 276 | * parseInit |
| 277 | * Parameters: rule - a string containing the rule we are interested in |
| 278 | * No return type |
| 279 | * Extracts an initial state rule, and stores it appropriately |
| 280 | */ |
| 281 | void parseInit(string rule); |
| 282 | |
| 283 | /* |
| 284 | * parseInit |
| 285 | * Parameters: rule - a string containing the rule we are interested in |
| 286 | * No return type |
| 287 | * Extracts a role rule, and stores it appropriately |
| 288 | */ |
| 289 | void parseRole(string rule); |
| 290 | |
| 291 | /* |
| 292 | * parseDatalogRule |
| 293 | * Parameters: rule - a string containing the rule we are interested in |
| 294 | * No return type |
| 295 | * Extracts a datalog rule, and stores it appropriately |
| 296 | */ |
| 297 | void parseDatalogRule(string rule); |
| 298 | |
| 299 | /* |
| 300 | * parseHead |
| 301 | * Parameters: headString - a string containing a text representation of |
| 302 | * the head for a rule |
| 303 | * Return type: Sentence pointer |
| 304 | * Returns the Sentence corresponding to the head |
| 305 | */ |
| 306 | Sentence* parseHead(string headString); |
| 307 | |
| 308 | /* |
| 309 | *parseSentence |
| 310 | *Parameters: tokens - a vector<string> containing in its first index the name of the sentence and the rest of them are the parameters of the sentence |
| 311 | * Return type: Sentence pointer |
| 312 | * Returns the parsed version of the sentence |
| 313 | */ |
| 314 | Sentence* parseSentence(vector<string> tokens); |
| 315 | |
| 316 | /* |
| 317 | * parseAction |
| 318 | * Parameters: tokens - a vector<string containing in its first index the |
| 319 | * name of the action, and the rest are the parameters |
| 320 | * of the action |
| 321 | * actor - the agent who performs this action |
| 322 | * inGame - this variable is true if we are currently playing |
| 323 | * the game, and do not want to be updating the rules |
| 324 | * Return type: Action* |
| 325 | * Returns the parsed version of the action |
| 326 | */ |
| 327 | Action* parseAction(vector<string> tokens, string actor, bool inGame = false); |
| 328 | |
| 329 | /* |
| 330 | *updateSentenceStructure |
| 331 | *Parameters: tokens - The string in the first index of tokens, new size of tokens, boolean to see if sentence represents action |
| 332 | *Return type: a SentenceStructure pointer |
| 333 | *Returns the new structure of the sentence after it has been updated |
| 334 | */ |
| 335 | |
| 336 | SentenceStructure* updateSentenceStructure(vector<string> name, bool isAction); |
| 337 | |
| 338 | /* |
| 339 | * parseBody |
| 340 | * Parameters: body - a string containing one component of the body of the |
| 341 | * rule |
| 342 | * head - a Sentence that is the head of this rule |
| 343 | * No return type |
| 344 | * Parses this piece of the body, and adds it as a dependency to the head |
| 345 | */ |
| 346 | void parseBody(string body, Sentence* head); |
| 347 | |
| 348 | |
| 349 | /* |
| 350 | * initializeFields |
| 351 | * No parameters or return type |
| 352 | * Initializes the appropriate fields for this class |
| 353 | */ |
| 354 | void initializeFields(); |
| 355 | |
| 356 | /* |
| 357 | * checkSentenceStructure |
| 358 | * Parameters: tokens - a vector of strings containing the tokens for |
| 359 | * this sentence |
| 360 | * isAction - a boolean that is true if the sentence should |
| 361 | * be an action, and false otherwise |
| 362 | * Return type: bool |
| 363 | * Returns true if the Sentence is legal in the rules, and false |
| 364 | * otherwise |
| 365 | */ |
| 366 | bool checkSentenceStructure(vector<string> tokens, bool isAction); |
| 367 | |
| 368 | |
| 369 | |
| 370 | /** |
| 371 | * applyUpdateRule |
| 372 | * Parameters: updateRule - the current rule to apply |
| 373 | * state - the state that we are building |
| 374 | * currentActions - the most recently performed actions |
| 375 | * No Return type |
| 376 | * Adds properties determined by the updateRules to the state |
| 377 | */ |
| 378 | void applyUpdateRule(Sentence* updateRule, State* state, |
| 379 | vector<Action*> currentActions); |
| 380 | |
| 381 | /** |
| 382 | * applyDefinitions |
| 383 | * Parameters: state - the state that we are building |
| 384 | * No return type |
| 385 | * Runs through the definitions and adds any applicable properties |
| 386 | * to the state |
| 387 | */ |
| 388 | void applyDefinitions(State* state); |
| 389 | |
| 390 | /** |
| 391 | * applyDefinition |
| 392 | * Parameters: definition - the current definition |
| 393 | * state - where to apply the definition |
| 394 | * Return type: none |
| 395 | * Applies the definition to the current state. |
| 396 | */ |
| 397 | void applyDefinition(Sentence* definition, State* state); |
| 398 | |
| 399 | /** |
| 400 | * applyRule |
| 401 | * Parameters: rule - the rule to apply: sentence with dependencies |
| 402 | * newState - the state to write the new rule to |
| 403 | * oldState - the state to get information from |
| 404 | * currentActions - a set of the most recent actions |
| 405 | * Return type: none |
| 406 | * Applies the rule to the newState given the oldState and the |
| 407 | * current actions. |
| 408 | */ |
| 409 | void applyRule(Sentence* rule, State* newState, State* oldState, |
| 410 | vector<Action*> currentActions); |
| 411 | |
| 412 | /** |
| 413 | * initializeState |
| 414 | * No parameters or return type |
| 415 | * Using the discovered sentence structures, get the appropriate size |
| 416 | * for a state and initialize initialState and currentState appropriately |
| 417 | */ |
| 418 | void initializeState(); |
| 419 | |
| 420 | /* |
| 421 | * findLegalMoves |
| 422 | * No parameters or return types |
| 423 | * Finds all the legal moves for the current state |
| 424 | */ |
| 425 | void findLegalMoves(); |
| 426 | |
| 427 | /* |
| 428 | * applyLegalMoveRule |
| 429 | * Parameters: rule - the rule to apply |
| 430 | * actions - a reference to a vector of actions where any |
| 431 | * new legal moves should be stored |
| 432 | * No return type |
| 433 | * Applies the rule and puts any legal moves that are found in the |
| 434 | * actions vector. |
| 435 | */ |
| 436 | void applyLegalMoveRule(Action* rule, vector<Action*>& actions); |
| 437 | |
| 438 | /* |
| 439 | * areDependenciesSatified |
| 440 | * Parameters: rule - the Sentence* rule to check |
| 441 | * state - the State to look for dependencies in |
| 442 | * Return type: bool |
| 443 | * Returns true if all of the dependencies for the rule are satisfied, |
| 444 | * and false otherwise. |
| 445 | */ |
| 446 | bool areDependenciesSatisfied(Sentence* rule, State* state); |
| 447 | |
| 448 | /* |
| 449 | * isDependencySatisfied |
| 450 | * Parameters: dependency - the Sentence* to check |
| 451 | * state - the state in which to check if the dependency is |
| 452 | * satisfied |
| 453 | * Return type: bool |
| 454 | * Returns true if the dependency is satisfied in this state, and false |
| 455 | * otherwise |
| 456 | */ |
| 457 | bool isDependencySatisfied(Sentence* dependency, State* state); |
| 458 | |
| 459 | /* |
| 460 | * fillInValues |
| 461 | * No parameters or return type |
| 462 | * fills in missing values in the sentence structures |
| 463 | */ |
| 464 | void fillInValues(); |
| 465 | |
| 466 | }; |
| 467 | |
| 468 | |
| 469 | #endif |
| 470 |
Note: See TracBrowser for help on using the browser.
