public int EvaluateFiniteGame(CombinedGameState state) { int xor = 0; foreach (IGameState gameState in state.SubGames) { xor ^= EvaluateFiniteGame(gameState); } return(xor); }
public void TestInfiniteGameEvaluation() { string[] map = new string[] { "", "h", "AE", "h", "hAC", "hADGH", "hCE", "CEG", "BEG" }; var game = new NimCity2Game(map); var ge = new GameEvaluation(); int[] expected = new[] { 0, 1, 0, 1, 2, -1, 2, -1, 1 }; int ix = 0; foreach (IGameState state in game.AllStates()) { int res = ge.EvaluateInfiniteGame(game, state); Assert.AreEqual(expected[ix++], res); } var cgs = new CombinedGameState(new[] { game.states[2], game.states[4], game.states[8] }); int cgsRes = ge.EvaluateInfiniteGame(game, cgs); Assert.IsTrue(cgsRes > 0); cgs = new CombinedGameState(new[] { game.states[7] }); cgsRes = ge.EvaluateInfiniteGame(game, cgs); Assert.IsTrue(cgsRes < 0); cgs = new CombinedGameState(new[] { game.states[5] }); cgsRes = ge.EvaluateInfiniteGame(game, cgs); Assert.IsTrue(cgsRes > 0); cgs = new CombinedGameState(new[] { game.states[5], game.states[3] }); cgsRes = ge.EvaluateInfiniteGame(game, cgs); Assert.IsTrue(cgsRes > 0); cgs = new CombinedGameState(new[] { game.states[5], game.states[3], game.states[4] }); cgsRes = ge.EvaluateInfiniteGame(game, cgs); Assert.IsTrue(cgsRes < 0); }
public int EvaluateInfiniteGame(IGame game, CombinedGameState state) { // -1 = draw, 0 = losing, >0 = winning int xor = 0; IGameState infiniteSubGame = null; foreach (IGameState gameState in state.SubGames) { int res = EvaluateInfiniteGame(game, gameState); if (res < 0) { if (infiniteSubGame != null) { return(-1); } infiniteSubGame = gameState; } else { xor ^= res; } } if (infiniteSubGame == null) { return(xor); } foreach (IGameState[] states in infiniteSubGame.NextStates()) { int v = EvaluateInfiniteGame(game, states[0]); if (v >= 0 && (v ^ xor) == 0) { return(1); } } return(-1); }
public void TestFiniteGameEvaluator() { // Squaring the number int[] expected = new int[] { 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 3, 2, 3, 4, 5 }; GameEvaluation ge = new GameEvaluation(); for (int i = 0; i < expected.Length; i++) { int actual = ge.EvaluateFiniteGame(new SquaringTheNumberState(i)); Assert.AreEqual(expected[i], actual); } // Nim city string[] map = new string[] { "", "h", "hA", "hB", "h", "hABD", "BCE", "CF", "D", "hDEGH", "CFI", "CGJ" }; ge = new GameEvaluation(); for (int i = 1; i < 12; i++) { int v = ge.EvaluateFiniteGame(new NimCity(map, i)); // Console.WriteLine((char)('A' + i -1) + ": " + v); } int g1 = ge.EvaluateFiniteGame(new NimCity(map, 6)); int g2 = ge.EvaluateFiniteGame(new NimCity(map, 10)); int g3 = ge.EvaluateFiniteGame(new NimCity(map, 11)); Assert.AreEqual(2, g1 ^ g2 ^ g3); // Treblecross string[] input = new[] { ".....", "X.....X..X.............X....X..X", ".X.X...X", "...............................................", "..................", "...........................................", "X..............................X..........X............X.X..X..X.......................", "....................................................X.................................................................", "......................................................................................" }; ; var expectedOutput = new[] { new[] { 3 }, new int[0], new[] { 3 }, new[] { 1, 12, 15, 17, 20, 24, 28, 31, 33, 36, 47 }, new[] { 5, 6, 13, 14 }, new[] { 6, 9, 22, 35, 38 }, new[] { 57 }, new[] { 58, 64, 68, 69, 74, 77, 78, 79, 85, 89, 95, 96, 97, 100, 105, 106, 110, 116 }, new int[0] }; ge = new GameEvaluation(); for (int caseNo = 0; caseNo < input.Length; caseNo++) { List <int> winningMoves = new List <int>(); string s = input[caseNo]; for (int i = 0; i < s.Length; i++) { if (s[i] == 'X') { continue; } string t = s.Substring(0, i) + "X" + s.Substring(i + 1); if (t.Contains("XXX")) { winningMoves.Add(i + 1); } else if (!t.Contains("XX") && !t.Contains("X.X")) { CombinedGameState game = TrebleCross.CreateGames(t); if (ge.EvaluateFiniteGame(game) == 0) { winningMoves.Add(i + 1); } } } Assert.AreEqual(expectedOutput[caseNo].Length, winningMoves.Count); for (int i = 0; i < winningMoves.Count; i++) { Assert.AreEqual(expectedOutput[caseNo][i], winningMoves[i]); } // Console.Write("#" + caseNo + ": "); // foreach (int x in winningMoves) // Console.Write(x + " "); // Console.WriteLine(); } }