public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("HTTP request for a specific board."); Guid gameID = new Guid(req.Query["gameID"]); Board.playerType playerType = (Board.playerType)System.Enum.Parse(typeof(Board.playerType), req.Query["playerType"]); Board requestedBoard = null; if (playerType == Board.playerType.playerOne) { requestedBoard = Board.boards[gameID][0]; } else if (playerType == Board.playerType.playerTwo) { requestedBoard = Board.boards[gameID][1]; } var response = JsonConvert.SerializeObject(requestedBoard.getBattleGround(), Formatting.Indented); return(new OkObjectResult(response)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Clear a specific board given the gameID."); string name = req.Query["gameID"]; Board.playerType playerType = (Board.playerType)Enum.Parse(typeof(Board.playerType), req.Query["playerType"]); Guid gameID = new Guid(req.Query["gameID"]); if (playerType == Board.playerType.playerOne) { playerType = Board.playerType.playerOne; AddShip.counters[gameID][0] = 0; Board.boards[gameID][0] = new Board(Board.boards[gameID][0].boardSize, playerType); } else if (playerType == Board.playerType.playerTwo) { AddShip.counters[gameID][1] = 0; playerType = Board.playerType.playerTwo; Board.boards[gameID][1] = new Board(Board.boards[gameID][1].boardSize, playerType); } return(new OkObjectResult("Board cleared.")); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Block till it is the requestin player's turn."); Guid gameID = Guid.Parse(req.Query["gameID"]); Board.playerType playerType = (Board.playerType)System.Enum.Parse(typeof(Board.playerType), req.Query["playerType"]); // Block till the previousTurn changes while (FireResponse.previousTurn[gameID] == playerType) { } ; return(new OkObjectResult(true)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("The player has hit a cell"); FireResponse response = new FireResponse(); Guid gameID = Guid.Parse(req.Query["gameID"]); Board.playerType playerType = (Board.playerType)Enum.Parse(typeof(Board.playerType), req.Query["playerType"]); int xPosition = Int32.Parse(req.Query["xPosition"]); int yPosition = Int32.Parse(req.Query["yPosition"]); int boardSize = Board.boards[gameID][0].boardSize; Board currentBoard = null; if (FireResponse.previousTurn[gameID] == playerType) { if (playerType == Board.playerType.playerOne) { response.playerTurn = Enum.GetName(typeof(Board.playerType), Board.playerType.playerTwo); } else if (playerType == Board.playerType.playerTwo) { response.playerTurn = Enum.GetName(typeof(Board.playerType), Board.playerType.playerOne); } // Return a JSON response var wrongPlayerResponse = JsonConvert.SerializeObject(response, Formatting.Indented); return((ActionResult) new OkObjectResult(wrongPlayerResponse)); } // check playerOne (index 0) and playerTwo (index 1) ship count if (AddShip.counters[gameID][0] != 5 || AddShip.counters[gameID][1] != 5) { // Return a JSON response var errorResponse = JsonConvert.SerializeObject(response, Formatting.Indented); return((ActionResult) new OkObjectResult(errorResponse)); } if (playerType == Board.playerType.playerOne) { currentBoard = Board.boards[gameID][1]; } else if (playerType == Board.playerType.playerTwo) { currentBoard = Board.boards[gameID][0]; } switch (currentBoard.getBattleGround()[xPosition, yPosition].shipState) { case Ship.ShipState.noShip: { currentBoard.getBattleGround()[xPosition, yPosition].shipState = Ship.ShipState.shipMiss; response.state = Ship.ShipState.shipMiss; response.wonGame = wonGame(currentBoard); changeTurn(response, gameID); break; } case Ship.ShipState.ship: { currentBoard.getBattleGround()[xPosition, yPosition].shipState = Ship.ShipState.shipHit; response.state = Ship.ShipState.shipHit; response.wonGame = wonGame(currentBoard); changeTurn(response, gameID); break; } case Ship.ShipState.shipHit: { /* This ship is already hit, the click is redundant. To get to this case, * the user must have clicked on the tile of a ship he's already hit. * */ response.state = Ship.ShipState.shipHit; response.wonGame = wonGame(currentBoard); keepTurn(response, gameID); break; } case Ship.ShipState.shipMiss: { // Return miss, you hit water! response.state = Ship.ShipState.shipMiss; response.wonGame = wonGame(currentBoard); keepTurn(response, gameID); break; } } // Return a JSON response var fireResponse = JsonConvert.SerializeObject(response, Formatting.Indented); return((ActionResult) new OkObjectResult(fireResponse)); }
public FireResponse(Board.playerType turn, Ship.ShipState state) { this.wonGame = false; this.state = state; }