Beispiel #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);
            }
        }
Beispiel #2
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;
                }
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        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.ReadKey(true);
        }
Beispiel #5
0
        public void StartNewGame(int playerIndex, int gridSize, Ships ships)
        {
            _gridSize = gridSize;
            _index    = playerIndex;

            spaces = new string[_gridSize, _gridSize];

            for (int x = 0; x < _gridSize; x++)
            {
                for (int y = 0; y < _gridSize; y++)
                {
                    spaces[x, y] = "good";
                }
            }

            //variable for placement loop
            bool okayMove = false;

            //loop to push the desired ships on a stack
            foreach (var ship in ships._ships)
            {
                okayMove = false;

                //loop to place ships until gone
                while (!okayMove)
                {
                    //counter to verify that there are no collisions
                    int goodSpace = 0;

                    //generates random cooridinates
                    int x = rand.Next(1, 10);
                    int y = rand.Next(1, 10);

                    //generates a random number for use in assigning direction
                    int directionNumber = rand.Next(1, 10);

                    //assigns a direction
                    if (directionNumber > 0 && directionNumber < 6)
                    {
                        direction = Direction.Horizontal;
                    }
                    else
                    {
                        direction = Direction.Vertical;
                    }



                    //checks for direction and valid starting coordinate
                    if (spaces[y, x] == "good" && direction == Direction.Horizontal)
                    {
                        //check to make sure variables stay within scope
                        if (y + ship.Length < _gridSize)
                        {
                            //checks for more valid spaces
                            for (int i = 0; i < ship.Length; i++)
                            {
                                if (spaces[y + i, x] == "good")
                                {
                                    goodSpace++;
                                }
                            }
                        }

                        //if no collisions places boat in grid, adds to the ship's position array, and pops it out of the stack
                        if (goodSpace == ship.Length)
                        {
                            ship.Place(new Position(y, x), Direction.Horizontal);

                            for (int i = 0; i < ship.Length; i++)
                            {
                                spaces[y + i, x] = "used";
                            }

                            okayMove = true;
                        }
                    }

                    //same as the last block of code, but for different direction
                    else if (spaces[y, x] == "good" && direction == Direction.Vertical)
                    {
                        //check to make sure variables stay within scope
                        if (x + ship.Length < _gridSize)
                        {
                            //checks for more valid spaces
                            for (int i = 0; i < ship.Length; i++)
                            {
                                if (spaces[y, x + i] == "good")
                                {
                                    goodSpace++;
                                }
                            }
                        }

                        //if no collisions places boat in grid, adds to the ship's position array, and pops it out of the stack
                        if (goodSpace == ship.Length)
                        {
                            ship.Place(new Position(y, x), Direction.Vertical);

                            for (int i = 0; i < ship.Length; i++)
                            {
                                spaces[y, x + i] = "used";
                            }

                            okayMove = true;
                        }
                    }
                }
            }
        }