Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Alexandre Ducarne
ai12-othello
Commits
907cf695
Commit
907cf695
authored
Dec 03, 2019
by
ZETEA Lucas
Browse files
added board creation and management
parent
12bfc5fd
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/main/java/client/game/GameCore.java
View file @
907cf695
package
client.game
;
import
java.io.IOException
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
client.game.controller.MainGameController
;
import
client.game.model.DataToGameImpl
;
import
client.game.model.GameModel
;
import
client.game.model.Square
;
import
common.interfaces.client.IGameToData
;
import
javafx.fxml.FXMLLoader
;
import
javafx.scene.Scene
;
import
javafx.scene.layout.BorderPane
;
import
javafx.stage.Stage
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
java.io.IOException
;
public
class
GameCore
{
/**
* Class logger
*/
private
static
final
Logger
LOGGER
=
LogManager
.
getLogger
(
GameCore
.
class
);
/**
* JavaFX stage
*/
private
Stage
fxStage
;
/**
* MainGameController reference
*/
private
MainGameController
controller
;
/**
* Reference to interface implementation for calls to the outside
*/
private
IGameToData
gameToData
;
/**
* Reference to interface implementation for calls from the outside
*/
private
DataToGameImpl
dataToGame
;
/**
* Class constructor
*/
public
GameCore
()
{
controller
=
new
MainGameController
();
controller
.
setCore
(
this
);
dataToGame
=
new
DataToGameImpl
(
this
);
}
/**
* Getter for the Game module main controller
*
* @return
*/
public
MainGameController
getMainGameController
()
{
return
controller
;
}
/**
* Resets the FX stage content in order to put the game module one inside.
*
* @param fxmlResource
* @param stage
* @throws IOException
*/
public
void
initializeFxStage
(
String
fxmlResource
,
Stage
stage
)
throws
IOException
{
fxStage
=
stage
;
FXMLLoader
fxmlLoader
=
new
FXMLLoader
(
getClass
().
getResource
(
fxmlResource
));
fxmlLoader
.
setControllerFactory
(
c
->
{
return
controller
;
});
BorderPane
root
=
fxmlLoader
.
load
();
Scene
scene
=
new
Scene
(
root
,
1200
,
800
);
scene
.
getStylesheets
().
add
(
"view/game.css"
);
fxStage
.
setScene
(
scene
);
fxStage
.
centerOnScreen
();
}
/**
* Getter for gameToData implementation
*
* @return
*/
public
final
IGameToData
getGameToData
()
{
return
gameToData
;
}
/**
* Setter for gameToData interface implementation
*
* @param gameToData
*/
public
final
void
setGameToData
(
IGameToData
gameToData
)
{
this
.
gameToData
=
gameToData
;
}
/**
* Getter for dataToGame implementation
*
* @return
*/
public
final
DataToGameImpl
getDataToGame
()
{
return
dataToGame
;
}
/**
* Class logger
*/
private
static
final
Logger
LOGGER
=
LogManager
.
getLogger
(
GameCore
.
class
);
/**
* JavaFX stage
*/
private
Stage
fxStage
;
/**
* MainGameController reference
*/
private
MainGameController
controller
;
/**
* Reference to interface implementation for calls to the outside
*/
private
IGameToData
gameToData
;
/**
* Reference to interface implementation for calls from the outside
*/
private
DataToGameImpl
dataToGame
;
/**
* Reference to the game model
*/
private
GameModel
gameModel
;
/**
* Class constructor
*/
public
GameCore
()
{
gameModel
=
new
GameModel
();
controller
=
new
MainGameController
();
controller
.
setCore
(
this
);
dataToGame
=
new
DataToGameImpl
(
this
);
}
/**
* Getter for the Game module main controller
*
* @return
*/
public
MainGameController
getMainGameController
()
{
return
controller
;
}
/**
* Resets the FX stage content in order to put the game module one inside.
*
* @param fxmlResource
* @param stage
* @throws IOException
*/
public
void
initializeFxStage
(
String
fxmlResource
,
Stage
stage
)
throws
IOException
{
fxStage
=
stage
;
FXMLLoader
fxmlLoader
=
new
FXMLLoader
(
getClass
().
getResource
(
fxmlResource
));
fxmlLoader
.
setControllerFactory
(
c
->
{
return
controller
;
});
BorderPane
root
=
fxmlLoader
.
load
();
Scene
scene
=
new
Scene
(
root
,
1200
,
750
);
scene
.
getStylesheets
().
add
(
"view/game.css"
);
fxStage
.
setScene
(
scene
);
fxStage
.
centerOnScreen
();
fxStage
.
setResizable
(
false
);
}
/**
* Getter for gameToData implementation
*
* @return
*/
public
final
IGameToData
getGameToData
()
{
return
gameToData
;
}
/**
* Setter for gameToData interface implementation
*
* @param gameToData
*/
public
final
void
setGameToData
(
IGameToData
gameToData
)
{
this
.
gameToData
=
gameToData
;
}
/**
* Getter for dataToGame implementation
*
* @return
*/
public
final
DataToGameImpl
getDataToGame
()
{
return
dataToGame
;
}
/**
* @return the gameModel
*/
public
GameModel
getGameModel
()
{
return
gameModel
;
}
/**
* @param gameModel the gameModel to set
*/
public
void
setGameModel
(
GameModel
gameModel
)
{
this
.
gameModel
=
gameModel
;
}
/**
* Is called when the player clicks a SquareShape, then uses the playMove method
* from GameToData to process the move. This method also asks the controller to
* freeze the board meanwhile
*
* @param sq
*/
public
void
playMove
(
Square
sq
)
{
System
.
out
.
println
(
"Moved played ["
+
sq
.
getX
()
+
" / "
+
sq
.
getY
()
+
"]"
);
controller
.
freezeBoard
(
true
);
// TODO gameToData.playMove(sq.getX(), sq.getY(), -UUID/UserZero-);
}
}
src/main/java/client/game/controller/MainGameController.java
View file @
907cf695
package
client.game.controller
;
import
java.text.SimpleDateFormat
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
client.game.GameCore
;
import
client.game.model.Square
;
import
client.game.view.SquareShape
;
import
common.dataModel.Message
;
import
javafx.collections.ListChangeListener
;
import
javafx.fxml.FXML
;
import
javafx.scene.control.Button
;
import
javafx.scene.control.TabPane
;
...
...
@@ -9,10 +17,7 @@ import javafx.scene.control.TextArea;
import
javafx.scene.control.Tooltip
;
import
javafx.scene.input.KeyCode
;
import
javafx.scene.input.KeyEvent
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
java.text.SimpleDateFormat
;
import
javafx.scene.layout.GridPane
;
/**
* IHM-Game main controller class.
...
...
@@ -20,137 +25,192 @@ import java.text.SimpleDateFormat;
* Links the FXML elements to the correct methods in the specialized controllers
*/
public
class
MainGameController
{
/**
* Class logger
*/
private
static
final
Logger
LOGGER
=
LogManager
.
getLogger
(
MainGameController
.
class
);
@FXML
Button
openButton
;
@FXML
TextArea
areaSendMessage
;
@FXML
TextArea
areaChatHistory
;
@FXML
TabPane
sideRightTabPane
;
/**
* Side controller
*/
private
MainGameControllerSide
sideController
;
/**
* Board controller
*/
private
MainGameControllerBoard
boardController
;
/**
* Application game core
*/
private
GameCore
gameCore
;
/**
* Controller constructor, instantiates the secondary controllers
*/
public
MainGameController
()
{
sideController
=
new
MainGameControllerSide
();
boardController
=
new
MainGameControllerBoard
();
}
/**
* Sets the module Core reference
*
* @param core
*/
public
void
setCore
(
GameCore
core
)
{
gameCore
=
core
;
sideController
.
setCore
(
core
);
}
/**
* Method called after the display of the game Java FX scene
*/
public
void
initialize
()
{
// Remove access to the chat panel if the game configuration says so
if
(!
gameCore
.
getDataToGame
().
isChatAuthorized
())
{
sideRightTabPane
.
getTabs
().
get
(
1
).
setDisable
(
true
);
sideRightTabPane
.
getTabs
().
get
(
1
)
.
setTooltip
(
new
Tooltip
(
"La discussion Chat est désactivée pour cette partie."
));
}
}
/**
* Called upon a click on the send button
*/
public
void
onSendMessage
()
{
if
(!
areaSendMessage
.
getText
().
isEmpty
())
{
sendMessage
();
}
}
/**
* Displays a message to the chat history area after its receiving.
*/
public
void
newMessageToHistory
(
Message
m
)
{
LOGGER
.
debug
(
"Message received -> "
+
m
.
getMessage
());
String
time
=
new
SimpleDateFormat
(
"HH:mm:ss"
).
format
(
m
.
getHourMessage
());
areaChatHistory
.
appendText
(
"---- "
+
m
.
getAuthor
().
getPseudo
()
+
" à "
+
time
+
" ----"
);
areaChatHistory
.
appendText
(
"\n"
);
areaChatHistory
.
appendText
(
"> "
+
m
.
getMessage
());
areaChatHistory
.
appendText
(
"\n"
);
}
/**
* Called upon a click on the forfeit button
*/
public
void
onForfeit
()
{
LOGGER
.
debug
(
"Forfeit"
);
}
/**
* ActionKeyPressed event managed by the areaSendMessage TextArea.
* <p>
* It is used to listen to Enter key, where we want to send a message when Enter
* is pressed
*
* @param event: KeyEvent created upon the key pressed action
*/
@FXML
public
void
onActionKeyPressed
(
KeyEvent
event
)
{
/*
* If there is no message and we press enter, we do not want to send an empty
* message. In that case we consume the event, and do nothing afterwards.
*/
if
(
areaSendMessage
.
getText
().
trim
().
isEmpty
()
&&
event
.
getCode
()
==
KeyCode
.
ENTER
)
{
areaSendMessage
.
clear
();
event
.
consume
();
}
/*
* If the message is not empty, we send it if enter is pressed. We check if
* shift is down, if it is the case, the message will not be send, but a new
* line is added to the text area.
*/
else
if
(
event
.
getCode
()
==
KeyCode
.
ENTER
)
{
event
.
consume
();
if
(
event
.
isShiftDown
())
{
// New line
areaSendMessage
.
appendText
(
System
.
getProperty
(
"line.separator"
));
}
else
{
sendMessage
();
// New message
}
}
}
/**
* Calls for the main SendMessage in the side controller and clears the message
* in the TextArea.
*/
private
void
sendMessage
()
{
sideController
.
onSendMessage
(
areaSendMessage
.
getText
().
trim
());
areaSendMessage
.
clear
();
}
/**
* Class logger
*/
private
static
final
Logger
LOGGER
=
LogManager
.
getLogger
(
MainGameController
.
class
);
@FXML
Button
openButton
;
@FXML
TextArea
areaSendMessage
;
@FXML
TextArea
areaChatHistory
;
@FXML
TabPane
sideRightTabPane
;
@FXML
GridPane
gameBoard
;
/**
* Side controller
*/
private
MainGameControllerSide
sideController
;
/**
* Board controller
*/
private
MainGameControllerBoard
boardController
;
/**
* Application game core
*/
private
GameCore
gameCore
;
/**
* Controller constructor, instantiates the secondary controllers
*/
public
MainGameController
()
{
sideController
=
new
MainGameControllerSide
();
boardController
=
new
MainGameControllerBoard
();
}
/**
* Sets the module Core reference
*
* @param core
*/
public
void
setCore
(
GameCore
core
)
{
gameCore
=
core
;
sideController
.
setCore
(
core
);
}
/**
* Method called after the display of the game Java FX scene
*/
public
void
initialize
()
{
// Remove access to the chat panel if the game configuration says so
if
(!
gameCore
.
getDataToGame
().
isChatAuthorized
())
{
sideRightTabPane
.
getTabs
().
get
(
1
).
setDisable
(
true
);
sideRightTabPane
.
getTabs
().
get
(
1
)
.
setTooltip
(
new
Tooltip
(
"La discussion Chat est désactivée pour cette partie."
));
}
initModelBiding
();
gameCore
.
getGameModel
().
buildBoardSquares
();
}
/**
* Called upon a click on the send button
*/
public
void
onSendMessage
()
{
if
(!
areaSendMessage
.
getText
().
isEmpty
())
{
sendMessage
();
}
}
/**
* Displays a message to the chat history area after its receiving.
*/
public
void
newMessageToHistory
(
Message
m
)
{
LOGGER
.
debug
(
"Message received -> "
+
m
.
getMessage
());
String
time
=
new
SimpleDateFormat
(
"HH:mm:ss"
).
format
(
m
.
getHourMessage
());
areaChatHistory
.
appendText
(
"---- "
+
m
.
getAuthor
().
getPseudo
()
+
" à "
+
time
+
" ----"
);
areaChatHistory
.
appendText
(
"\n"
);
areaChatHistory
.
appendText
(
"> "
+
m
.
getMessage
());
areaChatHistory
.
appendText
(
"\n"
);
}
/**
* Called upon a click on the forfeit button
*/
public
void
onForfeit
()
{
LOGGER
.
debug
(
"Forfeit"
);
int
board
[][]
=
new
int
[
8
][
8
];
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
for
(
int
j
=
0
;
j
<
8
;
j
++)
{
board
[
i
][
j
]
=
0
;
}
}
board
[
0
][
0
]
=
1
;
board
[
1
][
1
]
=
1
;
board
[
0
][
7
]
=
2
;
board
[
7
][
7
]
=
2
;
gameCore
.
getGameModel
().
update
(
board
);
}
/**
* ActionKeyPressed event managed by the areaSendMessage TextArea.
* <p>
* It is used to listen to Enter key, where we want to send a message when Enter
* is pressed
*
* @param event: KeyEvent created upon the key pressed action
*/
@FXML
public
void
onActionKeyPressed
(
KeyEvent
event
)
{
/*
* If there is no message and we press enter, we do not want to send an empty
* message. In that case we consume the event, and do nothing afterwards.
*/
if
(
areaSendMessage
.
getText
().
trim
().
isEmpty
()
&&
event
.
getCode
()
==
KeyCode
.
ENTER
)
{
areaSendMessage
.
clear
();
event
.
consume
();
}
/*
* If the message is not empty, we send it if enter is pressed. We check if
* shift is down, if it is the case, the message will not be send, but a new
* line is added to the text area.
*/
else
if
(
event
.
getCode
()
==
KeyCode
.
ENTER
)
{
event
.
consume
();
if
(
event
.
isShiftDown
())
{
// New line
areaSendMessage
.
appendText
(
System
.
getProperty
(
"line.separator"
));
}
else
{
sendMessage
();
// New message
}
}
}
/**
* Calls for the main SendMessage in the side controller and clears the message
* in the TextArea.
*/
private
void
sendMessage
()
{
sideController
.
onSendMessage
(
areaSendMessage
.
getText
().
trim
());
areaSendMessage
.
clear
();
}
/**
* Initiates the model binding. First it creates the response function, then it
* ads it as a listener.
*/
private
void
initModelBiding
()
{
ListChangeListener
<
Square
>
f
;
f
=
change
->
{
while
(
change
.
next
())
{
if
(
change
.
wasRemoved
())
{
gameBoard
.
getChildren
().
remove
(
change
.
getRemoved
().
get
(
0
));
}
if
(
change
.
wasAdded
())
{
SquareShape
sq
=
new
SquareShape
(
change
.
getAddedSubList
().
get
(
0
),
gameCore
);
gameBoard
.
add
(
sq
,
change
.
getAddedSubList
().
get
(
0
).
getX
(),
change
.
getAddedSubList
().
get
(
0
).
getY
());
sq
.
getRect
().
widthProperty
().
bind
(
gameBoard
.
widthProperty
().
divide
(
8
));
sq
.
getRect
().
heightProperty
().
bind
(
gameBoard
.
widthProperty
().
divide
(
8
));
}
}
};
gameCore
.
getGameModel
().
getSquareList
().
addListener
(
f
);
}
/**
* Disables or enables the gameBoard. Called after the player made a move and
* after the board update
*
* @param doFreeze
*/
public
void
freezeBoard
(
boolean
doFreeze
)
{
gameBoard
.
setDisable
(
doFreeze
);
}
}
src/main/java/client/game/model/DataToGameImpl.java
View file @
907cf695
package
client.game.model
;
import
java.io.IOException
;
import
java.util.UUID
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
client.game.GameCore
;
import
common.dataModel.*
;
import
common.dataModel.Board
;
import
common.dataModel.GameHeavy
;
import
common.dataModel.GameLight
;
import
common.dataModel.Message
;
import
common.dataModel.UserLight
;