Example #1
0
        //Startup code for the BattleShip window
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            humans = game.Human;
            aI     = game.AI;
            AddCells(pnlHuman);
            AddCells(pnlAI);

            Checktimer.Start();
            DecremTimer.Start();
        }
Example #2
0
        private static ShotResult HandleShot(Grid grid, TargetGrid targetGrid, OceanGrid oceanGrid)
        {
            Console.WriteLine("Call out your shot!");

            var shotCoordinate = new Coordinate(grid.Size, Console.ReadLine());
            var shotResult     = oceanGrid.TakeTheShot(shotCoordinate);

            targetGrid.MarkShot(new TargetGridMark(shotCoordinate, shotResult.Result));

            return(shotResult);
        }
        //Shows the state of the grid in passed into its parameter
        public string UpdateState(OceanGrid ocean)
        {
            Debug.WriteLine("Board:");

            StringBuilder fnlState = new StringBuilder();

            for (int x = 0; x < Size; x++)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("\r");
                for (int y = 0; y < Size; y++)
                {
                    switch (ocean.BoardLoc[x, y])
                    {
                    case OceanGrid.States.Ship:
                        builder.Append('X');
                        break;

                    case OceanGrid.States.Hit:
                        builder.Append('*');
                        break;

                    case OceanGrid.States.Missed:
                        builder.Append('O');
                        break;

                    default:
                        builder.Append('~');
                        break;
                    }
                    //if (ocean.BoardLoc[x, y] == OceanGrid.States.Ship)
                    //{
                    //    builder.Append('X');
                    //}
                    //else
                    //{
                    //    builder.Append('~');
                    //}
                }

                string state = builder.ToString();
                fnlState.AppendFormat("{0}\r\n", state);
                Debug.WriteLine(state);
            }
            Debug.WriteLine("\n");

            return(fnlState.ToString());
        }
        }                                                       //Sound to play when game ends;

        //Constructor for Game class
        public Game(int size)
        {
            Size     = size;
            NumShips = 5;
            Human    = new OceanGrid(size);
            AI       = new OceanGrid(size);


            Place();
            Attacked  = new List <Array>();
            timeLimit = 5;
            HShips    = Human.Ships.Count;
            AIShips   = AI.Ships.Count;

            Players = new List <string>();
        }
Example #5
0
        static void Main(string[] args)
        {
            var grid       = new Grid();
            var targetGrid = new TargetGrid(grid);
            var oceanGrid  = new OceanGrid(grid);

            ShotResult shotResult = null;

            while (!oceanGrid.IsTheEntireFleetSunk)
            {
                WriteTargetGrid(targetGrid);
                WriteShotResult(shotResult);
                shotResult = HandleShot(grid, targetGrid, oceanGrid);

                Console.Clear();
            }

            Console.WriteLine("The entire fleet is sunk. You won the game!");
        }
 //Checks in the grid in the params for a ship object with the coordinates x and y, and returns true if not found, false if found
 public bool ValidatePosition(OceanGrid grid, int x, int y)
 {
     return(!grid.GetShipCoord(x, y));
 }