Ejemplo n.º 1
0
        /// <summary>
        /// A function that updates most of the content on the screen
        /// </summary>
        private void UpdateScreen()
        {
            ViewData[,] playerData   = PlayerBoard.GenerateViewData();
            ViewData[,] computerData = ComputerBoard.GenerateViewData();
            FlowDocument a = new FlowDocument(new Paragraph());

            a.Blocks.Add(new Paragraph());
            for (int y = 0; y < 21; y++)
            {
                Paragraph p = new Paragraph();
                Run       r = new Run
                {
                    Text = "       "
                };
                p.Foreground = Brushes.DodgerBlue;
                r.Foreground = Brushes.DarkGoldenrod;
                for (int x = 0; x < 44; x++)
                {
                    p.Inlines.Add("" + (char)playerData[x, y]);
                    r.Text += "" + (char)computerData[x, y];
                }
                p.Inlines.Add(r);
                a.Blocks.Add(p);
            }

            GameScreen.Document = a;
        }
Ejemplo n.º 2
0
        public ShootResultDTO MakePlayerMovement(int shotPositionX, int shotPositionY)
        {
            CheckIfGameEnded();

            var result = ComputerBoard.Shoot(shotPositionX, shotPositionY);

            return(result);
        }
Ejemplo n.º 3
0
        public void FireOpponentMissile()
        {
            if (EasyMode == true)
            {
                FireOpponentMissileEasy();
            }
            else
            {
                FireOpponentMissileDifficult();
            }

            // check if the game is done
            if (UserBoard.AreAllShipsSunken())
            {
                GameOver = true;
                MessageBox.Show("Computer won the game!", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (ComputerBoard.AreAllShipsSunken())
            {
                GameOver = true;
                MessageBox.Show("You won the game!", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Simple keyboard event handler
        /// </summary>
        /// <param name="key">Key pressed</param>
        public void KeyPressed(Key key)
        {
            switch (key)
            {
            case Key.Left:
                PlayerCursor.X = Math.Max(PlayerCursor.X - 1, 0);
                break;

            case Key.Right:
                PlayerCursor.X = Math.Min(PlayerCursor.X + 1, (CurrentPhase == GamePhase.BoardPlanning && !currentShipOrientation) ? 10 - currentShipLength : 9);
                break;

            case Key.Up:
                PlayerCursor.Y = Math.Max(PlayerCursor.Y - 1, 0);
                break;

            case Key.Down:
                PlayerCursor.Y = Math.Min(PlayerCursor.Y + 1, (CurrentPhase == GamePhase.BoardPlanning && currentShipOrientation) ? 10 - currentShipLength : (CurrentPhase == GamePhase.MainMenu ? 2 : 9));
                break;

            case Key.E:
                if (CurrentPhase == GamePhase.Game && currentPlacementViable)
                {
                    ComputerBoard.MarkSpot(PlayerCursor.X, PlayerCursor.Y);
                    UpdateScreen();
                }
                break;

            case Key.R:
                if (CurrentPhase == GamePhase.BoardPlanning)
                {
                    if (currentShipOrientation && PlayerCursor.X > 10 - currentShipLength)
                    {
                        PlayerCursor.X = 10 - currentShipLength;
                    }
                    else if (!currentShipOrientation && PlayerCursor.Y > 10 - currentShipLength)
                    {
                        PlayerCursor.Y = 10 - currentShipLength;
                    }
                    currentShipOrientation = !currentShipOrientation;
                }
                break;

            case Key.Enter:
                if (CurrentPhase == GamePhase.End)
                {
                    Console.WriteLine("Game reset");
                    ResetGame();
                }
                break;

            case Key.Space:
                if (CurrentPhase == GamePhase.BoardPlanning && currentPlacementViable)
                {
                    PlayerBoard.PlaceShipOnBoard(PlayerCursor.X, PlayerCursor.Y, currentShipLength, currentShipOrientation);
                    UpdateScreen();
                    if (--shipsLeftToPlace == 0)
                    {
                        if (--currentShipLength <= 0)
                        {
                            PlayerCursor.X = PlayerCursor.Y = 0;
                            ChangePhase(GamePhase.Game);
                            return;
                        }
                        shipsLeftToPlace = 5 - currentShipLength;
                    }
                }
                else if (CurrentPhase == GamePhase.Game && currentPlacementViable)
                {
                    Coordinate computerMove   = ComputerBrain.GenerateNextMove(PlayerBoard.GetBoardValues());
                    BoardValue playerResult   = ComputerBoard.Shoot(PlayerCursor.X, PlayerCursor.Y);
                    BoardValue computerResult = PlayerBoard.Shoot(computerMove.x, computerMove.y);

                    if (playerResult == BoardValue.Hit || playerResult == BoardValue.Sank)
                    {
                        playerScore++;
                    }
                    if (computerResult == BoardValue.Hit || computerResult == BoardValue.Sank)
                    {
                        computerScore++;
                    }

                    if (playerScore == 20 || computerScore == 20)
                    {
                        Console.WriteLine("End of the game!!");
                        string result = computerScore == playerScore ? "Remis" : playerScore == 20 ? "Wygrałeś" : "Wygrałem";
                        PostComputerMessage($"{result}! Twoj wynik: {100 + 5 * (playerScore - computerScore)}([ENTER], aby zagrać ponownie)");
                        ChangePhase(GamePhase.End);
                    }
                    else
                    {
                        PostComputerMessage(ComputerBrain.GetComputerResponse(playerResult, computerResult));
                    }

                    UpdateScreen();
                }
                else if (CurrentPhase == GamePhase.MainMenu)
                {
                    switch (PlayerCursor.Y)
                    {
                    case 0:
                        ChangePhase(GamePhase.BoardPlanning);
                        AnimateTransition(MainMenu, false);
                        UpdateScreen();
                        break;

                    case 1:
                        ChangePhase(GamePhase.Information);
                        AnimateTransition(Information, true);
                        break;

                    case 2:
                        Application.Current.Shutdown();
                        break;
                    }
                }
                else if (CurrentPhase == GamePhase.Information)
                {
                    ChangePhase(GamePhase.MainMenu);
                    AnimateTransition(Information, false);
                }
                break;
            }

            if (CurrentPhase == GamePhase.BoardPlanning)
            {
                currentPlacementViable = PlayerBoard.IsPlaceViable(PlayerCursor.X, PlayerCursor.Y, currentShipLength, currentShipOrientation);
                PlayerCursor.UpdatePhase(GamePhase.BoardPlanning, currentShipLength, currentShipOrientation, currentPlacementViable);
            }
            else if (CurrentPhase == GamePhase.Game)
            {
                currentPlacementViable = ComputerBoard.CanShoot(PlayerCursor.X, PlayerCursor.Y);
                PlayerCursor.UpdatePhase(GamePhase.Game, 1, false, currentPlacementViable);
            }
        }