Exemple #1
0
 public Gunner(int rows, int columns, IEnumerable <int> shipLengths)
 {
     evidenceGrid           = new Grid(rows, columns);
     shipsToShoot           = new List <int>(shipLengths.OrderByDescending(l => l));
     ShootingTactics        = ShootingTactics.Random;
     squareTerminator       = new squareTerminator(rows, columns);
     ShootingTacticsFactory = new ShootingTacticsFactory(evidenceGrid, SquaresHit, shipsToShoot);
     targetSelect           = ShootingTacticsFactory.GetTactics(ShootingTactics.Random);
 }
Exemple #2
0
        private int ChooseRandomShipPositions_AddToFleet_Eliminate(IEnumerable <IEnumerable <modelNmspc.Square> > availablePositions)
        {
            int result         = availablePositions.Count();
            int randomPosition = rand.Next(0, result);
            int counter        = 0;
            List <modelNmspc.Square>        squaresToAddAndElim = null;
            IEnumerable <modelNmspc.Square> ship = null;

            using (var sequenceEnum = availablePositions.GetEnumerator())
            {
                while (sequenceEnum.MoveNext())
                {
                    if (counter == randomPosition)
                    {
                        ship = sequenceEnum.Current;
                    }
                    ++counter;
                }
            }
            int initCounter = 0;

            if (ship == null)
            {
                return(404);
            }

            using (var sequenceEnum = ship.GetEnumerator())
            {
                while (sequenceEnum.MoveNext())
                {
                    if (initCounter == 0)
                    {
                        var square = sequenceEnum.Current;
                        squaresToAddAndElim = new List <modelNmspc.Square> {
                            new modelNmspc.Square(square.row, square.column)
                        };
                        initCounter = 1;
                    }
                    else
                    {
                        var square = sequenceEnum.Current;
                        squaresToAddAndElim.Add(square);
                    }
                }
            }
            computerFleet.addShip(squaresToAddAndElim);
            modelNmspc.squareTerminator terminator = new modelNmspc.squareTerminator(computerGrid.Rw, computerGrid.Cl);
            var toElim = terminator.ToEliminate(squaresToAddAndElim);

            computerGrid.eliminateSquares(toElim);

            return(1);
        }
Exemple #3
0
 private void button_WOC2_Click(object sender, EventArgs e) //reset fleet button
 {
     resetButtonsColor();
     playerGrid               = new modelNmspc.Grid(10, 10);
     playerFleet              = new modelNmspc.fleet();
     currentShipMaking        = new List <modelNmspc.Square>();
     currentPlacingShipLength = 0;
     shipHead   = null;
     terminator = new modelNmspc.squareTerminator(10, 10);
     resetLabels();
     button_WOC1.Enabled     = false;
     button_WOC1.ButtonColor = Color.Black;
 }
Exemple #4
0
        public void ToEliminateReturns6SquaresAroundTheShip8_9to9_9()
        {
            Grid             grid       = new Grid(10, 10);
            squareTerminator terminator = new squareTerminator(10, 10);
            var toEliminate             = terminator.ToEliminate(new List <Square> {
                new Square(8, 9), new Square(9, 9)
            });

            Assert.AreEqual(6, toEliminate.Count());
            Assert.IsTrue(toEliminate.Contains(new Square(7, 8)));
            Assert.IsTrue(toEliminate.Contains(new Square(7, 9)));
            Assert.IsTrue(toEliminate.Contains(new Square(9, 8)));
            Assert.IsTrue(toEliminate.Contains(new Square(9, 9)));
        }
Exemple #5
0
        public void ToEliminateReturns6SquaresAroundTheShip0_0to0_1()
        {
            Grid             grid       = new Grid(10, 10);
            squareTerminator terminator = new squareTerminator(10, 10);
            var toEliminate             = terminator.ToEliminate(new List <Square> {
                new Square(0, 0), new Square(0, 1)
            });

            Assert.AreEqual(6, toEliminate.Count());
            Assert.IsTrue(toEliminate.Contains(new Square(0, 0)));
            Assert.IsTrue(toEliminate.Contains(new Square(1, 0)));
            Assert.IsTrue(toEliminate.Contains(new Square(1, 2)));
            Assert.IsTrue(toEliminate.Contains(new Square(0, 2)));
        }
Exemple #6
0
        public void ToEliminateReturns18SquaresAroundTheShip4_3to4_6()
        {
            Grid             grid       = new Grid(10, 10);
            squareTerminator terminator = new squareTerminator(10, 10);
            var toEliminate             = terminator.ToEliminate(new List <Square> {
                new Square(4, 3), new Square(4, 4), new Square(4, 5), new Square(4, 6)
            });

            Assert.AreEqual(18, toEliminate.Count());
            Assert.IsTrue(toEliminate.Contains(new Square(3, 2)));
            Assert.IsTrue(toEliminate.Contains(new Square(5, 2)));
            Assert.IsTrue(toEliminate.Contains(new Square(3, 7)));
            Assert.IsTrue(toEliminate.Contains(new Square(5, 7)));
        }
        private int ChooseRandomShipPositions_AddToFleet_Eliminate(IEnumerable <IEnumerable <modelNmspc.Square> > availablePositions)
        {
            int result         = availablePositions.Count();
            int randomPosition = random.Next(0, result);
            int counter        = 0;
            List <modelNmspc.Square>        squaresToAddAndElim = null;
            IEnumerable <modelNmspc.Square> ship = null;

            using (var sequenceEnum = availablePositions.GetEnumerator())
            {
                while (sequenceEnum.MoveNext())                            //prolazak kroz validne pozicije,
                {                                                          // te random odabir jedne od njih
                    if (counter == randomPosition)
                    {
                        ship = sequenceEnum.Current;
                    }
                    ++counter;
                }
            }
            int initCounter = 0;

            if (ship == null)
            {
                return(404);
            }

            using (var sequenceEnum = ship.GetEnumerator())
            {
                while (sequenceEnum.MoveNext())
                {
                    if (initCounter == 0)                                      //prolazak kroz svaki square  u izabranoj validnoj poziciji
                    {
                        var square = sequenceEnum.Current;
                        squaresToAddAndElim = new List <modelNmspc.Square> {
                            new modelNmspc.Square(square.row, square.column)
                        };
                        initCounter = 1;
                    }
                    else
                    {
                        var square = sequenceEnum.Current;
                        squaresToAddAndElim.Add(square);
                    }
                }
            }
            //using (var sequenceEnum = squaresToAddAndElim.GetEnumerator())
            //{
            //    while (sequenceEnum.MoveNext())
            //    {                                                                         //used for debugging only

            //            var square = sequenceEnum.Current;
            //        var c = square.column;
            //        var r = square.row;
            //    }
            //}
            fl.addShip(squaresToAddAndElim);                                                         //dodaje u flotu sve shipove koji su random odabrani
            modelNmspc.squareTerminator terminator = new modelNmspc.squareTerminator(gr.Rw, gr.Cl);  //eliminira iz grida sve okolne pozicije odabranih shipova
            var toElim = terminator.ToEliminate(squaresToAddAndElim);

            gr.eliminateSquares(toElim);                                                             //eliminira iz grida sve square-ove odabranih shipova
            //Console.WriteLine("eliminirano "+toElim.Count());                                 //used for debugging
            return(1);
        }