Esempio n. 1
0
        /// <summary>
        /// Called when opponent attacks you and handles the game logic accordingly
        /// </summary>
        /// <param name="attack">Attack data</param>
        private void Attack(SalvoAttack attack)
        {
            Coordinates attackCoords = new Coordinates(attack.X, attack.Y);
            bool        wasHit       = _salvo.IsHit(attackCoords);

            if (wasHit)
            {
                MarkHit(attackCoords);
                _salvo.MarkHitOnMyShip(attackCoords);
            }

            bool wasFinalBlow = _salvo.IsMyShipDead();

            if (wasFinalBlow)
            {
                _server?.SendWinResult(new SalvoData()
                {
                    IsWinResult    = true,
                    SalvoWinResult = new SalvoWinResult()
                    {
                        DidWin = true
                    }
                });
                _client?.SendWinResult(new SalvoData()
                {
                    IsWinResult    = true,
                    SalvoWinResult = new SalvoWinResult()
                    {
                        DidWin = true
                    }
                });
                new FormGameOver(false).ShowDialog();
                Environment.Exit(0);
            }
            else
            {
                _server?.SendAttackResult(new SalvoData()
                {
                    IsAttackResult = true,
                    AttackResult   = new SalvoAttackResult()
                    {
                        WasHit = wasHit,
                    }
                });
                _client?.SendAttackResult(new SalvoData()
                {
                    IsAttackResult = true,
                    AttackResult   = new SalvoAttackResult()
                    {
                        WasHit = wasHit,
                    }
                });
            }

            lOGrid.Invoke(new MethodInvoker(() => lOGrid.Text          = GetGridLocation(attackCoords)));
            lOHit.Invoke(new MethodInvoker(() => lOHit.Text            = (wasHit ? "Hit!" : "Miss!")));
            btnLaunch.Invoke(new MethodInvoker(() => btnLaunch.Enabled = true));
        }
Esempio n. 2
0
        /// <summary>
        /// Launches attack on opponent and sends attack over network
        /// </summary>
        private void btnLaunch_Click(object sender, EventArgs e)
        {
            Coordinates coords = GetCoordinates();
            var         attack = new SalvoAttack()
            {
                X = coords.X,
                Y = coords.Y
            };

            _server?.LaunchAttack(new SalvoData()
            {
                Message  = "thjis is from the client",
                IsAttack = true,
                Attack   = attack
            });
            _client?.LaunchAttack(new SalvoData()
            {
                Message  = "thjis is from the client",
                IsAttack = true,
                Attack   = attack
            });
            lGrid.Text        = cbLocation.Text;
            btnLaunch.Enabled = false;
        }