コード例 #1
0
 public BattleshipGameForm(ref LocalClient client, int ID)
 {
     InitializeComponent();
     Text += " - " + client.Username;
     InitGrid(EnemyGrid);
     AssignGridClickEvent(EnemyGrid, false);
     InitGrid(PlayerGrid);
     AssignGridClickEvent(PlayerGrid, true);
     shipPositions = new GridPosition[5, 2];
     this.client = client;
     this.client.CommandRecieved += new CommandRecievedEventHandler(GameCommandRecieved);
     gameID = ID;
     gridTarget = new GridPosition(-1, -1);
 }
コード例 #2
0
        private void PlayerGrid_Click(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            switch (selectedShipType)
            {
                #region Destroyer
                case ShipType.Destroyer:
                    if (shipPositions[0, 0] == null)
                    {
                        shipPositions[0, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);

                    }
                    else if (shipPositions[0, 1] == null)
                    {
                        shipPositions[0, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                        if (PlaceShip(ShipType.Destroyer) == false)
                        {
                            MessageBox.Show("Invalid ship placement: " + ShipType.Destroyer + " Please try again.", "Invalid Placement", MessageBoxButtons.OK);
                            shipPositions[0, 0] = null;
                            shipPositions[0, 1] = null;
                        }
                        else
                        {
                            for (int i = 0; i < destroyer.occupiedSquares.Length; i++)
                            {
                                pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(destroyer.occupiedSquares[i].x, destroyer.occupiedSquares[i].y);
                                pictureBox.Image = Properties.Resources.Ship;
                            }
                            btnDestroyer.Enabled = false;
                            btnDestroyer.Text = "Placed!";
                        }
                    }
                    break;
                #endregion
                #region Cruiser
                case ShipType.Cruiser:
                    if (shipPositions[1, 0] == null)
                    {
                        shipPositions[1, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                    }
                    else if (shipPositions[1, 1] == null)
                    {
                        shipPositions[1, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                        if (PlaceShip(ShipType.Cruiser) == false)
                        {
                            MessageBox.Show("Invalid ship placement: " + ShipType.Cruiser + " Please try again.", "Invalid Placement", MessageBoxButtons.OK);
                            shipPositions[1, 0] = null;
                            shipPositions[1, 1] = null;
                        }
                        else
                        {
                            for (int i = 0; i < cruiser.occupiedSquares.Length; i++)
                            {
                                pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(cruiser.occupiedSquares[i].x, cruiser.occupiedSquares[i].y);
                                pictureBox.Image = Properties.Resources.Ship;
                            }
                            btnCruiser.Enabled = false;
                            btnCruiser.Text = "Placed!";
                        }
                    }
                    break;
                #endregion
                #region Submarine
                case ShipType.Submarine:
                    if (shipPositions[2, 0] == null)
                    {
                        shipPositions[2, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                    }
                    else if (shipPositions[2, 1] == null)
                    {
                        shipPositions[2, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                        if (PlaceShip(ShipType.Submarine) == false)
                        {
                            MessageBox.Show("Invalid ship placement: " + ShipType.Submarine + " Please try again.", "Invalid Placement", MessageBoxButtons.OK);
                            shipPositions[2, 0] = null;
                            shipPositions[2, 1] = null;
                        }
                        else
                        {
                            for (int i = 0; i < submarine.occupiedSquares.Length; i++)
                            {
                                pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(submarine.occupiedSquares[i].x, submarine.occupiedSquares[i].y);
                                pictureBox.Image = Properties.Resources.Ship;
                            }
                            btnSubmarine.Enabled = false;
                            btnSubmarine.Text = "Placed!";
                        }
                    }
                    break;
                #endregion
                #region Battleship
                case ShipType.Battleship:
                    if (shipPositions[3, 0] == null)
                    {
                        shipPositions[3, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                    }
                    else if (shipPositions[3, 1] == null)
                    {
                        shipPositions[3, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                        if (PlaceShip(ShipType.Battleship) == false)
                        {
                            MessageBox.Show("Invalid ship placement: " + ShipType.Battleship + " Please try again.", "Invalid Placement", MessageBoxButtons.OK);
                            shipPositions[3, 0] = null;
                            shipPositions[3, 1] = null;
                        }
                        else
                        {
                            for (int i = 0; i < battleship.occupiedSquares.Length; i++)
                            {
                                pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(battleship.occupiedSquares[i].x, battleship.occupiedSquares[i].y);
                                pictureBox.Image = Properties.Resources.Ship;
                            }
                            btnBattleship.Enabled = false;
                            btnBattleship.Text = "Placed!";
                        }
                    }
                    break;
                #endregion
                #region Carrier
                case ShipType.Carrier:
                    if (shipPositions[4, 0] == null)
                    {
                        shipPositions[4, 0] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                    }
                    else if (shipPositions[4, 1] == null)
                    {
                        shipPositions[4, 1] = new GridPosition(PlayerGrid.GetPositionFromControl(control).Column, PlayerGrid.GetPositionFromControl(control).Row);
                        if (PlaceShip(ShipType.Carrier) == false)
                        {
                            MessageBox.Show("Invalid ship placement: " + ShipType.Carrier + " Please try again.", "Invalid Placement", MessageBoxButtons.OK);
                            shipPositions[4, 0] = null;
                            shipPositions[4, 1] = null;
                        }
                        else
                        {
                            for (int i = 0; i < carrier.occupiedSquares.Length; i++)
                            {
                                pictureBox = (PictureBox)PlayerGrid.GetControlFromPosition(carrier.occupiedSquares[i].x, carrier.occupiedSquares[i].y);
                                pictureBox.Image = Properties.Resources.Ship;
                            }
                            btnCarrier.Enabled = false;
                            btnCarrier.Text = "Placed!";
                        }
                    }
                    break;
                    #endregion
            }
            if (carrier != null && battleship != null && submarine != null && cruiser != null && destroyer != null)
            {
                btnSubmit.Enabled = true;
            }
        }
コード例 #3
0
        private void CalculateShipPositions(string ship, ShipType type, int length, int clientNo)
        {
            string[] points = new string[2];
            points[0] = ship.Split('.')[0];
            points[1] = ship.Split('.')[1];
            bool isHorizontal = false;
            GridPosition startPos = new GridPosition(int.Parse(points[0].Split(',')[0]), int.Parse(points[0].Split(',')[1]));
            GridPosition endPos = new GridPosition(int.Parse(points[1].Split(',')[0]), int.Parse(points[1].Split(',')[1]));

            if (startPos.x == endPos.x)//Ship is vertical
            {
                isHorizontal = false;
            }
            else if (startPos.y == endPos.y)//Ship is horizontal
            {
                isHorizontal = true;
            }

            Ship tempShip = new Ship(type, startPos, endPos, length, isHorizontal);

            for (int i = 0; i < tempShip.occupiedSquares.Length; i++)
            {
                if (clientNo == 0)
                {
                    client1TempPositions.Add(tempShip.occupiedSquares[i]);
                }
                else if (clientNo == 1)
                {
                    client2TempPositions.Add(tempShip.occupiedSquares[i]);
                }
            }
        }
コード例 #4
0
        private void EnemyGrid_Click(object sender, EventArgs e)
        {
            if (myTurn)
            {
                Control control = (Control)sender;
                if (gridTarget.x > -1 && gridTarget.y > -1)
                {
                    if (pictureBox != null)
                    {
                        if (pictureBox.Tag.ToString() == "WaterTarget")
                        {
                            pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y);
                            pictureBox.Image = Properties.Resources.Water;
                            pictureBox.Tag = "Water";
                        }
                    }
                }
                gridTarget = new GridPosition(EnemyGrid.GetPositionFromControl(control).Column, EnemyGrid.GetPositionFromControl(control).Row);
                pictureBox = (PictureBox)EnemyGrid.GetControlFromPosition(gridTarget.x, gridTarget.y);
                if (pictureBox.Tag.ToString() == "Water")
                {
                    pictureBox.Image = Properties.Resources.WaterTarget;
                    pictureBox.Tag = "WaterTarget";
                    btnFire.Enabled = true;
                }
                else
                {
                    btnFire.Enabled = false;
                }

            }
        }
コード例 #5
0
 public bool CheckForHits(int clientNo, string data)
 {
     GridPosition pos = new GridPosition(int.Parse(data.Split(',')[0]), int.Parse(data.Split(',')[1]));
     Command cmd = new Command();
     switch (clientNo)
     {
         case 0:
             if (client2Board.GetGridType(pos.x, pos.y) == GridType.Ship)
             {
                 client2Board.ChangeGridType(pos.x, pos.y, GridType.Hit);
                 client2Board.ShipSquaresRemaining--;
                 return true;
             }
             else
                 return false;
         case 1:
             if (client1Board.GetGridType(pos.x, pos.y) == GridType.Ship)
             {
                 client1Board.ChangeGridType(pos.x, pos.y, GridType.Hit);
                 client1Board.ShipSquaresRemaining--;
                 return true;
             }
             else
                 return false;
     }
     return false;
 }