예제 #1
0
        static void Main(string[] args)
        {
            var player = new Player(new Board());

            player.AddShip(1, 1, 3, Orientation.Vertical);

            while (true)
            {
                try
                {
                    Console.WriteLine("Enter Attack Position:");
                    var data   = Console.ReadLine()?.Split(',');
                    var result = player.Fire(int.Parse(data[0]), int.Parse(data[1]));

                    Console.WriteLine(result);

                    if (!player.IsLost)
                    {
                        continue;
                    }
                    Console.WriteLine("Game Over!");
                    break;
                }
                catch
                {
                    Console.WriteLine("Wrong Input");
                }
            }
        }
예제 #2
0
 // Places ship if it can be placed
 private bool PlaceShip(Ship ship)
 {
     if (IsValidShipPlacement(ship) && IsNotOverLapping(ship))
     {
         _player.AddShip(ship);
         return(true);
     }
     return(false);
 }
예제 #3
0
        static void Main(string[] args)
        {
            //First Create the BattleField
            string      battleFieldDimensions = Console.ReadLine();
            BattleField battleField           = new BattleField(battleFieldDimensions);

            //Create the 2 Players
            Player player1 = new Player("Player-1");
            Player player2 = new Player("Player-2");

            int          noOfBattleShipsForEachPlayer = Convert.ToInt32(Console.ReadLine());
            List <IShip> player1Ships = new List <IShip>();
            List <IShip> player2Ships = new List <IShip>();

            for (int i = 0; i < noOfBattleShipsForEachPlayer; i++)
            {
                string      inputLine          = Console.ReadLine();
                string[]    inputs             = inputLine.Split(' ');
                char        shipType           = Convert.ToChar(inputs[0]);
                int         shipWidth          = Convert.ToInt32(inputs[1]);
                int         shipHeight         = Convert.ToInt32(inputs[2]);
                Coordinates player1Coordinates = new Coordinates(inputs[3]);
                Coordinates player2Coordinates = new Coordinates(inputs[4]);

                player1.AddShip(ShipFactory.GetShip(shipType, player1Coordinates, shipHeight, shipWidth));
                player2.AddShip(ShipFactory.GetShip(shipType, player2Coordinates, shipHeight, shipWidth));
            }

            Queue <Coordinates> player1Shots = new Queue <Coordinates>();
            Queue <Coordinates> player2Shots = new Queue <Coordinates>();

            string player1ShotsInput = Console.ReadLine();

            string[] player1ShotsSplit = player1ShotsInput.Split(' ');
            for (int i = 0; i < player1ShotsSplit.Length; i++)
            {
                player1.shots.Enqueue(new Coordinates(player1ShotsSplit[i]));
            }


            string player2ShotsInput = Console.ReadLine();

            string[] player2ShotsSplit = player2ShotsInput.Split(' ');
            for (int i = 0; i < player2ShotsSplit.Length; i++)
            {
                player2.shots.Enqueue(new Coordinates(player2ShotsSplit[i]));
            }


            Game game = new Game(battleField, player1, player2);

            game.Begin();
            Console.ReadLine();
        }
예제 #4
0
        private void playerBoard_Click(Object sender, EventArgs e)
        {
            gs = (GraphicSquare)sender;
            MouseEventArgs temp = (MouseEventArgs)e;

            if (!gameStarted && temp.Button == MouseButtons.Right && selectedShip != null)
            {
                selectedShip.Rotate();
            }

            else if (!gameStarted && temp.Button == MouseButtons.Left && selectedShip != null)
            {
                if (possibleToPlaceShip)
                {
                    markedSquares = new Square[markedGraphicSquares.Length];

                    for (int i = 0; i < markedGraphicSquares.Length; i++)
                    {
                        markedSquares[i] = markedGraphicSquares[i].GetSquare();
                        markedGraphicSquares[i].Image = Properties.Resources.shipMark;
                    }

                    // A clone is needed because Stack pushes a pointer and not a value.
                    placedShips.Push((GraphicSquare[])markedGraphicSquares);
                    placedShipsBoxes.Push(selectedShip);
                    player.AddShip(new Ship(markedSquares, selectedShip.Type));
                    selectedShip.Lock();
                    selectedShip = null;
                }

                else
                {
                    MessageBox.Show("You can't place the ship here.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }