/// <summary>
        /// Returns a list of legal moves according to the board given.
        /// </summary>
        public List <int> GetPossibleMoves(GridBoard board)
        {
            string jCommand = CreateCommandPossibleMoves(board);
            string moves    = SendCommandReceiveMessage(jCommand);

            return(ParseResponsePossibleMoves(moves));
        }
        private static GridBoard ParseJsonGridBoard(JObject jState, int width, int height)
        {
            string gridString = jState.GetValue(Fields.board).ToString();

            int[,] grid = JsonConvert.DeserializeObject <int[, ]>(gridString);
            grid        = GridBoard.Transpose(grid, height, width); // Received as [y, x], we want [x, y]
            return(new GridBoard(width, height, grid));
        }
        /// <summary>
        /// Returns the state of the board. (PlayerWon|EnemyWon|Draw|Ongoing)
        /// </summary>
        public BoardState EvaluateBoard(GridBoard board)
        {
            string jCommand = CreateCommandEvaluateBoard(board);

            string winner = SendCommandReceiveMessage(jCommand);

            return(ParseResponseEvaluateBoard(winner));
        }
        /// <summary>
        /// Returns a board in which the given move has been played by the given player.
        /// </summary>
        /// <param name="board">Board which the move should be applied to</param>
        /// <param name="player">Player to make the move</param>
        /// <param name="move">Move to be played</param>
        /// <returns></returns>
        public GridBoard SimulateMove(GridBoard board, int player, int move)
        {
            string jCommand = CreateCommandSimulateMove(board, player, move);

            string state = SendCommandReceiveMessage(jCommand);

            return(ParseResponseSimulateMove(state, width, height));
        }
        internal static string CreateCommandEvaluateBoard(GridBoard board)
        {
            JObject jsonCommand = new JObject(
                new JProperty(Fields.action, Actions.evalBoard),
                //new JProperty(Fields.board, board.Serialize())
                new JProperty(Fields.board, JArray.FromObject(GridBoard.Transpose(board.grid, board.WIDTH, board.HEIGHT)))
                );

            //Console.WriteLine("Command evaluate: \n" + jsonCommand.ToString() + "\n");
            return(jsonCommand.ToString());
        }
        internal static string CreateCommandSimulateMove(GridBoard board, int player, int move)
        {
            JObject jsonCommand = new JObject(
                new JProperty(Fields.action, Actions.simMove),
                //new JProperty(Fields.board, board.Serialize()),
                new JProperty(Fields.board, JArray.FromObject(GridBoard.Transpose(board.grid, board.WIDTH, board.HEIGHT))),
                new JProperty(Fields.player, player),
                new JProperty(Fields.move, move)
                );

            //Console.WriteLine("Command simMove: \n" + jsonCommand.ToString() + "\n");
            return(jsonCommand.ToString());
        }
 /// <summary>
 /// Sends a command to restart the game.
 /// </summary>
 public new void RestartGame()
 {
     currentBoard = null;
     base.RestartGame();
 }
 protected abstract void UpdateCurrentBoard(GridBoard board);
 protected override void ExtractState(JObject jState)
 {
     currentBoard = ParseResponseState(jState, width, height);
     UpdateCurrentBoard(currentBoard);
 }
Exemplo n.º 10
0
 // Deep copy
 public GridBoard(GridBoard gridBoard)
 {
     WIDTH     = gridBoard.WIDTH;
     HEIGHT    = gridBoard.HEIGHT;
     this.grid = (int[, ])gridBoard.grid.Clone();
 }