/// <summary> /// Get the action given start tile and end tile. /// </summary> /// <param name="startTile"></param> /// <param name="endTile"></param> /// <returns></returns> internal Action GetAction(Tile startTile, Tile endTile) { Action a = new Action(); if (startTile._posX > endTile._posX) { a.CurrentCommand = Action.Command.Left; } else if (startTile._posX < endTile._posX) { a.CurrentCommand = Action.Command.Right; } else if (startTile._posY > endTile._posY) { a.CurrentCommand = Action.Command.Up; } else if (startTile._posY < endTile._posY) { a.CurrentCommand = Action.Command.Down; } return a; }
public void ReLoadGame() { //Loads Font header1 = _content.Load<SpriteFont>("Header1"); //Loads the Player if (IsSmartTrial) { _player = new Player(boardY - 1, SCREEN, 160); } // should reference default player speed - DMC else { _player = new Player(boardY - 1, SCREEN, FAST_SPEED); } _player.LoadContent(_content); //Load Game Control // gameInfo = new GameControl(); gameInfo.setSeed(settingMenu.seed); gameInfo.SetOutputList(_player.playerLog); gameInfo.UpdateScore(_player.Score); gameInfo.SetStatus(""); gameInfo.SetLearningTrialDisplay(gameCounter, QLearningTrials, QLearning, winCounter); gameInfo.Show(); //Loads the Board Sprite mRoom = new Sprite(); bool visible = settingMenu.allVisible; bool autoPlay = settingMenu.autoPlay; Random rand = new Random(settingMenu.seed); //Gets the # of pits, wumpus, and gold // double pitLow = (((double)boardX * (double)boardY) / 100) * settingMenu.pitLower; // double pitHigh = (((double)boardX * (double)boardY) / 100) * settingMenu.pitHigher; // Get the original count int pits = originalPits.Count; int wumpus = originalWumpi.Count; int gold = originalGold.Count; bool top = false; bool bottom = false; bool left = false; bool right = false; //Prepares the tiles for (int x = 0; x < boardX; x++) { for (int y = 0; y < boardY; y++) { string _resource; if (x == 0 & y == 0) { _resource = "RoomTileLeftTop"; left = true; top = true; bottom = false; right = false; } else if (x == 0 & y == boardY - 1) { _resource = "RoomTileLeftBot"; bottom = true; left = true; right = false; top = false; } else if (x == boardX - 1 && y == 0) { _resource = "RoomTileRightTop"; right = true; top = true; left = false; bottom = false; } else if (x == boardX - 1 && y == boardY - 1) { _resource = "RoomTileRightBot"; right = true; bottom = true; left = false; top = false; } else if (x == 0) { _resource = "RoomTileLeft"; left = true; top = false; bottom = false; right = false; } else if (y == 0) { _resource = "RoomTileTop"; top = true; bottom = false; left = false; right = false; } else if (y == boardY - 1) { _resource = "RoomTileBot"; bottom = true; top = false; left = false; right = false; } else if (x == boardX - 1) { _resource = "RoomTileRight"; right = true; top = false; bottom = false; left = false; } else { _resource = "RoomTile"; top = false; bottom = false; left = false; right = false; } mBoard[x, y] = new Tile(x, y, visible, top, bottom, right, left); mBoard[x, y].LoadContent(_content, _resource); if (y == boardY - 1 && x == 0) { mBoard[x, y]._explored = true; } } } //Prepares the Pits while (pits != 0) { int x = originalPits[pits - 1].X; int y = originalPits[pits - 1].Y; EntityPit ep = new EntityPit(x, y, visible); ep.LoadContent(_content); entityMap[x, y] = ep; if (x - 1 >= 0) mBoard[x - 1, y]._breeze = true; if (y - 1 >= 0) mBoard[x, y - 1]._breeze = true; if (x + 1 < boardX) mBoard[x + 1, y]._breeze = true; if (y + 1 < boardY) mBoard[x, y + 1]._breeze = true; pits--; } //Prepares the Gold while (gold != 0) { int x = originalGold[gold - 1].X; int y = originalGold[gold - 1].Y; EntityGold eg = new EntityGold(x, y, visible); eg.LoadContent(_content); entityMap[x, y] = eg; mBoard[x, y]._gold = true; // added for agents gold--; } //Prepares the Wumpus while (wumpus != 0) { int x = originalWumpi[wumpus - 1].X; int y = originalWumpi[wumpus - 1].Y; EntityWumpus ep = new EntityWumpus(x, y, visible); ep.LoadContent(_content); entityMap[x, y] = ep; if (x - 1 >= 0) mBoard[x - 1, y]._stench = true; if (y - 1 >= 0) mBoard[x, y - 1]._stench = true; if (x + 1 < boardX) mBoard[x + 1, y]._stench = true; if (y + 1 < boardY) mBoard[x, y + 1]._stench = true; wumpus--; } // sets the base - DMC mBoard[0, 4].IsBase = true; // Set the reward state to not carrying gold (needed to know which Q tables to use) PlayerCarryingGold = false; // LastAction.CurrentCommand = Action.Command.None; }
public void LoadGame() { if (QLearning) { SetupQLearning(); } // added for Q learning....... // DMC //Loads Font header1 = _content.Load<SpriteFont>("Header1"); //Loads the Player if (QLearning) { _player = new Player(boardY - 1, SCREEN, FAST_SPEED); } // fast for QLearning - DMC else { _player = new Player(boardY - 1, SCREEN); } // else regular speed & DStar by default- DMC _player.LoadContent(_content); //Load Game Control gameInfo = new GameControl(); gameInfo.setSeed(settingMenu.seed); gameInfo.SetOutputList(_player.playerLog); gameInfo.UpdateScore(_player.Score); gameInfo.SetStatus(""); if (QLearning) { gameInfo.SetLearningTrialDisplay(gameCounter, QLearningTrials, QLearning, winCounter); } // added Q Learning display gameInfo.Show(); //Loads the Board Sprite mRoom = new Sprite(); bool visible = settingMenu.allVisible; bool autoPlay = settingMenu.autoPlay; Random rand = new Random(settingMenu.seed); //Gets the # of pits, wumpus, and gold double pitLow = (((double)boardX * (double)boardY) / 100) * settingMenu.pitLower; double pitHigh = (((double)boardX * (double)boardY) / 100) * settingMenu.pitHigher; int pits = rand.Next((int)pitLow, (int)pitHigh); int wumpus = settingMenu.wumpus; int gold = settingMenu.gold; bool top = false; bool bottom = false; bool left = false; bool right = false; //Prepares the tiles for (int x = 0; x < boardX; x++) { for (int y = 0; y < boardY; y++) { string _resource; if (x == 0 & y == 0) { _resource = "RoomTileLeftTop"; left = true; top = true; bottom = false; right = false; } else if (x == 0 & y == boardY - 1) { _resource = "RoomTileLeftBot"; bottom = true; left = true; right = false; top = false; } else if (x == boardX - 1 && y == 0) { _resource = "RoomTileRightTop"; right = true; top = true; left = false; bottom = false; } else if (x == boardX - 1 && y == boardY - 1) { _resource = "RoomTileRightBot"; right = true; bottom = true; left = false; top = false; } else if (x == 0) { _resource = "RoomTileLeft"; left = true; top = false; bottom = false; right = false; } else if (y == 0) { _resource = "RoomTileTop"; top = true; bottom = false; left = false; right = false; } else if (y == boardY - 1) { _resource = "RoomTileBot"; bottom = true; top = false; left = false; right = false; } else if (x == boardX - 1) { _resource = "RoomTileRight"; right = true; top = false; bottom = false; left = false; } else { _resource = "RoomTile"; top = false; bottom = false; left = false; right = false; } mBoard[x, y] = new Tile(x, y, visible, top, bottom, right, left); mBoard[x, y].LoadContent(_content, _resource); if (y == boardY - 1 && x == 0) { mBoard[x, y]._explored = true; } } } //Prepares the Pits while (pits != 0) { int x = rand.Next(0, boardX); int y = rand.Next(0, boardY); if (entityMap[x, y] == null && !(x == 0 && y == boardY - 1)) { EntityPit ep = new EntityPit(x, y, visible); ep.LoadContent(_content); entityMap[x, y] = ep; if (x - 1 >= 0) mBoard[x - 1, y]._breeze = true; if (y - 1 >= 0) mBoard[x, y - 1]._breeze = true; if (x + 1 < boardX) mBoard[x + 1, y]._breeze = true; if (y + 1 < boardY) mBoard[x, y + 1]._breeze = true; pits--; if (QLearning) { originalPits.Add(new Point(x, y)); }// record pits for repeated trials - DMC } } //Prepares the Gold while (gold != 0) { int x = rand.Next(0, boardX); int y = rand.Next(0, boardY); if (entityMap[x, y] == null && !(x == 0 && y == boardY - 1)) { EntityGold eg = new EntityGold(x, y, visible); eg.LoadContent(_content); entityMap[x, y] = eg; mBoard[x, y]._gold = true; gold--; if (QLearning) { originalGold.Add(new Point(x, y)); } // record gold for repeated trials - DMC } } //Prepares the Wumpus while (wumpus != 0) { int x = rand.Next(0, boardX); int y = rand.Next(0, boardY); if (entityMap[x, y] == null && !(x == 0 && y == boardY - 1)) { EntityWumpus ep = new EntityWumpus(x, y, visible); ep.LoadContent(_content); entityMap[x, y] = ep; if (x - 1 >= 0) mBoard[x - 1, y]._stench = true; if (y - 1 >= 0) mBoard[x, y - 1]._stench = true; if (x + 1 < boardX) mBoard[x + 1, y]._stench = true; if (y + 1 < boardY) mBoard[x, y + 1]._stench = true; wumpus--; if (QLearning) { originalWumpi.Add(new Point(x, y)); }// record wumpi for repeated trials - DMC } } // sets the base - DMC mBoard[0, 4].IsBase = true; }
public List<Tile> GetNeighbors(Tile curTile) { var lst = new List<Tile>(); Tile t; if (curTile._posX == 0) { t = mBoard[curTile._posX + 1, curTile._posY]; lst.Add(t); } else if (curTile._posX == this.boardX - 1) { t = mBoard[curTile._posX - 1, curTile._posY]; lst.Add(t); } else { t = mBoard[curTile._posX + 1, curTile._posY]; lst.Add(t); t = mBoard[curTile._posX - 1, curTile._posY]; lst.Add(t); } if (curTile._posY == 0) { t = mBoard[curTile._posX, curTile._posY + 1]; lst.Add(t); } else if (curTile._posY == this.boardY - 1) { t = mBoard[curTile._posX, curTile._posY - 1]; lst.Add(t); } else { t = mBoard[curTile._posX, curTile._posY + 1]; lst.Add(t); t = mBoard[curTile._posX, curTile._posY - 1]; lst.Add(t); } return lst; }
/// <summary> /// Abstract method to retrieve an action from the agent. /// </summary> /// <param name="CurrentTile">Current tile location</param> /// <returns>Agent-selected action</returns> public abstract Action GetAction(Tile CurrentTile);
//Updates the Player public void Update(GameTime theGameTime, Tile CurrentTile) { Action newAction = new Action(); KeyboardState aCurrentKeyboardState = new KeyboardState(); //if (isHuman == true) //if(!Game.Instance.AutoPlay) // human playable //{ //Animates Walking if (CurrentState == State.Walking && (theGameTime.TotalGameTime.TotalMilliseconds > nextUpdate)) { if (switchTexture == true) { base.ChangeSprite(_PlayerWalk); switchTexture = false; } else { base.ChangeSprite(_PlayerStop); switchTexture = true; } nextUpdate = theGameTime.TotalGameTime.TotalMilliseconds + millisecondsPerFrame; } if (CurrentState == State.Stopped && Game.Instance.AutoPlay) { newAction = AI.GetAction(CurrentTile); if (Game.Instance.QLearning) { Game.Instance.LastAction = newAction; // make the last action taken available publicly if (newAction.IsFiringAction()) { CurrentState = State.Firing; playerLog.Add("Preparing to Fire ARROW"); listUpdated = true; } } else { if (newAction.IsFiringAction() && arrows > 0) { CurrentState = State.Firing; playerLog.Add("Preparing to Fire ARROW"); listUpdated = true; } } } else { aCurrentKeyboardState = Keyboard.GetState(); } if (aCurrentKeyboardState.IsKeyDown(Keys.F) == true && CurrentState == State.Stopped && arrows > 0) { CurrentState = State.Firing; playerLog.Add("Preparing to Fire ARROW"); listUpdated = true; } if (!Game.Instance.AutoPlay) { newAction = getInput(aCurrentKeyboardState); } if (newAction.CurrentCommand == Action.Command.Climb && escaped == false && (posX == 0 && posY == startY)) { escaped = true; CurrentState = State.Escaped; playerLog.Add("Player Escaped! Final Score: " + Score + " Points"); listUpdated = true; if (Game.Instance.QLearning) { Game.Instance.EndGameX = posX; Game.Instance.EndGameY = posY; Game.Instance.EndGameCarryingGold = true; Game.Instance.EndGameAction = Game.Instance.LastAction; Game.Instance.EndGameRewardState = Game.RewardState.PlayerWon; Game.Instance.CurrentRewardState = Game.RewardState.PlayerWon; } } else if (newAction.CurrentCommand == Action.Command.PickUp) { picking_up = true; if (Game.Instance.QLearning) { if (!Game.Instance.PlayerCarryingGold) { Game.Instance.CurrentRewardState = Game.RewardState.PickedUpGold; } else Game.Instance.CurrentRewardState = Game.RewardState.TriedImpossibleAction; } } UpdateMovement(newAction); if (CurrentState == State.Firing) shootArrow(newAction); lastAction = newAction; base.Update(theGameTime, Speed, Direction); }