public ListMap GetCopyOf() { var newMap = new ListMap(Size); newMap.InjectFrom <CloneInjection>(this); newMap.Board.ForEach(x => x.PostCloneWork()); return(newMap); }
public int ScoreFromBoard(ListMap map, ListPlayer player) { var opponent = player.Me == PlayerType.Blue ? PlayerType.Red : PlayerType.Blue; // player score var playerScore = PlayerScore(map, player.Me); // opponent score var opponentScore = PlayerScore(map, opponent); return(playerScore - opponentScore); }
private int PlayerScore(ListMap map, PlayerType player) { var scout = new Pathfinder(map, player); var path = scout.GetPathForPlayer(); var isFullyAttached = path.Where(x => x.Owner == player).Any(x => x.IsAttachedToBothEnds()); if (isFullyAttached) { Console.WriteLine((string)("Winning move found! " + player)); return(player == PlayerType.Blue ? 5000 : -5000); } return(PlayerScore(path)); }
public Pathfinder(ListMap searchThis, PlayerType searchForThisPlayer, int friendlyCost = 0, int openCost = 20, int costPerNodeTillEnd = 10, bool isLogging = false) { _searchSpace = searchThis; _playerSearchingFor = searchForThisPlayer; IsLogging = isLogging; costPerFriendlyNode = friendlyCost; costPerNodeLeft = costPerNodeTillEnd; costPerOpenNode = openCost; }
private int PlayerScore_Bad(ListMap map, PlayerType player, Matrix <double> playerMatrix) { if (player == PlayerType.Blue) { var pathVector = playerMatrix.RowSums(); var numberLeft = Enumerable.AsEnumerable <double>(pathVector).Count(x => (int)x == 0); return(map.Size - numberLeft); } else { var pathVector = playerMatrix.ColumnSums(); var numberLeft = Enumerable.AsEnumerable <double>(pathVector).Count(x => (int)x == 0); return(map.Size - numberLeft); } }
public void StartInquisition(ListMap searchMap, ListPlayer searchPlayer) { var mapToSearch = searchMap.GetCopyOf(); var searchScout = new Pathfinder(mapToSearch, searchPlayer.Me, searchPlayer.CostToMoveToClaimedNode, searchPlayer.CostToMoveToUnclaimedNode, searchPlayer.CostPerNodeTillEnd); _finalChoice = null; _finalScore = -9999; ThinkAboutTheNextMove( searchPlayer, mapToSearch, searchScout.GetPathForPlayer(), null, searchPlayer.CurrentLevels, -9999, 9999, true); }
public void SetMap(ListMap map) { _searchSpace = map; }
private void Startup() { Memory = new ListMap(Size); CurrentChoice = null; }
private int ThinkAboutTheNextMove( ListPlayer player, ListMap map, List <ListHex> path, ListHex currentMove, int depth, int alpha, int beta, bool isMaximizing) { var judge = new Appraiser(); var scout = new Pathfinder(map, isMaximizing ? player.Me : player.Opponent()); var myPath = scout.GetPathForPlayer(); if (depth == 0 || map.Board.All(x => x.Owner != PlayerType.White)) { return(judge.ScoreFromBoard(player, myPath, path)); } var possibleMoves = GetPossibleMoves(path, myPath, isMaximizing ? player.Me : player.Opponent(), map); if (isMaximizing) { foreach (var move in possibleMoves) { var bestScore = -9999; var newMap = map.GetCopyOf(); newMap.TakeHex(player.Me, move.Row, move.Column); bestScore = Math.Max(bestScore, ThinkAboutTheNextMove( player, newMap, myPath, move, depth - 1, alpha, beta, false)); if (bestScore > alpha) { _finalChoice = move.ToTuple(); alpha = bestScore; } if (beta <= alpha) { break; } } return(alpha); } else { foreach (var move in possibleMoves) { var worstScore = 9999; var newMap = map.GetCopyOf(); newMap.TakeHex(player.Opponent(), move.Row, move.Column); worstScore = Math.Min(worstScore, ThinkAboutTheNextMove( player, newMap, myPath, move, depth - 1, alpha, beta, true)); beta = Math.Min(worstScore, beta); if (beta <= alpha) { break; } } return(beta); } }
public IEnumerable <ListHex> GetPossibleMoves(List <ListHex> path, List <ListHex> myPath, PlayerType player, ListMap map) { var opponent = player == PlayerType.Blue ? PlayerType.Red : PlayerType.Blue; var lastOpponentMove = opponent == PlayerType.Blue ? map.LastBlueMove : map.LastRedMove; var possibleMoves = new HashSet <ListHex>(); if (lastOpponentMove != null) { foreach (var move in lastOpponentMove.Neighbours) { var hex = map.HexAt(move.ToTuple()); if (hex != null && hex.Owner == PlayerType.White) { hex.Priority += 3; possibleMoves.Add(hex); } } } foreach (var move in path) { var hex = map.HexAt(move.ToTuple()); if (hex != null && hex.Owner == PlayerType.White) { hex.Priority += 2; possibleMoves.Add(hex); } } foreach (var move in myPath) { var hex = map.HexAt(move.ToTuple()); if (hex != null && hex.Owner == PlayerType.White) { hex.Priority++; possibleMoves.Add(hex); } } //foreach (var move in map.Board) //{ // if (move.Owner == PlayerType.White) // { // possibleMoves.Add(move); // } //} var possibleMovesList = possibleMoves .OrderByDescending(x => x.Priority) .ThenBy(x => x.DistanceTo(lastOpponentMove)) .ThenBy(x => x.RandomValue).Where(x => x.Owner == PlayerType.White).ToList(); //Console.Write("Possible moves: "); //possibleMovesList.ForEach(x => Console.Write(x + " ")); //Console.WriteLine(""); return(possibleMovesList); }