Exemple #1
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 #2
0
        private int computerTurnToShoot()
        {
            int counter = 0;
            var square  = gun.NextTarget();
            var sq      = new modelNmspc.Square(square.column, square.row);

            foreach (var ship in playerFleet.Ships)
            {
                if (ship.squares.Contains(sq))
                {
                    foreach (var x in ship.squares)
                    {
                        if (x == sq)
                        {
                            x.SetState(modelNmspc.HitResult.Hit);
                        }
                    }
                    foreach (var x in ship.squares)
                    {
                        if (x.SquareState == modelNmspc.SquareState.Hit)
                        {
                            ++counter;
                        }
                    }
                    if (counter == ship.squares.Count())
                    {
                        playerButtons[square.column][square.row].BackColor = shipSunkColor;
                        gun.ProcessHitResult(modelNmspc.HitResult.Sunken);
                        if (checkIfComputerWon() == 2)
                        {
                            return(4);
                        }
                        return(3);
                    }
                    else
                    {
                        playerButtons[square.column][square.row].BackColor = shipHitColor;
                        gun.ProcessHitResult(modelNmspc.HitResult.Hit);
                        return(2);
                    }
                }
            }
            playerButtons[square.column][square.row].BackColor = shipMissedColor;
            gun.ProcessHitResult(modelNmspc.HitResult.Missed);
            return(1);
        }
Exemple #3
0
        private void ChangeColorOfAvailableSquares(List <modelNmspc.Square> horizontalAndVertical)
        {
            var avaPlaces = playerGrid.GetAvailablePlacements(1);
            List <modelNmspc.Square> avaSquares = new List <modelNmspc.Square>();

            foreach (var s in avaPlaces)
            {
                foreach (var square in s)
                {
                    avaSquares.Add(square);
                }
            }
            modelNmspc.Square sq  = null;
            modelNmspc.Square sq2 = null;
            foreach (var list in playerButtons)
            {
                foreach (var button in list)
                {
                    sq  = new modelNmspc.Square(list.IndexOf(button), playerButtons.IndexOf(list));
                    sq2 = new modelNmspc.Square(playerButtons.IndexOf(list), list.IndexOf(button));
                    if (!horizontalAndVertical.Contains(sq) && avaSquares.Contains(sq2))
                    {
                        button.BackColor = Color.Gray;
                        button.Enabled   = false;
                    }
                }
            }
            foreach (var square in currentShipMaking)
            {
                playerButtons[square.row][square.column].BackColor = Color.Blue;
                playerButtons[square.row][square.column].Enabled   = false;
            }
            foreach (var i in horizontalAndVertical)
            {
                playerButtons[i.column][i.row].BackColor = Color.Green;
            }
        }
Exemple #4
0
        private List <modelNmspc.Square> checkForAvailableSquaresAfterSquareIsChosen(modelNmspc.Square b)
        {
            List <modelNmspc.Square> horizontalAndVerticalSquares = new List <modelNmspc.Square>();
            var aPlaces = playerGrid.GetAvailablePlacements(1);
            List <modelNmspc.Square> aSquares = new List <modelNmspc.Square>();
            List <modelNmspc.Square> temp     = new List <modelNmspc.Square>();
            int y = b.column;
            int x = b.row - 1;
            int j = 0;

            foreach (var item in aPlaces)
            {
                foreach (var square in item)
                {
                    aSquares.Add(new modelNmspc.Square(square.column, square.row));
                }
            }
            while (x >= 0)
            {
                temp.Add(new modelNmspc.Square(y, x));
                if (temp.Count == currentPlacingShipLength - 1)
                {
                    foreach (var sq in temp)
                    {
                        if (aSquares.Contains(sq))
                        {
                            j++;
                        }
                    }
                    if (j == currentPlacingShipLength - 1)
                    {
                        for (int i = 0; i < currentPlacingShipLength - 1; ++i)
                        {
                            horizontalAndVerticalSquares.Add(temp[i]);
                        }
                    }
                    break;
                }
                x -= 1;
            }
            temp.Clear();
            x = b.row + 1;
            j = 0;
            while (x <= 9)
            {
                temp.Add(new modelNmspc.Square(y, x));
                if (temp.Count == currentPlacingShipLength - 1)
                {
                    foreach (var sq in temp)
                    {
                        if (aSquares.Contains(sq))
                        {
                            j++;
                        }
                    }
                    if (j == currentPlacingShipLength - 1)
                    {
                        for (int i = 0; i < currentPlacingShipLength - 1; ++i)
                        {
                            horizontalAndVerticalSquares.Add(temp[i]);
                        }
                    }
                    break;
                }
                x += 1;
            }
            temp.Clear();
            y = b.column - 1;
            x = b.row;
            j = 0;
            while (y >= 0)
            {
                temp.Add(new modelNmspc.Square(y, x));
                if (temp.Count == currentPlacingShipLength - 1)
                {
                    foreach (var sq in temp)
                    {
                        if (aSquares.Contains(sq))
                        {
                            j++;
                        }
                    }
                    if (j == currentPlacingShipLength - 1)
                    {
                        for (int i = 0; i < currentPlacingShipLength - 1; ++i)
                        {
                            horizontalAndVerticalSquares.Add(temp[i]);
                        }
                    }
                    break;
                }
                y -= 1;
            }
            temp.Clear();
            y = b.column + 1;
            j = 0;
            while (y <= 9)
            {
                temp.Add(new modelNmspc.Square(y, x));
                if (temp.Count == currentPlacingShipLength - 1)
                {
                    foreach (var sq in temp)
                    {
                        if (aSquares.Contains(sq))
                        {
                            j++;
                        }
                    }
                    if (j == currentPlacingShipLength - 1)
                    {
                        for (int i = 0; i < currentPlacingShipLength - 1; ++i)
                        {
                            horizontalAndVerticalSquares.Add(temp[i]);
                        }
                    }
                    break;
                }
                y += 1;
            }
            horizontalAndVerticalSquares = horizontalAndVerticalSquares.Distinct().ToList();
            return(horizontalAndVerticalSquares);
        }
Exemple #5
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;
                }
            }
        }