Example #1
0
        /// <summary>
        /// Returns the opponent.
        /// </summary>
        /// <param name="competitor">The competitor.</param>
        /// <returns>The opponent.</returns>
        private Competitor Opponent(Competitor competitor)
        {
            if (competitor == this.CompetitorA)
            {
                return(this.CompetitorB);
            }

            return(this.CompetitorA);
        }
Example #2
0
        /// <summary>
        /// Handles the LeftGame event of the Competitor class.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void Competitor_LeftGame(object sender, EventArgs e)
        {
            Competitor quitter = (Competitor)sender;

            quitter.LeftGame -= this.Competitor_LeftGame;
            Competitor other = this.Opponent(quitter);

            other.SendGameWon();
            this.FireFinished(this, new FinishedArgs(this, other, quitter));
        }
Example #3
0
        /// <summary>
        /// Handles the received ship positions. Initiates shooting if positions of both players are received.
        /// </summary>
        /// <param name="sender">The sender of the ShipPositionsReceived event.</param>
        /// <param name="args">The event arguments.</param>
        private void Competitor_ShipPositionsReceived(object sender, ShipPositionsReceivedEventArgs args)
        {
            Competitor competitor = (Competitor)sender;

            competitor.BattleField.Ships      = args.Ships;
            competitor.ShipPositionsReceived -= this.Competitor_ShipPositionsReceived;
            competitor.PositionsReady         = true;

            if (this.CompetitorB.PositionsReady && this.CompetitorA.PositionsReady)
            {
                this.InitiateShooting();
            }
        }
Example #4
0
        /// <summary>
        /// Evaluates a received move and and sends the information if if it hit something.
        /// Sends a new MoveRequest to the other competitor.
        /// </summary>
        /// <param name="sender">The sender of the MoveReceived event.</param>
        /// <param name="args">The event arguments.</param>
        private void Competitor_MoveReceived(object sender, MoveReceivedEventArgs args)
        {
            Competitor competitor = (Competitor)sender;
            Competitor opponent   = this.Opponent(competitor);

            // Stop listening for incoming moves until the next request to this competitor is sent.
            competitor.MoveReceived -= this.Competitor_MoveReceived;

            Marker marker;
            bool   validMove;

            validMove = opponent.BattleField.AddMarker(args.Move, out marker);

            if (!validMove)
            {
                // Someone cheated.
                opponent.SendGameWon();
                competitor.Connection.Close();
                this.FireFinished(this, new FinishedArgs(this, opponent, competitor));
                return;
            }

            competitor.SendMoveReport(marker);
            opponent.SendOpponentsMove(marker);

            if (opponent.BattleField.Ships.Count == 0)
            {
                // Game finished.
                competitor.SendGameWon();
                opponent.SendGameLost();
                this.FireFinished(this, new FinishedArgs(this, competitor, opponent));
            }
            else
            {
                opponent.SendMoveRequest();
                opponent.MoveReceived += this.Competitor_MoveReceived;
            }
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FinishedArgs"/> class.
 /// </summary>
 /// <param name="battle">The battle.</param>
 /// <param name="winner">The winner.</param>
 /// <param name="loser">The loser.</param>
 /// <exception cref="ArgumentNullException">
 /// Battle - The value must not be null.
 /// or
 /// winner - The value must note be null.
 /// or
 /// winner - The value must note be null.
 /// </exception>
 public FinishedArgs(Battle battle, Competitor winner, Competitor loser)
 {
     this.Battle = battle ?? throw new ArgumentNullException(nameof(battle), "The value must not be null.");
     this.Winner = winner ?? throw new ArgumentNullException(nameof(winner), "The value must note be null.");
     this.Loser  = loser ?? throw new ArgumentNullException(nameof(winner), "The value must note be null.");
 }