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
83916419
Commit
83916419
authored
1 month ago
by
Marc Daginnus
Browse files
Options
Downloads
Patches
Plain Diff
Bugfix und hinzufügen von einem Restart Button
parent
5ed674b4
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
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
+61
-24
61 additions, 24 deletions
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
with
61 additions
and
24 deletions
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
+
61
−
24
View file @
83916419
...
...
@@ -29,63 +29,100 @@ public class TicTacToeFX extends Application {
public
void
start
(
Stage
primaryStage
)
{
EventHandler
<
GameEvent
>
gameEventHandler
=
gameEvent
->
{
String
currentPlayer
=
this
.
isXTurn
?
"X"
:
"O"
;
String
winner
=
this
.
isXTurn
?
"X"
:
"O"
;
// Dialogfenster erstellen
final
Stage
dialog
=
new
Stage
();
dialog
.
initModality
(
Modality
.
APPLICATION_MODAL
);
dialog
.
initOwner
(
primaryStage
);
VBox
dialogVbox
=
new
VBox
(
20
);
dialogVbox
.
setAlignment
(
Pos
.
CENTER
);
// Dialogtexte
if
(
gameEvent
.
getEventType
()
==
GameEvent
.
VICTORY_EVENT
)
{
dialogVbox
.
getChildren
().
add
(
new
Text
(
"This is a victory for player "
+
currentPlay
er
+
" in turn "
+
this
.
currentTurn
+
"!"
));
dialogVbox
.
getChildren
().
add
(
new
Text
(
"This is a victory for player "
+
winn
er
+
" in turn "
+
this
.
currentTurn
+
"!"
));
}
else
if
(
gameEvent
.
getEventType
()
==
GameEvent
.
DRAW_EVENT
)
{
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!"
));
}
dialogVbox
.
getChildren
().
add
(
new
Text
(
"This is a Draw!"
));
}
Button
closeButton
=
new
Button
();
closeButton
.
setText
(
"Close"
);
closeButton
.
setOnAction
(
event
->
primaryStage
.
close
());
dialogVbox
.
getChildren
().
add
(
closeButton
);
dialogVbox
.
setAlignment
(
Pos
.
CENTER
);
// Schaltfläche "Schließen"
Button
closeButton
=
new
Button
(
"Close"
);
closeButton
.
setOnAction
(
event
->
primaryStage
.
close
());
dialogVbox
.
getChildren
().
add
(
closeButton
);
Scene
dialogScene
=
new
Scene
(
dialogVbox
,
200
,
100
);
dialog
.
setScene
(
dialogScene
);
dialog
.
show
();
}
//Schaltfläche "Restart"
Button
restartButton
=
new
Button
(
"Restart"
);
restartButton
.
setOnAction
(
event
->
{
restartGame
();
dialog
.
close
();
});
dialogVbox
.
getChildren
().
add
(
restartButton
);
Scene
dialogScene
=
new
Scene
(
dialogVbox
,
300
,
200
);
dialog
.
setScene
(
dialogScene
);
dialog
.
show
();
};
//
Erstelle das das Spielfeld
//
Spielbrett erstellen
GridPane
grid
=
new
GridPane
();
// 3x3 Buttons hinzufügen
for
(
int
row
=
0
;
row
<
3
;
row
++)
{
for
(
int
col
=
0
;
col
<
3
;
col
++)
{
Button
button
=
new
Button
();
button
.
setPrefSize
(
100
,
100
);
// Größe der Buttons
grid
.
add
(
button
,
col
,
row
);
// Hinzufügen zum Grid
button
.
setPrefSize
(
100
,
100
);
grid
.
add
(
button
,
col
,
row
);
buttons
[
row
][
col
]
=
button
;
button
.
setOnAction
(
event
->
handleButtonClick
(
button
));
}
}
// Ereignisse für Sieg und Unentschieden hinzufügen
grid
.
addEventHandler
(
GameEvent
.
VICTORY_EVENT
,
gameEventHandler
);
grid
.
addEventHandler
(
GameEvent
.
DRAW_EVENT
,
gameEventHandler
);
currentPlayer
=
new
Text
();
currentPlayer
.
setText
(
"Current Player: "
+
(
isXTurn
?
"X"
:
"O"
));
// Texte für aktuellen Spieler und Züge
currentPlayer
=
new
Text
(
"Current Player: "
+
(
isXTurn
?
"X"
:
"O"
));
turnCounter
=
new
Text
(
"Current Turn: "
+
currentTurn
);
// Elemente dem Spielfeld hinzufügen
grid
.
add
(
currentPlayer
,
3
,
0
);
turnCounter
=
new
Text
();
turnCounter
.
setText
(
"Current Turn: "
+
currentTurn
);
grid
.
add
(
turnCounter
,
3
,
1
);
Button
restartButton
=
new
Button
(
"Restart"
);
restartButton
.
setOnAction
(
event
->
restartGame
());
VBox
vbox
=
new
VBox
(
10
);
vbox
.
setAlignment
(
Pos
.
CENTER
);
vbox
.
getChildren
().
addAll
(
grid
,
currentPlayer
,
turnCounter
,
restartButton
);
// Szene und Stage erstellen
Scene
scene1
=
new
Scene
(
grid
,
400
,
3
00
);
// Größe der Szene
Scene
scene1
=
new
Scene
(
vbox
,
400
,
4
00
);
primaryStage
.
setTitle
(
"Tic-Tac-Toe"
);
primaryStage
.
setScene
(
scene1
);
primaryStage
.
show
();
}
private
void
restartGame
(){
isXTurn
=
true
;
isGameOver
=
false
;
currentTurn
=
0
;
currentPlayer
.
setText
(
"Current Player: X"
);
turnCounter
.
setText
(
"Current Turn: 0"
);
for
(
int
i
=
0
;
i
<
3
;
i
++){
for
(
int
j
=
0
;
j
<
3
;
j
++){
buttons
[
i
][
j
].
setText
(
""
);
}
}
}
private
void
handleButtonClick
(
Button
button
)
{
if
(
button
.
getText
().
isEmpty
()
&&
!
this
.
isGameOver
)
{
if
(
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