void GameRef_GameCycleEvent(MazeCycleOutcome mazeCycle, Traveler traveler) { // A Cycle Ended, Mark the Score, then make your next move // determine score double score = WRONG_SCORE; if (mazeCycle.GameOver) { score = CORRECT_SCORE * 2; } else if (!mazeCycle.BumpedIntoWall) { score = CORRECT_SCORE; } rnn_net.ScoreCycle(currentCycleID, score); // start next AI's move var inputs = new List <rnn_io_with_value>() { new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "X"), traveler.location.X.ToString()), new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "Y"), traveler.location.Y.ToString()), new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "BumpedIntoWall"), mazeCycle.BumpedIntoWall.ToString()) }; rnn_cycle cycle = new rnn_cycle(); cycle.RunCycle(rnn_net, currentSessionID, inputs, learn); }
private void GameLoop(bool windowless = false) { int i = 0; //Cycle MazeCycleOutcome LastOutcome = new MazeCycleOutcome(); while (LastOutcome.GameOver == false) { if (CancelGame) { return; } Int16 dir = -1; if (DirectionsStack.TryDequeue(out dir)) { //incriment the count i++; //Run the maze cycle LastOutcome = CycleMaze(dir, windowless); //GameCycledEvent if (GameCycleEvent != null) { GameCycleEvent(LastOutcome, traveler); } //Check for Game Over if (LastOutcome.GameOver) { //Build Final Game MazeGameFinalOutcome FinalOutcome = new MazeGameFinalOutcome(); FinalOutcome.CycleCount = i; FinalOutcome.FinalScore = CalculateFinalScore(i); //Event if (GameOverEvent != null) { GameOverEvent(FinalOutcome); } } } else { System.Threading.Thread.Sleep(10); } } }
private MazeCycleOutcome CycleMaze(int direction, bool windowless = false) { MazeCycleOutcome outcome = new MazeCycleOutcome(); outcome.BumpedIntoWall = false; outcome.GameOver = false; outcome.FinalScore = 0; TravelerLocation new_location = new TravelerLocation(); //Calculate new location switch (direction) { case 0: if (traveler.location.Y <= 0) { OutOfBoundsOccurred(); outcome.BumpedIntoWall = true; return(outcome); } new_location.X = traveler.location.X; new_location.Y = traveler.location.Y - 1; break; case 1: if (traveler.location.X >= 49) { OutOfBoundsOccurred(); outcome.BumpedIntoWall = true; return(outcome); } new_location.X = traveler.location.X + 1; new_location.Y = traveler.location.Y; break; case 2: if (traveler.location.Y >= 49) { OutOfBoundsOccurred(); outcome.BumpedIntoWall = true; return(outcome); } new_location.X = traveler.location.X; new_location.Y = traveler.location.Y + 1; break; case 3: if (traveler.location.X <= 0) { OutOfBoundsOccurred(); outcome.BumpedIntoWall = true; return(outcome); } new_location.X = traveler.location.X - 1; new_location.Y = traveler.location.Y; break; default: throw new Exception("Not valid input"); } //Is BumpedIntoWall? if (maze.TheMazeGrid[new_location.X, new_location.Y]) { outcome.BumpedIntoWall = true; outcome.FinalScore = 0; outcome.GameOver = false; //Play sound SystemSounds.Hand.Play(); return(outcome); } //New location is now current location TravelerLocation old_location = traveler.location; traveler.location = new_location; //Is GameOver? if (traveler.location.X == GoalLocation.X && traveler.location.Y == GoalLocation.Y) { StopGame(); outcome.GameOver = true; return(outcome); } if (!windowless) { //Clear old location Action act = new Action(delegate { maze.ChangeCellColor(old_location, false); }); RunThreadUI(act); //first blink at new location //Clear old location Action act2 = new Action(delegate { maze.ChangeCellColor(traveler.location, true); }); RunThreadUI(act2); } return(outcome); }