/// <summary> /// This constructor passes up the width and height to the abstract class constructor. Let it deal with /// the size. We just want to get the required bits that we need to run the game /// </summary> public Game3x3(IGameState gameState, IGameDrawer gameDrawer, IPlayer player1, IPlayer player2) : base(WIDTH, HEIGHT) { _gameState = gameState; _gameDrawer = gameDrawer; _player1 = player1; _player2 = player2; _currentPlayer = player1; InitializeBoard(); }
private void setGameHandler(IGameHandler handler) { if (handler == null) throw new ArgumentNullException("handler"); if (this.gameHandler != null) this.gameHandler.Stopped -= this.setGameHandler; this.gameHandler = handler; this.gameDrawer = handler as IGameDrawer; this.gameHandler.Stopped += this.setGameHandler; }
private void setGameHandler(IGameHandler handler) { if (handler == null) { throw new ArgumentNullException("handler"); } if (this.gameHandler != null) { this.gameHandler.Stopped -= this.setGameHandler; } this.gameHandler = handler; this.gameDrawer = handler as IGameDrawer; this.gameHandler.Stopped += this.setGameHandler; }
public void MakeAMove(out int shapeId, out string placement, Board board, IDictionary <int, Shape> shapes, IGameDrawer renderer) { shapeId = 0; placement = ""; if (CalcMoves.Count == 0) { possibleMoves = 0; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); var gamePaths = new List <GamePath>(); renderer.ShowUpdateMessageStart("Calculating possible moves... "); InnerMakeAMove(board, shapes, gamePaths, null, renderer); var best = SelectBestPath(gamePaths); foreach (var cand in best.Moves) { CalcMoves.Add(cand); } Console.WriteLine(); stopWatch.Stop(); if (possibleMoves > 0) { renderer.ShowMessage("Throughput: " + (float)possibleMoves / (float)(stopWatch.ElapsedMilliseconds / 1000) + "/sec"); } } shapeId = CalcMoves[0].ShapeId; placement = CalcMoves[0].Placement; CalcMoves.RemoveAt(0); }
public void MakeAMove(out int shapeId, out string placement, Board board, IDictionary <int, Shape> shapes, IGameDrawer renderer) { placement = ""; shapeId = 0; var valGain = InitVal(); foreach (var shape in shapes) { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { var newBoard = new Board(board); var curPlacement = "" + (char)(97 + x) + (char)(49 + y); if (newBoard.TryPlace(shape.Value, curPlacement)) { var curValGain = Gain(newBoard, board); if (Eval(curValGain, valGain)) { valGain = curValGain; placement = curPlacement; shapeId = shape.Key; } } } } } }
private void InnerMakeAMove(Board board, IDictionary <int, Shape> shapes, IList <GamePath> gamePaths, GamePath startGamePath, IGameDrawer renderer) { if (shapes.Count == 0) { gamePaths.Add(startGamePath); GatherPathStats(startGamePath, board); } var placed = false; foreach (var shape in shapes) { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { var curPlacement = "" + (char)(97 + x) + (char)(49 + y); var newBoard = new Board(board); if (newBoard.TryPlace(shape.Value, curPlacement)) { possibleMoves++; placed = true; var gamePath = new GamePath(startGamePath); var candidate = new Candidate() { ShapeId = shape.Key, Placement = curPlacement, }; gamePath.Moves.Add(candidate); GatherStepStats(candidate, gamePath, board, newBoard); var newShapes = new Dictionary <int, Shape>(); foreach (var sh in shapes) { if (sh.Key != shape.Key) { newShapes.Add(sh.Key, sh.Value); } } InnerMakeAMove(newBoard, newShapes, gamePaths, gamePath, renderer); } } } } renderer.ShowUpdateMessage("[" + possibleMoves.ToString("N0") + "]"); if (!placed) { gamePaths.Add(startGamePath); } }
public void MakeAMove(out int shapeId, out string placement, Board board, IDictionary <int, Shape> shapes, IGameDrawer renderer) { placement = ""; shapeId = 0; var candidates = new List <Candidate>(); foreach (var shape in shapes) { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { var newBoard = new Board(board); var curPlacement = "" + (char)(97 + x) + (char)(49 + y); if (newBoard.TryPlace(shape.Value, curPlacement)) { var candidate = new Candidate() { Placement = curPlacement, ShapeId = shape.Key, ScoreGain = (newBoard.Score - shape.Value.Score) - board.Score, CellsGain = newBoard.CellCount() - board.CellCount(), LinesScore = newBoard.LinesScore(), MaxArea = MaxArea.MaximalRectangle(newBoard.Cells), FragScore = Fragmentation.GetFragmentationScore(newBoard.Cells) }; candidates.Add(candidate); } } } } var maxAreaList = from x in candidates orderby x.MaxArea descending, x.FragScore descending select x; var fragScoreList = from x in candidates orderby x.FragScore descending, x.MaxArea descending select x; var maxArea = maxAreaList.First(); var fragScore = fragScoreList.First(); var final = fragScore.MaxArea < 0.32F ? maxArea : fragScore; placement = final.Placement; shapeId = final.ShapeId; }
public void MakeAMove(out int shapeId, out string placement, Board board, IDictionary <int, Shape> shapes, IGameDrawer renderer) { shapeId = renderer.ChooseShape(); placement = renderer.ChoosePlacement(); }