public string GetNextMove(Game game) { IList<String> availableMoves = game.AvailableMoves; int randomInt = _random.Next(availableMoves.Count); string nextMove = availableMoves[randomInt]; return nextMove; }
private Graph BuildTree(Game currentGame, int depth) { var graph = new Graph(false); var currentNode = new GameNode("00", null, _player); graph.AddVertex(currentNode); BuildTree(graph, currentGame, currentNode, depth); return graph; }
public string GetNextMove(Game game) { if (game.AvailableMoves.Count < 6) { return _minmax3.GetBestMove(game); } return _minmax25.GetBestMove(game); }
public ReversiLab.Rules.Game ToGame() { var game = new ReversiLab.Rules.Game(); game.CurrentPlayer = CurrentPlayer; game.AvailableMoves = AvailableMoves; game.BoardState = BoardState; return game; }
public ReversiLab.Rules.Game ToGame() { var game = new ReversiLab.Rules.Game(); game.CurrentPlayer = CurrentPlayer; game.AvailableMoves = AvailableMoves; game.BoardState = BoardState; return(game); }
public String GetBestMove(Game game) { Graph graph = BuildTree(game, _depth); //Console.WriteLine("Graph Size:" + graph.VertexCount); //long memory = GC.GetTotalMemory(true); //Console.WriteLine("Memory Usage" + memory/1000 + " Kb"); GameNode root = graph.Vertices.First(); GameNode gameNode = GetMax(graph, root, 0); return gameNode.Move; }
//~ ---------------------------------------------------------------------------------------------------------------- /** * Scans all directions for valid pattern. Valid pattern: * * <pre> [SAME COLOR] [DIFF. COLOR] ... [DIFF. COLOR] [TARGET] * </pre> * * pattern rules: * * <ol> * <li>Target location cannot be an empty place.</li> * <li>Location before target location should be in different color according to current Color</li> * <li>Pattern should start with same color according to current Color</li> * </ol> * * @param game current game * @param position target position * @param Color Color value for pattern validation * * @return Returns found path if valid pattern found. */ public static IDictionary<Position, List<Path>> FindAvailablePaths(Game game, Position position, int player) { IList<List<int>> boardState = game.BoardState; if (boardState[position.Row][position.Col] != EmptyPlace) { return null; } IDictionary<Position, List<Path>> playerPaths = new Dictionary<Position, List<Path>>(); int differentColor = player == WhitePlayer ? BlackDisk : WhiteDisk; int sameColor = player; // Redundant constant for code readability for (int direction = 0; direction < 8; direction++) { int value = GetPositionValue(game, position, direction, 1); if (value == differentColor) { for (int step = 2; step < 8; step++) { value = GetPositionValue(game, position, direction, step); if (value == EndOfDirection || value == EmptyPlace) { break; } if (value == sameColor) { List<Path> paths = null; if (playerPaths.ContainsKey(position)) { paths = playerPaths[position]; } if (paths == null) { paths = new List<Path>(); } paths.Add(new Path(position, direction, step)); if (!playerPaths.ContainsKey(position)) { playerPaths.Add(position, paths); } break; } } // end for } } // end for return playerPaths; }
public string GetNextMove(Game game) { //if (game.CurrentPlayer != Color) //{ // throw new WrongOrderException(); //} if (game.AvailableMoves != null) { return _strategy.GetNextMove(game); } return null; }
public string GetNextMove(Game game) { IList<String> availableMoves = game.AvailableMoves; IBoardStateEvaluator evaluator = new ScoreAndCornersEvaluator(); int max = 0; int maxIndex = 0; for (int i = 0; i < availableMoves.Count; i++) { Game pGame = GameService.Generate(game, availableMoves[i], true); int value = evaluator.Evaluate(pGame.BoardState, pGame.CurrentPlayer); if (value > max) { max = value; maxIndex = i; } } string nextMove = availableMoves[maxIndex]; return nextMove; }
private void BuildTree(Graph graph, Game parentGame, GameNode parentNode, int depth) { //A potantial next parentNode may have no available moves if (depth == 0 || parentGame.AvailableMoves == null) { return; } bool leafNode = depth == 1; foreach (string move in parentGame.AvailableMoves) { Game childGame = GameService.Generate(parentGame, move, leafNode); string id = parentNode.Id + "=>" + move; var childNode = new GameNode(id, move, parentGame.CurrentPlayer) { Value = _evaluator.Evaluate(childGame.BoardState, _player) }; graph.AddVertex(childNode); graph.AddEdge(new Edge<GameNode>(parentNode, childNode)); BuildTree(graph, childGame, childNode, depth - 1); } }
public GameResult MakeFight(ReversiPlayer black, ReversiPlayer white, bool v=true) { var players = new ReversiPlayer[3]; //[0] won't be used! players[GameService.BlackPlayer] = black; players[GameService.WhitePlayer] = white; var game = new Game(); GameService.Start(game, _boardSize); int player = GameService.BlackPlayer; while (game.CurrentPlayer != GameService.NoPlayer) { try { PrintLine(game.AvailableMovesToString(), v); var sw = new Stopwatch(); sw.Start(); string move = players[player].GetNextMove(game); sw.Stop(); PrintLine(sw.ElapsedMilliseconds + " ms", v); GameService.Move(game, move, player); PrintLine(players[player] + @" moves: " + move, v); PrintLine(game.StateToString(), v); player = Switch(player); } catch (WrongOrderException) { player = Switch(player); } } var result = new GameResult(game.GetScore(GameService.BlackPlayer), game.GetScore(GameService.WhitePlayer)); result.Print(); return result; }
//~ ---------------------------------------------------------------------------------------------------------------- /** * Occupies path for current Color * * <pre> c d e f g 4 [2] [1] [1] [0] [0] >>> direction 2 * </pre> * * After occupation: (Direction: 2, Step: 3) * * <pre> c d e f g 4 [2] [2] [2] [2] [0] >>> direction 2 * </pre> * * @param position start position * @param direction direction number. Range 0 - 7 * @param step translation distance */ public static void OccupyPath(Game game, Position position, int direction, int step) { IList<List<int>> boardState = game.BoardState; int currentPlayer = game.CurrentPlayer; for (int i = 0; i < step; i++) { Position translatedPosition = GetTranslatedPosition(position, direction, i); if (translatedPosition == null) { break; } // Color constants and disk constants are equivalent. So can be used "currentPlayer" for // current Color's disk color. boardState[translatedPosition.Row][translatedPosition.Col] = currentPlayer; } }
public string GetNextMove(Game game) { return game.AvailableMoves.First(); }
/// <summary> /// To create a new copy of a game object. /// </summary> /// <param name="game"></param> public Game(Game game) { BoardState = game.BoardState.Select(list => list.ToList()).ToList(); AvailableMoves = game.AvailableMoves.ToList(); CurrentPlayer = game.CurrentPlayer; }
/** * There are 8 legal directions for Color move. This method returns desired position value for given step and * directions and also returns -1 for out of bound locations. * * <pre> c d e f 4 [2] [0] [0] [1] >>> direction 2 * </pre> * * If start position is c4 and step is 3 then target location is f4 and the value is 1. * * @param position start position * @param direction direction number. Range 0 - 7 * @param step translation distance * * @return -1 for out of bound locations, 0 for empty locations, 1 for black disk, 2 for white disk */ private static int GetPositionValue(Game game, Position position, int direction, int step) { Position translatedPosition = GetTranslatedPosition(position, direction, step); if (translatedPosition == null) { return EndOfDirection; } IList<List<int>> boardState = game.BoardState; return boardState[translatedPosition.Row][translatedPosition.Col]; }
//~ ---------------------------------------------------------------------------------------------------------------- private static IDictionary<Position, List<Path>> FindAllAvailablePaths(Game game, int player) { var availablePaths = new Dictionary<Position, List<Path>>(); for (int row = 0; row < _size; row++) { for (int col = 0; col < _size; col++) { var position = new Position(row, col); IDictionary<Position, List<Path>> positionPaths = FindAvailablePaths(game, position, player); if (positionPaths != null && positionPaths.Count != 0) { availablePaths = availablePaths.Concat(positionPaths).ToDictionary(x => x.Key, x => x.Value); } } } return availablePaths; }
public static void Start8X8(Game game) { _size = 8; if (game.Started) { throw new AlreadyStartedException(); } game.Started = true; game.CurrentPlayer = BlackPlayer; game.BoardState = new List<List<int>>(new[] // 0 1 2 3 4 5 6 7 // a b c d e f g h { new List<int>(new[] {0, 0, 0, 0, 0, 0, 0, 0}), // 1 0 new List<int>(new[] {0, 0, 0, 0, 0, 0, 0, 0}), // 2 1 new List<int>(new[] {0, 0, 0, 0, 0, 0, 0, 0}), // 3 2 new List<int>(new[] {0, 0, 0, 2, 1, 0, 0, 0}), // 4 3 new List<int>(new[] {0, 0, 0, 1, 2, 0, 0, 0}), // 5 4 new List<int>(new[] {0, 0, 0, 0, 0, 0, 0, 0}), // 6 5 new List<int>(new[] {0, 0, 0, 0, 0, 0, 0, 0}), // 7 6 new List<int>(new[] {0, 0, 0, 0, 0, 0, 0, 0}) // 8 7 }); game.AvailableMoves = new List<string>(new[] {"c4", "d3", "e6", "f5"}); }
public static void Start4X4(Game game) { _size = 4; if (game.Started) { throw new AlreadyStartedException(); } game.Started = true; game.CurrentPlayer = BlackPlayer; game.BoardState = new List<List<int>>(new[] { // 0 1 2 3 // a b c d new List<int>(new[] {0, 0, 0, 0}), // 1 0 new List<int>(new[] {0, 2, 1, 0}), // 2 1 new List<int>(new[] {0, 1, 2, 0}), // 3 2 new List<int>(new[] {0, 0, 0, 0}) // 4 3 }); game.AvailableMoves = new List<string>(new[] {"a2", "b1", "c4", "d3"}); }
//~ ---------------------------------------------------------------------------------------------------------------- public static void Start(Game game, int boardSize) { switch (boardSize) { case 4: Start4X4(game); break; case 8: Start8X8(game); break; default: throw new ArgumentOutOfRangeException(@"boardSize must be either 4 or 8"); } }
//~ ---------------------------------------------------------------------------------------------------------------- /** * Occupies path for current Color * * <pre> c d e f g 4 [2] [1] [1] [0] [0] >>> direction 2 * </pre> * * After occupation: (Direction: 2, Step: 3) * * <pre> c d e f g 4 [2] [2] [2] [2] [0] >>> direction 2 * </pre> * * @param path found path */ public static void OccupyPath(Game game, Path path) { OccupyPath(game, path.Position, path.Direction, path.Step); }
public static void Move(Game game, string piece, int player, bool leafNode = false) { int currentPlayer = game.CurrentPlayer; if (player != currentPlayer) { throw new WrongOrderException(); } var position = new Position(piece); IDictionary<Position, List<Path>> playerPaths = FindAvailablePaths(game, position, currentPlayer); if (playerPaths == null || playerPaths.Count == 0) { throw new IllegalMoveException(); } OccupyPaths(game, playerPaths, position); if (leafNode) { return; } int otherPlayer = currentPlayer == BlackPlayer ? WhitePlayer : BlackPlayer; // Are there any legal moves for other color if ((playerPaths = FindAllAvailablePaths(game, otherPlayer)).Count != 0) { game.CurrentPlayer = otherPlayer; game.AvailableMoves = PositionSet2StringList(playerPaths.Keys); } // if have legal moves for current Color else if ((playerPaths = FindAllAvailablePaths(game, currentPlayer)).Count != 0) { game.AvailableMoves = PositionSet2StringList(playerPaths.Keys); } else { game.Started = false; game.CurrentPlayer = NoPlayer; game.AvailableMoves = null; } }
public static Game Generate(Game game, string move, bool leafNode) { var newGame = new Game(game); Move(newGame, move, newGame.CurrentPlayer, leafNode); return newGame; }
//~ ---------------------------------------------------------------------------------------------------------------- public static void OccupyPaths(Game game, IDictionary<Position, List<Path>> playerPaths, Position position) { if (playerPaths != null && playerPaths.Count != 0) { List<Path> foundPaths = playerPaths[position]; if (foundPaths != null && foundPaths.Count != 0) { foreach (Path path in foundPaths) { OccupyPath(game, path); } } //else //{ // throw new IllegalMoveException(); //} } }
public string GetNextMove(Game game) { return _minMax.GetBestMove(game); }