/// <summary> /// Начислить очки игроку /// </summary> /// <param name="player">Игрок</param> private void AddPointsToWinner(Player player) { player.Points++; MakeNotification(string.Format("{0} wins round", player.Name)); Turn currentTurn = Turns[CurrentTurn - 1]; currentTurn.Winner = player; SendPlayerActionsInformation(); if (player.Points == VictoryLimit) { Winner = player; MakeNotification(string.Format("{0} win the game!", Winner.Name)); GameFinished(this, new GameChangedEventArgs() { GameName = Name, GameWinner = Winner.Name, Time = DateTime.Now.ToShortTimeString() }); FinishGame(); } NextTurn(); }
/// <summary> /// Обработка действия игрока /// </summary> /// <param name="player">Игрок</param> /// <param name="action">Действие</param> public void SetAction(Player player, GameAction action) { if (Status != GameStatus.Game) return; var gameActions = Turns[CurrentTurn - 1].GameActions; if (gameActions.Count < 2) { gameActions.Add(new KeyValuePair<Player, GameAction>(player, action)); } if (gameActions.Count == 2) { Player player1 = Players[0]; Player player2 = Players[1]; Player winner = null; GameAction player1Action = gameActions.Find(x => x.Key.Name == Players[0].Name).Value; GameAction player2Action = gameActions.Find(x => x.Key.Name == Players[1].Name).Value; GameActionResult result = (GameActionResult)Engine.Instance.gameConfig[(int)player1Action, (int)player2Action]; switch (result) { case GameActionResult.Draw: Turns[CurrentTurn - 1].IsDraw = true; MakeNotification(string.Format("Turn {0} - nobody win!", CurrentTurn)); SendPlayerActionsInformation(); NextTurn(); break; case GameActionResult.Victory: winner = player1; break; case GameActionResult.Defeat: winner = player2; break; } if (winner != null) { AddPointsToWinner(winner); } } }
/// <summary> /// Начать игру /// </summary> /// <param name="playerName">Игрок</param> /// <returns></returns> public bool StartGame(Player player) { if (Players.Count < 1) { return false; } if (Creator == player) { Status = GameStatus.Game; StartedOn = DateTime.Now; Turns.Add(new Turn()); StartTimer(); MakeNotification(string.Format("Game {0} started", Name)); StateChanged(this, new GameChangedEventArgs() { GameName = Name, Status = "Game", Id = Id }); return true; } return false; }
/// <summary> /// Выйти из игры /// </summary> /// <param name="player">Игрок</param> public void LeaveGame(Player player) { if(Players.Count == 2 && Status == GameStatus.Waiting) { PlayerLeaveNotStartedGame(this, new GameChangedEventArgs() { GameName = Name, Player = player.Name }); } if (Players.Count == 2 && Status == GameStatus.Game) { player.CantPlayFrom = DateTime.Now; Winner = Players.Find(x => x != player); Status = GameStatus.Finished; StateChanged(this, new GameChangedEventArgs() { GameName = Name, Status = "Finished", Id = Id }); MakeNotification(string.Format("Player {0} exit game. Player {1} win!", player.Name, Winner.Name)); } RemoveFromPlayers(player); }
/// <summary> /// Удалить игрока из списка /// </summary> /// <param name="player">Игрок</param> /// <returns></returns> public bool RemoveFromPlayers(Player player) { Players.Remove(player); player.CurrentGame = null; player.HintUsed = false; MakeNotification(string.Format("Player {0} removed from player list", player.Name)); PlayersCountChanged(this, new GameChangedEventArgs() { GameName = Name, PlayersCount = Players.Count, Id = Id }); if (Players.Count == 0) { Debug.WriteLine("All player exit game {0}. Game will deleted", Name); Engine.Instance.RemoveGame(this); } return true; }
/// <summary> /// Получить подсказку /// </summary> /// <returns>Код действия</returns> public int GetHint(Player player) { Turn turn = Turns[CurrentTurn - 1]; foreach (var item in turn.GameActions) { if (item.Key != player) { turn.HintsUsed.Add(player); player.HintUsed = true; Random rand = new Random(); int realAction = (int)item.Value; if (rand.Next(0, 2) == 0) { return realAction; } else { int[] randomActions = new int[] { 0, 1, 2, 3, 4 }; //По количеству возможных действий randomActions = randomActions = randomActions.Where(val => val != realAction).ToArray(); //Исключим из выборки правильный ответ int itemIndex = rand.Next(0, randomActions.Length); return randomActions[itemIndex]; } } } return -1; }
/// <summary> /// Подключить игрока к игре /// </summary> /// <param name="playerName">Игрок</param> /// <returns></returns> public bool JoinToGame(Player player) { if (player == Creator) return true; if (Players.Contains(player)) return true; if (Players.Count != 2 && Status == GameStatus.Waiting) { Players.Add(player); player.CurrentGame = this; player.HintUsed = false; Status = GameStatus.Waiting; MakeNotification(string.Format("Player {0} connect to game {1}", player.Name, Name)); PlayerConnected(this, new GameChangedEventArgs() { GameName = Name, Player2Name = player.Name }); PlayersCountChanged(this, new GameChangedEventArgs() { GameName = Name, PlayersCount = Players.Count, Id = Id }); return true; } throw new Exception("Player list is full!"); //return false; }
public Game(string name, int turnDuration, int victoryLimit, Player creator) { Name = name; TurnDuration = turnDuration; VictoryLimit = victoryLimit; Status = GameStatus.Waiting; Turns = new List<Turn>(); CurrentTurn = 1; Players = new List<Player>(); Creator = creator; CreatedOn = DateTime.Now; Id = Guid.NewGuid(); }
/// <summary> /// Добавить игрока в лист /// </summary> /// <param name="playerName">Возвращает объект созданного игрока</param> public bool AddPlayer(string playerName) { if(Players.Find(x => x.Name == playerName) == null) { Player player = new Player(playerName); Players.Add(player); PlayerAdded(this, new EntityAddedEventArgs() { Id = player.Id, Name = player.Name }); } return true; }
public Lives(Player player, Rectangle screenBounds) { _player = player; _screenBounds = screenBounds; }
public Score(Player player, Rectangle screenBounds) { _player = player; _screenBounds = screenBounds; }
public MainGame() { CurrentGameState = new GameState(); CurrentPlayerState = new Player(); }