internal WirePath GetPath(Point start, Point end, IOInfo startIO, IOInfo endIO, bool pathToWire) { List <Point> pathAsTurns = new List <Point>(); List <int> allBoardPoses = new List <int>(); List <int> boardPosTurns = new List <int>(); MoveDirs prevDir = MoveDirs.None; Point boardPos = end; Point actualPos = end * CellSize; while (boardPos != start) { allBoardPoses.Add(CellIndex(boardPos)); ScorePath path = GetCellScorePath(boardPos); if (path.DirFrom != prevDir) { pathAsTurns.Add(actualPos); if (prevDir != MoveDirs.None) { boardPosTurns.Add(CellIndex(boardPos)); } } prevDir = path.DirFrom; boardPos = path.DirFrom.MovePoint(boardPos); actualPos = actualPos + path.DirFrom.MovePoint(Point.Zero) * CellSize; } allBoardPoses.Add(CellIndex(start)); pathAsTurns.Add(actualPos); pathAsTurns.Reverse(); boardPosTurns.Reverse(); return(new WirePath(startIO, endIO, pathAsTurns, allBoardPoses, boardPosTurns, pathToWire)); }
public MoveData(MoveDirs dir) { this.Dir = dir; this.RevDir = dir.Reverse(); this.DirVec = Dir.MovePoint(Point.Zero); this.RevDirVec = RevDir.MovePoint(Point.Zero); }
internal void SetAllOutgoingMoves(Point pos) { int cellPosX = pos.X; int cellPosY = pos.Y; //Not allowed to make move that goes //outside the bounds of the board MoveDirs allowedMoved = MoveDirs.None; if (cellPosX - 1 >= 0) { allowedMoved |= MoveDirs.Left; } if (cellPosX + 1 < CellsWide) { allowedMoved |= MoveDirs.Right; } if (cellPosY - 1 >= 0) { allowedMoved |= MoveDirs.Up; } if (cellPosY + 1 < CellsHigh) { allowedMoved |= MoveDirs.Down; } CellAllowedDirs[CellIndex(cellPosX, cellPosY)] = allowedMoved; }
private WirePath PathLine(RouterBoard board, IOInfo start, IOInfo end, Rectangle?startRect, Rectangle?endRect, List <WirePath> allPaths) { board.ReloadCheckpoint(); Point relativeStart = board.GetRelativeBoardPos(start.DirIO.Position); Point relativeEnd = board.GetRelativeBoardPos(end.DirIO.Position); //Start position may lie inside the component rectangle //and therefore the is no way to reach it. //This here makes a straight path out of the component //so it's possible to find a path. if (endRect.HasValue) { Rectangle endRectRelative = board.GetRelativeBoard(endRect.Value); Point endGo = relativeEnd; MoveDirs allowedDir = end.DirIO.InitialDir.Reverse(); do { board.SetCellAllowedMoves(endGo, allowedDir); endGo = allowedDir.MovePoint(endGo); } while (endRectRelative.Within(endGo)); } else { board.SetCellAllowedMoves(relativeEnd, end.DirIO.InitialDir.Reverse()); } if (startRect.HasValue) { Rectangle startRectRelative = board.GetRelativeBoard(startRect.Value); Point startGo = relativeStart; MoveDirs allowedDir = start.DirIO.InitialDir.Reverse(); do { board.SetCellAllowedMoves(startGo, allowedDir); startGo = allowedDir.Reverse().MovePoint(startGo); } while (startRectRelative.Within(startGo)); } foreach (var path in allPaths) { MoveDirs wireType; if (path.StartIO.DirIO.Position == start.DirIO.Position || path.EndIO.DirIO.Position == end.DirIO.Position) { wireType = MoveDirs.FriendWire; } else { wireType = MoveDirs.EnemyWire; } path.PlaceOnBoard(board, wireType); } ref ScorePath startScore = ref board.GetCellScorePath(relativeEnd);
public ScorePath Move(MoveDirs revDir, bool onEnemyWire, bool moveToEnemyWire, bool moveToFriendWire, bool moveToWireCorner, bool isTurning) { int turns = TravelDist; turns += isTurning ? 5 : 0; turns += moveToEnemyWire ? 5 : 0; turns += moveToEnemyWire && moveToWireCorner ? 500 : 0; turns += isTurning && onEnemyWire ? 50 : 0; turns += moveToFriendWire ? 0 : 1; return(new ScorePath(turns, revDir)); }
void _moveNode(MoveDirs moveDir, bool positive) { if (_selectedNode == null) { Debug.Log("[NH]: No attachment node selected!"); return; } var debugtext = new StringBuilder(5); debugtext.Append(_getNodeName(_selectedNode)); var newPos = _selectedNode.position; var sw = positive ? _stepWidth : _stepWidth * -1f; debugtext.Append(sw); debugtext.Append(" into "); switch (moveDir) { case MoveDirs.X: { newPos = new Vector3(newPos.x + sw, newPos.y, newPos.z); debugtext.Append("x position"); } break; case MoveDirs.Y: { newPos = new Vector3(newPos.x, newPos.y + sw, newPos.z); debugtext.Append("y position"); } break; default: { newPos = new Vector3(newPos.x, newPos.y, newPos.z + sw); debugtext.Append("z position"); } break; } Debug.Log(debugtext.ToString()); _setToPos(newPos); }
internal string BoardStateToString(Point start, Point end) { StringBuilder sBuilder = new StringBuilder(); for (int y = 0; y < CellsHigh; y++) { for (int x = 0; x < CellsWide; x++) { Point pos = new Point(x, y); ScorePath scorePath = GetCellScorePath(pos); MoveDirs allowedDirs = GetCellMoves(pos); if (pos == start) { sBuilder.Append('S'); } else if (pos == end) { sBuilder.Append('E'); } else if (allowedDirs.HasFlag(MoveDirs.FriendWire)) { sBuilder.Append('%'); } else if (scorePath.DirFrom == MoveDirs.Up) { sBuilder.Append("↑"); } else if (scorePath.DirFrom == MoveDirs.Down) { sBuilder.Append("↓"); } else if (scorePath.DirFrom == MoveDirs.Left) { sBuilder.Append("←"); } else if (scorePath.DirFrom == MoveDirs.Right) { sBuilder.Append("→"); } else if (scorePath.DirFrom == MoveDirs.None) { sBuilder.Append(' '); } } sBuilder.AppendLine(); } return(sBuilder.ToString()); }
private Boolean isLegalMove(CheckersGame game, Square origin, MoveDirs destination) { var stop = (destination == MoveDirs.TLEFT ? origin.tl : (destination == MoveDirs.TRIGHT ? origin.tr : (destination == MoveDirs.BLEFT ? origin.bl : origin.br))); if (stop == null || stop.squareType != CheckerType.empty) { return(false); } if (origin.squareType == CheckerType.whiteKing || origin.squareType == CheckerType.redKing) { return(true); } else if (origin.squareType == CheckerType.whiteChecker) { if (stop == origin.tl || stop == origin.tr) { return(true); } else { return(false); } } else if (origin.squareType == CheckerType.redChecker) { if (stop == origin.bl || stop == origin.br) { return(true); } else { return(false); } } else { return(false); } }
private MoveDirs canCapture(CheckersGame game, Square origin) { MoveDirs capDirs = MoveDirs.NONE; if (origin.tl != null && origin.tl.tl != null && isLegalCapture(game, origin, MoveDirs.TLEFT)) { capDirs = capDirs | MoveDirs.TLEFT; } if (origin.tr != null && origin.tr.tr != null && isLegalCapture(game, origin, MoveDirs.TRIGHT)) { capDirs = capDirs | MoveDirs.TRIGHT; } if (origin.bl != null && origin.bl.bl != null && isLegalCapture(game, origin, MoveDirs.BLEFT)) { capDirs = capDirs | MoveDirs.BLEFT; } if (origin.br != null && origin.br.br != null && isLegalCapture(game, origin, MoveDirs.BRIGHT)) { capDirs = capDirs | MoveDirs.BRIGHT; } return(capDirs); }
internal void PrepareBoard(List <Rectangle> usedSpace) { //Start with all moves are legal MoveDirs allDirs = MoveDirs.All; Array.Fill(CellAllowedDirs, allDirs); Array.Fill(CellScoreAndPath, ScorePath.NotReachedYet()); //Moves that go outside the board are not allowed for (int x = 0; x < CellsWide; x++) { CellAllowedDirs[CellIndex(x, 0)] &= MoveDirs.None; CellAllowedDirs[CellIndex(x, CellsHigh - 1)] &= MoveDirs.None; } for (int y = 0; y < CellsHigh; y++) { CellAllowedDirs[CellIndex(0, y)] &= MoveDirs.None; CellAllowedDirs[CellIndex(CellsWide - 1, y)] &= MoveDirs.None; } //Moves that go into used space are not allowed. //Moves inside used space are not removed because //they shouldn't be reachable. for (int i = 0; i < usedSpace.Count; i++) { Rectangle spaceRel = GetRelativeBoard(usedSpace[i]); for (int x = spaceRel.LeftX + 1; x < spaceRel.RightX; x++) { CellAllowedDirs[CellIndex(x, spaceRel.TopY)] &= MoveDirs.ExceptDown; CellAllowedDirs[CellIndex(x, spaceRel.BottomY)] &= MoveDirs.ExceptUp; } for (int y = spaceRel.TopY + 1; y < spaceRel.BottomY; y++) { CellAllowedDirs[CellIndex(spaceRel.LeftX, y)] &= MoveDirs.ExceptRight; CellAllowedDirs[CellIndex(spaceRel.RightX, y)] &= MoveDirs.ExceptLeft; } } }
public void singleCapture(CheckersGame game, Square origin, MoveDirs capDir) { switch (capDir) { case MoveDirs.TLEFT: origin.tl.tl.squareType = origin.squareType; origin.tl.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.tl.tl); break; case MoveDirs.TRIGHT: origin.tr.tr.squareType = origin.squareType; origin.tr.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.tr.tr); break; case MoveDirs.BLEFT: origin.bl.bl.squareType = origin.squareType; origin.bl.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.bl.bl); break; case MoveDirs.BRIGHT: origin.br.br.squareType = origin.squareType; origin.br.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.br.br); break; default: break; } }
private void _moveNode(MoveDirs moveDir, bool positive) { if (this._selectedNode == null) { Debug.Log("[NH] no node selected"); return; } var debugtext = new StringBuilder(5); debugtext.Append(this._getNodeName(this._selectedNode)); var newPos = this._selectedNode.position; var sw = positive ? this._stepWidth : this._stepWidth*-1f; debugtext.Append(sw); debugtext.Append(" into "); switch (moveDir) { case MoveDirs.X: { newPos = new Vector3(newPos.x + sw, newPos.y, newPos.z); debugtext.Append("x position"); } break; case MoveDirs.Y: { newPos = new Vector3(newPos.x, newPos.y + sw, newPos.z); debugtext.Append("y position"); } break; default: { newPos = new Vector3(newPos.x, newPos.y, newPos.z + sw); debugtext.Append("z position"); } break; } Debug.Log(debugtext.ToString()); this._setToPos(newPos); }
/** * @param capDir MoveDirs single direction * @return whether proposed capture is legal */ private Boolean isLegalCapture(CheckersGame game, Square origin, MoveDirs capDir, Square ignore = null) { Boolean isWhite = ( (origin.squareType == CheckerType.whiteChecker || origin.squareType == CheckerType.whiteKing) ? true : false ); Square toCapture, stop; if (capDir == MoveDirs.TLEFT) { if (origin.squareType == CheckerType.redChecker) return false; stop = origin.tl.tl; toCapture = origin.tl; } else if (capDir == MoveDirs.TRIGHT) { if (origin.squareType == CheckerType.redChecker) return false; stop = origin.tr.tr; toCapture = origin.tr; } else if (capDir == MoveDirs.BLEFT) { if (origin.squareType == CheckerType.whiteChecker) return false; stop = origin.bl.bl; toCapture = origin.bl; } else if (capDir == MoveDirs.BRIGHT) { if (origin.squareType == CheckerType.whiteChecker) return false; stop = origin.br.br; toCapture = origin.br; } else return false; if (toCapture == null || stop == null) return false; if (toCapture.squareType == CheckerType.empty) return false; if (stop.squareType != CheckerType.empty && stop != ignore) return false; if ((toCapture.squareType == CheckerType.redChecker || toCapture.squareType == CheckerType.redKing) ^ isWhite) return false; return true; }
private Boolean isLegalMove(CheckersGame game, Square origin, MoveDirs destination) { var stop = (destination == MoveDirs.TLEFT ? origin.tl : (destination == MoveDirs.TRIGHT ? origin.tr : (destination == MoveDirs.BLEFT ? origin.bl : origin.br))); if (stop == null || stop.squareType != CheckerType.empty) return false; if (origin.squareType == CheckerType.whiteKing || origin.squareType == CheckerType.redKing) { return true; } else if (origin.squareType == CheckerType.whiteChecker) { if (stop == origin.tl || stop == origin.tr) return true; else return false; } else if (origin.squareType == CheckerType.redChecker) { if (stop == origin.bl || stop == origin.br) return true; else return false; } else return false; }
public void singleCapture(CheckersGame game, Square origin, MoveDirs capDir) { switch (capDir) { case MoveDirs.TLEFT: origin.tl.tl.squareType = origin.squareType; origin.tl.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.tl.tl); break; case MoveDirs.TRIGHT: origin.tr.tr.squareType = origin.squareType; origin.tr.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.tr.tr); break; case MoveDirs.BLEFT: origin.bl.bl.squareType = origin.squareType; origin.bl.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.bl.bl); break; case MoveDirs.BRIGHT: origin.br.br.squareType = origin.squareType; origin.br.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.br.br); break; default: break; } }
public ScorePath(int travled, MoveDirs fromDir) { this.Data = (uint)travled | ((uint)fromDir << 24); }
internal void AddCellAllowedMoves(Point pos, MoveDirs moves) { AddCellAllowedMoves(CellIndex(pos), moves); }
internal void AddCellAllowedMoves(int posIndex, MoveDirs moves) { CellAllowedDirs[posIndex] |= moves; }
/** * @param capDir MoveDirs single direction * @return whether proposed capture is legal */ private Boolean isLegalCapture(CheckersGame game, Square origin, MoveDirs capDir, Square ignore = null) { Boolean isWhite = ( (origin.squareType == CheckerType.whiteChecker || origin.squareType == CheckerType.whiteKing) ? true : false ); Square toCapture, stop; if (capDir == MoveDirs.TLEFT) { if (origin.squareType == CheckerType.redChecker) { return(false); } stop = origin.tl.tl; toCapture = origin.tl; } else if (capDir == MoveDirs.TRIGHT) { if (origin.squareType == CheckerType.redChecker) { return(false); } stop = origin.tr.tr; toCapture = origin.tr; } else if (capDir == MoveDirs.BLEFT) { if (origin.squareType == CheckerType.whiteChecker) { return(false); } stop = origin.bl.bl; toCapture = origin.bl; } else if (capDir == MoveDirs.BRIGHT) { if (origin.squareType == CheckerType.whiteChecker) { return(false); } stop = origin.br.br; toCapture = origin.br; } else { return(false); } if (toCapture == null || stop == null) { return(false); } if (toCapture.squareType == CheckerType.empty) { return(false); } if (stop.squareType != CheckerType.empty && stop != ignore) { return(false); } if ((toCapture.squareType == CheckerType.redChecker || toCapture.squareType == CheckerType.redKing) ^ isWhite) { return(false); } return(true); }
internal void SetCellAllowedMoves(Point pos, MoveDirs moves) { CellAllowedDirs[CellIndex(pos)] = moves; }