public int GetMove(TicTacToe.Game.TicTacToe game) { var state = game.GetBoard(); var gameState = state.Select(s => s == _playerId?1:(0)).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?1:0))).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?0:1))).ToArray(); var result = m_network.Calculate(gameState.Select(s => (double)s).ToArray()); // Console.Error.WriteLine("Weights: " + string.Join(" ", result.Select(r => r.Value))); return(result.OrderByDescending(r => r.Value).First(r => game.IsPossible(r.Target)).Target); }
public int GetMove(TicTacToe.Game.TicTacToe game) { // if (rnd.NextDouble() < 0.5) // { // var move = -1; // while (!game.IsPossible(move)) // { // move = rnd.Next(0, 9); // } // // return move; // } var bestMoves = new List <int>(); var bestScore = -10; var a = -100; var b = 100; for (var i = 0; i < 9; i++) { if (game.IsPossible(i)) { game.DoMove(i, _playerId); var score = Search(game, a, b, false); if (score > bestScore) { bestMoves.Clear(); bestMoves.Add(i); bestScore = score; } else if (score == bestScore) { bestMoves.Add(i); } game.RevertMove(i); } } var best = bestMoves.OrderBy(i => rnd.NextDouble()).First(); var state = game.GetBoard(); if (_data.Add(GetHash(state))) { FileSaver.AddData(string.Join(",", state.Select(s => s == _playerId?1:(0)).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?1:0))).Concat(state.Select(s => s == _playerId?0:(s == OtherPlayer?0:1)))) + "," + best + "\n"); } // Console.Error.WriteLine(); // Console.Error.WriteLine("TIME: " + s.ElapsedMilliseconds); // Console.Error.WriteLine("BestScore: " + bestScore); return(best); }