Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TicTacToe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Peter Schweers
TicTacToe
Commits
64939e23
Commit
64939e23
authored
2 months ago
by
jan-schw
Browse files
Options
Downloads
Patches
Plain Diff
add custom event handler for victories and draws
parent
4b2be6e0
No related branches found
Branches containing commit
No related tags found
2 merge requests
!6
Add final version of the game to main branch
,
!5
Add basic implementation
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.idea/inspectionProfiles/Project_Default.xml
+8
-0
8 additions, 0 deletions
.idea/inspectionProfiles/Project_Default.xml
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
+34
-5
34 additions, 5 deletions
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
with
42 additions
and
5 deletions
.idea/inspectionProfiles/Project_Default.xml
0 → 100644
+
8
−
0
View file @
64939e23
<component
name=
"InspectionProjectProfileManager"
>
<profile
version=
"1.0"
>
<option
name=
"myName"
value=
"Project Default"
/>
<inspection_tool
class=
"TrivialIf"
enabled=
"true"
level=
"WARNING"
enabled_by_default=
"true"
>
<option
name=
"ignoreChainedIf"
value=
"true"
/>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
+
34
−
5
View file @
64939e23
package
com.example.tictactoe_game
;
import
javafx.application.Application
;
import
javafx.event.EventHandler
;
import
javafx.scene.Scene
;
import
javafx.scene.control.Button
;
import
javafx.scene.layout.GridPane
;
import
javafx.scene.layout.VBox
;
import
javafx.scene.text.Text
;
import
javafx.stage.Modality
;
import
javafx.stage.Stage
;
public
class
TicTacToeFX
extends
Application
{
...
...
@@ -14,6 +18,30 @@ public class TicTacToeFX extends Application {
@Override
public
void
start
(
Stage
primaryStage
)
{
EventHandler
<
GameEvent
>
handler
=
new
EventHandler
<
GameEvent
>()
{
@Override
public
void
handle
(
GameEvent
gameEvent
)
{
String
currentPlayer
=
isXTurn
?
"X"
:
"O"
;
final
Stage
dialog
=
new
Stage
();
dialog
.
initModality
(
Modality
.
APPLICATION_MODAL
);
dialog
.
initOwner
(
primaryStage
);
VBox
dialogVbox
=
new
VBox
(
20
);
if
(
gameEvent
.
getEventType
()
==
GameEvent
.
VICTORY_EVENT
)
{
dialogVbox
.
getChildren
().
add
(
new
Text
(
"This is a victory for player "
+
currentPlayer
+
"!"
));
}
else
if
(
gameEvent
.
getEventType
()
==
GameEvent
.
DRAW_EVENT
)
{
dialogVbox
.
getChildren
().
add
(
new
Text
(
"This is a Draw!"
));
}
Scene
dialogScene
=
new
Scene
(
dialogVbox
,
150
,
150
);
dialog
.
setScene
(
dialogScene
);
dialog
.
show
();
}
};
// Erstelle das das Spielfeld
GridPane
grid
=
new
GridPane
();
...
...
@@ -24,7 +52,8 @@ public class TicTacToeFX extends Application {
button
.
setPrefSize
(
100
,
100
);
// Größe der Buttons
grid
.
add
(
button
,
col
,
row
);
// Hinzufügen zum Grid
buttons
[
row
][
col
]
=
button
;
button
.
addEventHandler
(
GameEvent
.
VICTORY_EVENT
,
handler
);
button
.
addEventHandler
(
GameEvent
.
DRAW_EVENT
,
handler
);
button
.
setOnAction
(
event
->
handleButtonClick
(
button
));
}
}
...
...
@@ -40,17 +69,17 @@ public class TicTacToeFX extends Application {
if
(
button
.
getText
().
isEmpty
())
{
if
(
this
.
isXTurn
)
{
button
.
setText
(
"X"
);
this
.
isXTurn
=
false
;
}
else
{
button
.
setText
(
"O"
);
this
.
isXTurn
=
true
;
}
if
(
checkForWin
())
{
button
s
[
1
][
1
].
setText
(
"juhu"
);
button
.
fireEvent
(
new
GameEvent
(
GameEvent
.
VICTORY_EVENT
)
);
}
else
if
(
checkForDraw
()){
button
s
[
1
][
1
].
setText
(
"draw"
);
button
.
fireEvent
(
new
GameEvent
(
GameEvent
.
DRAW_EVENT
)
);
}
this
.
isXTurn
=
!
this
.
isXTurn
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment