// uncomment this for random delay between game actions //readonly static int[] gamePlayTimeCycles = { 10000, 5000, 20000, 15000, 25000 }; public static void Play() { // New game played by Vinny Game game = new Game("Vinny"); GameSaveSnapshots gameSaveSnapshots = new GameSaveSnapshots(); game.Start(); Thread.Sleep(getNextGamePlayTimeCycle()); game.Pause(); Thread.Sleep(getNextGamePlayTimeCycle()); game.Resume(); Thread.Sleep(getNextGamePlayTimeCycle()); // Pause and exit the game GameStateMemento gameStateMemento = game.PauseAndExit(); gameSaveSnapshots.SetSnapshot(gameStateMemento); Thread.Sleep(getNextGamePlayTimeCycle()); // Resume old game by Sony. game = new Game("Sony", gameSaveSnapshots.GetSnapshot()); game.Resume(); Thread.Sleep(getNextGamePlayTimeCycle()); game.Pause(); Thread.Sleep(getNextGamePlayTimeCycle()); // Terminate game.Exit(); // New game played by Anny. The game will end itself without any interruptions game = new Game("Anny"); game.Start(); Console.ReadKey(); }
void play() { try { Gamer.WriteGameLog("Playing the game."); GameTimeCycle(); while (!gameState.IsGameOver()) { gameState.AdvanceGameAction(); if (gameState.IsCurrentActionLevelComplete()) { // All good and level complete. Which means this level is won. gameSaveSnapshots.SetSnapshot(gameSaver.SaveGame(gameState)); } else if (gameState.IsCurrentActionLost()) { if (gameState.AreLivesGone() == false) { int currentLife = gameState.GetCurrentLife(); Gamer.WriteGameLog(""); Gamer.WriteGameLog("Player lost life. Restoring the life and game.", gameState.GetCurrentLevel()); Gamer.WriteGameLog("Lives Used: " + gameState.GetLivesUsed() + ",Lives Left: " + gameState.GetLivesLeft()); Gamer.WriteGameLog("Playing the level: " + gameState.GetCurrentLevel()); Gamer.WriteGameLog(""); // Great. Restore the previous state GameStateMemento gameStateMemento = gameSaveSnapshots.GetSnapshot(); gameState = gameStateMemento.GetState(); gameState.SetCurrentLife(currentLife); } else { // No lives left. Game over man. } } GameTimeCycle(); } gameState.SetEndTime(DateTime.Now); displayGameEndSummary(); } catch (ThreadAbortException exception) { gameSaveSnapshots.SetSnapshot(gameSaver.SaveGame(gameState)); } }