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 }); }
public BoardState evaluateBoard() { SnakePlayer p = players[0], e = players[1]; //Debug.Log("Player: " + p.x + "," + p.y + "\nEnemy: " + e.x + "," + e.y); if (p.x == e.x && p.y == e.y) // Players Crash into eachother { return(BoardState.draw); } bool playerCrash = coordCrash(p.x, p.y); bool enemyCrash = coordCrash(e.x, e.y); // Debug.Log("Playercrash: " + playerCrash.ToString() + ", Enemycrash: " + enemyCrash.ToString()); if (playerCrash && enemyCrash) { return(BoardState.draw); } if (playerCrash && enemyCrash == false) { return(BoardState.enemyWon); } if (playerCrash == false && enemyCrash) { return(BoardState.playerWon); } return(BoardState.ongoing); }
public static void addPlayerField(ref JSONObject jObj, SnakePlayer p, string name) { JSONObject player = new JSONObject(); player.AddField(Constants.JProtocol.dir, p.dir); player.AddField(Constants.JProtocol.posX, p.x); player.AddField(Constants.JProtocol.posY, p.y); jObj.AddField(name, player); }
public static Board generateBoard(string rawBoard, string playerDir, string enemyDir, int[] pPos = null, int[] ePos = null) { string[] cells = rawBoard.Split(' '); int size = (int)Math.Round(Math.Sqrt(cells.Length)); List <Position2D> blocked = new List <Position2D>(); SnakePlayer[] players = new SnakePlayer[2]; if (pPos != null) { players[0] = new SnakePlayer() { x = pPos[0], y = pPos[1], dir = playerDir } } ; if (ePos != null) { players[1] = new SnakePlayer() { x = ePos[0], y = ePos[1], dir = playerDir } } ; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { switch (cells[size * y + x]) { case "X": blocked.Add(new Position2D() { x = x, y = y }); break; case "P": players[0] = new SnakePlayer() { x = x, y = y, dir = playerDir }; break; case "E": players[1] = new SnakePlayer() { x = x, y = y, dir = enemyDir }; break; } } } bool[,] grid = gengerateBlockedGrid(blocked, size); return(new Board(grid, players, blocked, false)); }
private void applyPlayMove(ref SnakePlayer p, string dir) { int[] coordChange = Constants.Movement.dirToCoords(dir); blockedGrid[p.x, p.y] = true; blockedList.Add(new Position2D() { x = p.x, y = p.y }); p.x += coordChange[0]; p.y += coordChange[1]; p.dir = dir; }
public static void printPlayer(SnakePlayer p, string name) { Debug.Log(name + ": " + p.x + "." + p.y + " " + p.dir); }
private void parsePlayer(JSONObject jPlayer, ref SnakePlayer p) { p.x = (int)jPlayer.GetField(Constants.JProtocol.posX).n; p.y = (int)jPlayer.GetField(Constants.JProtocol.posY).n; p.dir = jPlayer.GetField(Constants.JProtocol.dir).str; }