public PegBoard Clone() { var temp = new PegBoard(); foreach (var hole in Holes.Select((x, i) => new { x.Filled, Index = i })) { temp.Holes[hole.Index].Filled = hole.Filled; } return(temp); }
/// <summary> /// Converts the face to a SFA polygon. /// </summary> /// <param name="factory">The geometry factory used to produce the polygon.</param> /// <returns>The polygon geometry representing the face.</returns> public IPolygon ToGeometry(IGeometryFactory factory = null) { if (factory == null) { factory = FactoryRegistry.GetFactory <IGeometryFactory>(); } return(factory.CreatePolygon(Vertices.Select(vertex => vertex.Position), Holes.Select(hole => hole.Vertices.Select(vertex => vertex.Position).Reverse()))); }
private DirectionResponse MoveMarbles(Sides liftedSide) { var newBoardState = new Board { Size = this.Size, Marbles = Marbles.Select(m => new Marble { IsInHole = m.IsInHole, Number = m.Number, Position = new Position { Row = m.Position.Row, Column = m.Position.Column } }).ToArray(), Holes = Holes.Select(h => new Hole { IsFilled = h.IsFilled, Number = h.Number, Position = new Position { Row = h.Position.Row, Column = h.Position.Column } }).ToArray(), Walls = this.Walls }; var lineMarbles = new List <Marble>(); for (int i = 0; i < Size; i++) { lineMarbles = newBoardState.GetMarblesInTheLine(i, liftedSide); //in a row or column, depending on the direction for (int j = 0; j < lineMarbles.Count; j++) { int targetPosition = newBoardState.MarbleTargetPosition(lineMarbles, j, liftedSide); while (MarbleDidNotGetTheDestination(lineMarbles[j].Position, targetPosition, liftedSide) && !lineMarbles[j].IsInHole) //move till it either doesn't reach the destination or fell in its hole { lineMarbles[j].Move(liftedSide); if (newBoardState.FellInAnotherHole(lineMarbles[j])) //wrong route, break the move { return(new DirectionResponse()); } newBoardState.FillTheHoleIfMarbleIsIn(lineMarbles[j]); if (newBoardState.Marbles.All(m => m.IsInHole)) //solution found { return new DirectionResponse { Success = true, Finished = true, BoardState = newBoardState } } ; } } } return(new DirectionResponse { Success = true, Finished = false, BoardState = newBoardState }); }
private Solution LiftBoard(Sides liftedSide, string moves) { var newBoardState = new Board { Size = this.Size, Marbles = Marbles.Select(m => new Marble { IsInHole = m.IsInHole, Number = m.Number, Position = new Position { Row = m.Position.Row, Column = m.Position.Column } }).ToArray(), Holes = Holes.Select(h => new Hole { IsFilled = h.IsFilled, Number = h.Number, Position = new Position { Row = h.Position.Row, Column = h.Position.Column } }).ToArray(), Walls = this.Walls }; DirectionResponse response = newBoardState.MoveMarbles(liftedSide); if (response.Success && !newBoardState.BoardStateDidNotChange(response.BoardState)) { moves += liftedSide.ToString()[0]; if (response.Finished) { return new Solution { Success = true, Route = moves } } ; else { bool marblesStateChanged = newBoardState.Marbles.Count(m => !m.IsInHole) != response.BoardState.Marbles.Count(m => !m.IsInHole); newBoardState.UpdateBoardState(response.BoardState); return(newBoardState.FindRoute(moves, marblesStateChanged)); } } return(new Solution { Route = moves += '*' }); }
internal List <Coordinate> GetCoordinates() => Holes.Select(element => element.Coordinate).ToList();