public SetIterator(GridSet<T> _set) { set = _set; posIterator = new Vector2i.Iterator(set.grid.SizeXY()); cellIterator = new CellIterator(); reset = true; }
/// <summary> /// If the given piece placement/movement creates a block of identical pieces, /// returns the min corner of that block. /// Otherwise, returns null. /// </summary> /// <param name="movedPiecePos">The position of the piece after its movement.</param> /// <param name="previousPiecePos"> /// The position of the piece before its movement, /// or "null" if the piece is being placed onto the board. /// </param> public static Vector2i?GetHostBlock(Board board, Vector2i movedPiecePos, Vector2i?previousPiecePos, Teams team) { //Assume the movement is made, and create a getter for the new board. //First, figure out what happens at the piece's previous position (if it exists). Teams?pieceAtPreviousPos = null; if (previousPiecePos.HasValue) { //If a piece moves off of a host, the host creates a new piece in its place. var host = board.Hosts.Get(previousPiecePos.Value); if (host != null) { pieceAtPreviousPos = host.Team; } } Func <Vector2i, Teams?> getPieceAt = (boardPos) => { //Special cases: //Previous position of the moving piece. if (previousPiecePos.HasValue && boardPos == previousPiecePos.Value) { return(pieceAtPreviousPos); } //New position of the moving piece. else if (boardPos == movedPiecePos) { return(team); } //Outside the board. else if (!board.IsInRange(boardPos)) { return(null); } var piece = board.Pieces.Get(boardPos); return(piece == null ? new Teams?() : piece.Team); }; //Try to find a square of identical pieces around the moved piece. var minCornerRegion = new Vector2i.Iterator(movedPiecePos - GameConsts.HostBlockSize + 1, movedPiecePos + 1); foreach (Vector2i minCorner in minCornerRegion) { bool failed = false; var blockRegion = new Vector2i.Iterator(minCorner, minCorner + GameConsts.HostBlockSize); foreach (Vector2i blockPos in blockRegion) { Teams?blockTeam = getPieceAt(blockPos); if (!blockTeam.HasValue || blockTeam.Value != team) { failed = true; break; } } if (!failed) { return(minCorner); } } //Nothing was found. return(null); }