public void GameTreeRootTest() { tree = new GameTree(testDomain, testProblem, Parser.GetTopDirectory() + @"GameTrees\Data\"); GameTreeNode root = tree.Root; Assert.AreEqual(root.ID, 0); Assert.IsFalse(root.IsGoal); Assert.AreEqual(root.Outgoing.Count, 4); Assert.IsTrue(tree.Simulate(root.ID)); }
static void Main(string[] args) { //Tester.Train(); string[] inputs; Referee.GameReferee.Reset(); GameTree tree = null; // game loop bool firstStep = true; while (true) { inputs = Console.ReadLine().Split(' '); int opponentRow = int.Parse(inputs[0]); int opponentCol = int.Parse(inputs[1]); var oponentMove = new Tuple <int, int>(opponentRow, opponentCol); int validActionCount = int.Parse(Console.ReadLine()); int row = 0, col = 0; for (int i = 0; i < validActionCount; i++) { inputs = Console.ReadLine().Split(' '); row = int.Parse(inputs[0]); col = int.Parse(inputs[1]); } Referee.GameReferee.Move(opponentRow, opponentCol); if (firstStep) { tree = new GameTree(oponentMove, Referee.GameReferee._field, 6 - Referee.GameReferee._field.Player); tree.Simulate(980); firstStep = false; } else { tree.Update(oponentMove, Referee.GameReferee._field); tree.Simulate(80); } // Write an action using Console.WriteLine() // To debug: Console.Error.WriteLine("Debug messages..."); var move = tree.GetNextStep(); if (move == null) { move = new Tuple <int, int>(row, col); } //Console.Error.WriteLine($"{tree._startLeaf.Move.Item1}:{tree._startLeaf.Move.Item2} "); //foreach (var m in tree._startLeaf.SubLeafs) //{ // Console.Error.Write($"{m.Move.Item1}:{m.Move.Item2} "); //} //Console.Error.WriteLine(); //foreach (var m in Referee.GameReferee.GetNextMoves()) //{ // Console.Error.Write($"{m.Item1}:{m.Item2} "); //} var test = Referee.GameReferee.Move(move.Item1, move.Item2); Console.Error.WriteLine(tree._startLeaf.Simulations); tree.Update(move, Referee.GameReferee._field); Console.WriteLine($"{move.Item1} {move.Item2}"); } }
public static void Train() { var startMove = new Tuple <int, int>(-1, -1); double bestC = 0; double bestB = 0; for (double c = 0.0001; c <= 10; c += 0.0001) { for (double b = 0.0001; b < 10; b += 0.0001) { int firstWins = 0; int secondWins = 0; { for (int i = 0; i < 50; i++) { Referee.GameReferee.Reset(); GameTree tree1 = null, tree2 = null; BoardStatus status = BoardStatus.InProgress; bool firstStep = true; while ((status = Referee.GameReferee.GetStatus()) == BoardStatus.InProgress) { if (firstStep) { firstStep = false; tree1 = new GameTree(startMove, Referee.GameReferee._field, BoardCells.Tac); tree1.c = bestC; tree1.b = bestB; tree1.Simulate(980); var move = tree1.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2 = new GameTree(move, Referee.GameReferee._field, BoardCells.Tac); tree2.c = c; tree2.b = b; } else { tree1.Simulate(80); var move = tree1.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2.Update(move, Referee.GameReferee._field); } if ((status = Referee.GameReferee.GetStatus()) != BoardStatus.InProgress) { break; } if (firstStep) { tree2.Simulate(980); var move = tree2.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2.Update(move, Referee.GameReferee._field); } else { tree2.Simulate(80); var move = tree2.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2.Update(move, Referee.GameReferee._field); } } if (status == BoardStatus.TicWin) { firstWins++; } else if (status == BoardStatus.TacWin) { secondWins++; } } } { for (int i = 0; i < 50; i++) { Referee.GameReferee.Reset(); var tree1 = new GameTree(startMove, Referee.GameReferee._field, BoardCells.Tac); tree1.c = bestC; tree1.b = bestB; var tree2 = new GameTree(startMove, Referee.GameReferee._field, BoardCells.Tac); tree1.c = c; tree2.b = b; BoardStatus status = BoardStatus.InProgress; bool firstStep = true; while ((status = Referee.GameReferee.GetStatus()) == BoardStatus.InProgress) { if (firstStep) { firstStep = false; tree2 = new GameTree(startMove, Referee.GameReferee._field, BoardCells.Tac); tree2.c = bestC; tree2.b = bestB; tree2.Simulate(980); var move = tree2.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree2.Update(move, Referee.GameReferee._field); tree1 = new GameTree(move, Referee.GameReferee._field, BoardCells.Tac); tree1.c = c; tree1.b = b; } else { tree2.Simulate(80); var move = tree2.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2.Update(move, Referee.GameReferee._field); } if ((status = Referee.GameReferee.GetStatus()) != BoardStatus.InProgress) { break; } if (firstStep) { tree1.Simulate(980); var move = tree1.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2.Update(move, Referee.GameReferee._field); } else { tree1.Simulate(80); var move = tree1.GetNextStep(); Referee.GameReferee.Move(move.Item1, move.Item2); tree1.Update(move, Referee.GameReferee._field); tree2.Update(move, Referee.GameReferee._field); } } if (status == BoardStatus.TicWin) { secondWins++; } else if (status == BoardStatus.TacWin) { firstWins++; } } } if (secondWins > firstWins) { bestB = b; bestC = c; Console.WriteLine($"Winner c:{c} b:{b}"); } else { Console.WriteLine($"Winner c:{bestC} b:{bestB}"); } } } Console.ReadLine(); }