Package com.neven.ticTacToeGame.model
Class Game
- java.lang.Object
-
- com.neven.ticTacToeGame.model.Game
-
- Direct Known Subclasses:
PlayerVsAlgorithmGame
,PlayerVsPlayerGame
,PlayerVsRandomGame
public abstract class Game extends java.lang.Object
ClassGame
is an abstract class which contain main logic for the games. All child classes should realize 2 abstract methods(makeFirstMove()
,makeMove()
), realization of this methods should be depended on type of the current game in which this method is implemented.Main logical method of the games is
winningCheckAndMakingMove(int)
- Author:
- Arterm Koliushko, https://www.linkedin.com/in/artem-koliushko/
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Integer,java.lang.String>
cells
Field withMap<Integer, String>
value, is theHashMap
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 containString
value of the current user.
Has access level "PROTECTED" for setter.private boolean
draw
Field which containBoolean
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 containBoolean
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 containBoolean
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 containBoolean
value is the player 2 is win.
Default value "false".
-
Constructor Summary
Constructors Constructor Description Game(java.lang.String player_2)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private boolean
isItDraw()
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 extendedGame
should make first move.protected abstract void
makeMove()
The abstract method should consist the logic how exactly the extendedGame
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.
-
-
-
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 containString
value of the current user.
Has access level "PROTECTED" for setter.
-
player1win
private boolean player1win
Field which containBoolean
value is the user is win.
Default value "false".
Has access level "PROTECTED" for setter.
-
player2win
private boolean player2win
Field which containBoolean
value is the player 2 is win.
Default value "false".
Has access level "PROTECTED" for setter.
-
draw
private boolean draw
Field which containBoolean
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 containBoolean
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 withMap<Integer, String>
value, is theHashMap
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 valuesplayer1
,player2
,emptyCell
)
Has access level "PRIVATE" for setter.
-
-
Constructor Detail
-
Game
public Game(java.lang.String player_2)
Constructor for classGame
.
Constructor initializedplayer2
field by incoming param,currentPlayer
field byplayer1
value andcells
by filling all game filed withemptyCell
- 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 callisPlayerWin(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 callisItDraw()
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 isPlayerVsRandomGame
, methodswitchCurrentPlayer()
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 methodsisPlayerWin(String currentPlayer)
andisItDraw()
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, returnedGame
also will contain the result of the game.
-
switchCurrentPlayer
protected void switchCurrentPlayer()
The method is switch the current player. Changing depends oncurrentPlayer
, ifcurrentPlayer
equals toplayer1
the method setcurrentPlayer
asplayer2
, and ifcurrentPlayer
equals toplayer2
the method setcurrentPlayer
asplayer1
-
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 lineBoolean
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.
-
makeFirstMove
public abstract Game makeFirstMove()
-
makeMove
protected abstract void makeMove()
The abstract method should consist the logic how exactly the extendedGame
should make move.
Logically move should be made byplayer2
and the result of the method should an empty cell on game field filled byplayer2
.
!!! Not for Player versus Player games likePlayerVsPlayerGame
, logically in this case the method should just call theswitchCurrentPlayer()
method.
-
-