Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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)));
        }
Ejemplo n.º 3
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)));
        }
Ejemplo n.º 4
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)));
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        private void playersButtonGridClickHandler(Button clickedButton)    // event handler for buttons in pre game state (player choosing ships positions)
        {
            int x = 0, y = 0;

            foreach (var list in playerButtons)
            {
                if (list.IndexOf(clickedButton) != -1)       // get index of clicked button
                {
                    x = playerButtons.IndexOf(list);
                    y = list.IndexOf(clickedButton);
                }
            }
            if (shipHead == null)                   // if button that's clicked is first square of ship
            {
                clickedButton.BackColor = shipPlacedOnGridColor;
                getCurrentPlacingShipLength();
                shipHead          = new modelNmspc.Square(x, y);
                currentShipMaking = new List <modelNmspc.Square>
                {
                    shipHead
                };
                List <modelNmspc.Square> s = checkForAvailableSquaresAfterSquareIsChosen(new modelNmspc.Square(x, y));
                if (s.Count() == 0)
                {
                    clickedButton.BackColor = Color.Green;
                    shipHead = null;
                    currentShipMaking.Clear();
                }
                else
                {
                    ChangeColorOfAvailableSquares(s);
                }
            }
            else
            {
                if (currentShipMaking[0].row < x)
                {
                    for (int i = 1; i < currentPlacingShipLength; i++)
                    {
                        currentShipMaking.Add(new modelNmspc.Square(currentShipMaking[0].row + i, currentShipMaking[0].column));
                    }
                    foreach (var s in currentShipMaking)
                    {
                        playerButtons[s.row][s.column].BackColor = shipPlacedOnGridColor;
                        playerButtons[s.row][s.column].Enabled   = false;
                    }
                }
                if (currentShipMaking[0].row > x)
                {
                    for (int i = 1; i < currentPlacingShipLength; i++)
                    {
                        currentShipMaking.Add(new modelNmspc.Square(currentShipMaking[0].row - i, currentShipMaking[0].column));
                    }
                    foreach (var s in currentShipMaking)
                    {
                        playerButtons[s.row][s.column].BackColor = shipPlacedOnGridColor;
                        playerButtons[s.row][s.column].Enabled   = false;
                    }
                    currentShipMaking.Reverse();
                }
                if (currentShipMaking[0].column < y)
                {
                    for (int i = 1; i < currentPlacingShipLength; i++)
                    {
                        currentShipMaking.Add(new modelNmspc.Square(currentShipMaking[0].row, currentShipMaking[0].column + i));
                    }
                    foreach (var s in currentShipMaking)
                    {
                        playerButtons[s.row][s.column].BackColor = shipPlacedOnGridColor;
                        playerButtons[s.row][s.column].Enabled   = false;
                    }
                }
                if (currentShipMaking[0].column > y)
                {
                    for (int i = 1; i < currentPlacingShipLength; i++)
                    {
                        currentShipMaking.Add(new modelNmspc.Square(currentShipMaking[0].row, currentShipMaking[0].column - i));
                    }
                    foreach (var s in currentShipMaking)
                    {
                        playerButtons[s.row][s.column].BackColor = shipPlacedOnGridColor;
                        playerButtons[s.row][s.column].Enabled   = false;
                    }
                    currentShipMaking.Reverse();
                }
                shipHead = null;
                playerFleet.addShip(currentShipMaking);
                var toElim = terminator.ToEliminate(currentShipMaking);
                playerGrid.eliminateSquares(toElim);
                var avaSquares = playerGrid.GetAvailablePlacements(1);
                foreach (var list in playerButtons)
                {
                    foreach (var button in list)
                    {
                        if (button.BackColor == Color.Green)
                        {
                            button.BackColor = Color.Gray;
                            button.Enabled   = false;
                        }
                    }
                }
                foreach (var item in avaSquares)
                {
                    foreach (var square in item)
                    {
                        playerButtons[square.row][square.column].BackColor = Color.Green;
                        playerButtons[square.row][square.column].Enabled   = true;
                    }
                }
                currentShipMaking = null;
                changeLabel();
                if (playerFleet.getNumberOfShips() == 10)
                {
                    button_WOC1.Enabled     = true;
                    button_WOC1.ButtonColor = Color.DarkRed;
                }
            }
        }