Jump to content

Help optimize funcktions

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Abbath1349

Abbath1349

    Newbie

  • Members
  • PipPip
  • 22 posts
Can u help me optimize this functions? There are labels array5*5.
if Label[i,j] has symbol then IsEmpty[i,j] has false if it hasn't IsEmpty[i,j] is true;
Funcktions for game Royal Square )

        public bool IsOpenForLetter(bool[,] IsEmpty, int x, int y)

        {

            int imin, imax, jmin, jmax;

            int xSize = IsEmpty.GetLength(0);

            int ySize = IsEmpty.GetLength(1);

            if (IsEmpty[x, y])

            {

                if (x > 0) imin = x - 1;

                else imin = x;

                if (y > 0) jmin = y - 1;

                else jmin = y;

                if (x < xSize - 1) imax = x + 1;

                else imax = x;

                if (y < ySize - 1) jmax = y + 1;

                else jmax = y;


                for (int i = imin; i <= imax; i++)

                    for (int j = jmin; j <= jmax; j++)

                        if (!IsEmpty[i, j])

                            return true;

            }

            return false;

        }



        public bool IsItNear(int fx, int fy, int sx, int sy, bool[,] IsEmpty)

        {

            bool[,] Action = new bool[3, 3];

            for (int i = 0; i < 3; i++)

                for (int j = 0; j < 3; j++)

                    Action[i, j] = true;

            int ftx, fty, sdx, sdy;

            ftx = fx; fty = fy; sdx = sx; sdy = sy;

            if (sdx == 0)

                for (int i = 0; i < 3; i++)

                    Action[0, i] = false;

            if (sdy == 0)

                for (int i = 0; i < 3; i++)

                    Action[i, 0] = false;

            if (sdx == 4)

                for (int i = 0; i < 3; i++)

                    Action[2, i] = false;

            if (sdy == 4)

                for (int i = 0; i < 3; i++)

                    Action[i, 2] = false;

            Action[1, 1] = false;

            sdx--; sdy--;

            for (int i = 0; i < 3; i++)

            {

                for (int j = 0; j < 3; j++)

                {

                    if (Action[i, j] == true)

                       if (IsEmpty[sdx, sdy] == false)

                            if (sdx == ftx && sdy == fty)

                                return true;

                    sdy++;

                }

                sdx++; sdy=sy-1;

            }

                    return false;

        }



#2
Parabola

Parabola

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 331 posts
Without the rest of the code, I can't do too much, but I would replace:
if (x > 0) imin = x - 1;

                else imin = x;

                if (y > 0) jmin = y - 1;

                else jmin = y;

                if (x < xSize - 1) imax = x + 1;

                else imax = x;

                if (y < ySize - 1) jmax = y + 1;

                else jmax = y;

With:

                imin = (x > 0) ? x - 1 : x;

                jmin = (y > 0) ? y - 1 : y;

                imax = (x < xSize - 1) ? x + 1 : x;

                jmax = (y < ySize - 1) ? y + 1 : y;

Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users