/// <summary> /// /// </summary> /// <param name="token"></param> /// <param name="playerKey"></param> /// <param name="nextAction"></param> private static void WatchGame(object playerKey) { var key = playerKey.ToString(); var game = GameBusiness.GetGame(gameToken, key); Console.WriteLine("Me: " + game.Me.HealthPoints + "pv, foe: " + game.Foe.HealthPoints + "pv."); nextAction = AiBusiness.GetNextAction(game, lastAction); }
/// <summary> /// Game played by our AI /// </summary> private static Game PlayGame(Game game) { //Create a callback to get the current game state each 100ms Timer timer = new Timer(WatchGame, playerKey, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100)); while (game.Status != GameStatus.FINISHED) { lastAction = GameBusiness.PlayAndWaitCoolDown(game.Token, playerKey, nextAction); game = GameBusiness.GetGame(game.Token, playerKey); Console.WriteLine("Me: " + game.Me.HealthPoints + "pv, foe: " + game.Foe.HealthPoints + "pv."); } timer.Dispose(); return(game); }
/// <summary> /// Launch the game /// </summary> /// <param name="args">the arguments to create/join a game</param> public static void Main(string[] args) { // Check the program arguments CheckArguments(args); try { Game game = null; // Create a game if (mode == Mode.CREATE) { Console.WriteLine("Creating the game..."); game = GameBusiness.CreateGame(gameName, speedy, versusPlayer); gameToken = game.Token; } // Then join the game Console.WriteLine(); Console.WriteLine("Joining the game..."); game = GameBusiness.JoinGame(gameToken, playerKey, character.ToString(), playerName); // Wait for the opponent if (game.Status == GameStatus.WAITING) { Console.WriteLine("Waiting the future victim to join the game..."); while (game.Status == GameStatus.WAITING) { Thread.Sleep(500); game = GameBusiness.GetGame(game.Token, playerKey); } } // Opponent is connected. Waiting the countdown game = GameBusiness.GetGame(game.Token, playerKey); Console.WriteLine("Waiting countdown during " + game.CountDown + "ms..."); Thread.Sleep((int)game.CountDown); Console.WriteLine(); Console.WriteLine("Fight !"); Console.WriteLine(); // AI playing the game game = PlayGame(game); Console.WriteLine(); // game result if (game.Me.HealthPoints > 0) { Console.WriteLine("You WIN ! :D"); } else { Console.WriteLine("You lose... :'("); } } catch (Exception e) { Console.WriteLine("Something goes wrong..."); Console.WriteLine(e.Message); } QuitApplication(); }