Esempio n. 1
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));
        }
Esempio n. 2
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;
            }
        }