private void onShipClick(object sender, EventArgs e)
        {
            Ship button = (Ship) sender;
            if(!button.IsTarget) {
                int x = button.X;
                int y = button.Y;
                if(button.isNotShip()) {
                    if(currentTarget != null)
                        currentTarget.IsTarget = false;

                    currentTarget = button;
                    button.IsTarget = true;
                }
            }
            else if(currentTarget != null) {	// shot
                button.IsTarget = false;
                button.setLaF(Ship.LAF.SHOT);
                mainBoard.Enabled = false;
                wifiService.send(new GamePacket(currentTarget.X, currentTarget.Y));
            }
        }
예제 #2
0
        public static Ship CreateBattleShip(Player owner)
        {
            var ship = new Ship(ShipType.Battleship, owner);
            owner.AddShip(ship);

            return ship;
        }
예제 #3
0
        private List<string> GetShipReferences(Ship ship, string orientation, string gridReference)
        {
            var shipReferences = new List<string>();

            ValidateGridReference(gridReference);

            for (int i = 0; i < ship.Size; i++)
            {
                shipReferences.Add(gridReference);

                if (i == ship.Size - 1)
                {

                }
                else
                {
                    if (orientation == "vertical")
                    {
                        gridReference = IncrementVertically(gridReference);
                    }
                    else
                    {
                        gridReference = IncrementHorizontally(gridReference);
                    }
                }
            }
            return shipReferences;
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GridManager"/> class.
        /// For testing purposes only.
        /// </summary>
        /// <param name="arrangedShips">An array of ships with predefined positions.</param>
        public GridManager(Ship[] arrangedShips)
        {
            if (arrangedShips == null || arrangedShips.Count() == 0)
            {
                throw new BattleshipException("The Ships collection cannot be null or empty.");
            }

            foreach (Ship ship in arrangedShips)
            {
                if (ship == null)
                {
                    throw new BattleshipException("The Ship object cannot be null.");
                }

                if (ship.Size > GridSize)
                {
                    throw new BattleshipException(
                        string.Format(
                        "Ship with type = {0} is too big for the grid (GridSize = {1}).",
                        ship.Type,
                        GridSize));
                }
            }

            this.InitGrid();

            this.ships = arrangedShips;
            this.PlaceShips(this.ships);
        }
예제 #5
0
        public void TestConstructor_CreateSquareWithShipProvided()
        {
            Ship ship = new Ship(ShipType.Battleship);
            Square square = new Square(ship, SquareState.EmptyNoShot);

            Assert.AreSame(ship, square.Ship);
            Assert.AreEqual(SquareState.EmptyNoShot, square.State);
        }
 /// <summary>
 /// Set the _Value to the PossibleAttack value
 /// </summary>
 /// <param name="value">either hit, miss, destroyed, shotalready</param>
 public AttackResult(ResultOfAttack value, string text, int row, int column)
 {
     _Value = value;
     _Text = text;
     _Ship = null;
     _Row = row;
     _Column = column;
 }
예제 #7
0
        /// <summary>
        /// Initializes static members of the <see cref="GameEngineTest"/> class.
        /// </summary>
        static GameEngineTest()
        {
            Ship battleship = new Ship(new Bow(0, 6), ShipType.Battleship, ShipDirection.Vertical);
            Ship destroyer1 = new Ship(new Bow(2, 2), ShipType.Destroyer, ShipDirection.Vertical);
            Ship destroyer2 = new Ship(new Bow(8, 6), ShipType.Destroyer, ShipDirection.Horizontal);

            ships = new Ship[] { battleship, destroyer1, destroyer2 };
        }
예제 #8
0
        /// <summary>
        /// Initializes static members of the <see cref="GridManagerTest"/> class.
        /// </summary>
        static GridManagerTest()
        {
            Ship ship1 = new Ship(new Bow(0, 6), ShipType.AircraftCarrier, ShipDirection.Vertical);
            Ship ship2 = new Ship(new Bow(2, 2), ShipType.Battleship, ShipDirection.Vertical);
            Ship ship3 = new Ship(new Bow(8, 6), ShipType.Destroyer, ShipDirection.Horizontal);
            Ship ship4 = new Ship(new Bow(7, 6), ShipType.PatrolBoat, ShipDirection.Horizontal);
            Ship ship5 = new Ship(new Bow(0, 8), ShipType.Submarine, ShipDirection.Vertical);

            ships = new Ship[] { ship1, ship2, ship3, ship4, ship5 };
        }
예제 #9
0
 public static Component CreateComponent(string type, Ship parent, double x, double y, Arena arena)
 {
     switch (type)
     {
     case "SimpleGunTurret":
         return new SimpleGunTurret(parent, x, y, arena);
     default:
         return null;
     }
 }
예제 #10
0
        public void TestConstructor_BowAndDirectionProvided()
        {
            Ship ship = new Ship(new Bow(0, 6), ShipType.Destroyer, ShipDirection.Vertical);

            Assert.AreEqual(4, ship.Size);
            Assert.AreEqual(0, ship.Bow.Row);
            Assert.AreEqual(6, ship.Bow.Col);
            Assert.AreEqual(false, ship.IsSunk);
            Assert.AreEqual(ShipType.Destroyer, ship.Type);
            Assert.AreEqual(ShipDirection.Vertical, ship.Direction);
        }
예제 #11
0
        public void TestConstructor_NoBowAndDirectionProvided()
        {
            Ship ship = new Ship(ShipType.AircraftCarrier);

            Assert.AreEqual(6, ship.Size);
            Assert.AreEqual(0, ship.Bow.Row);
            Assert.AreEqual(0, ship.Bow.Col);
            Assert.AreEqual(false, ship.IsSunk);
            Assert.AreEqual(ShipType.AircraftCarrier, ship.Type);
            Assert.AreEqual(ShipDirection.Horizontal, ship.Direction);
        }
예제 #12
0
 public GameBoard(BoardType type, bool[,] matrix)
 {
     shipsMatrix = matrix;
     boardType = type;
     this.ships = new Ship[matrix.GetLength(0), matrix.GetLength(1)];
     for (int i = 0; i < this.ships.GetLength(0); i++)
         for (int j = 0; j < this.ships.GetLength(1); j++)
         {
             ships[i, j] = new Ship(i, j, type, matrix[i, j]);
             ships[i, j].Name = i + "" + j;
         }
 }
예제 #13
0
        private bool ShipPlaced(Ship ship, Func<IList<Cell>> shipPlacingAction)
        {
            var shipCells = shipPlacingAction();

            if (shipCells != null)
            {
                ship.SetPlace(shipCells);
                this.placedShips.Add(ship);

                return true;
            }

            return false;
        }
예제 #14
0
        public void Play()
        {
            // Wait on pairings
            Ship[] ships = new Ship[5];
            bool placed = false;
            for (int i = 0; i < ships.Length; i++ )
            {
                placed = false;
                if (i < 2)
                {
                    ships[i] = new Minesweeper(1, 1, true, yourMap);
                }
                else if (i < 3)// Feel the magic
                {
                    ships[i] = new Frigate(1, 1, true, yourMap);
                }
                else if (i < 4)
                {
                    ships[i] = new Cruiser(1, 1, true, yourMap);
                }
                else if (i < 5)
                {
                    ships[i] = new Battleship(1, 1, true, yourMap);
                }
                ships[i].Draw();
                while (!placed)
                {
                    ConsoleKey key = (ConsoleKey)Console.ReadKey(true).Key;
                    placed = ships[i].MoveShip(key);
                }
                // Place ships
            }

            shipsPlaced = true;

            foreach (Ship s in ships)
            {
                s.Draw();
            }
            // Send map to server
            while (true)
            {
                // Place coordinate on enemy map
                // Send point bto server
                // Recieve hit
                // Recieve enemy hit
                // If enemy ships/your ships destroyed - Break
            }
        }
예제 #15
0
        public void Add(Ship item)
        {
            foreach (var ship in ships.Where(item.OverlapsWith))
                throw new ShipOverlapException("Ship " + item + " overlaps with " + ship);

            if (item.Direction == Direction.Horizontal)
                if (Enumerable.Range((int)item.X, (int)item.Length).Any(i => i < 1 || i > 10) || (item.Y < 1 || item.Y > 10))
                    throw new ArgumentOutOfRangeException();

            if (item.Direction == Direction.Vertical)
                if (Enumerable.Range((int)item.Y, (int)item.Length).Any(i => i < 1 || i > 10) || (item.X < 1 || item.X > 10))
                    throw new ArgumentOutOfRangeException();

            ships.Add(item);
        }
예제 #16
0
        public void Occupy(Ship ship, string orientation, string gridReference)
        {
            var shipReferences = GetShipReferences(ship, orientation, gridReference);

            if (GridReferencesAreEmpty(shipReferences))
            {
                foreach (var shipReference in shipReferences)
                {
                    _occupiedCells.Add(shipReference, ship);
                }
            }
            else
            {
                throw new GridReferenceIsOccupiedByAnotherShipException();
            }
        }
예제 #17
0
        public Player(string name)
        {
            Name = name;
            G = new Grid(8, 8);

            //TODO: Ships cannot be larger than grid
            Ships = new List<Ship>();
            Ship aircraftcarrier = new Ship("Aircraft Carrier", 5);
            Ship battleship = new Ship("Battleship", 4);
            Ship destroyer = new Ship("Destroyer", 3);
            Ship submarine = new Ship("Submarine", 3);
            Ship patrolboat = new Ship("Patrol Boat", 2);
            Ships.Add(aircraftcarrier);
            Ships.Add(battleship);
            Ships.Add(destroyer);
            Ships.Add(submarine);
            Ships.Add(patrolboat);
        }
예제 #18
0
 /// <summary>
 /// Constructor for the Fleet class. Creates a Fleet of ships in the right size and the right amounts.
 /// </summary>
 public Fleet()
 { 
     for (int i = 0; i <= ships.Length-1; i++)
     {
         if (i<3)
         {
             ships[i] = new Ship(5-i);
         }
         else if(i <5 && i>2)
         {
             ships[i] = new Ship(2);
         }
         else if (i>4)
         {
             ships[i] = new Ship(1);
         } 
     }
 }
예제 #19
0
 public bool CheckShipPosition(Coordinate c, Ship s)
 {
     if (s.O == Ship.Orientation.Horizontal)
     {
         int shipSize = s.Size;
         for (int i = 0; i < shipSize; i++)
         {
             Coordinate checkc = new Coordinate(c.X + i, c.Y);
             Square sq = GetSquare(checkc);
             if (sq != null && sq.Ship == null)
             {
                 // WOOP! Success
             }
             else
             {
                 return false;
             }
         }
     }
     else if (s.O == Ship.Orientation.Vertical)
     {
         int shipSize = s.Size;
         for (int i = 0; i < shipSize; i++)
         {
             Coordinate checkc = new Coordinate(c.X, c.Y + i);
             Square sq = GetSquare(checkc);
             if (sq != null && sq.Ship == null)
             {
                 // WOOP! Success
             }
             else
             {
                 return false;
             }
         }
     }
     return true;
 }
예제 #20
0
 public void TestConstructor_ShipsEqualToNullProvided()
 {
     Ship[] testShips = new Ship[] { null };
     GridManager gridManager = new GridManager(testShips);
 }
예제 #21
0
        /// <summary>
        /// ProcessDetroy is able to process the destroyed ships targets and remove _LastHit targets.
        /// It will also call RemoveShotsAround to remove targets that it was going to shoot at
        /// </summary>
        /// <param name="row">the row that was shot at and destroyed</param>
        /// <param name="col">the row that was shot at and destroyed</param>
        /// <param name="ship">the row that was shot at and destroyed</param>
        private void ProcessDestroy(int row, int col, Ship ship)
        {
            bool foundOriginal = false;
            Location source = null;
            Target current = null;
            current = _CurrentTarget;

            foundOriginal = false;

            //i = 1, as we dont have targets from the current hit...
            int i = 0;

            for (i = 1; i <= ship.Hits - 1; i++) {
                if (!foundOriginal) {
                    source = current.Source;
                    //Source is nnothing if the ship was originally hit in
                    // the middle. This then searched forward, rather than
                    // backward through the list of targets
                    if (source == null) {
                        source = current.ShotAt;
                        foundOriginal = true;
                    }
                } else {
                    source = current.ShotAt;
                }

                //find the source in _LastHit
                foreach (Target t in _LastHit) {
                    if ((!foundOriginal && t.ShotAt == source) || (foundOriginal & t.Source == source)) {
                        current = t;
                        _LastHit.Remove(t);
                        break; // TODO: might not be correct. Was : Exit For
                    }
                }

                RemoveShotsAround(current.ShotAt);
            }
        }
예제 #22
0
        private bool IsWholeShipSunk(Ship ship)
        {
            IEnumerable<Square> squaresOccupied = this.GetSquaresForShip(ship);

            return squaresOccupied.All(s => s.State == SquareState.Hit);
        }
예제 #23
0
        private IEnumerable<Square> GetSquaresForShip(Ship ship)
        {
            int deltaRow = ship.Direction == ShipDirection.Horizontal ? 0 : 1;
            int deltaCol = ship.Direction == ShipDirection.Horizontal ? 1 : 0;

            Square[] squares = new Square[ship.Size];

            for (int i = 0; i < ship.Size; i++)
            {
                int row = ship.Bow.Row + (i * deltaRow);
                int col = ship.Bow.Col + (i * deltaCol);

                squares[i] = this.grid[row, col];
            }

            return squares;
        }
예제 #24
0
        private void OccupyArea(Ship ship)
        {
            IEnumerable<Square> squares = this.GetSquaresForShip(ship);

            foreach (Square square in squares)
            {
                square.Ship = ship;
                square.State = SquareState.OccupiedNoShot;
            }
        }
예제 #25
0
        private bool IsAreaOccupied(Ship ship)
        {
            IEnumerable<Square> squaresForShip = this.GetSquaresForShip(ship);

            return squaresForShip.Any(s => s.Ship != null);
        }
예제 #26
0
 private void CalcMaxPossibleSquare(Ship ship, out int maxRow, out int maxCol)
 {
     if (ship.Direction == ShipDirection.Horizontal)
     {
         maxRow = this.grid.GetLength(0);
         maxCol = this.grid.GetLength(1) - ship.Size + 1;
     }
     else
     {
         maxRow = this.grid.GetLength(0) - ship.Size + 1;
         maxCol = this.grid.GetLength(1);
     }
 }
예제 #27
0
        private bool TryPlaceShip(Ship ship)
        {
            int maxRow;
            int maxCol;
            this.CalcMaxPossibleSquare(ship, out maxRow, out maxCol);

            int maxTries = maxRow * maxCol;

            // keeps the squares already tried for the bow
            HashSet<Bow> bowsTried = new HashSet<Bow>();

            do
            {
                do
                {
                    // try a random square for the bow
                    ship.Bow = new Bow(
                        this.randomGenerator.Next(0, maxRow),
                        this.randomGenerator.Next(0, maxCol));
                }
                while (bowsTried.Contains(ship.Bow));

                // mark the square as tried; in this way, if it is already occupied
                // by another ship, we make sure that we will not try it again
                // if the random generator suggests it next time.
                bowsTried.Add(ship.Bow);

                if (!this.IsAreaOccupied(ship))
                {
                    this.OccupyArea(ship);
                    return true;
                }
            }
            while (bowsTried.Count < maxTries);

            return false;
        }
예제 #28
-1
        public static Ship CreateDestroyer(Player owner)
        {
            var ship = new Ship(ShipType.Destroyer, owner);
            owner.AddShip(ship);

            return ship;
        }
예제 #29
-1
        private void PlaceShip(Ship ship)
        {
            do
            {
                var direction = (Direction)new Random().Next(0, 2);
                var randomHorizontal = new Random().Next(0, ValidCharacters.Length - 1);
                var randomVertical = new Random().Next(0, ValidNumbers.Length - 1);

                if (direction == Direction.Horizontal && randomHorizontal + ship.Size <= ValidCharacters.Length)
                {
                    if (ShipPlaced(ship, () => TryPlaceShipHorizontally(ship, randomVertical, randomHorizontal)))
                    {
                        return;
                    }
                }

                if (direction == Direction.Vertical && randomVertical + ship.Size <= ValidNumbers.Length)
                {
                    if (ShipPlaced(ship, () => TryPlaceShipVertically(ship, randomVertical, randomHorizontal)))
                    {
                        return;
                    }
                }
            } while (true);
        }
예제 #30
-1
        public Tuple<Tuple<int, int>, Tuple<int, int>> placeShip(Ship s)
        {
            Random random = new Random();
            int randomX = random.Next(0, 10);
            int randomY = random.Next(0, 10);
            int randomDir = random.Next(0, 2);
            //0 means horizontally
            //1 means vertically

            if (randomDir == 0)
            {
                Tuple<int, int> start = new Tuple<int, int>(randomX, randomY);
                Tuple<int, int> end = new Tuple<int, int>(randomX + s.getSize() - 1, randomY);
                Tuple<Tuple<int, int>, Tuple<int, int>> startEnd = new Tuple<Tuple<int, int>, Tuple<int, int>>(start, end);
                return startEnd;
            }
            else if (randomDir == 1)
            {
                Tuple<int, int> start = new Tuple<int, int>(randomX, randomY);
                Tuple<int, int> end = new Tuple<int, int>(randomX, randomY + s.getSize() - 1);
                Tuple<Tuple<int, int>, Tuple<int, int>> startEnd = new Tuple<Tuple<int, int>, Tuple<int, int>>(start, end);
                return startEnd;
            }
            else
            {
                Console.WriteLine("[AI.placeShip] randomDir not within bounds");
            }

            Console.WriteLine("[AI.placeShip] Something f****d up");
            Tuple<Tuple<int, int>, Tuple<int, int>> tmp = new Tuple<Tuple<int, int>, Tuple<int, int>>(null, null);
            return tmp;
        }