Implement Number Guessing Game

This commit is contained in:
Alan Moon 2025-06-27 12:56:35 -07:00
parent d736420986
commit f90f2399cf
2 changed files with 47 additions and 0 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using qtc_api.Services.CurrencyGamesService; using qtc_api.Services.CurrencyGamesService;
using System.Security.Claims; using System.Security.Claims;
using static qtc_api.Services.CurrencyGamesService.CurrencyGamesService;
namespace qtc_api.Controllers namespace qtc_api.Controllers
{ {
@ -16,6 +17,8 @@ namespace qtc_api.Controllers
_currencyGamesService = currencyGamesService; _currencyGamesService = currencyGamesService;
} }
// Stock Market
[HttpGet("stock-market/current-price")] [HttpGet("stock-market/current-price")]
public ActionResult<ServiceResponse<int>> GetCurrentPricePerStock() public ActionResult<ServiceResponse<int>> GetCurrentPricePerStock()
{ {
@ -91,5 +94,21 @@ namespace qtc_api.Controllers
} }
else return Ok(new ServiceResponse<UserStockActionResultDto> { Success = false, Message = "No Identity" }); else return Ok(new ServiceResponse<UserStockActionResultDto> { Success = false, Message = "No Identity" });
} }
// Number Guess
[HttpGet("number-guess/get-number")]
[Authorize]
public ActionResult<ServiceResponse<int>> GetRandomNumber()
{
return Ok(_currencyGamesService.GetRandomNumber());
}
[HttpGet("number-guess/guess-number")]
[Authorize]
public ActionResult<ServiceResponse<NumberGuessResult>> GuessNumber(int original, int guess)
{
return Ok(_currencyGamesService.GuessRandomNumber(original, guess));
}
} }
} }

View File

@ -86,11 +86,39 @@ namespace qtc_api.Services.CurrencyGamesService
} }
} }
// Number Guess
public ServiceResponse<int> GetRandomNumber()
{
Random rnd = new Random();
return new ServiceResponse<int> { Success = true, Data = rnd.Next(1, 500) };
}
public ServiceResponse<NumberGuessResult> GuessRandomNumber(int original, int guess)
{
var response = new ServiceResponse<NumberGuessResult> { Success = true };
if (original - guess == 10) response.Data = NumberGuessResult.Lower;
else if (guess - original == 10) response.Data = NumberGuessResult.Higher;
else if (guess == original) response.Data = NumberGuessResult.Correct;
else response.Data = NumberGuessResult.Incorrect;
return response;
}
private void Timer_Elapsed(object? state) private void Timer_Elapsed(object? state)
{ {
Random rnd = new Random(); Random rnd = new Random();
CurrentPricePerStock = rnd.Next(5, 200); CurrentPricePerStock = rnd.Next(5, 200);
logger.LogInformation($"Current Price Per Stock - {CurrentPricePerStock}"); logger.LogInformation($"Current Price Per Stock - {CurrentPricePerStock}");
} }
public enum NumberGuessResult
{
Higher,
Lower,
Correct,
Incorrect
}
} }
} }