Пример #1
0
        public void StartNewGame(int playerIndex, int gridSize, Ships ships)
        {
            _gridSize = gridSize;
            _index    = playerIndex;

            GenerateGuesses();

            //Random player just puts the ships in the grid in Random columns
            //Note it cannot deal with the case where there's not enough columns
            //for 1 per ship
            var availableColumns = new List <int>();

            for (int i = 0; i < gridSize; i++)
            {
                availableColumns.Add(i);
            }

            foreach (var ship in ships._ships)
            {
                //Choose an X from the set of remaining columns
                var x = availableColumns[Random.Next(availableColumns.Count)];
                availableColumns.Remove(x); //Make sure we can't pick it again

                //Choose a Y based o nthe ship length and grid size so it always fits
                var y = Random.Next(gridSize - ship.Length);
                ship.Place(new Position(x, y), Direction.Vertical);
            }
        }
Пример #2
0
        /// <summary>
        /// Logic to start new game
        /// </summary>
        /// <param name="playerIndex">Player's index in turn order.</param>
        /// <param name="gridSize">Size of the grid.</param>
        /// <param name="ships">Ships to be placed on grid.</param>
        public void StartNewGame(int playerIndex, int gridSize, Ships ships)
        {
            //TODO: Intelligently place Ships.

            HitPositions  = new List <Position>();
            MissPositions = new List <Position>();
            SankPositions = new List <Position>();
            _gridSize     = gridSize;
            _index        = playerIndex;



            // Currently: this borrows from RandomPlayer, which just puts the ships in the grid in Random columns


            var availableRows = new List <int>();

            for (int i = 0; i < gridSize; i++)
            {
                availableRows.Add(i);
            }

            _ships = ships;
            foreach (var ship in ships._ships)
            {
                // Pick an open y from the remaining rows
                var y = availableRows[Random.Next(availableRows.Count)];
                availableRows.Remove(y); //Make sure we can't pick it again

                // Pick a Y that fits
                var x = Random.Next(gridSize - ship.Length);
                ship.Place(new Position(x, y), Direction.Horizontal);
            }
        }
Пример #3
0
        public void Add(Ships ships)
        {
            foreach (var ship in ships._ships)
            {
                if (ship.Positions == null)
                {
                    throw new ArgumentException("A player has not set the ships positions");
                }

                foreach (var pos in ship.Positions)
                {
                    if (pos.X < 0 || pos.X > _gridSize || pos.Y < 0 || pos.Y >= _gridSize)
                    {
                        throw new ArgumentException("One of the ships is outside the grid");
                    }

                    if (pos.Hit)
                    {
                        throw new ArgumentException("One of the players is adding a hit ship to the game");
                    }

                    if (_grid[pos.X, pos.Y].Ship != null)
                    {
                        throw new ArgumentException("One of the players has an overlapping ship");
                    }

                    _grid[pos.X, pos.Y].Ship = ship;
                }
            }
        }
Пример #4
0
        public void StartNewGame(int playerIndex, int gridSize, Ships ships)
        {
            _gridSize = gridSize;
            _index    = playerIndex;

            //DumbPlayer just puts the ships in the grid one on each row
            int y = 0;

            foreach (var ship in ships._ships)
            {
                ship.Place(new Position(0, y++), Direction.Horizontal);
            }
        }
        internal void Play(PlayMode playMode)
        {
            currentPlayers = new List <IPlayer>();
            var availablePlayers = new List <IPlayer>(_players);

            _playerGrids = new List <Grid>();
            _playerShips = new List <Ships>();

            //Add each player in a random order
            for (int i = 0; i < _players.Count; i++)
            {
                var player = availablePlayers[Random.Next(availablePlayers.Count)];
                availablePlayers.Remove(player);
                currentPlayers.Add(player);
            }

            //Tell each player the game is about to start
            for (int i = 0; i < currentPlayers.Count; i++)
            {
                var ships = new Ships();
                ships.Add(new AircraftCarrier());
                ships.Add(new Submarine());
                ships.Add(new Destroyer());
                ships.Add(new Destroyer());
                ships.Add(new PatrolBoat());
                ships.Add(new PatrolBoat());
                ships.Add(new PatrolBoat());
                ships.Add(new Battleship());

                var count       = ships._ships.Count();
                int totalLength = ships._ships.Sum(ship => ship.Length);

                currentPlayers[i].StartNewGame(i, GridSize, ships);

                //Make sure player didn't change ships
                if (count != ships._ships.Count() ||
                    totalLength != ships._ships.Sum(ship => ship.Length))
                {
                    throw new Exception("Ship collection has ships added or removed");
                }

                var grid = new Grid(GridSize);
                grid.Add(ships);
                _playerGrids.Add(grid);
                _playerShips.Add(ships);
            }

            int currentPlayerIndex = 0;

            while (currentPlayers.Count > 1)
            {
                var currentPlayer = currentPlayers[currentPlayerIndex];

                //Ask the current player for their move
                Position pos = currentPlayer.GetAttackPosition();

                //Work out if anything was hit
                var results = CheckAttack(pos);

                //Notify each player of the results
                foreach (var player in currentPlayers)
                {
                    player.SetAttackResults(results);
                }

                DrawGrids();

                Console.WriteLine("\nPlayer " + currentPlayer.Index + "[" + currentPlayer.Name + "]  turn.");
                Console.WriteLine("    Attack: " + pos.X + "," + pos.Y);
                Console.WriteLine("\nResults:");
                foreach (var result in results)
                {
                    Console.Write("    Player " + result.PlayerIndex + " " + result.ResultType);
                    if (result.ResultType == AttackResultType.Sank)
                    {
                        Console.Write(" - " + result.SunkShip);
                    }
                    Console.WriteLine();
                }

                //Remove any ships with sunken battleships
                //Iterate backwards so that we don't mess with the indexes
                for (int i = currentPlayers.Count - 1; i >= 0; --i)
                {
                    var player = currentPlayers[i];
                    if (_playerShips[player.Index].SunkMyBattleShip)
                    {
                        currentPlayers.Remove(player);
                        //We never want to remvoe all the players...
                        if (currentPlayers.Count == 1)
                        {
                            break;
                        }
                    }
                }

                //Move to next player wrapping around the end of the collection
                currentPlayerIndex = (currentPlayerIndex + 1) % currentPlayers.Count;

                if (playMode == PlayMode.Pause)
                {
                    Console.WriteLine("\nPress a key to continue");
                    Console.ReadKey(true);
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Winner is '" + currentPlayers[0].Name + "'");
            Console.WriteLine();
            Console.ReadKey(true);
        }