Пример #1
0
        public void Move(Direction d)
        {
            ValidateMove(d);
            int xs = 0;
            int ys = 0;
            int sectionMoved;

            int xd = 0;
            int yd = 0;
            int moved;
            int pushsize; //if you push a bunch of boxes to the next call, how many is it?

            //zero means you hit a 1.

            switch (d)
            {
            //move in the row/col matching the direction of movement.
            //remove the zero from the opposite list starting point
            //calculate bump from opposite
            //if push size > 0 also add that to the opposite.
            //so much work!
            case Direction.D:
                //fix cur
                BoardUtils.MoveInArray(Cols[BallPosition.X], BallYSection, out sectionMoved, out moved,
                                       out pushsize);
                yd = moved;
                ys = BallYSection + sectionMoved;

                //fix opp source
                BoardUtils.RemoveZeroAtSectionFromList(Rows[BallPosition.Y], BallXSection);

                //fix hit source, also adjusts xs

                BumpIntoArrayAtPosition(Rows[BallPosition.Y + yd], BallPosition.X, out xs);

                //adjust push.  no need to mess with xs or ys.
                BoardUtils.AddPushToListAt(Rows[BallPosition.Y + yd + 1], BallPosition.X, pushsize);

                break;

            case Direction.U:     //this requires some careful reversing.
                var revsec    = Cols[BallPosition.X].Count - BallYSection - 1;
                var beforeLen = Cols[BallPosition.X].Count;

                //reverse this column, then move in it.
                Cols[BallPosition.X] = Cols[BallPosition.X].ToArray().Reverse().ToList();
                BoardUtils.MoveInArray(Cols[BallPosition.X], revsec, out sectionMoved, out moved,
                                       out pushsize);
                //now reverse it again.
                Cols[BallPosition.X] = Cols[BallPosition.X].ToArray().Reverse().ToList();
                yd = -1 * moved;

                //in reverse mode then shrinking/expanding the list is equivalent to changing sections!
                sectionMoved += beforeLen - Cols[BallPosition.X].Count;

                ys = BallYSection - sectionMoved;
                //the naive way to determine sectionmoved doesn't work when reversing.
                //instead look at

                //fix opp source
                BoardUtils.RemoveZeroAtSectionFromList(Rows[BallPosition.Y], BallXSection);

                //fix hit source, also adjusts xs

                BumpIntoArrayAtPosition(Rows[BallPosition.Y + yd], BallPosition.X, out xs);

                //adjust push.  no need to mess with xs or ys.
                BoardUtils.AddPushToListAt(Rows[BallPosition.Y + yd - 1], BallPosition.X, pushsize);

                break;

            case Direction.R:
                BoardUtils.MoveInArray(Rows[BallPosition.Y], BallXSection, out sectionMoved, out moved,
                                       out pushsize);
                xd = moved;
                xs = BallXSection + sectionMoved;

                BoardUtils.RemoveZeroAtSectionFromList(Cols[BallPosition.X], BallYSection);

                BumpIntoArrayAtPosition(Cols[BallPosition.X + xd], BallPosition.Y, out ys);

                BoardUtils.AddPushToListAt(Cols[BallPosition.X + xd + 1], BallPosition.Y, pushsize);
                break;

            case Direction.L:
                var revsec2    = Rows[BallPosition.Y].Count - BallXSection - 1;
                var beforeLen2 = Rows[BallPosition.Y].Count;

                Rows[BallPosition.Y] = Rows[BallPosition.Y].ToArray().Reverse().ToList();
                BoardUtils.MoveInArray(Rows[BallPosition.Y], revsec2, out sectionMoved, out moved,
                                       out pushsize);
                Rows[BallPosition.Y] = Rows[BallPosition.Y].ToArray().Reverse().ToList();

                xd            = -1 * moved;
                sectionMoved += beforeLen2 - Rows[BallPosition.Y].Count;
                xs            = BallXSection - sectionMoved;

                //fix opp source
                BoardUtils.RemoveZeroAtSectionFromList(Cols[BallPosition.X], BallYSection);

                //fix hit source, also adjusts xs

                BumpIntoArrayAtPosition(Cols[BallPosition.X + xd], BallPosition.Y, out ys);

                //adjust push.  no need to mess with xs or ys.
                BoardUtils.AddPushToListAt(Cols[BallPosition.X + xd - 1], BallPosition.Y, pushsize);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(d), d, null);
            }
            //I also have to adjust the position and xsection shit.
            BallXSection = xs;
            BallYSection = ys;
            MoveBallTo(BallPosition.X + xd, BallPosition.Y + yd);
        }