public Game(PacManGameParameters pgp) { Console.Title = "Pacman AI Project"; Console.ForegroundColor = ConsoleColor.Blue; Console.BackgroundColor = ConsoleColor.White; //GameStart(); //GameLoop(pgp); //GameEnd(); }
public void GameLoop(PacManGameParameters pgp) { var isWorking = true; var t = ' '; //prev Ghost Position int?pacManPrevX = null; int?pacManPrevY = null; while (isWorking) { Console.Clear(); Console.WriteLine("\tPacman Deep Mind"); Console.WriteLine(" Score: " + _score + "\tMax: " + _maxScore); Console.WriteLine("Curent turn: " + _turnCounter + " \tIdle for: " + _idleCounter + " turns"); Console.WriteLine("Generation: " + _generation); //SetPacManData(pacman.getX(), pacman.getY()); //SetGhostData(ghost.getX(), ghost.getY()); //level.Draw(); PacManEnvironment pe = new PacManEnvironment( level._board, pacman.getX(), pacman.getY(), pacManPrevX, pacManPrevY, ghost.getX(), ghost.getY()); Directions?directions = pgp.GetMoveDirection(pe); pacManPrevX = pacman.getX(); pacManPrevY = pacman.getY(); //pacman.Input(); //ghost.Input(); pacman.SetDir(Converter.DirectionToDIRECTION(directions)); pacman.SetDir(level.Check(pacman.getX(), pacman.getY(), pacman.GetDir())); pacman.Movement(); //ghost.SetDir(path.FindDir(pacman.getX(), pacman.getY(), ghost.getX(), ghost.getY(), pacman.GetDir())); if (_isChasing) { ghost.SetDir(path.A_Star(ghost.getX(), ghost.getY(), pacman.getX(), pacman.getY())); _aiCounter++; if (_aiCounter > 20) { _isChasing = false; _aiCounter = 0; } } else { ghost.SetDir(path.A_Star(ghost.getX(), ghost.getY(), level.gX, level.gY)); _aiCounter++; if (_aiCounter > 10) { _isChasing = true; _aiCounter = 0; } } ghost.SetDir(level.Check(ghost.getX(), ghost.getY(), ghost.GetDir())); ghost.Movement(); SetPacManData(pacman.getX(), pacman.getY()); t = SetGhostData(ghost.getX(), ghost.getY(), t); pacman.Update(); ghost.Update(); /* * isWorking = ai.MoveNext(); * Tuple<int, int> coords = ai.Current; * SetData(coords.Item1, coords.Item2); */ if (_isIdle == true) { _idleCounter++; } else { _idleCounter = 0; } _turnCounter++; //level.Draw(); //Thread.Sleep(100); if (_score == _maxScore) { _exitStatus = "Victory!"; isWorking = false; } if (pacman.getX() == ghost.getX() && pacman.getY() == ghost.getY()) { _exitStatus = "Eaten"; isWorking = false; } if (_idleCounter > _idleMax) { _exitStatus = "Lost"; isWorking = false; } } }
static void Main(string[] args) { Logger.logNewSession(); PacManGameParameters pgp = new PacManGameParameters(); ResultsContainer rc = new ResultsContainer(); /*do * { * Game game = new Game(pgp); * * pgp.RandomMutation(); * * } while (true);*/ for (int i = 0; i < 10; i++) { Game game = new Game(pgp); game.GameStart(); game.GameLoop(pgp); game.GameEnd(); //TODO: Set Flag for Gene isCrossbred : bool rc.AddResult(new ResultItem(game._score, game._turnCounter, pgp.GenesActive)); do { pgp.RandomMutation(); } while (rc.AreGenesAttempted(pgp.GenesActive)); } for (int i = 0; i < 100; i++) { Game game = new Game(pgp); game.GameStart(); game.GameLoop(pgp); game.GameEnd(); rc.AddResult(new ResultItem(game._score, game._turnCounter, pgp.GenesActive)); do { bool isValidCrossBreed = false; bool isCrosbreedFound = pgp.CrossBreed(rc); if (isCrosbreedFound) { if (!rc.AreGenesAttempted(pgp.GenesActive)) { isValidCrossBreed = true; } } if (!isValidCrossBreed) { pgp.RandomMutation(); } } while (rc.AreGenesAttempted(pgp.GenesActive)); } List <ResultItem> list = rc.SortByScore(); Logger.logList(list); Logger.logEndSession(); //Console.WriteLine("Press any key to exit"); //Console.ReadKey(); }