Example #1
0
 public bool IsAvilable(int i, int j, Ship s)
 {
     if (s != null)
     {
         //הפעולה מקבלת צוללת, שורה ועמודה ובודקת האם אפשר להניח שם את הצוללת הרצויה
         if (s.GetPosition() == true)
         {
             if ((i - 4) + s.GetSize() <= 10)
             {
                 if (this.GetBoard()[i, j].GetStatus() == true)//התא פנוי
                 {
                     return(true);
                 }
             }
         }
         else if ((j - 4) + s.GetSize() <= 10)
         {
             if (this.GetBoard()[i, j].GetStatus() == true)//התא פנוי
             {
                 return(true);
             }
         }
         return(false);
     }
     return(false);
 }
Example #2
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;
        }
Example #3
0
        public bool CheckShip(Ship s, int i, int j)
        {
            // הפעולה מקבלת צוללת ושורה ןעמודה רצויים לראש צוללת
            //הפעולה תחזיר שקר אם הצוללת חורגת מגבולות המטריצה
            bool position = s.GetPosition();
            int  size     = s.GetSize();
            int  i1       = i;
            int  j1       = j;    //משתני עזר כדי לא "לקלקל" את המשתנים המקוריים

            if (position == true) //אם הצוללת מונחת במאונך
            {
                if (i1 + size >= 14)
                {
                    return(false);
                }
            }
            else
            if (j1 + size >= 14)
            {
                return(false);
            }
            //הפעולה תחזיר שקר אם הצוללת מונחת על גבי צוללת אחרת

            //הפעולה מחזירה אמת אם הצוללת מונחת באופן חוקי על הלוח - אחרת מחזירה שקר
            if (position == true)//אם הצוללת מונחת במאונך
            {
                for (int k = i - 1; k <= i + size; k++)
                {
                    for (int l = j - 1; l < j + 2; l++)
                    {
                        if (board[k, l].GetStatus() == false)
                        {
                            return(false);
                        }
                    }
                }
            }
            else // הצוללת מונחת במאוזן
            {
                for (int k = i - 1; k < i + 2; k++)
                {
                    for (int l = j - 1; l <= j + size; l++)
                    {
                        if (board[k, l].GetStatus() == false)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Example #4
0
        public int[] GetOpenLoc(Ship BS)
        {
            bool cantPos=false;
            int[] coords=new int[3];
            Random R=new Random();
            int size = BS.GetSize();
            int times = 0;
            do
            {
                //Console.WriteLine("{0} try to find loc", times);
                times++;
                coords[0] = R.Next(0,10);
                coords[1] = R.Next(0,10);
                coords[2] = R.Next(0,2);

                if (coords[2] == 0)
                {
                    int countSize = 0;
                    int c=coords[1];
                    for (int r = coords[0]; r < coords[0]+size&&r<10; r++)
                    {
                        countSize++;
                        if (board[r, c] == 1)
                        {
                            cantPos = true;
                            break;
                        }
                    }
                    if (countSize < size)
                        cantPos = true;
                }
                else
                {
                    int countSize = 0;
                    for (int r = coords[0]; r == coords[0]; r++)
                    {
                        for (int c = coords[1]; c < coords[1] + size&&c<10; c++)
                        {
                            countSize++;
                            if (board[r, c] == 1)
                            {
                                cantPos = true;
                                break;
                            }
                        }
                        if (cantPos)
                            break;
                    }
                    if (countSize < size)
                        cantPos = true;
                }
                if (times > 1000)
                {
                    coords[0] = -1;
                    break;
                }

            } while (cantPos);
            return coords;
        }
Example #5
0
        public bool UpdateShip(Ship BS)
        {
            if (BS == null)
                return false;

            int[,] locs = BS.GetPositions();
            for (int r = 0; r < BS.GetSize(); r++)
            {
                if (board[locs[r, 0], locs[r, 1]] == 1)
                {
                    return false;
                }
            }
            return true;
        }