Пример #1
0
        private void PositionClick(object sender, EventArgs e)
        {
            // consider adding a check for type in the future to made it more modular
            Label    selected    = (Label)sender;
            ShipInfo ship        = shipsToPlace.Peek();
            int      size        = ship.size;
            int      startRow    = BoardTable.GetRow(selected);
            int      startColumn = BoardTable.GetColumn(selected);

            validPlacement = true;

            // display horizontal if this is the first click or if the label is the same one clicked
            if (placementOutline.Count == 0 || ((placementOutline[0].x != BoardTable.GetColumn(selected)) || (placementOutline[0].y != BoardTable.GetRow(selected))))
            {
                // get rid of the old outline
                resetOutline();

                if (size > (10 - startColumn))
                {
                    size           = (10 - startColumn);
                    validPlacement = false;
                }

                // make a new outline, checking if it's valid
                for (int i = 0; i < size; i++)
                {
                    placementOutline.Add(new Coords((startColumn + i), startRow));

                    // if the space has a ship already in it mark as invalid
                    if (BoardTable.GetControlFromPosition((startColumn + i), startRow).Text != "")
                    {
                        validPlacement = false;
                    }
                }
            }
            // otherwise flip it to vertical
            else
            {
                // get rid of the old outline
                resetOutline();

                if (size > (10 - startRow))
                {
                    size           = (10 - startRow);
                    validPlacement = false;
                }

                // make a new outline, checking if it's valid
                for (int i = 0; i < size; i++)
                {
                    placementOutline.Add(new Coords(startColumn, (startRow + i)));

                    // if the space has a ship already in it mark as invalid
                    if (BoardTable.GetControlFromPosition(startColumn, (startRow + i)).Text != "")
                    {
                        validPlacement = false;
                    }
                }
            }

            // display the placeholder on the board
            foreach (Coords coords in placementOutline)
            {
                Control label = BoardTable.GetControlFromPosition(coords.x, coords.y);
                if (validPlacement)
                {
                    label.BackColor = Color.Lime;
                }
                else
                {
                    label.BackColor = Color.Red;
                }
            }
        }
Пример #2
0
        // place the ship if the placement is valid
        private void PlaceButton_Click(object sender, EventArgs e)
        {
            if (!validPlacement)
            {
                MessageBox.Show("That is an invalid placement for that ship.", "Warning");
            }
            else
            {
                ShipInfo ship   = shipsToPlace.Dequeue();
                string   symbol = ship.symbol;

                // for every coordinate reset the background color and set the symbol
                foreach (Coords coords in placementOutline)
                {
                    Control label = BoardTable.GetControlFromPosition(coords.x, coords.y);
                    label.BackColor = Color.DodgerBlue;
                    label.Text      = symbol;
                }

                // if the first two coords have the same y value it is horizontal, then add the ship based on the symbol
                // side note: I could probably find a more elegant way to do this but I was feeling tired and just needed it implemented
                bool horizontal = (placementOutline[0].y == placementOutline[1].y);
                if (symbol == "C")
                {
                    playerShips.Add(new Carrier(placementOutline[0], horizontal));
                }
                else if (symbol == "B")
                {
                    playerShips.Add(new Battleship(placementOutline[0], horizontal));
                }
                else if (symbol == "D")
                {
                    playerShips.Add(new Destroyer(placementOutline[0], horizontal));
                }
                else if (symbol == "S")
                {
                    playerShips.Add(new Submarine(placementOutline[0], horizontal));
                }
                else if (symbol == "P")
                {
                    playerShips.Add(new PTBoat(placementOutline[0], horizontal));
                }
                else
                {
                    // *** implement the somethign has gone horribly wrong contingency ***
                }
            }

            // if all ships are placed, start the game
            if (shipsToPlace.Count == 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        BoardTable.GetControlFromPosition(i, j).Enabled    = false;
                        OpponentBoard.GetControlFromPosition(i, j).Enabled = true;
                    }
                }

                PlaceButton.Enabled = false;
                shipsSunk           = 0;
            }
        }