Пример #1
0
        public static Ship CreateBattleShip(Player owner)
        {
            var ship = new Ship(ShipType.Battleship, owner);
            owner.AddShip(ship);

            return ship;
        }
Пример #2
0
 public Game()
 {
     List<Player> players = new List<Player>();
     Player player1 = new Player("Player One");
     players.Add(player1);
     Player player2 = new Player("Player Two");
     players.Add(player2);
     Players = players;
 }
Пример #3
0
        public void Initialize(Player player, Player computer)
        {
            this.PlaceShip(Ship.CreateDestroyer(player, computer));
            this.PlaceShip(Ship.CreateDestroyer(player, computer));
            this.PlaceShip(Ship.CreateBattleShip(player, computer));

            this.PlaceShip(Ship.CreateDestroyer(computer, player));
            this.PlaceShip(Ship.CreateDestroyer(computer, player));
            this.PlaceShip(Ship.CreateBattleShip(computer, player));
        }
Пример #4
0
 /// <summary>
 /// AddDeployedPlayer adds both players and will make sure
 /// that the AI player deploys all ships
 /// </summary>
 /// <param name="p"></param>
 public void AddDeployedPlayer(Player p)
 {
     if (_players[0] == null) {
         _players[0] = p;
     } else if (_players[1] == null) {
         _players[1] = p;
         CompleteDeployment();
     } else {
         throw new ApplicationException("You cannot add another player, the game already has two players.");
     }
 }
Пример #5
0
        private bool TakeMove(Player hero, Player enemy)
        {
            bool wasHit;

            do
            {
                var guessedCell = hero.TakeShot();
                wasHit = enemy.CheckForHit(guessedCell);

                if (wasHit && enemy.Dead())
                {
                    return true;
                }
            } while (wasHit);

            return false;
        }
Пример #6
0
        /// <summary>
        /// Starts a new game.
        /// </summary>
        /// <remarks>
        /// Creates an AI player based upon the _aiSetting.
        /// </remarks>
        public static void StartGame()
        {
            if (_theGame != null)
                EndGame();

            //Create the game
            _theGame = new BattleShipsGame();

            //create the players
            switch (_aiSetting) {
                case AIOption.Easy:
                    _ai = new AIEasyPlayer(_theGame);
                    break;
                case AIOption.Medium:
                    _ai = new AIMediumPlayer(_theGame);
                    break;
                case AIOption.Hard:
                    _ai = new AIHardPlayer(_theGame);
                    break;
                default:
                    _ai = new AIMediumPlayer(_theGame);
                    break;
            }

            _human = new Player(_theGame);

            //AddHandler _human.PlayerGrid.Changed, AddressOf GridChanged
            _ai.PlayerGrid.Changed += GridChanged;
            _theGame.AttackCompleted += AttackCompleted;

            AddNewState(GameState.Deploying);
        }
        /// <summary>
        /// Draws the player's grid and ships.
        /// </summary>
        /// <param name="grid">the grid to show</param>
        /// <param name="thePlayer">the player to show the ships of</param>
        /// <param name="small">true if the small grid is shown</param>
        /// <param name="showShips">true if ships are to be shown</param>
        /// <param name="left">the left side of the grid</param>
        /// <param name="top">the top of the grid</param>
        /// <param name="width">the width of the grid</param>
        /// <param name="height">the height of the grid</param>
        /// <param name="cellWidth">the width of each cell</param>
        /// <param name="cellHeight">the height of each cell</param>
        /// <param name="cellGap">the gap between the cells</param>
        private static void DrawCustomField(ISeaGrid grid, Player thePlayer, bool small, bool showShips, int left, int top, int width, int height, int cellWidth, int cellHeight,
            int cellGap)
        {
            //SwinGame.FillRectangle(Color.Blue, left, top, width, height)

            int rowTop = 0;
            int colLeft = 0;

            //Draw the grid
            for (int row = 0; row <= 9; row++) {
                rowTop = top + (cellGap + cellHeight) * row;

                for (int col = 0; col <= 9; col++) {
                    colLeft = left + (cellGap + cellWidth) * col;

                    Color fillColor = default(Color);
                    bool draw = false;

                    draw = true;

                    switch (grid[row, col]) {
            //                    case TileView.Ship:
            //                        draw = false;
            //                        break;
                        //If small Then fillColor = _SMALL_SHIP Else fillColor = _LARGE_SHIP
                    case TileView.Miss:
                        if (small)
                            fillColor = SMALL_MISS;
                        else
                            fillColor = LARGE_MISS;
                        break;
                    case TileView.Hit:
                        if (small)
                            fillColor = SMALL_HIT;
                        else
                            fillColor = LARGE_HIT;
                        break;
                    case TileView.Sea:
                    case TileView.Ship:
                        if (small)
                            fillColor = SMALL_SEA;
                        else
                            draw = false;
                        break;
                    }

                    if (draw) {
                        SwinGame.FillRectangle(fillColor, colLeft, rowTop, cellWidth, cellHeight);
                        if (!small) {
                            SwinGame.DrawRectangle(OUTLINE_COLOR, colLeft, rowTop, cellWidth, cellHeight);
                        }
                    }
                }
            }

            if (!showShips) {
                return;
            }

            int shipHeight = 0;
            int shipWidth = 0;
            string shipName = null;

            //Draw the ships
            foreach (Ship s in thePlayer) {
                if (s == null || !s.IsDeployed)
                    continue;
                rowTop = top + (cellGap + cellHeight) * s.Row + SHIP_GAP;
                colLeft = left + (cellGap + cellWidth) * s.Column + SHIP_GAP;

                if (s.Direction == Direction.LeftRight) {
                    shipName = "ShipLR" + s.Size;
                    shipHeight = cellHeight - (SHIP_GAP * 2);
                    shipWidth = (cellWidth + cellGap) * s.Size - (SHIP_GAP * 2) - cellGap;
                } else {
                    //Up down
                    shipName = "ShipUD" + s.Size;
                    shipHeight = (cellHeight + cellGap) * s.Size - (SHIP_GAP * 2) - cellGap;
                    shipWidth = cellWidth - (SHIP_GAP * 2);
                }

                if (!small) {
                    SwinGame.DrawBitmap(GameResources.GameImage(shipName), colLeft, rowTop);
                } else {
                    SwinGame.FillRectangle(SHIP_FILL_COLOR, colLeft, rowTop, shipWidth, shipHeight);
                    SwinGame.DrawRectangle(SHIP_OUTLINE_COLOR, colLeft, rowTop, shipWidth, shipHeight);
                }
            }
        }
        /// <summary>
        /// Draws a small field, showing the attacks made and the locations of the player's ships
        /// </summary>
        /// <param name="grid">the grid to show</param>
        /// <param name="thePlayer">the player to show the ships of</param>
        public static void DrawSmallField(ISeaGrid grid, Player thePlayer)
        {
            const int SMALL_FIELD_LEFT = 39;
            const int SMALL_FIELD_TOP = 373;
            const int SMALL_FIELD_WIDTH = 166;
            const int SMALL_FIELD_HEIGHT = 166;
            const int SMALL_FIELD_CELL_WIDTH = 13;
            const int SMALL_FIELD_CELL_HEIGHT = 13;
            const int SMALL_FIELD_CELL_GAP = 4;

            DrawCustomField(grid, thePlayer, true, true, SMALL_FIELD_LEFT, SMALL_FIELD_TOP, SMALL_FIELD_WIDTH, SMALL_FIELD_HEIGHT, SMALL_FIELD_CELL_WIDTH, SMALL_FIELD_CELL_HEIGHT,
                SMALL_FIELD_CELL_GAP);
        }
 /// <summary>
 /// Draws a large field using the grid and the indicated player's ships.
 /// </summary>
 /// <param name="grid">the grid to draw</param>
 /// <param name="thePlayer">the players ships to show</param>
 /// <param name="showShips">indicates if the ships should be shown</param>
 public static void DrawField(ISeaGrid grid, Player thePlayer, bool showShips)
 {
     DrawCustomField(grid, thePlayer, false, showShips, FIELD_LEFT, FIELD_TOP, FIELD_WIDTH, FIELD_HEIGHT, CELL_WIDTH, CELL_HEIGHT,
         CELL_GAP);
 }
Пример #10
0
 private Ship(ShipType type, Player owner)
 {
     this.ShipType = type;
     this.Owner = owner;
     this.hitCells = new List<Cell>();
 }
Пример #11
-1
        public static Ship CreateDestroyer(Player owner)
        {
            var ship = new Ship(ShipType.Destroyer, owner);
            owner.AddShip(ship);

            return ship;
        }