Beispiel #1
0
        public void CreateBoard()
        {
            int  i1 = 0, j1 = 0;
            bool position;

            for (int i = 1; i <= 5; i++)
            {
                Ship sT = new Ship();
                switch (i)
                {
                case 1:
                    sT = new Ship();
                    sT.SetSize(s4.GetSize());
                    break;

                case 2:
                    sT = new Ship();
                    sT.SetSize(s5.GetSize());
                    break;

                case 3:
                    sT = new Ship();
                    sT.SetSize(s3.GetSize());
                    break;

                case 4:
                    sT = new Ship();
                    sT.SetSize(s2.GetSize());
                    break;

                case 5:
                    sT = new Ship();
                    sT.SetSize(s1.GetSize());
                    break;
                }
                i1 = rnd.Next(10) + 4;
                j1 = rnd.Next(10) + 4;
                if (rnd.Next(2) == 0)
                {
                    position = true;
                }
                else
                {
                    position = false;
                }
                sT.SetPosition(position);
                while (game2.CheckShip(sT, i1, j1) == false)
                {
                    i1 = rnd.Next(10) + 4;
                    j1 = rnd.Next(10) + 4;
                }
                game2.GetBoard()[i1, j1].SetShip(sT);
                game2.SetShipOnBoard(i1, j1);
            }
            boardCreated = true;
        }
Beispiel #2
0
        private void Game1_MouseClick(object sender, MouseEventArgs e)
        {
            bool legalClick = true;//הלחיצה חוקית
            int  x          = e.X;
            int  y          = e.Y;

            if (boardCreated == false)  //אם עדיין לא בנינו את הלוח שלנו
            {
                if (s == null)          //לא נבחרה צוללת
                {
                    legalClick = false; //הלחיצה לא חוקית
                }
            }

            if (legalClick == true && e.Button == MouseButtons.Right)
            {
                int i = (y - 150) / 30;
                int j = (x - 280) / 30;
                //ship5
                if (s1.GetPosition() == true)
                {
                    if (x >= s1.GetX() && x <= s1.GetX() + 22 && y >= s1.GetY() && y <= s1.GetY() + 125)
                    {
                        s1.SetPosition(!(s1.GetPosition()));
                        s1.PaintShip(g);
                    }
                }
                else if (y >= s1.GetY() && y <= s1.GetY() + 22 && x >= s1.GetX() && x <= s1.GetX() + 125)
                {
                    s1.SetPosition(!(s1.GetPosition()));
                    s1.PaintShip(g);
                }
                //ship4
                if (s2.GetPosition() == true)
                {
                    if (x >= s2.GetX() && x <= s2.GetX() + 22 && y >= s2.GetY() && y <= s2.GetY() + 105)
                    {
                        s2.SetPosition(!(s2.GetPosition()));
                        s2.PaintShip(g);
                    }
                }
                else if (y >= s2.GetY() && y <= s2.GetY() + 22 && x >= s2.GetX() && x <= s2.GetX() + 105)
                {
                    s2.SetPosition(!(s2.GetPosition()));
                    s2.PaintShip(g);
                }
                //ship3
                if (s3.GetPosition() == true)
                {
                    if (x >= s3.GetX() && x <= s3.GetX() + 22 && y >= s3.GetY() && y <= s3.GetY() + 85)
                    {
                        s3.SetPosition(!(s3.GetPosition()));
                        s3.PaintShip(g);
                    }
                }
                else if (y >= s3.GetY() && y <= s3.GetY() + 22 && x >= s3.GetX() && x <= s3.GetX() + 85)
                {
                    s3.SetPosition(!(s3.GetPosition()));
                    s3.PaintShip(g);
                }
                //ship3(2)
                if (s4.GetPosition() == true)
                {
                    if (x >= s4.GetX() && x <= s4.GetX() + 22 && y >= s4.GetY() && y <= s4.GetY() + 85)
                    {
                        s4.SetPosition(!(s4.GetPosition()));
                        s4.PaintShip(g);
                    }
                }
                else if (y >= s4.GetY() && y <= s4.GetY() + 22 && x >= s4.GetX() && x <= s4.GetX() + 85)
                {
                    s4.SetPosition(!(s4.GetPosition()));
                    s4.PaintShip(g);
                }
                //ship2
                if (s5.GetPosition() == true)
                {
                    if (x >= s5.GetX() && x <= s5.GetX() + 22 && y >= s5.GetY() && y <= s5.GetY() + 65)
                    {
                        s5.SetPosition(!(s5.GetPosition()));
                        s5.PaintShip(g);
                    }
                }
                else if (y >= s5.GetY() && y <= s5.GetY() + 22 && x >= s5.GetX() && x <= s5.GetX() + 65)
                {
                    s5.SetPosition(!(s5.GetPosition()));
                    s5.PaintShip(g);
                }
            }
            else
            {
                if (legalClick == true && boardCreated == true)
                {
                    g = CreateGraphics();
                    if (t == 0)
                    {
                        int i = (y - 150) / 30;
                        int j = (x - 650) / 30;
                        if (x >= 770 && x <= 1070 && y >= 270 && y <= 570 && game2.GetBoard()[i, j].GetHit() == 0)//האם הלחיצה הייתה בגבולות המשחק
                        {
                            if (game2.GetBoard()[i, j].GetStatus())
                            {
                                game2.GetBoard()[i, j].SetHit(1);
                            }
                            else
                            {
                                game2.GetBoard()[i, j].SetHit(2);
                                if (game2.GetBoard()[i, j].GetShip() != null)
                                {
                                    sizeP   = game2.GetBoard()[i, j].GetShip().GetSize();
                                    posP    = game2.GetBoard()[i, j].GetShip().GetPosition();
                                    firstIP = i;
                                    firstJP = j;
                                }
                                countP++;
                            }
                            if (countP != 0 && sizeP == countP)
                            {
                                if (posP == true)
                                {
                                    for (int k = firstIP - 1; k <= firstIP + sizeP; k++)
                                    {
                                        for (int l = firstJP - 1; l < firstJP + 2; l++)
                                        {
                                            if (game2.GetBoard()[k, l].GetHit() == 0)
                                            {
                                                game2.GetBoard()[k, l].SetHit(1);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    for (int l = firstJP - 1; l <= firstJP + sizeP; l++)
                                    {
                                        for (int k = firstIP - 1; k < firstIP + 2; k++)
                                        {
                                            if (game2.GetBoard()[k, l].GetHit() == 0)
                                            {
                                                game2.GetBoard()[k, l].SetHit(1);
                                            }
                                        }
                                    }
                                }
                                sizeP   = 0;
                                countP  = 0;
                                posP    = true;
                                firstIP = 0;
                                firstJP = 0;
                                countFoundsP++;
                            }
                            Refresh();
                            if (game2.CheckWinP(countFoundsP))
                            {
                                MessageBox.Show("כל הכבוד! ניצחת בקרב!");
                                this.Close();
                            }
                            if (game2.GetBoard()[i, j].GetHit() == 1)
                            {
                                t = (t * -1) + 1;
                            }
                        }
                    }
                    Refresh();
                    Thread.Sleep(1000);
                    EnemyMove();
                }
            }

            Refresh();
        }