Exemplo n.º 1
0
        /// <summary>
        /// Gets the player ready to play a new game.  Since the
        /// player can be reused for mutliple games this method
        /// must initialize all needed data structures.
        /// </summary>
        /// <param name="game">Game for the player to play.</param>
        public override void StartGame(BattleShipGame game)
        {
            base.StartGame(game);
            // Initialize the player data structures.
            plannedShots = new Stack <Position>();
            ShotCursor   = new Position(0, 0);
            offset       = StepSize;

            plannedShots.Push(new Position(0, 0));
        }
Exemplo n.º 2
0
 public override void StartGame(BattleShipGame game)
 {
     base.StartGame(game);
     if (CountUp)
     {
         count = 0;
     }
     else
     {
         count = Game.GridSize * Game.GridSize - 1;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the player ready to play a new game.  First creates
        /// a list of all game Positions in order.  Then randomly picks
        /// Positions from this list and moves them to the random active
        /// target list that will be used when playing the game.
        /// </summary>
        /// <param name="game">Game for the player to play.</param>
        public override void StartGame(BattleShipGame game)
        {
            base.StartGame(game);
            targets       = new LinkedList <Position>();
            activeTargets = new Stack <Position>();

            int max = Game.GridSize * Game.GridSize;

            for (int i = 0; i < max; ++i)
            {
                Position p = new Position(i / Game.GridSize, i % Game.GridSize);
                targets.AddLast(p);
            }
            for (int i = 0; i < max; ++i)
            {
                int      index = rnd.Next((int)targets.LongCount());
                Position p     = targets.ElementAt(index);
                targets.Remove(p);
                activeTargets.Push(p);
            }
        }
 /// <summary>
 /// Gets the player ready to play a new game.  Since the
 /// player can be reused for mutliple games this method
 /// must initialize all needed data structures.
 /// </summary>
 /// <param name="game">Game for the player to play.</param>
 public override void StartGame(BattleShipGame game)
 {
     base.StartGame(game);
     // Initialize the player data structures.
 }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            int gridSize    = 15;
            var winCriteria = BattleShipGame.WinCriteriaEnum.BATTLESHIP;
            int numTrials   = 30;

            // Define the ships to use for testing.
            Ship[] shipsToPlace =
            {
                new AircraftCarrier(),
                new BattleShip(),
                new Cruiser(),
                new Submarine(),
                new PatrolBoat(),
                new PatrolBoat()
            };

            // Define the AI players to test.
            IPlayer[] players =
            {
                //new DumbPlayer("Dumb Top Down", true),
                //new DumbPlayer("Dumb Bottom Up", false),
                new RandomPlayer("Random"),
                // Add your player here
                new CS3110_Module_8_Group2("Group 2"),
            };

            int[] wins = new int[players.Length];
            for (int trial = 0; trial < numTrials; ++trial)
            {
                Fleet fleet = new Fleet();

                // Place the ships randomly.  Will be used for all players.
                foreach (Ship s in shipsToPlace)
                {
                    bool shipAddedFlag = false;
                    do
                    {
                        try
                        {
                            shipAddedFlag = false;
                            s.RandomPlace(gridSize);
                            fleet.Add(s); // Might throw exception here
                            shipAddedFlag = true;
                        }
                        catch (Exception ex)
                        {
                            // do nothing
                        }
                    } while (!shipAddedFlag);
                }

                // Setup a game for each player and let the player know we are starting.
                BattleShipGame[] games = new BattleShipGame[players.Length];
                for (int i = 0; i < players.Length; ++i)
                {
                    games[i] = new BattleShipGame(gridSize, players[i], shipsToPlace, winCriteria);
                    players[i].StartGame(games[i]);
                }

                // Play the game seeing who wins first.
                bool gameOver = false;
                while (!gameOver)
                {
                    for (int i = 0; i < games.Length; ++i)
                    {
                        games[i].Turn();
                        if (games[i].GameOver())
                        {
                            gameOver = true;
                            ++wins[i];
                            Console.WriteLine();
                            Console.WriteLine(players[i].Name + " won!");
                            games[i].Draw();
                        }
                    }
                }
            }

            for (int i = 0; i < players.Length; ++i)
            {
                Console.WriteLine("{0} wins: {1}", players[i].Name, wins[i]);
            }
            System.Console.ReadLine();
        }
Exemplo n.º 6
0
 /// <summary>
 /// Gets the player ready to play a new game.  In overridden
 /// methods be sure to call base.StartGame(game) to call this
 /// base method.
 /// </summary>
 /// <param name="game">Game for the player to play.</param>
 public virtual void StartGame(BattleShipGame game)
 {
     Game = game;
 }