Exemplo n.º 1
0
        private APIMsgConclusion handleSimulateMoveDelta(JSONObject jObj)
        {
            JSONObject jPlayer = jObj.GetField(Constants.JProtocol.player);
            JSONObject jEnemy  = jObj.GetField(Constants.JProtocol.enemy);
            BoardMoves moves   = parseBoardMoves(jObj);

            SnakePlayer p = new SnakePlayer()
            {
                x   = (int)jPlayer.GetField(Constants.JProtocol.posX).i,
                y   = (int)jPlayer.GetField(Constants.JProtocol.posY).i,
                dir = moves.playerMove
            };
            SnakePlayer e = new SnakePlayer()
            {
                x   = (int)jEnemy.GetField(Constants.JProtocol.posX).i,
                y   = (int)jEnemy.GetField(Constants.JProtocol.posY).i,
                dir = moves.enemyMove
            };

            Board b = new Board(new SnakePlayer[] { p, e }, false);

            b.playMove(new string[] { moves.playerMove, moves.enemyMove });
            SimulatedMove temp = new SimulatedMove()
            {
                enemyMove = moves.enemyMove, playerMove = moves.playerMove, board = b
            };

            string encodedBoards = SnakeProtocolEncoder.compressSimulatedMove(temp);

            return(new APIMsgConclusion()
            {
                status = ResponseStatus.Success, msg = encodedBoards, target = MsgTarget.Player
            });
        }
Exemplo n.º 2
0
        public void encodeDecodeBoard(string playerDir, string enemyDir, string rawBoard)
        {
            Board      startBoard   = SnakeTestUtils.generateBoard(rawBoard, playerDir, enemyDir);
            JSONObject jBoard       = SnakeProtocolEncoder.compressBoard(startBoard);
            Board      decodedBoard = new Board(jBoard);

            Assert.True(SnakeTestUtils.compareBoards(startBoard, decodedBoard));
        }
Exemplo n.º 3
0
        private APIMsgConclusion handleEvaluateBoard(JSONObject jObj)
        {
            JSONObject jBoard            = jObj.GetField(Constants.JProtocol.board);
            Board      board             = new Board(jBoard);
            string     encodedBoardState = SnakeProtocolEncoder.encodeBoardState(board.evaluateBoard().ToString());

            return(new APIMsgConclusion()
            {
                status = ResponseStatus.Success, msg = encodedBoardState,
                target = MsgTarget.Player
            });
        }
Exemplo n.º 4
0
        private APIMsgConclusion activateSimulateMove(JSONObject jObj, Func <Board, string, string, SimulatedMove> simMove)
        {
            JSONObject jBoard     = jObj.GetField(Constants.JProtocol.board);
            Board      startBoard = new Board(jBoard);
            BoardMoves moves      = parseBoardMoves(jObj);

            SimulatedMove temp          = simMove(startBoard, moves.playerMove, moves.enemyMove);//SnakeAPILogic.simulateMove(startBoard, moves.playerMove, moves.enemyMove, false);
            string        encodedBoards = SnakeProtocolEncoder.compressSimulatedMove(temp);

            return(new APIMsgConclusion()
            {
                status = ResponseStatus.Success, msg = encodedBoards, target = MsgTarget.Player
            });
        }
Exemplo n.º 5
0
        private APIMsgConclusion handleGetPossibleMoves(JSONObject jObj)
        {
            string[] directions = new string[2];
            directions[0] = jObj.GetField(Constants.JProtocol.player).str;
            directions[1] = jObj.GetField(Constants.JProtocol.enemy).str;
            //PossibleMoves possMoves = SnakeAPILogic.getPossibleMoves(new Board(jObj));
            PossibleMoves possMoves = SnakeAPILogic.getPossibleMoves(directions);

            string responseMsg = SnakeProtocolEncoder.encodePossibleMoves(possMoves).Print();

            return(new APIMsgConclusion()
            {
                status = ResponseStatus.Success, msg = responseMsg, target = MsgTarget.Player
            });
        }
        public void protocolSimulateBothPlayerMove(string playerDir, string enemyDir, int pX, int pY, int eX, int eY, string rawBoard, string rawExpected)
        {
            SnakeAPIRouter router     = new SnakeAPIRouter();
            Board          startBoard = SnakeTestUtils.generateBoard(rawBoard, playerDir, enemyDir);
            Board          expected   = SnakeTestUtils.generateBoard(rawExpected, playerDir, enemyDir, new int[] { pX, pY }, new int[] { eX, eY });
            JSONObject     jObj       = SnakeProtocolEncoder.generateSimulateJMsg(startBoard, playerDir, enemyDir);
            //Debug.Log("Generated simulate move request: \n" + jObj.Print(true) + "\n");
            APIMsgConclusion response = router.handleIncomingMsg(jObj.Print());

            Assert.True(response.target == MsgTarget.Player);
            Assert.True(response.status == ResponseStatus.Success);
            JSONObject newJBoard = new JSONObject(response.msg);
            //Debug.Log("Generated simulate move response: \n" + newJBoard.Print(true) + "\n");
            //JSONObject newJBoard = jResponse.GetField(Constants.JProtocol.board);
            Board newBoard = new Board(newJBoard);

            // startBoard.printBoard();
            // newBoard.printBoard();

            Assert.True(SnakeTestUtils.compareBoards(expected, newBoard));
        }
Exemplo n.º 7
0
        public void getPossibleMovesTest(string playerDir, string enemyDir, string[] playerExpected, string[] enemyExpected)
        {
            List <string>  playerExpectedList = playerExpected.ToList();
            List <string>  enemyExpectedList  = enemyExpected.ToList();
            SnakeAPIRouter router             = new SnakeAPIRouter();
            //Board startBoard = SnakeTestUtils.generateBoard(rawBoard, playerDir, enemyDir);
            JSONObject jMsg = SnakeProtocolEncoder.generateGetPossMovesJMsg(playerDir, enemyDir);

            APIMsgConclusion response = router.handleIncomingMsg(jMsg.Print());

            Assert.True(response.target == MsgTarget.Player);
            Assert.True(response.status == ResponseStatus.Success);

            Debug.Log(response.msg);

            PossibleMoves moves = extractPossibleMoves(new JSONObject(response.msg));

            Assert.True(SnakeTestUtils.comparePossibleMoves(playerExpectedList, moves.playerMoves));
            Assert.True(SnakeTestUtils.comparePossibleMoves(enemyExpectedList, moves.enemyMoves));

            Debug.Log("True....");
        }