public MoveResult(MoveType type, CheckersPiece piece, int nx, int ny) { Type = type; OriginalPieceLocation = new Location(piece.X, piece.Y); FinalPieceLocation = new Location(nx, ny); JumpResults = new List<JumpResult>(); }
public CheckersPiece GetCapturedPieceForColorAtLocation(Location loc, PieceColor col) { var listToCheck = col == PieceColor.Black ? CapturedPlayerOnePieces : CapturedPlayerTwoPieces; for (var i = 0; i < listToCheck.Count; i++) { var p = listToCheck[i]; if (p.Color == col && p.X == loc.X && p.Y == loc.Y) return p; } return null; }
public CheckersPiece GetPieceAtPosition(Location loc) { CheckersPiece retVal; for (var i = 0; i < AlivePlayerOnePieces.Count; i++) { retVal = AlivePlayerOnePieces[i]; if (retVal.X == loc.X && retVal.Y == loc.Y) return retVal; } for (var i = 0; i < AlivePlayerTwoPieces.Count; i++) { retVal = AlivePlayerTwoPieces[i]; if (retVal.X == loc.X && retVal.Y == loc.Y) return retVal; } return null; }
public InvalidMoveException(CheckersPiece piece, int newX, int newY) { MovingPiece = piece; AttemptedLocation = new Location(newX, newY); }
//returns a list of all availables jumps from a target location, given a piece color, and makes sure it doesn't jump over a jumped location public List<Jump> GetAvailableJumps(int pieceX, int pieceY, PieceColor pieceColor, Location? jumpedLoc = null) { var possibleEndValues = new List<Jump>(); //check 4 corners var potentialJumpedLocs = new List<Location> { new Location(pieceX - 1, pieceY - 1), new Location(pieceX + 1, pieceY - 1), new Location(pieceX - 1, pieceY + 1), new Location(pieceX + 1, pieceY + 1) }; //remove locations that are invalid for (var i = potentialJumpedLocs.Count - 1; i >= 0; i--) { var j = potentialJumpedLocs[i]; if (jumpedLoc != null) if (j == jumpedLoc) { potentialJumpedLocs.RemoveAt(i); continue; } //if the jumped location is invalid, if the jump would not jump over a piece, or if the jump would jump over a friendly piece, remove it if(!TileBoard.IsValidLocation(j) || Pieces.GetPieceAtPosition(j) == null || Pieces.GetPieceAtPosition(j).Color == pieceColor) potentialJumpedLocs.RemoveAt(i); } //now all the locations in possibleEndValues are pieces that we can jump over - advance them all forward //check if the final location is now valid (within game board) for (var i = potentialJumpedLocs.Count-1; i >= 0; i--) { var jump = new Jump(); var pos = potentialJumpedLocs[i]; Location recentlyJumpedLoc; var dirX = pos.X - pieceX; var dirY = pos.Y - pieceY; var newFinalLoc = recentlyJumpedLoc = pos; newFinalLoc.X += dirX; newFinalLoc.Y += dirY; if (TileBoard.IsValidLocation(newFinalLoc) && Pieces.GetPieceAtPosition(newFinalLoc) == null) { jump.AddJumpedLocation(new JumpResult(recentlyJumpedLoc, newFinalLoc)); //see if we can make even more jumps var jumpsAvailableAgain = GetAvailableJumps(newFinalLoc.X, newFinalLoc.Y, pieceColor, recentlyJumpedLoc); if (jumpsAvailableAgain.Count > 0) { //for each jump, make a new permutation of that jump and added it to the list for (var index = 0; index < jumpsAvailableAgain.Count; index++) { var newJump = jumpsAvailableAgain[index]; var branchOfJump = jump.Clone(); //skip any further jumps that jump back to where we started var anyJumpBackwards = false; for (var j = 0; j < newJump.LocationsJumped.Count; j++) { var locJumped = newJump.LocationsJumped[j]; if (locJumped.JumpedLocation == new Location(pieceX, pieceY)) { anyJumpBackwards = true; break; } } if (anyJumpBackwards) break; branchOfJump.LocationsJumped.AddRange(newJump.LocationsJumped); possibleEndValues.Add(branchOfJump); } } else { possibleEndValues.Add(jump); } } } return possibleEndValues; }
// returns a list of locations between two tiles that are diagonal to each other, throws if they aren't diagonal public List<Location> LocationsBetweenDiagonals(Location one, Location two) { var locs = new List<Location>(); var dispX = Math.Abs(two.X - one.X); var signX = Math.Sign(two.X - one.X); var dispY = Math.Abs(two.Y - one.Y); var signY = Math.Sign(two.Y - one.Y); if (dispX != dispY) throw new Exception("Parameters are not diagonal to one another."); for (var i = 1; i != dispX; i++) { locs.Add(new Location(one.X + signX*i, one.Y + signY*i)); } return locs; }
public JumpResult(Location jumpedLoc, Location finalLoc) { JumpedLocation = jumpedLoc; FinalLocation = finalLoc; }