/// <summary> /// Given a pice and its new location, check if the move is allowed based on obstructions, limits, etc /// </summary> /// <param name="p"></param> /// <param name="targetCoord"></param> /// <param name="currentBoard"></param> private bool InitializeAndValidateMove(Piece p, CoordAbs targetCoord, Board currentBoard, GameRules rules) { FromCoord = new CoordAbs(p.pos.X, p.pos.Y); movingPiece = p; outcome = GameRules.MoveOutcomes.Unknown; opponentPiece = null; ToCoord = targetCoord; // finish populating the Move and check the legality Legal = false; // ensure the move stays on the board if (ToCoord.X >= currentBoard.Width || ToCoord.X < 0 || ToCoord.Y >= currentBoard.Height || ToCoord.Y < 0) { return(false); } opponentPiece = currentBoard.GetPieceAtCoord(ToCoord); // may be null if (opponentPiece?.Owner == movingPiece.Owner || // cannot move into owned space currentBoard.GetLocationAtCoord(ToCoord)?.Passable == false) // cannot move into obstacles { return(false); } // check if theres is an empty line to the target location if (p.Type.CanJump == false) { if (rules.LoggingSettings.debugJumpchecks) { Console.WriteLine($"Checking space between {FromCoord} to {ToCoord}..."); } int xmin = Math.Min(FromCoord.X, ToCoord.X); int xmax = Math.Max(FromCoord.X, ToCoord.X); int ymin = Math.Min(FromCoord.Y, ToCoord.Y); int ymax = Math.Max(FromCoord.Y, ToCoord.Y); for (int y = ymin; y <= ymax; y++) { for (int x = xmin; x <= xmax; x++) { // dont include the coords of FromCoord nor ToCoord, just between if (FromCoord.X == x && FromCoord.Y == y || ToCoord.X == x && ToCoord.Y == y) { continue; } // check if the path is clear of empty pieces and is traversable if (currentBoard.PiecesLayout[x, y] != null || !currentBoard.LocationsLayout[x, y].Passable) { Legal = false; return(false); } if (rules.LoggingSettings.debugJumpchecks) { Console.WriteLine($"{x} {y} is " + currentBoard.PiecesLayout[x, y] + " / " + currentBoard.LocationsLayout[x, y].Passable); } } } } // todo: is legality and battle result predictions premature at this stage? // outcome = movingPiece.PredictWinner(opponentPiece, rules);//defaults to "move" if nothing is in the way Legal = true; return(true); }
public Piece GetPieceAtCoord(CoordAbs c) => PiecesLayout.TryIndex(c.X, c.Y); // can return null public LocationType GetLocationAtCoord(CoordAbs c) => LocationsLayout.TryIndex(c.X, c.Y); // can return null
public Move(Piece p, CoordAbs targetCoord, Board currentBoard, GameRules rules) { InitializeAndValidateMove(p, targetCoord, currentBoard, rules); }
} // probably static per session public Piece GetPieceAtCoord(CoordAbs c) => PiecesLayout.TryIndex(c.X, c.Y); // can return null