Class Game

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.Integer,​java.lang.String> cells
      Field with Map<Integer, String> value, is the HashMap which contain game field cells.
      Firs param "Integer" - is the number of game field (proper values 1-9).
      private java.lang.String currentPlayer
      Field which contain String value of the current user.
      Has access level "PROTECTED" for setter.
      private boolean draw
      Field which contain Boolean value is the game is finished and game has no winner.
      private java.lang.String emptyCell
      Field with a name of the empty cell on the game field which uses to fill the game field and checking draws and making moves.
      private boolean incorrectMove
      Field which contain Boolean value is the move made by user is incorrect.
      private java.lang.String player1
      Field with a name of the player(always user) which uses to fill the game field and checking wins.
      private boolean player1win
      Field which contain Boolean value is the user is win.
      Default value "false".
      private java.lang.String player2
      Field with a name of the player, depend on type of the game and initialized in constructor, which uses to fill the game field and checking wins.
      private boolean player2win
      Field which contain Boolean value is the player 2 is win.
      Default value "false".
    • Constructor Summary

      Constructors 
      Constructor Description
      Game​(java.lang.String player_2)
      Constructor for class Game.
      Constructor initialized player2 field by incoming param, currentPlayer field by player1 value and cells by filling all game filed with emptyCell
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean isItDraw()
      The method check this Game has no winner and game field has no emptyCell left.
      protected boolean isPlayerWin​(@NotNull java.lang.String player)
      The method check is the user with name incoming as the parameter of the method has complete the line(in classic Tic Tac Toe game has 8 different lines, each of that lines consist from 3 game field cells).
      abstract Game makeFirstMove()
      The abstract method should consist the logic how exactly the extended Game should make first move.
      protected abstract void makeMove()
      The abstract method should consist the logic how exactly the extended Game should make move.
      private void setWinner()
      The method set current user as a winner.
      protected void switchCurrentPlayer()
      The method is switch the current player.
      Game winningCheckAndMakingMove​(int cellNumber)
      This method is the main logical method of the game.
      • Methods inherited from class java.lang.Object

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

      • player1

        private final java.lang.String player1
        Field with a name of the player(always user) which uses to fill the game field and checking wins.
        See Also:
        Constant Field Values
      • player2

        private final java.lang.String player2
        Field with a name of the player, depend on type of the game and initialized in constructor, which uses to fill the game field and checking wins.
      • emptyCell

        private final java.lang.String emptyCell
        Field with a name of the empty cell on the game field which uses to fill the game field and checking draws and making moves.
        See Also:
        Constant Field Values
      • currentPlayer

        private java.lang.String currentPlayer
        Field which contain String value of the current user.
        Has access level "PROTECTED" for setter.
      • player1win

        private boolean player1win
        Field which contain Boolean value is the user is win.
        Default value "false".
        Has access level "PROTECTED" for setter.
      • player2win

        private boolean player2win
        Field which contain Boolean value is the player 2 is win.
        Default value "false".
        Has access level "PROTECTED" for setter.
      • draw

        private boolean draw
        Field which contain Boolean value is the game is finished and game has no winner.
        Default value "false".
        Has access level "PROTECTED" for setter.
      • incorrectMove

        private boolean incorrectMove
        Field which contain Boolean value is the move made by user is incorrect.
        Default value "false".
        Has access level "PROTECTED" for setter.
      • cells

        private java.util.Map<java.lang.Integer,​java.lang.String> cells
        Field with Map<Integer, String> value, is the HashMap which contain game field cells.
        Firs param "Integer" - is the number of game field (proper values 1-9).
        Second param "String" - this is param which contain an information about value of game field (proper values player1, player2, emptyCell)
        Has access level "PRIVATE" for setter.
    • Constructor Detail

      • Game

        public Game​(java.lang.String player_2)
        Constructor for class Game.
        Constructor initialized player2 field by incoming param, currentPlayer field by player1 value and cells by filling all game filed with emptyCell
        Parameters:
        player_2 - String value of the second player(first always user) which depend on type of the created game.
    • Method Detail

      • winningCheckAndMakingMove

        public Game winningCheckAndMakingMove​(int cellNumber)
        This method is the main logical method of the game.
        The method is accept incoming param(user move) and check if this game field cell is empty:
         -If No, incorrectMove will be set to "true" and game will be returned.
         -If Yes, sell will be filled by the current user.
        Then method will call isPlayerWin(String currentPlayer) method for check is user does win:
         -If Yes, current user will be set as winner and game will be returned.
        Then method will call isItDraw() method to check did the user move was the last empty game field in this game:
         -If Yes, draw will be set to "true" and game will be returned.
        Then method depend no type of the game will:
         -If game type is PlayerVsRandomGame, method switchCurrentPlayer() will be called and game will be returned.
         -In all another cases current user will be switched(switchCurrentPlayer()) and game will make a move(makeMove()), then methods isPlayerWin(String currentPlayer) and isItDraw() will call(if any of this methods will return "true" value, result of the game will be set and game will be returned), then current user will be switched(switchCurrentPlayer()) again and game will be returned.
        Parameters:
        cellNumber - Integer value(acceptable 1-9) which contain a number of the game field cell where the user want to make a move.
        Returns:
        Game with a user move and second player move(second player depend on type of the game). If the game will be finished, returned Game also will contain the result of the game.
      • isPlayerWin

        protected boolean isPlayerWin​(@NotNull
                                      @NotNull java.lang.String player)
        The method check is the user with name incoming as the parameter of the method has complete the line(in classic Tic Tac Toe game has 8 different lines, each of that lines consist from 3 game field cells). Teh method set for each line Boolean result if the each cell of this line is equals to user with name incoming as the parameter of the method, then returned all the results.
        Parameters:
        player - String value of the name of the user, to check is this user is win.
        Returns:
        Boolean value is the user with name incoming as the parameter of the method has complete any of 8 possible lines to win.
      • setWinner

        private void setWinner()
        The method set current user as a winner.
      • isItDraw

        private boolean isItDraw()
        The method check this Game has no winner and game field has no emptyCell left.
        Returns:
        Boolean value if the Game has the draw.
      • makeFirstMove

        public abstract Game makeFirstMove()
        The abstract method should consist the logic how exactly the extended Game should make first move.
        Logically first move should be made by player2.
        Returns:
        the Game with a first move made by player2.
      • makeMove

        protected abstract void makeMove()
        The abstract method should consist the logic how exactly the extended Game should make move.
        Logically move should be made by player2 and the result of the method should an empty cell on game field filled by player2.
        !!! Not for Player versus Player games like PlayerVsPlayerGame, logically in this case the method should just call the switchCurrentPlayer() method.