Esempio n. 1
0
        public void formBoards()
        {
            int horizontalLoc = 49;
            int verticalLoc = 126;

            for (int k = 0; k < 10; k++)
                playerGridButtons[k] = new GridButton[10];
            for (int l = 0; l < 10; l++)
                enemyGridButtons[l] = new GridButton[10];

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    setGridButtonAttributes(i, j, horizontalLoc, verticalLoc, ref playerGridButtons);
                    setGridButtonAttributes(i, j, horizontalLoc + 584, verticalLoc, ref enemyGridButtons);
                    playerGridButtons[i][j].MouseUp += new MouseEventHandler(playerGridButton_Click);
                    playerGridButtons[i][j].MouseEnter += new EventHandler(playerGridButton_MouseEnter);
                    playerGridButtons[i][j].MouseLeave += new EventHandler(playerGridButton_MouseLeave);
                    this.Controls.Add(playerGridButtons[i][j]);
                    this.Controls.Add(enemyGridButtons[i][j]);
                    horizontalLoc += 37;
                }
                horizontalLoc = 49;
                verticalLoc += 35;
            }

            singlePlayerMode();
        }
Esempio n. 2
0
 public void setGridButtonAttributes(int i, int j, int hLoc, int vLoc, ref GridButton[][] buttonGrid)
 {
     buttonGrid[i][j]           = new GridButton();
     buttonGrid[i][j].Size      = new Size(37, 35);
     buttonGrid[i][j].Location  = new Point(hLoc, vLoc);
     buttonGrid[i][j].FlatStyle = FlatStyle.Flat;
     buttonGrid[i][j].FlatAppearance.BorderColor = Color.FromArgb(0, 24, 80);
     buttonGrid[i][j].FlatAppearance.BorderSize  = 1;
     buttonGrid[i][j].BackColor   = Color.Transparent;
     playerGridButtons[i][j].XLoc = j;
     playerGridButtons[i][j].YLoc = i;
 }
Esempio n. 3
0
 public void setGridButtonAttributes(int i, int j, int hLoc, int vLoc, ref GridButton[][] buttonGrid)
 {
     buttonGrid[i][j] = new GridButton();
     buttonGrid[i][j].Size = new Size(37, 35);
     buttonGrid[i][j].Location = new Point(hLoc, vLoc);
     buttonGrid[i][j].FlatStyle = FlatStyle.Flat;
     buttonGrid[i][j].FlatAppearance.BorderColor = Color.FromArgb(0, 24, 80);
     buttonGrid[i][j].FlatAppearance.BorderSize = 1;
     buttonGrid[i][j].BackColor = Color.Transparent;
     playerGridButtons[i][j].XLoc = j;
     playerGridButtons[i][j].YLoc = i;
 }
Esempio n. 4
0
        public void formBoards()
        {
            int horizontalLoc = 49;
            int verticalLoc   = 126;

            for (int k = 0; k < 10; k++)
            {
                playerGridButtons[k] = new GridButton[10];
            }
            for (int l = 0; l < 10; l++)
            {
                enemyGridButtons[l] = new GridButton[10];
            }

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    setGridButtonAttributes(i, j, horizontalLoc, verticalLoc, ref playerGridButtons);
                    setGridButtonAttributes(i, j, horizontalLoc + 584, verticalLoc, ref enemyGridButtons);
                    enemyGridButtons[i][j].XLoc         = j;
                    enemyGridButtons[i][j].YLoc         = i;
                    playerGridButtons[i][j].Text        = playerGridButtons[i][j].XLoc + ", " + playerGridButtons[i][j].YLoc;
                    enemyGridButtons[i][j].Text         = enemyGridButtons[i][j].XLoc + ", " + enemyGridButtons[i][j].YLoc;
                    playerGridButtons[i][j].MouseUp    += new MouseEventHandler(playerGridButton_Click);
                    playerGridButtons[i][j].MouseEnter += new EventHandler(playerGridButton_MouseEnter);
                    playerGridButtons[i][j].MouseLeave += new EventHandler(playerGridButton_MouseLeave);
                    enemyGridButtons[i][j].MouseUp     += new MouseEventHandler(enemyGridButton_Click);
                    //                enemyGridButtons[i][j].MouseEnter += new EventHandler(enemyGridButton_MouseEnter);
                    //                enemyGridButtons[i][j].MouseLeave += new EventHandler(enemyGridButton_MouseLeave);
                    this.Controls.Add(playerGridButtons[i][j]);
                    this.Controls.Add(enemyGridButtons[i][j]);
                    controller.setPlayerTurn(0);
                    horizontalLoc += 37;
                }
                horizontalLoc = 49;
                verticalLoc  += 35;
            }

            singlePlayerMode();
        }
Esempio n. 5
0
        public void singlePlayerMode()
        {
            Random rand = new Random();

            foreach (Ship selectedShip in computer.getShips())
            {
                GridButton clickedSquare = new GridButton();

                placeable = false;
                while (!placeable)
                {
                    int orientation = rand.Next(1001) % 2;
                    clickedSquare.XLoc = rand.Next(10);
                    clickedSquare.YLoc = rand.Next(10);

                    //If orientation == 0 rotate; else don't
                    if (orientation == 0)
                    {
                        horizontal = true;
                    }
                    else
                    {
                        horizontal = false;
                    }

                    for (int i = 0; i < selectedShip.Length; i++)
                    {
                        if (horizontal)
                        {
                            if (clickedSquare.XLoc + i < 10 && AIshipFits(clickedSquare.XLoc, clickedSquare.YLoc, selectedShip.Length, computer))
                            {
                                placeable = true;
                            }
                            else
                            {
                                placeable = false;
                            }
                        }
                        else
                        {
                            if (clickedSquare.YLoc + i < 10 && AIshipFits(clickedSquare.XLoc, clickedSquare.YLoc, selectedShip.Length, computer))
                            {
                                placeable = true;
                            }
                            else
                            {
                                placeable = false;
                            }
                        }
                    }

                    if (selectedShip != null && placeable)
                    {
                        if (horizontal)
                        {
                            for (int i = 0; i < selectedShip.Length; i++)
                            {
                                selectedShip.Position[i].setXLoc(clickedSquare.XLoc + i);
                                selectedShip.Position[i].setYLoc(clickedSquare.YLoc);
                                Board.enemyGrid[clickedSquare.YLoc][clickedSquare.XLoc + i].setState(State.occupied);
                                enemyGridButtons[clickedSquare.YLoc][clickedSquare.XLoc + i].BackColor = Color.White; //To see placment. Comment out
                            }
                        }
                        else
                        {
                            for (int i = 0; i < selectedShip.Length; i++)
                            {
                                selectedShip.Position[i].setXLoc(clickedSquare.XLoc);
                                selectedShip.Position[i].setYLoc(clickedSquare.YLoc + i);
                                Board.enemyGrid[clickedSquare.YLoc + i][clickedSquare.XLoc].setState(State.occupied);
                                enemyGridButtons[clickedSquare.YLoc + i][clickedSquare.XLoc].BackColor = Color.White; //To see placement. Comment out
                            }
                        }
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        enemyGridButtons[i][j].BackColor = Color.Transparent;
                    }
                }

                // Turned on to see Ships after placement. Delete code for final project

                /*   switch (selectedShip.Name)
                 * {
                 *     case ShipName.patrol:
                 *         ship2PlacedPictureBox.Visible = true;
                 *         ship2PlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                 *         if (horizontal)
                 *         {
                 *             ship2PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship2PictureBox.BackgroundImage")));
                 *             ship2PlacedPictureBox.Size = new System.Drawing.Size(74, 35);
                 *         }
                 *         else
                 *         {
                 *             ship2PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship2vertPictureBox.BackgroundImage")));
                 *             ship2PlacedPictureBox.Size = new System.Drawing.Size(36, 70);
                 *         }
                 *         break;
                 *     case ShipName.submarine:
                 *         ship3aPlacedPictureBox.Visible = true;
                 *         ship3aPlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                 *         if (horizontal)
                 *         {
                 *             ship3aPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3aPictureBox.BackgroundImage")));
                 *             ship3aPlacedPictureBox.Size = new System.Drawing.Size(111, 35);
                 *         }
                 *         else
                 *         {
                 *             ship3aPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3avertPictureBox.BackgroundImage")));
                 *             ship3aPlacedPictureBox.Size = new System.Drawing.Size(36, 104);
                 *         }
                 *         break;
                 *     case ShipName.battleship:
                 *         ship3bPlacedPictureBox.Visible = true;
                 *         ship3bPlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                 *         if (horizontal)
                 *         {
                 *             ship3bPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3bPictureBox.BackgroundImage")));
                 *             ship3bPlacedPictureBox.Size = new System.Drawing.Size(111, 35);
                 *         }
                 *         else
                 *         {
                 *             ship3bPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3bvertPictureBox.BackgroundImage")));
                 *             ship3bPlacedPictureBox.Size = new System.Drawing.Size(36, 104);
                 *         }
                 *         break;
                 *     case ShipName.destroyer:
                 *         ship4PlacedPictureBox.Visible = true;
                 *         ship4PlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                 *         if (horizontal)
                 *         {
                 *             ship4PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship4PictureBox.BackgroundImage")));
                 *             ship4PlacedPictureBox.Size = new System.Drawing.Size(147, 35);
                 *         }
                 *         else
                 *         {
                 *             ship4PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship4vertPictureBox.BackgroundImage")));
                 *             ship4PlacedPictureBox.Size = new System.Drawing.Size(36, 140);
                 *         }
                 *         break;
                 *     case ShipName.carrier:
                 *         ship5PlacedPictureBox.Visible = true;
                 *         ship5PlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                 *         if (horizontal)
                 *         {
                 *             ship5PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship5PictureBox.BackgroundImage")));
                 *             ship5PlacedPictureBox.Size = new System.Drawing.Size(185, 35);
                 *         }
                 *         else
                 *         {
                 *             ship5PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship5vertPictureBox.BackgroundImage")));
                 *             ship5PlacedPictureBox.Size = new System.Drawing.Size(36, 174);
                 *         }
                 *         break;
                 * }
                 */
            }
        }
Esempio n. 6
0
        public void singlePlayerMode()
        {
            Random rand = new Random();
            AIPlayer computer = new AIPlayer();

            foreach(Ship selectedShip in computer.getShips())
            {
                GridButton clickedSquare = new GridButton();

                placeable = false;
                while (!placeable)
                {
                    int orientation = rand.Next(1001) % 2;
                    clickedSquare.XLoc = rand.Next(10);
                    clickedSquare.YLoc = rand.Next(10);

                    //If orientation == 0 rotate; else don't
                    if (orientation == 0) horizontal = true;
                    else horizontal = false;

                    for (int i = 0; i < selectedShip.Length; i++)
                    {
                        if (horizontal)
                        {
                            if (clickedSquare.XLoc + i < 10 && AIshipFits(clickedSquare.XLoc, clickedSquare.YLoc, selectedShip.Length, computer)) placeable = true;
                            else placeable = false;
                        }
                        else
                        {
                            if (clickedSquare.YLoc + i < 10 && AIshipFits(clickedSquare.XLoc, clickedSquare.YLoc, selectedShip.Length, computer)) placeable = true;
                            else placeable = false;
                        }
                    }

                    if (selectedShip != null && placeable)
                    {
                        if (horizontal)
                        {
                            for (int i = 0; i < selectedShip.Length; i++)
                            {
                                selectedShip.Position[i].setXLoc(clickedSquare.XLoc + i);
                                selectedShip.Position[i].setYLoc(clickedSquare.YLoc);
                                Board.enemyGrid[clickedSquare.YLoc][clickedSquare.XLoc + i].setState(State.occupied);
                                enemyGridButtons[clickedSquare.YLoc][clickedSquare.XLoc + i].BackColor = Color.White; //To see placment. Comment out
                            }
                        }
                        else
                        {
                            for (int i = 0; i < selectedShip.Length; i++)
                            {
                                selectedShip.Position[i].setXLoc(clickedSquare.XLoc);
                                selectedShip.Position[i].setYLoc(clickedSquare.YLoc + i);
                                Board.enemyGrid[clickedSquare.YLoc + i][clickedSquare.XLoc].setState(State.occupied);
                                enemyGridButtons[clickedSquare.YLoc + i][clickedSquare.XLoc].BackColor = Color.White; //To see placement. Comment out
                            }
                        }
                    }
                }

                // Turned on to see Ships after placement. Delete code for final project
                switch (selectedShip.Name)
                {
                    case ShipName.patrol:
                        ship2PlacedPictureBox.Visible = true;
                        ship2PlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                        if (horizontal)
                        {
                            ship2PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship2PictureBox.BackgroundImage")));
                            ship2PlacedPictureBox.Size = new System.Drawing.Size(74, 35);
                        }
                        else
                        {
                            ship2PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship2vertPictureBox.BackgroundImage")));
                            ship2PlacedPictureBox.Size = new System.Drawing.Size(36, 70);
                        }
                        break;
                    case ShipName.submarine:
                        ship3aPlacedPictureBox.Visible = true;
                        ship3aPlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                        if (horizontal)
                        {
                            ship3aPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3aPictureBox.BackgroundImage")));
                            ship3aPlacedPictureBox.Size = new System.Drawing.Size(111, 35);
                        }
                        else
                        {
                            ship3aPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3avertPictureBox.BackgroundImage")));
                            ship3aPlacedPictureBox.Size = new System.Drawing.Size(36, 104);
                        }
                        break;
                    case ShipName.battleship:
                        ship3bPlacedPictureBox.Visible = true;
                        ship3bPlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                        if (horizontal)
                        {
                            ship3bPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3bPictureBox.BackgroundImage")));
                            ship3bPlacedPictureBox.Size = new System.Drawing.Size(111, 35);
                        }
                        else
                        {
                            ship3bPlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship3bvertPictureBox.BackgroundImage")));
                            ship3bPlacedPictureBox.Size = new System.Drawing.Size(36, 104);
                        }
                        break;
                    case ShipName.destroyer:
                        ship4PlacedPictureBox.Visible = true;
                        ship4PlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                        if (horizontal)
                        {
                            ship4PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship4PictureBox.BackgroundImage")));
                            ship4PlacedPictureBox.Size = new System.Drawing.Size(147, 35);
                        }
                        else
                        {
                            ship4PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship4vertPictureBox.BackgroundImage")));
                            ship4PlacedPictureBox.Size = new System.Drawing.Size(36, 140);
                        }
                        break;
                    case ShipName.carrier:
                        ship5PlacedPictureBox.Visible = true;
                        ship5PlacedPictureBox.Location = new Point(634 + selectedShip.Position[0].getXLoc() * 37, 126 + selectedShip.Position[0].getYLoc() * 35);
                        if (horizontal)
                        {
                            ship5PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship5PictureBox.BackgroundImage")));
                            ship5PlacedPictureBox.Size = new System.Drawing.Size(185, 35);
                        }
                        else
                        {
                            ship5PlacedPictureBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ship5vertPictureBox.BackgroundImage")));
                            ship5PlacedPictureBox.Size = new System.Drawing.Size(36, 174);
                        }
                        break;
                }
            }
        }