예제 #1
0
 private void DebugUpdate()
 {
     if (!clicked)
     {
         if (s.GetShip() != null)
         {
             this.Image = Properties.Resources.bombed;
         }
     }
 }
예제 #2
0
        public KeyValuePair <Square, Result> Attack()
        {
            if (inTheMiddleOfShip)
            {
                target = null;

                while (target == null)
                {
                    if (direction == Direction.Unknown)
                    {
                        direction = directionsStack.Pop();
                    }

                    do // Avoid board edges
                    {
                        try
                        {
                            switch (direction)
                            {
                            case Direction.Down:
                                target = gameManager.GetPlayer().board[lastHit.I + 1, lastHit.J];
                                break;

                            case Direction.Left:
                                target = gameManager.GetPlayer().board[lastHit.I, lastHit.J - 1];
                                break;

                            case Direction.Right:
                                target = gameManager.GetPlayer().board[lastHit.I, lastHit.J + 1];
                                break;

                            case Direction.Up:
                                target = gameManager.GetPlayer().board[lastHit.I - 1, lastHit.J];
                                break;
                            }

                            break;
                        }
                        catch (IndexOutOfRangeException)
                        {
                            // Looping again with next direction
                            if (directionsStack.Contains(GetOppositeDirection(direction)))
                            {
                                direction = GetOppositeDirection(direction);
                            }
                            else
                            {
                                direction = directionsStack.Pop();
                            }
                        }
                    } while (true);

                    // Target is now somewhere in the board

                    if (target.WasBombed && target.GetShip() == null)
                    {
                        if (directionsStack.Contains(GetOppositeDirection(direction)))
                        {
                            direction = GetOppositeDirection(direction);
                        }
                        else
                        {
                            direction = Direction.Unknown;
                        }

                        target = null;
                    }
                    else if (target.WasBombed && target.GetShip() != null && !target.GetShip().IsDown())
                    {
                        while (target.WasBombed)
                        {
                            lastHit = target;
                            switch (direction)
                            {
                            case Direction.Down:
                                target = gameManager.GetPlayer().board[lastHit.I + 1, lastHit.J];
                                break;

                            case Direction.Left:
                                target = gameManager.GetPlayer().board[lastHit.I, lastHit.J - 1];
                                break;

                            case Direction.Right:
                                target = gameManager.GetPlayer().board[lastHit.I, lastHit.J + 1];
                                break;

                            case Direction.Up:
                                target = gameManager.GetPlayer().board[lastHit.I - 1, lastHit.J];
                                break;
                            }
                        }
                        // Now targets points at the next not bombed square
                    }
                }

                lastResult = gameManager.Attack(this, target);

                if (lastResult == Result.Hit)
                {
                    lastHit = target;
                }
                else if (lastResult == Result.Miss)
                {
                    if (directionsStack.Contains(GetOppositeDirection(direction)))
                    {
                        direction = GetOppositeDirection(direction);
                    }
                    else
                    {
                        direction = Direction.Unknown;
                    }
                }
                else if (lastResult == Result.ShipDestroyed)
                {
                    lastHit           = null;
                    inTheMiddleOfShip = false;
                    direction         = Direction.Unknown;
                }
                else if (lastResult == Result.Victory)
                {
                }
                else if (lastResult == Result.None)
                {
                    throw new Exception();
                }
            }

            else
            {
                int i, j;

                do
                {
                    i = r.Next(0, board.GetLength(0));
                    j = r.Next(0, board.GetLength(1));

                    target = gameManager.GetPlayer().board[i, j];
                }while (target.WasBombed);

                direction  = Direction.Unknown;
                lastResult = gameManager.Attack(this, target);

                if (lastResult == Result.Hit)
                {
                    RandomizeStack();
                    firstHit          = target;
                    lastHit           = target;
                    inTheMiddleOfShip = true;
                }
            }

            return(new KeyValuePair <Square, Result>(target, lastResult));
        }
예제 #3
0
        private async void computerBoard_Click(Object sender, EventArgs e)
        {
            gs = (GraphicSquare)sender;
            MouseEventArgs temp = (MouseEventArgs)e;

            if (!gameStarted && temp.Button == MouseButtons.Right && selectedShip != null)
            {
                selectedShip.Rotate();
            }


            else if (gameStarted && gs.Clickable && !gs.Clicked)
            {
                playersResult = gameManager.Attack(player, gs.GetSquare());

                if (playersResult != Result.None)
                {
                    playerTurnsCount++;
                    playersTurnsCountLabel.Text = playerTurnsCount.ToString();

                    gs.Attack();

                    if (playersResult == Result.ShipDestroyed || playersResult == Result.Victory)
                    {
                        UpdateShipDestroyed(Player.PlayerType.Player, gs.GetSquare().GetShip());

                        if (playersResult == Result.Victory)
                        {
                            foreach (GraphicSquare temp2 in computerGraphicBoard)
                            {
                                temp2.Clickable = false;
                            }

                            if (sound)
                            {
                                win.Play();
                            }

                            GameEnded(player);
                        }
                    }

                    PlaySound(playersResult);

                    #region Computer's Play
                    if (playersResult == Result.Miss)
                    {
                        do
                        {
                            await Delay();

                            KeyValuePair <Square, Result> values = computer.Attack();
                            computersAttackSquare = values.Key;
                            computersResult       = values.Value;
                            playerGraphicBoard[computersAttackSquare.I, computersAttackSquare.J].Attack();

                            if (computersResult == Result.ShipDestroyed)
                            {
                                UpdateShipDestroyed(Player.PlayerType.Computer, computersAttackSquare.GetShip());
                            }

                            PlaySound(computersResult);
                            this.Update(); //?

                            computerTurnsCount++;
                            computersTurnsCountLabel.Text = computerTurnsCount.ToString();
                        } while (computersResult != Result.Miss && computersResult != Result.Victory);

                        if (computersResult == Result.Victory)
                        {
                            UpdateShipDestroyed(Player.PlayerType.Computer, computersAttackSquare.GetShip());
                            this.Update();

                            if (sound)
                            {
                                lose.Play();
                            }

                            GameEnded(computer);
                        }
                    }
                    #endregion
                }
            }
        }