public bool Move(Position from, Position to) { List<Position> moves = this.getMoves(from); bool cont = false; for (int i = 0; i < moves.Count; i++) { if (to.Left == moves[i].Left && to.Top == moves[i].Top) { cont = true; break; } } if (!cont) { return false; } Ball temp = this.Balls[to.Top, to.Left]; this.Balls[to.Top, to.Left] = this.Balls[from.Top, from.Left]; this.Balls[from.Top, from.Left] = temp; return true; }
private void getNextMoves(ref Position from, ref bool[,] bp, ref List<Position> lp) { if (bp[from.Top, from.Left] || null != this.Balls[from.Top, from.Left]) { return; } lp.Add(from); bp[from.Top, from.Left] = true; if (from.Left < HSize - 1) { Position pr = new Position() { Left = from.Left+1, Top = from.Top }; getNextMoves(ref pr, ref bp, ref lp); } if (from.Top < VSize - 1) { Position pd = new Position() { Left = from.Left, Top = from.Top + 1 }; getNextMoves(ref pd, ref bp, ref lp); } }
public List<Position> getMoves(Position from) { List<Position> lp = new List<Position>(); Position pr = new Position() { Left = 1, Top = 0 }; Position pd = new Position() { Left = 0, Top = 1 }; bool[,] bp = new bool[VSize, HSize]; getNextMoves(ref pr, ref bp, ref lp); getNextMoves(ref pd, ref bp, ref lp); return lp; }