Class PlayerVsAlgorithmController


  • @Controller
    @RequestMapping("/game/algorithm")
    @SessionAttributes({"game","statistic"})
    public class PlayerVsAlgorithmController
    extends java.lang.Object
    Class PlayerVsAlgorithmController is a Spring Boot Controller class witch allow to serve HTTP requests from pages with Player versus Algorithm game. Class has injected statistic service(StatisticService) for showing statistic for games with an algorithm(MiniMax) on game page.
    Path for requests "{pageContext}/game/algorithm"
    Author:
    Arterm Koliushko, https://www.linkedin.com/in/artem-koliushko/
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String ALGORITHM_START_NAME
      Constant with a String to verify does the algorithm start the game.
      private static java.lang.String GAME_MODEL_ATTRIBUTE_NAME
      Constant with a name of the model attribute for game.
      private static java.lang.String GAME_PAGE_NAME
      Constant with a name of the player versus algorithm game view.
      private static java.lang.String GAME_START_PAGE_NAME
      Constant with a name of the view where user should make a decision who will start the game, user or algorithm.
      private static java.lang.String STATISTIC_MODEL_ATTRIBUTE_NAME
      Constant with a name of the model attribute for statistic.
      private StatisticService statisticService
      This is an injected statistic service(StatisticService) witch allow to get(from DB) and update(save to DB) statistic for games with an algorithm(MiniMax).
      private static java.lang.String USER_START_NAME
      Constant with a String to verify does a user start the game.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) org.springframework.web.servlet.ModelAndView game​(org.springframework.ui.Model model, int cell)
      Allow to serve HTTP requests for requests "{pageContext}/game/algorithm" by method POST.
      (package private) org.springframework.web.servlet.ModelAndView getGameWithAlgorithm()
      Allow to serve HTTP requests for requests "{pageContext}/game/algorithm" by method GET.
      (package private) org.springframework.web.servlet.ModelAndView startGameWithAlgorithm​(java.lang.String whoStart)
      Allow to serve HTTP requests for requests "{pageContext}/game/algorithm/{whoStart}" by method GET.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • GAME_START_PAGE_NAME

        private static final java.lang.String GAME_START_PAGE_NAME
        Constant with a name of the view where user should make a decision who will start the game, user or algorithm.
        See Also:
        Constant Field Values
      • GAME_PAGE_NAME

        private static final java.lang.String GAME_PAGE_NAME
        Constant with a name of the player versus algorithm game view.
        See Also:
        Constant Field Values
      • GAME_MODEL_ATTRIBUTE_NAME

        private static final java.lang.String GAME_MODEL_ATTRIBUTE_NAME
        Constant with a name of the model attribute for game.
        See Also:
        Constant Field Values
      • STATISTIC_MODEL_ATTRIBUTE_NAME

        private static final java.lang.String STATISTIC_MODEL_ATTRIBUTE_NAME
        Constant with a name of the model attribute for statistic.
        See Also:
        Constant Field Values
      • USER_START_NAME

        private static final java.lang.String USER_START_NAME
        Constant with a String to verify does a user start the game.
        See Also:
        Constant Field Values
      • ALGORITHM_START_NAME

        private static final java.lang.String ALGORITHM_START_NAME
        Constant with a String to verify does the algorithm start the game.
        See Also:
        Constant Field Values
      • statisticService

        private final StatisticService statisticService
        This is an injected statistic service(StatisticService) witch allow to get(from DB) and update(save to DB) statistic for games with an algorithm(MiniMax).
    • Constructor Detail

      • PlayerVsAlgorithmController

        public PlayerVsAlgorithmController​(StatisticService statisticService)
        Create class with an injection of statistic service for games with an algorithm(MiniMax).
        Parameters:
        statisticService - Injection of StatisticService class
    • Method Detail

      • getGameWithAlgorithm

        @GetMapping
        @ResponseBody
        org.springframework.web.servlet.ModelAndView getGameWithAlgorithm()
        Allow to serve HTTP requests for requests "{pageContext}/game/algorithm" by method GET.
        Returns:
        ModelAndView class with mane of the view where user should make a decision who will start the game, user or algorithm.
      • startGameWithAlgorithm

        @GetMapping("/{whoStart}")
        org.springframework.web.servlet.ModelAndView startGameWithAlgorithm​(@PathVariable(name="whoStart")
                                                                            java.lang.String whoStart)
        Allow to serve HTTP requests for requests "{pageContext}/game/algorithm/{whoStart}" by method GET. Method will create the PlayerVsAlgorithmGame and depends on who will start it will call PlayerVsAlgorithmGame.makeFirstMove() method if game will be started by algorithm. Created game will be added as model attribute to ModelAndView with a name of player versus algorithm game page view. If {whoStart} parameter will be wrong, ModelAndView will return player versus algorithm game start page name.
        Parameters:
        whoStart - is a String value with an information who will start the game, "user" or "algorithm".
        Returns:
        ModelAndView with name of player versus algorithm game page view and PlayerVsAlgorithmGame in model.
      • game

        @PostMapping
        org.springframework.web.servlet.ModelAndView game​(org.springframework.ui.Model model,
                                                          @RequestParam("cell")
                                                          int cell)
        Allow to serve HTTP requests for requests "{pageContext}/game/algorithm" by method POST. Method receive PlayerVsAlgorithmGame from model with a number of the cell where user want to make a move. Method Game.winningCheckAndMakingMove(int) will be called with user move. PlayerVsAlgorithmGame with a Statistic will be returned in ModelAndView with name of player versus algorithm game page view. If game will be finished, game Statistic will be updated and returned PlayerVsAlgorithmGame will care the result.
        Parameters:
        model - Spring framework interface which care PlayerVsAlgorithmGame ang Statistic as session attributes.
        cell - Number of cell on game field with acceptable values 1-9 where user want to make his move
        Returns:
        ModelAndView with name of player versus algorithm game page view and PlayerVsAlgorithmGame in model.