-
Notifications
You must be signed in to change notification settings - Fork 3
/
MaxPrestigeBot.cs
108 lines (97 loc) · 3.62 KB
/
MaxPrestigeBot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Bots;
using ScriptsOfTribute;
using ScriptsOfTribute.AI;
using ScriptsOfTribute.Board;
using ScriptsOfTribute.Serializers;
namespace Bots;
public class MaxPrestigeBot : AI
{
private string patronLogPath = "patronsMaxPrestigeBot.txt";
private Apriori apriori = new Apriori();
private int support = 4;
private double confidence = 0.3;
private PlayerEnum myID;
private string patrons;
private bool startOfGame = true;
private const ulong Seed = 123;
private readonly SeededRandom rng = new(Seed);
public override PatronId SelectPatron(List<PatronId> availablePatrons, int round)
//PatronId? selectedPatron = apriori.AprioriBestChoice(availablePatrons, patronLogPath, support, confidence);
//return selectedPatron ?? availablePatrons.PickRandom(Rng);
=> availablePatrons.PickRandom(rng);
public override Move Play(GameState gameState, List<Move> possibleMoves, TimeSpan remainingTime)
{
if (startOfGame)
{
myID = gameState.CurrentPlayer.PlayerID;
patrons = string.Join(",", gameState.Patrons.FindAll(x => x != PatronId.TREASURY).Select(n => n.ToString()).ToArray());
startOfGame = false;
}
var movesToCheck = possibleMoves.Where(m => m.Command != CommandEnum.END_TURN).ToList();
if (movesToCheck.Count == 0)
{
Log(Move.EndTurn().ToString());
return Move.EndTurn();
}
Dictionary<int, List<Move>> prestigeToMove = new();
foreach (var move in movesToCheck)
{
var (newState, newPossibleMoves) = gameState.ApplyMove(move, Seed);
if (newState.GameEndState?.Winner == Id)
{
Log(move.ToString());
return move;
}
var newMovesToCheck = newPossibleMoves.Where(m => m.Command != CommandEnum.END_TURN).ToList();
if (newMovesToCheck.Count == 0)
{
var val = newState.CurrentPlayer.Prestige + newState.CurrentPlayer.Power;
if (prestigeToMove.ContainsKey(val))
{
prestigeToMove[val].Add(move);
}
else
{
prestigeToMove.Add(val, new List<Move> { move });
}
continue;
}
foreach (var newMove in newMovesToCheck)
{
var (newestState, _) = newState.ApplyMove(newMove);
if (newestState.GameEndState?.Winner == Id)
{
Log(move.ToString());
return move;
}
var val = newestState.CurrentPlayer.Prestige + newestState.CurrentPlayer.Power;
if (prestigeToMove.ContainsKey(val))
{
prestigeToMove[val].Add(move);
}
else
{
prestigeToMove.Add(val, new List<Move> { move });
}
}
}
if (prestigeToMove.Keys.Count == 0)
{
var finalMove = possibleMoves.PickRandom(rng);
Log(finalMove.ToString());
return finalMove;
}
var bestMove = prestigeToMove[prestigeToMove.Keys.Max()].PickRandom(rng);
Log(bestMove.ToString());
return bestMove;
}
public override void GameEnd(EndGameState state, FullGameState? finalBoardState)
{
Log("Game ended : (");
if (state.Winner == myID)
{
File.AppendAllText(patronLogPath, patrons + System.Environment.NewLine);
}
startOfGame = true;
}
}