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
50d95328
Commit
50d95328
authored
2 months ago
by
jan-schw
Browse files
Options
Downloads
Patches
Plain Diff
add logic for victory conditions
parent
79b4a3a1
No related branches found
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
+74
-2
74 additions, 2 deletions
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
with
74 additions
and
2 deletions
src/main/java/com/example/tictactoe_game/TicTacToeFX.java
+
74
−
2
View file @
50d95328
...
@@ -8,6 +8,10 @@ import javafx.stage.Stage;
...
@@ -8,6 +8,10 @@ import javafx.stage.Stage;
public
class
TicTacToeFX
extends
Application
{
public
class
TicTacToeFX
extends
Application
{
private
Button
[][]
buttons
=
new
Button
[
3
][
3
];
private
boolean
isXTurn
=
true
;
@Override
@Override
public
void
start
(
Stage
primaryStage
)
{
public
void
start
(
Stage
primaryStage
)
{
// Erstelle das das Spielfeld
// Erstelle das das Spielfeld
...
@@ -19,16 +23,84 @@ public class TicTacToeFX extends Application {
...
@@ -19,16 +23,84 @@ public class TicTacToeFX extends Application {
Button
button
=
new
Button
();
Button
button
=
new
Button
();
button
.
setPrefSize
(
100
,
100
);
// Größe der Buttons
button
.
setPrefSize
(
100
,
100
);
// Größe der Buttons
grid
.
add
(
button
,
col
,
row
);
// Hinzufügen zum Grid
grid
.
add
(
button
,
col
,
row
);
// Hinzufügen zum Grid
buttons
[
row
][
col
]
=
button
;
button
.
setOnAction
(
event
->
handleButtonClick
(
button
));
}
}
}
}
// Szene und Stage erstellen
// Szene und Stage erstellen
Scene
scene
=
new
Scene
(
grid
,
300
,
300
);
// Größe der Szene
Scene
scene
1
=
new
Scene
(
grid
,
300
,
300
);
// Größe der Szene
primaryStage
.
setTitle
(
"Tic-Tac-Toe"
);
primaryStage
.
setTitle
(
"Tic-Tac-Toe"
);
primaryStage
.
setScene
(
scene
);
primaryStage
.
setScene
(
scene
1
);
primaryStage
.
show
();
primaryStage
.
show
();
}
}
private
void
handleButtonClick
(
Button
button
)
{
if
(
button
.
getText
().
isEmpty
())
{
if
(
this
.
isXTurn
)
{
button
.
setText
(
"X"
);
this
.
isXTurn
=
false
;
}
else
{
button
.
setText
(
"O"
);
this
.
isXTurn
=
true
;
}
if
(
checkForWin
())
{
buttons
[
1
][
1
].
setText
(
"juhu"
);
}
else
if
(
checkForDraw
()){
buttons
[
1
][
1
].
setText
(
"draw"
);
}
}
}
private
boolean
checkForWin
(){
for
(
int
row
=
0
;
row
<
3
;
row
++){
String
text1
=
buttons
[
row
][
0
].
getText
();
String
text2
=
buttons
[
row
][
1
].
getText
();
String
text3
=
buttons
[
row
][
2
].
getText
();
if
(!
text1
.
isEmpty
()
&&
text1
.
equals
(
text2
)
&&
text1
.
equals
(
text3
)){
return
true
;
}
}
for
(
int
col
=
0
;
col
<
3
;
col
++){
String
text1
=
buttons
[
0
][
col
].
getText
();
String
text2
=
buttons
[
1
][
col
].
getText
();
String
text3
=
buttons
[
2
][
col
].
getText
();
if
(!
text1
.
isEmpty
()
&&
text1
.
equals
(
text2
)
&&
text1
.
equals
(
text3
)){
return
true
;
}
}
String
upperLeftCorner
=
buttons
[
0
][
0
].
getText
();
String
center
=
buttons
[
1
][
1
].
getText
();
String
lowerRightCorner
=
buttons
[
2
][
2
].
getText
();
String
upperRightCorner
=
buttons
[
0
][
2
].
getText
();
String
lowerLeftCorner
=
buttons
[
2
][
0
].
getText
();
if
(!
upperLeftCorner
.
isEmpty
()
&&
upperLeftCorner
.
equals
(
center
)
&&
upperLeftCorner
.
equals
(
lowerRightCorner
)){
return
true
;
}
if
(!
upperRightCorner
.
isEmpty
()
&&
upperRightCorner
.
equals
(
center
)
&&
upperRightCorner
.
equals
(
lowerLeftCorner
)){
return
true
;
}
return
false
;
}
private
boolean
checkForDraw
(){
for
(
int
row
=
0
;
row
<
3
;
row
++){
for
(
int
col
=
0
;
col
<
3
;
col
++){
if
(
buttons
[
row
][
col
].
getText
().
isEmpty
()){
return
false
;
}
}
}
return
true
;
}
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
launch
(
args
);
launch
(
args
);
}
}
...
...
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