/// <summary> /// Returns the amount of wins the challenger got (from 100) /// </summary> private static int CalculateChallengerWinsFrom100(NoLookaheadStrategy strategy) { int totalPlacements = 0; PieceCollection empty = new PieceCollection(); //Parallel.For(0, GameCount, (i) => for (var i = 0; i < GameCount; i++) { var pieces = RandomPiecesCache[i]; var board = new BoardState(); var pieceIndex = 0; while (strategy.TryPlacePiece(board, pieces[pieceIndex], in empty, 0, out var bitmap, out var x, out var y)) { board.Place(bitmap, x, y); pieceIndex++; } totalPlacements += pieceIndex; //Interlocked.Add(ref totalPlacements, pieceIndex); } return(totalPlacements); }
public static void AddPieceToCollection(this PieceCollection collection, PieceType pieceType, params int[] squarePositions) { foreach (var position in squarePositions) { collection[position] = pieceType; } }
private SaberInstance( SaberModel model, BasePieceInstance.Factory pieceFactory, SiraLog logger, TrailConfig trailConfig, List <ISaberPostProcessor> saberMiddlewares) { _logger = logger; _trailConfig = trailConfig; Model = model; GameObject = new GameObject(SaberName); GameObject.AddComponent <SaberMonoBehaviour>().Init(this, _saberComponents, OnSaberGameObjectDestroyed); CachedTransform = GameObject.transform; PieceCollection = new PieceCollection <BasePieceInstance>(); var sectionInstantiator = new SectionInstantiator(this, pieceFactory, PieceCollection); sectionInstantiator.InstantiateSections(); GameObject.transform.localScale = new Vector3(model.SaberWidth, model.SaberWidth, model.SaberLength); saberMiddlewares.Do(x => x.ProcessSaber(GameObject)); SetupTrailData(); InitializeEvents(); }
public SectionInstantiator(SaberInstance saberInstance, BasePieceInstance.Factory pieceInstanceFactory, PieceCollection <BasePieceInstance> pieceCollection) { _saberInstance = saberInstance; _pieceInstanceFactory = pieceInstanceFactory; _pieceCollection = pieceCollection; }
/// <summary> /// Creates new instance of chess board. /// </summary> /// <param name="start">Optional flag to immediately start chess game.</param> public Board(bool start = false) { Pieces = new PieceCollection(); Tiles = new TileCollection(); MovesTaken = new MoveCollection(); if (start) { Start(); } }
private void TestStrategy(IPlacementStrategy strategy, int expectPiecesPlaced) { var pieces = new PieceCollection(); pieces.Populate(SimulationHelpers.GetRandomPieces(1)); int placed = 0; var board = new BoardState(); for (var i = 0; i < pieces.Count; i++) { if (strategy.TryPlacePiece(board, PieceDefinition.AllPieceDefinitions[pieces[i]], in pieces, i + 1, out var bitmap, out var x, out var y)) { placed++; board.Place(bitmap, x, y); }
static void TestPlacementOnly() { var stopwatch = Stopwatch.StartNew(); var strategies = new IPlacementStrategy[] { //FirstPossiblePlacementStrategy.Instance, //SimpleClosestToWallAndCornerStrategy.Instance, //ClosestToCornerLeastHolesTieBreakerPlacementStrategy.Instance, //NextToPieceEdgeLeastHolesTieBreakerPlacementStrategy.Instance, //TightPlacementStrategy.InstanceDoubler, //TightPlacementStrategy.InstanceIncrement, ExhaustiveMostFuturePlacementsPlacementStrategy.Instance1_1, ExhaustiveMostFuturePlacementsPlacementStrategy.Instance1_6, new BestEvaluatorStrategy(TuneablePattern2x2BoardEvaluator.HandTuned), new BestEvaluatorStrategy(TuneablePattern2x2BoardEvaluator.Tuning1), }; //foreach (var strategy in strategies) Parallel.ForEach(strategies, (strategy) => { //var rand = new Random(0); int totalPlaced = 0; for (var i = 0; i < 100; i++) { var pieces = new PieceCollection(); pieces.Populate(SimulationHelpers.GetRandomPieces(i)); int index = 0; int placed = 0; var board = new BoardState(); while (true) { var piece = pieces[index]; pieces.RemoveAt(index); index = index % pieces.Count; if (strategy.TryPlacePiece(board, PieceDefinition.AllPieceDefinitions[piece], in pieces, index, out var bitmap, out var x, out var y)) { placed++; board.Place(bitmap, x, y); //Advance to a random one in the next 6 pieces (TODO: Would be good to bias this towards 1-3 as these are more likely) //index = (index + rand.Next(0, 6)) % pieces.Count; }
private SaberInstance(SaberModel model, BasePieceInstance.Factory pieceFactory, SiraLog logger) { _logger = logger; Model = model; GameObject = new GameObject("SF Saber"); CachedTransform = GameObject.transform; PieceCollection = new PieceCollection <BasePieceInstance>(); _sectionInstantiator = new SectionInstantiator(this, pieceFactory, PieceCollection); _sectionInstantiator.InstantiateSections(); GameObject.transform.localScale = new Vector3(model.SaberWidth, model.SaberWidth, 1); _colorMaterials = new List <Material>(); GetColorableMaterials(_colorMaterials); SetupTrailData(); InitializeEvents(); }
private SaberInstance(SaberModel model, BasePieceInstance.Factory pieceFactory, SiraLog logger, TrailConfig trailConfig) { _logger = logger; _trailConfig = trailConfig; Model = model; GameObject = new GameObject("SF Saber"); CachedTransform = GameObject.transform; PieceCollection = new PieceCollection <BasePieceInstance>(); var sectionInstantiator = new SectionInstantiator(this, pieceFactory, PieceCollection); sectionInstantiator.InstantiateSections(); GameObject.transform.localScale = new Vector3(model.SaberWidth, model.SaberWidth, 1); GameObject.SetLayer <Renderer>(12); SetupTrailData(); InitializeEvents(); }
private static PieceCollection InterpretPieceSection(string boardRepresentation) { var rows = boardRepresentation.Split('/'); var pieces = new PieceCollection(); for (var rowIndex = 0; rowIndex < rows.Length; rowIndex++) { var currentColumn = 0; for (var charIndex = 0; charIndex < rows[rowIndex].Length; charIndex++) { var pieceRepresentation = rows[rowIndex][charIndex]; if (PieceMap.TryGetValue(pieceRepresentation, out var piece)) { pieces[rowIndex * 8 + currentColumn] = piece; currentColumn++; continue; } if (int.TryParse(pieceRepresentation.ToString(), out var emptySpaces)) { for (var i = 0; i < emptySpaces; i++) { pieces[rowIndex * 8 + currentColumn] = PieceType.None; currentColumn++; } } else { throw new Exception($"Invalid piece detected during FEN interpretation - {pieceRepresentation}"); } } } return(pieces); }
/// <summary> /// Returns the amount of wins the challenger got (from 100) /// </summary> private static int CalculateChallengerWinsFrom100(NoLookaheadStrategy strategy) { int totalPlacements = 0; PieceCollection empty = new PieceCollection(); Parallel.For(0, GameCount, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (i) => { var pieces = RandomPiecesCache[i]; var board = new BoardState(); var pieceIndex = 0; while (strategy.TryPlacePiece(board, pieces[pieceIndex], in empty, 0, out var bitmap, out var x, out var y)) { board.Place(bitmap, x, y); pieceIndex++; } Interlocked.Add(ref totalPlacements, pieceIndex); });
public PieceCollection(PieceCollection instanceToCopy) { this._pieces = instanceToCopy._pieces; }