public override int MakeOrder(Game game) { lastGames = 0; MonteKarloSum[] sums = new MonteKarloSum[orders.Count]; for (int i = 0; i < sums.Length; i++) { sums[i] = new MonteKarloSum(); } DateTime start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < SecondsToThink) { lastGames++; Game today = game.CopyRandom(); try { RandomPlayer my_copy = (RandomPlayer)today.players.Find((p) => p.type == type); today.Move(); int pos = my_copy.selectedOrderToMove; for (int i = 0; i < depth; i++) { today.Move(); } sums[pos].Add(CalcScore(ref today)); } catch (Exception excp) { throw new GameException(today, excp); } } float max = 0; int indexMax = 0; for (int i = 0; i < orders.Count; i++) { float avr = sums[i].Medium; if (!float.IsNaN(avr)) { if (avr > max) { max = avr; indexMax = i; } } } game.MakeOrder(this, orders[indexMax].place); orders.RemoveAt(indexMax); LastMaxScore = max; return(1); }
/// <summary> /// Копирует игру /// </summary> /// <returns></returns> public Game CopyRandom() { Game game = new Game(); game.gamePhase = gamePhase; game.orderCount = orderCount; game.moveIndex = moveIndex; for (int i = 0; i < players.Count; i++) { Player p = new RandomPlayer(); p.type = players[i].type; game.players.Add(p); } for (int i = 0; i < places.Count; i++) { game.places.Add(places[i].CopyUnlinked()); } for (int i = 0; i < places.Count; i++) { Place p = game.places[i]; for (int j = 0; j < p.names.Count; j++) { game.places.AddLink(p.name, p.names[j]); } } for (int i = 0; i < players.Count; i++) { Player p = game.players[i]; List <Order> orders = players[i].orders; for (int j = 0; j < orders.Count; j++) { OrderData data = orders[j].CopyUnlinked(); p.orders.Add(new Order { type = data.type, power = data.power, place = game.places.Find((place) => place.name == data.place) }); p.orders.Last().place.placed_order = p.orders.Last(); } } for (int i = 0; i < inMoveActions.Count; i++) { MoveData data = inMoveActions[i].CopyUnlinked(); Move m = new Move(); m.active_units = data.active_units; m.modifier = data.modifier; m.playerState = data.playerState; m.active_place = game.places.Find((place) => place.name == data.active_place); m.player = game.players.Find((player) => player.type == data.player); game.inMoveActions.Add(m); } return(game); }