double AlphaBeta(GameDescriptor descriptor, int depth, double alpha, double beta) { Ruler.GameResult result = Ruler.CheckGame(descriptor); if (result != Ruler.GameResult.NotYet) return Evaluator.EvaluateResult(result,descriptor.Turn)-depth; if (depth == 0 || nodeCount >= Max_Node) { nodeCount++; return Evaluator.Evaluate(descriptor) - depth; // the quicker, the better } List<AI_Action> actions = descriptor.QueryAllActions(); Disturb(actions); foreach (AI_Action tryAction in actions) { descriptor.DoAction(tryAction); var tmp = -AlphaBeta(descriptor, depth - 1, -beta, -alpha); tryAction.UnDo(descriptor); if (tmp >= beta) return beta; if (tmp > alpha) { alpha = tmp; if (depth == Max_Depth) action = tryAction; } } return alpha; }
/// <summary> /// Check Game Over for Game Descriptor /// </summary> /// <param name="descriptor">the descriptor to check</param> /// <returns></returns> public static GameResult CheckGame(GameDescriptor descriptor) { int blackTotal = descriptor.GetPlayerTotalCount(Unit.OwnerEnum.Black); int whiteTotal = descriptor.GetPlayerTotalCount(Unit.OwnerEnum.White); if (blackTotal == 0 && whiteTotal == 0) return GameResult.Draw; if ((descriptor.GetOwner(BoardInfo.Base[0]) == Unit.OwnerEnum.White && descriptor.GetType(BoardInfo.Base[0]) != Unit.TypeEnum.Bomb) || blackTotal == 0) return GameResult.White_Win; else if ((descriptor.GetOwner(BoardInfo.Base[1]) == Unit.OwnerEnum.Black && descriptor.GetType(BoardInfo.Base[1]) != Unit.TypeEnum.Bomb) || whiteTotal == 0) return GameResult.Black_Win; else { int blackRestUseful = blackTotal - descriptor.GetPlayerInfo(Unit.TypeEnum.Bomb, Unit.OwnerEnum.Black); int whiteRestUseful = whiteTotal - descriptor.GetPlayerInfo(Unit.TypeEnum.Bomb, Unit.OwnerEnum.White); if (blackRestUseful == 0 && whiteRestUseful == 0) return GameResult.Draw; else if (whiteRestUseful == 0) return GameResult.Black_Win; else if (blackRestUseful == 0) return GameResult.White_Win; return GameResult.NotYet; } }
double AlphaBeta_Slow(GameDescriptor descriptor, int depth, double alpha, double beta) { Ruler.GameResult result = Ruler.CheckGame(descriptor); if (result != Ruler.GameResult.NotYet) return Evaluator.EvaluateResult(result, descriptor.Turn) - depth; if (depth == 0) { nodeCount++; return Evaluator.Evaluate(descriptor) - depth; } List<AI_Action> actions = descriptor.QueryAllActions_Slow(); Disturb(actions); foreach (AI_Action tryAction in actions) { GameDescriptor clone = descriptor.Clone() as GameDescriptor; clone.DoAction(tryAction); var tmp = -AlphaBeta_Slow(clone, depth - 1, -beta, -alpha); if (tmp >= beta) return beta; if (tmp > alpha) { alpha = tmp; if (depth == Max_Depth) action = tryAction; } } return alpha; }
public static double Evaluate(GameDescriptor descriptor) { Unit.OwnerEnum owner = descriptor.Turn; Ruler.GameResult result = Ruler.CheckGame(descriptor); if (result != Ruler.GameResult.NotYet) return EvaluateResult(result, owner); double value = 0; value += EvalPosition(descriptor, owner) * PosFactor; value += EvalCard(descriptor, owner) * CardFactor; value += EvalDanger(descriptor, owner) * DangerFactor; value += EvalSpecial(descriptor, owner) * SpecialFactor; return value; }
private static double EvalCard(GameDescriptor descriptor, Unit.OwnerEnum owner) { double value = 0; for (Unit.TypeEnum type = Unit.TypeEnum.Bread; type < Unit.TypeEnum.Void; type++) { int tmp = descriptor.GetPlayerInfo(type, owner); if (tmp <= 2) value += CardEval[(int)type] * tmp; else value += CardEval[(int)type] * (tmp - 2) / 2 + CardEval[(int)type] * 2; tmp = descriptor.GetPlayerInfo(type, Unit.Opposite(owner)); if (tmp <= 2) value -= CardEval[(int)type] * tmp; else value -= CardEval[(int)type] * (tmp - 2) / 2 + CardEval[(int)type] * 2; } return value; }
double AlphaBeta(GameDescriptor descriptor, int depth, double alpha, double beta) { Ruler.GameResult result = Ruler.CheckGame(descriptor); if (result != Ruler.GameResult.NotYet) { return(Evaluator.EvaluateResult(result, descriptor.Turn) - depth); } if (depth == 0 || nodeCount >= Max_Node) { nodeCount++; return(Evaluator.Evaluate(descriptor) - depth); // the quicker, the better } List <AI_Action> actions = descriptor.QueryAllActions(); Disturb(actions); foreach (AI_Action tryAction in actions) { descriptor.DoAction(tryAction); var tmp = -AlphaBeta(descriptor, depth - 1, -beta, -alpha); tryAction.UnDo(descriptor); if (tmp >= beta) { return(beta); } if (tmp > alpha) { alpha = tmp; if (depth == Max_Depth) { action = tryAction; } } } return(alpha); }
public void ProcessMultipleGameDescriptor(List <GameDescriptor> gameDescriptors, Dictionary <string, MemoryStream> images) { if (gameDescriptors.Count == 0) { return; } GameDescriptor root = GetRootDescriptor(gameDescriptors); if (root == null) { root = gameDescriptors[0]; } string gameName = root.gameName; string version = root.version ?? ""; string gameStorageName = gameName + "_" + version; if (PersistanceHelper.CheckGameDirectory(gameStorageName)) { SetExistingGemAsCurrent(gameStorageName); return; } elementManager = new GameElementManager(gameStorageName); elementManager.DefLang = root.GameProperties.defaultLang; PersistanceHelper.CreateStorage(gameStorageName); PersistanceHelper.StoreImages(gameStorageName, images); foreach (GameDescriptor descripor in gameDescriptors) { ProcessGameDescriptor(descripor); } if (OnReferenceProcessing != null) { OnReferenceProcessing(null, EventArgs.Empty); } elementManager.SetFirstRoom(); PersistanceHelper.StoreInitialGEM(gameStorageName, elementManager); ObjectManager.CurrentGEM = elementManager; }
/// <summary> /// Check Game Over for Game Descriptor /// </summary> /// <param name="descriptor">the descriptor to check</param> /// <returns></returns> static public GameResult CheckGame(GameDescriptor descriptor) { int blackTotal = descriptor.GetPlayerTotalCount(Unit.OwnerEnum.Black); int whiteTotal = descriptor.GetPlayerTotalCount(Unit.OwnerEnum.White); if (blackTotal == 0 && whiteTotal == 0) { return(GameResult.Draw); } if ((descriptor.GetOwner(BoardInfo.Base[0]) == Unit.OwnerEnum.White && descriptor.GetType(BoardInfo.Base[0]) != Unit.TypeEnum.Bomb) || blackTotal == 0) { return(GameResult.White_Win); } else if ((descriptor.GetOwner(BoardInfo.Base[1]) == Unit.OwnerEnum.Black && descriptor.GetType(BoardInfo.Base[1]) != Unit.TypeEnum.Bomb) || whiteTotal == 0) { return(GameResult.Black_Win); } else { int blackRestUseful = blackTotal - descriptor.GetPlayerInfo(Unit.TypeEnum.Bomb, Unit.OwnerEnum.Black); int whiteRestUseful = whiteTotal - descriptor.GetPlayerInfo(Unit.TypeEnum.Bomb, Unit.OwnerEnum.White); if (blackRestUseful == 0 && whiteRestUseful == 0) { return(GameResult.Draw); } else if (whiteRestUseful == 0) { return(GameResult.Black_Win); } else if (blackRestUseful == 0) { return(GameResult.White_Win); } return(GameResult.NotYet); } }
double AlphaBeta_Slow(GameDescriptor descriptor, int depth, double alpha, double beta) { Ruler.GameResult result = Ruler.CheckGame(descriptor); if (result != Ruler.GameResult.NotYet) { return(Evaluator.EvaluateResult(result, descriptor.Turn) - depth); } if (depth == 0) { nodeCount++; return(Evaluator.Evaluate(descriptor) - depth); } List <PlayerAction> actions = descriptor.QueryAllActions_Slow(); Disturb(actions); foreach (PlayerAction tryAction in actions) { GameDescriptor clone = descriptor.Clone() as GameDescriptor; clone.DoAction(tryAction); var tmp = -AlphaBeta_Slow(clone, depth - 1, -beta, -alpha); if (tmp >= beta) { return(beta); } if (tmp > alpha) { alpha = tmp; if (depth == Max_Depth) { action = tryAction; } } } return(alpha); }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static GameDescriptor ReadGame(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentNullException("ReadGame: File name is empty"); } if (System.IO.Path.GetExtension(fileName) != GAMEFILE_EXTENSION) { fileName += GAMEFILE_EXTENSION; } GameDescriptor game = null; string path = GetFullPath(fileName); if (!FileSystemDI.File.Exists(path)) { throw new FileNotFoundException($"ReadGame: file {path} not found"); } System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(GameDescriptor)); try { using (var fileStream = new FileStream(path, FileMode.Open)) { game = (GameDescriptor)writer.Deserialize(fileStream); } } catch (Exception ex) { throw ex; } return(game); }
private static double EvalPosition(GameDescriptor descriptor, Unit.OwnerEnum owner) { double value = 0; for (int i = 0; i < BoardInfo.Row; i++) { for (int j = 0; j < BoardInfo.Col; j++) { UnitInfo info = descriptor.GetInfo(new Position(i, j)); if (info.owner != Unit.OwnerEnum.None) { if (info.owner == Unit.OwnerEnum.Black) { value += (owner == info.owner) ? PosEval[i, j] : (-PosEval[i, j]); } else { value += (owner == info.owner) ? PosEval[BoardInfo.Row - i - 1, BoardInfo.Col - j - 1] : (-PosEval[BoardInfo.Row - i - 1, BoardInfo.Col - j - 1]); } } } } return(value); }
/// <summary> /// Process la demande de chargement de fichier /// </summary> /// <param name="fileName"></param> /// <returns></returns> private MMessageModel ProcessFileLoadRequest(dynamic fileName) { MMessageModel returnedResult = null; try { _gameName = fileName.ToString(); _gameDescriptor = (GameDescriptor)gameFileManager.ReadGame(_gameName); var gameData = JsonConvert.SerializeObject(_gameDescriptor); returnedResult = new MMessageModel(MessageTypes.FILELOAD_ACCEPTED, gameData); } catch (Exception ex) { if (ex is FileNotFoundException || ex is ArgumentNullException) { returnedResult = new MMessageModel(MessageTypes.FILELOAD_ERROR_NOTFOUND, $"{{\"status\" : \"error : {ex.Message}\"}}"); } else { returnedResult = new MMessageModel(MessageTypes.FILELOAD_ERROR_CORRUPTED, $"{{\"status\" : \"error : {ex.Message}\"}}"); } } return(returnedResult); }
public bool Compare(GameDescriptor rhs) { if (rhs.restResource != restResource) return false; for (int i = 0; i < playerInfo[0].Length; i++) if (rhs.playerInfo[0][i] != playerInfo[0][i]) return false; for (int i = 0; i < playerInfo[1].Length; i++) if (rhs.playerInfo[1][i] != playerInfo[1][i]) return false; for (int i = 0; i < BoardInfo.Row; i++) for (int j = 0; j < BoardInfo.Col; j++) { if (!rhs.units[i, j].Compare(units[i, j])) return false; if (rhs.grids[i, j] != grids[i, j]) return false; } if (rhs.turn != turn) return false; if (rhs.hasBuy != hasBuy) return false; if (rhs.hasMove != hasMove) return false; return true; }
private static double EvalDanger(GameDescriptor descriptor, Unit.OwnerEnum owner) { double value = 0; List<double> equalList = new List<double>(); for (int i = 0; i < BoardInfo.Row; i++) for (int j = 0; j < BoardInfo.Col; j++) { UnitInfo src_info = descriptor.GetInfo(new Position(i, j)); if (src_info.owner == owner) foreach (Position delta in Controller.MoveOffsetList) { Position tar = src_info.pos + delta; if (tar.IsValid) { UnitInfo tar_info = descriptor.GetInfo(tar); if (tar_info.owner == Unit.Opposite(src_info.owner)) { if (src_info.type > tar_info.type) { value += CardEval[(int)tar_info.type]; if (src_info.type == Unit.TypeEnum.Bomb || tar_info.type == Unit.TypeEnum.Bomb) value -= CardEval[(int)src_info.type]; } else if (src_info.type < tar_info.type) { value -= CardEval[(int)src_info.type]; if (src_info.type == Unit.TypeEnum.Bomb || tar_info.type == Unit.TypeEnum.Bomb) value += CardEval[(int)src_info.type]; } else { equalList.Add(CardEval[(int)(src_info.type)]); } } } } } if (equalList.Count > 0) { equalList.Sort(); for (int i = 0; i < equalList.Count - 1; i++) value -= equalList[i]; value += equalList[equalList.Count - 1]; } return value; }
public void RunGame(GameDescriptor game) { ProcessUtils.StartSilent($"com.epicgames.launcher://apps/{game.AppId}?action=launch&silent=true"); }
public GameCache(Board board, Unit.OwnerEnum turn, int turnNum, LastMoveInfo lastMove) { descriptor = new GameDescriptor(board, turn); this.turnNum = turnNum; this.lastMove = lastMove; }
private static double EvalSpecial(GameDescriptor descriptor, Unit.OwnerEnum owner) { double value = 0; if (descriptor.GetPlayerInfo(Unit.TypeEnum.Boss, Unit.Opposite(owner)) > 0) value += descriptor.GetPlayerInfo(Unit.TypeEnum.Bomb, owner) > 0 ? 1 : 0; if (descriptor.GetPlayerInfo(Unit.TypeEnum.Boss, owner) > 0) value -= descriptor.GetPlayerInfo(Unit.TypeEnum.Bomb, Unit.Opposite(owner)) > 0 ? 1 : 0; if (descriptor.RestResource > 0) { if (descriptor.GetPlayerInfo(Unit.TypeEnum.Scout, owner) == 0) { if (descriptor.GetPlayerInfo(Unit.TypeEnum.Bread, owner) == 0) value -= 4; if (descriptor.RestResource >= 5) value -= 3; } if (descriptor.GetPlayerInfo(Unit.TypeEnum.Scout, owner) >= 2 && descriptor.RestResource > 5) value += 7; if (descriptor.GetPlayerInfo(Unit.TypeEnum.Scout, Unit.Opposite(owner)) >= 2 && descriptor.RestResource > 5) value -= 7; if (descriptor.GetPlayerInfo(Unit.TypeEnum.Scout, Unit.Opposite(owner)) == 0) { if (descriptor.GetPlayerInfo(Unit.TypeEnum.Bread, Unit.Opposite(owner)) == 0) value += 4; if (descriptor.RestResource >= 5) value += 3; } } if (descriptor.RestResource == 0) { value -= descriptor.GetPlayerInfo(Unit.TypeEnum.Scout, owner) * 0.5; value += descriptor.GetPlayerInfo(Unit.TypeEnum.Scout, Unit.Opposite(owner)) * 0.5; } return value; }
private static double EvalPosition(GameDescriptor descriptor, Unit.OwnerEnum owner) { double value = 0; for (int i = 0; i < BoardInfo.Row; i++) for (int j = 0; j < BoardInfo.Col; j++) { UnitInfo info = descriptor.GetInfo(new Position(i, j)); if (info.owner != Unit.OwnerEnum.None) { if (info.owner == Unit.OwnerEnum.Black) value += (owner == info.owner) ? PosEval[i, j] : (-PosEval[i, j]); else value += (owner == info.owner) ? PosEval[BoardInfo.Row - i - 1, BoardInfo.Col - j - 1] : (-PosEval[BoardInfo.Row - i - 1, BoardInfo.Col - j - 1]); } } return value; }
private void OnGameMenuItemClicked(GameDescriptor gameDescriptor) { registry.RunGame(gameDescriptor); BuildMainMenu(); }
public async Task InformGameCreated(GameDescriptor descriptor) { await Clients.All.SendAsync("InformGameCreated", descriptor); }
public void Do(GameDescriptor descriptor) { restore_Turn = descriptor.Turn; if (buy != null && buy.status == BuyAction.Status.Before_Move) buy.Do(descriptor); move.Do(descriptor); if (buy != null && buy.status == BuyAction.Status.After_Move) buy.Do(descriptor); }
public void RunGame(GameDescriptor game) { ProcessUtils.StartSilent($"origin://launchgame/{game.AppId}"); }
public void Do(GameDescriptor descriptor) { restore_hasBuy = descriptor.HasBuy; restore_Resource = descriptor.GetPlayerInfo(Unit.TypeEnum.Bread, descriptor.Turn); descriptor.Buy(type, descriptor.Turn); }
public void UnDo(GameDescriptor descriptor) { descriptor.Turn = restore_Turn; // should undo in the reversed order if (buy != null && buy.status == BuyAction.Status.After_Move) buy.UnDo(descriptor); move.UnDo(descriptor); if (buy != null && buy.status == BuyAction.Status.Before_Move) buy.UnDo(descriptor); }
public void UnDo(GameDescriptor descriptor) { descriptor.SetPlayerInfo(Unit.TypeEnum.Bread, descriptor.Turn, restore_Resource); if(type != Unit.TypeEnum.Void) descriptor.Pick(BoardInfo.Base[(int)descriptor.Turn]); descriptor.HasBuy = restore_hasBuy; }
// TODO: Exporter la méthode vers une autre classe // TODO: ne le faire que lorsque la partie vient d'être créée /// <summary> /// Prend en paramètre les gameDescriptor partiels envoyés par le client /// et les assemble en un seul, puis recopie les ID de chacun des éléments du jeu /// </summary> /// <param name="partialMessage"></param> /// <param name="serverGameDescriptor"></param> private IGameDescriptor InitializeEachGameItem(List <GameDescriptor> partialMessage, GameDescriptor serverGameDescriptor) { var assembledGameDescriptor = AssembleFromMultiParts(partialMessage); try { if (assembledGameDescriptor.Carries != null && assembledGameDescriptor.Carries.Count == serverGameDescriptor.Carries.Count) { for (var i = 0; i < assembledGameDescriptor.Carries.Count; i++) { serverGameDescriptor.Carries[i].Id = assembledGameDescriptor.Carries[i].Id; } } if (assembledGameDescriptor.Farms != null && assembledGameDescriptor.Farms.Count == serverGameDescriptor.Farms.Count) { for (var i = 0; i < assembledGameDescriptor.Farms.Count; i++) { serverGameDescriptor.Farms[i].Id = assembledGameDescriptor.Farms[i].Id; } } if (assembledGameDescriptor.GoldMines != null && assembledGameDescriptor.GoldMines.Count == serverGameDescriptor.GoldMines.Count) { for (var i = 0; i < assembledGameDescriptor.GoldMines.Count; i++) { serverGameDescriptor.GoldMines[i].Id = assembledGameDescriptor.GoldMines[i].Id; } } if (assembledGameDescriptor.TownHalls != null && assembledGameDescriptor.TownHalls.Count == serverGameDescriptor.TownHalls.Count) { for (var i = 0; i < assembledGameDescriptor.TownHalls.Count; i++) { serverGameDescriptor.TownHalls[i].Id = assembledGameDescriptor.TownHalls[i].Id; } } if (assembledGameDescriptor.Trees != null && assembledGameDescriptor.Trees.Count >= 0) { for (var i = 0; i < assembledGameDescriptor.Trees.Count; i++) { if (i >= serverGameDescriptor.Trees.Count) { serverGameDescriptor.Trees.Add( new Tree( assembledGameDescriptor.Trees[i].Id, "tree", new Coordinates { x = assembledGameDescriptor.Trees[i].Position.x, y = assembledGameDescriptor.Trees[i].Position.y, }).ToTreeDescriptor()); } } // Si c'est le premier chargement de la partie nouvellement créée, créer la liste des arbres //if (serverGameDescriptor.Trees.Count == 0) //{ // for (var i = 0; i < assembledGameDescriptor.Trees.Count; i++) // { // serverGameDescriptor.Trees.Add( // new Tree( // assembledGameDescriptor.Trees[i].Id, // "tree", // new Coordinates // { // x = assembledGameDescriptor.Trees[i].Position.x, // y = assembledGameDescriptor.Trees[i].Position.y, // }).ToTreeDescriptor()); // } //} //// Si c'est le chargement d'une partie précédement créée //else if (serverGameDescriptor.Trees.Count == assembledGameDescriptor.Trees.Count) //{ // for (var i = 0; i < assembledGameDescriptor.Trees.Count; i++) // { // serverGameDescriptor.Trees[i].Id = assembledGameDescriptor.Trees[i].Id; // } //} //else //{ // throw new Exception(); //} } if (assembledGameDescriptor.Workers != null && assembledGameDescriptor.Workers.Count == serverGameDescriptor.Workers.Count) { for (var i = 0; i < assembledGameDescriptor.Workers.Count; i++) { serverGameDescriptor.Workers[i].Id = assembledGameDescriptor.Workers[i].Id; } } if (assembledGameDescriptor.Resources != null && assembledGameDescriptor.Resources.Count == serverGameDescriptor.Resources.Count) { foreach (var res in assembledGameDescriptor.Resources) { serverGameDescriptor.Resources[res.Key] = assembledGameDescriptor.Resources[res.Key]; } } } catch (Exception ex) { throw ex; } return(serverGameDescriptor); }
public object Clone() { GameDescriptor clone = new GameDescriptor(turn); clone.restResource = restResource; for (int i = 0; i < playerInfo[0].Length; i++) clone.playerInfo[0][i] = playerInfo[0][i]; for (int i = 0; i < playerInfo[1].Length; i++) clone.playerInfo[1][i] = playerInfo[1][i]; for (int i = 0; i < BoardInfo.Row; i++) for (int j = 0; j < BoardInfo.Col; j++) { clone.units[i, j] = units[i, j].Clone() as UnitInfo; clone.grids[i, j] = grids[i, j]; } clone.turn = turn; clone.hasBuy = hasBuy; clone.hasMove = hasMove; return clone; }
public void Do(GameDescriptor descriptor) { restore_Rest = descriptor.RestResource; restore_Resource = descriptor.GetPlayerInfo(Unit.TypeEnum.Bread, descriptor.Turn); restore_hasMove = descriptor.HasMove; restore_srcInfo = descriptor.GetInfo(src).Clone() as UnitInfo; restore_tarInfo = descriptor.GetInfo(tar).Clone() as UnitInfo; descriptor.Move(src, tar); }
public void UnDo(GameDescriptor descriptor) { if(descriptor.GetType(src) != Unit.TypeEnum.Bread) descriptor.Pick(src); if (descriptor.GetType(tar) != Unit.TypeEnum.Bread) descriptor.Pick(tar); descriptor.Put(src, restore_srcInfo); descriptor.Put(tar, restore_tarInfo); descriptor.HasMove = restore_hasMove; descriptor.SetPlayerInfo(Unit.TypeEnum.Bread, descriptor.Turn, restore_Resource); descriptor.RestResource = restore_Rest; }
public void Think(Board board) { state = StateEnum.Thinking; costTime = Time.realtimeSinceStartup; descriptor = new GameDescriptor(board, myTurn); action = new AI_Action(); nodeCount = 0; aiTask = new Thread(DoCalculate); aiTask.Start(); //DoCalculate(); }
public HandHistorySummary() { GameDescription = new GameDescriptor(); }
public void RunGame(GameDescriptor game) { ProcessUtils.StartSilent($"steam://run/{game.AppId}"); }