/// <summary> /// Notify the players of the moves that the other players have made. /// </summary> /// <param name="format"> /// The format that we will use to notify the players. /// </param> public void MakePlayersNotifyEachOtherAboutTheirMoves(FormatNotificationToListeners format) { MoveListener notifyFunc; // Delete old listeners and set format foreach (Player player in players) { player.ClearListeners(); player.SetFormat(format); } // Set each player new listeners foreach (Player player in players) { notifyFunc = (move) => { try { player.NotifyAChangeInTheGame(move); } catch { // Do nothing, to avoid efecting the other players } }; foreach (Player p in players) { if (!player.Equals(p)) { p.NotifyMeWhenYouMove += notifyFunc; } } } }
/// <summary> /// Constructor for the player class that gets the client and it's id. /// </summary> /// <param name="client"> /// The client as a ICanBeNotified type. /// </param> /// <param name="id"> /// The id of the player. /// </param> public Player(ICanbeNotified client, int id) { this.client = client; this.id = id; // Initialize as empty NotifyMeWhenYouMove = new MoveListener((s) => { }); format = (dir) => ""; }
/// <summary> /// Set the format of the notification to the listeners as the /// function which we will get in the input as a delegate. /// </summary> /// <param name="format"> /// A delegate of the notification format function as we defined above. /// </param> public void SetFormat(FormatNotificationToListeners format) { this.format = format; }