private void Setup(string pageData) { var res = Regex.Match(pageData, Pattern); if (res.Groups.Count != 4) { throw new Exception("Invalid board."); } BoardX = int.Parse(res.Groups[1].Value); BoardY = int.Parse(res.Groups[2].Value); BoardString = res.Groups[3].Value; Rows = BoardUtils.MakeRows(BoardString); Cols = BoardUtils.MakeRows(BoardUtils.Transpose(BoardString)); }
//Undoes ball position. public void ClearBallPosition() { if (BallPosition == null) { throw new Exception("was asked to remove ball when ballposition is null!"); } //how to convert something like -4,3,0,-3, => -4,3,-4 //todo isn't this already taken care of by "move"...? BoardUtils.RemoveZeroAtSectionFromList(Rows[BallPosition.Y], BallXSection); BoardUtils.RemoveZeroAtSectionFromList(Cols[BallPosition.X], BallYSection); //we've already adjusted the row & cols. BallPosition = null; }
public void InitialBallPlacement(int x, int y) { if (BallPosition != null) { throw new Exception("Ballposition must be null"); } var pos = new BallPosition { X = x, Y = y }; //figure out the ballXsection. BoardUtils.AddZeroAtPositionInList(x, Rows[y], out var xs); BallXSection = xs; BoardUtils.AddZeroAtPositionInList(y, Cols[x], out var ys); BallYSection = ys; BallPosition = pos; }
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); }