Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Mathdoku
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
tmp1u19
Mathdoku
Commits
ec122b2c
Commit
ec122b2c
authored
5 years ago
by
tmp1u19
Browse files
Options
Downloads
Patches
Plain Diff
Class that is in charge of all the functionalities of the game, inclusive drawing.
parent
4ef9760f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Handler.java
+294
-0
294 additions, 0 deletions
Handler.java
with
294 additions
and
0 deletions
Handler.java
0 → 100644
+
294
−
0
View file @
ec122b2c
import
javafx.beans.value.ChangeListener
;
import
javafx.beans.value.ObservableValue
;
import
javafx.event.ActionEvent
;
import
javafx.event.EventHandler
;
import
javafx.geometry.Insets
;
import
javafx.scene.Node
;
import
javafx.scene.control.Button
;
import
javafx.scene.control.ButtonType
;
import
javafx.scene.input.MouseButton
;
import
javafx.scene.input.MouseEvent
;
import
javafx.scene.layout.Background
;
import
javafx.scene.layout.BackgroundFill
;
import
javafx.scene.layout.CornerRadii
;
import
javafx.scene.layout.GridPane
;
import
javafx.scene.paint.Color
;
public
class
Handler
{
private
Window
window
=
new
Window
();
private
int
n
=
4
;
private
int
[][]
cellValues
=
new
int
[
n
][
n
];
public
void
helpButton
(
Button
help
,
GridPane
grid
)
{
help
.
setOnAction
(
new
EventHandler
<
ActionEvent
>()
{
@Override
public
void
handle
(
ActionEvent
actionEvent
)
{
if
(
help
.
getText
().
equals
(
"Enable Help"
))
{
help
.
setText
(
"Disable Help"
);
highlight
(
grid
);
for
(
Node
cell
:
grid
.
getChildren
())
{
enableHighlight
((
Cell
)
cell
,
grid
);
}
}
else
{
help
.
setText
(
"Enable Help"
);
setTransparent
(
grid
);
}
}
});
}
public
void
submitButton
(
Button
submit
)
{
submit
.
setOnAction
(
new
EventHandler
<
ActionEvent
>()
{
@Override
public
void
handle
(
ActionEvent
actionEvent
)
{
for
(
int
i
=
0
;
i
<
getN
();
i
++)
{
for
(
int
j
=
0
;
j
<
getN
();
j
++)
{
System
.
out
.
print
(
getCellValues
()[
i
][
j
]);
}
System
.
out
.
println
();
}
if
(
verify
())
{
window
.
win
(
"Congratulations!"
,
"You won the game!"
);
}
else
{
window
.
win
(
"Too bad..."
,
"You lost the game"
);
}
}
});
}
public
void
clearBoard
(
Button
clear
,
GridPane
grid
)
{
clear
.
setOnAction
(
new
EventHandler
<
ActionEvent
>()
{
@Override
public
void
handle
(
ActionEvent
actionEvent
)
{
window
.
getAlertDialog
(
"Are you sure you want to clear the board?"
).
showAndWait
().
ifPresent
(
response
->
{
if
(
response
==
ButtonType
.
YES
)
{
clear
(
grid
);
}
});
}
});
}
public
void
doubleClick
(
Cell
cell
)
{
cell
.
getTextField
().
setOnMouseClicked
(
new
EventHandler
<
MouseEvent
>()
{
@Override
public
void
handle
(
MouseEvent
mouseEvent
)
{
if
(
mouseEvent
.
getButton
().
equals
(
MouseButton
.
PRIMARY
))
{
if
(
mouseEvent
.
getClickCount
()
==
2
)
{
window
.
showButtons
(
cell
,
getN
());
}
}
}
});
}
public
void
memoriseValues
(
Cell
cell
)
{
cell
.
getTextField
().
textProperty
().
addListener
(
new
ChangeListener
<
String
>()
{
@Override
public
void
changed
(
ObservableValue
<?
extends
String
>
observableValue
,
String
oldValue
,
String
newValue
)
{
if
(
cell
.
isNumeric
(
newValue
))
{
if
(
Integer
.
parseInt
(
newValue
)
>
getN
()
||
Integer
.
parseInt
(
newValue
)
==
0
)
{
cell
.
getTextField
().
clear
();
}
else
{
getCellValues
()[
cell
.
getRow
()][
cell
.
getColumn
()]
=
Integer
.
parseInt
(
newValue
);
}
}
else
if
(!
newValue
.
matches
(
"\\d*"
))
{
cell
.
getTextField
().
setText
(
newValue
.
replaceAll
(
"[^\\d]"
,
""
));
}
}
});
}
public
void
enableHighlight
(
Cell
cell
,
GridPane
grid
)
{
cell
.
getTextField
().
textProperty
().
addListener
(
new
ChangeListener
<
String
>()
{
@Override
public
void
changed
(
ObservableValue
<?
extends
String
>
observableValue
,
String
s
,
String
t1
)
{
setTransparent
(
grid
);
highlight
(
grid
);
}
});
}
public
void
draw
(
GridPane
grid
)
{
for
(
int
i
=
0
;
i
<
getN
();
i
++)
{
for
(
int
j
=
0
;
j
<
getN
();
j
++)
{
Cell
cell
=
new
Cell
(
getN
(),
i
,
j
);
drawLines
(
i
,
j
,
cell
);
doubleClick
(
cell
);
memoriseValues
(
cell
);
grid
.
add
(
cell
,
j
,
i
,
1
,
1
);
}
}
}
public
void
drawLines
(
int
i
,
int
j
,
Cell
cell
)
{
cell
.
setStyle
(
"-fx-border-color: black; -fx-border-width: 1 1 0 0;"
);
if
(
j
==
0
)
{
cell
.
setStyle
(
"-fx-border-color: black; -fx-border-width: 1 1 0 1;"
);
}
if
(
i
==
getN
()
-
1
&&
j
==
0
)
{
cell
.
setStyle
(
"-fx-border-color: black; -fx-border-width: 1 1 1 1;"
);
}
if
(
i
==
getN
()
-
1
&&
j
!=
0
)
{
cell
.
setStyle
(
"-fx-border-color: black; -fx-border-width: 1 1 1 0;"
);
}
}
public
void
clear
(
GridPane
grid
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
for
(
int
j
=
0
;
j
<
n
;
j
++)
{
cellValues
[
i
][
j
]
=
0
;
getCell
(
grid
,
i
,
j
).
getTextField
().
clear
();
getCell
(
grid
,
i
,
j
).
setBackground
(
new
Background
(
new
BackgroundFill
(
Color
.
TRANSPARENT
,
CornerRadii
.
EMPTY
,
Insets
.
EMPTY
)));
}
}
}
public
void
setTransparent
(
GridPane
grid
)
{
for
(
Node
cell
:
grid
.
getChildren
())
{
((
Cell
)
cell
).
setBackground
(
new
Background
(
new
BackgroundFill
(
Color
.
TRANSPARENT
,
CornerRadii
.
EMPTY
,
Insets
.
EMPTY
)));
}
}
public
void
highlight
(
GridPane
grid
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
for
(
int
j
=
0
;
j
<
n
;
j
++)
{
if
(
cellValues
[
i
][
j
]
!=
0
)
{
if
(!
verifyRow
(
cellValues
[
i
][
j
],
i
))
{
highlightRow
(
i
,
grid
,
true
);
}
if
(!
verifyColumn
(
cellValues
[
i
][
j
],
j
))
{
highlightColumn
(
j
,
grid
,
true
);
}
}
}
}
}
public
boolean
verify
()
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
for
(
int
j
=
0
;
j
<
n
;
j
++)
{
if
(
cellValues
[
i
][
j
]
!=
0
)
{
if
(
verifyRow
(
cellValues
[
i
][
j
],
i
)
==
false
||
verifyColumn
(
cellValues
[
i
][
j
],
j
)
==
false
)
{
System
.
out
.
println
(
cellValues
[
i
][
j
]
+
" "
+
i
+
" "
+
j
);
return
false
;
}
}
}
}
return
true
;
}
public
boolean
verifyRow
(
int
x
,
int
row
)
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
if
(
cellValues
[
row
][
i
]
==
x
)
{
count
++;
}
}
if
(
count
==
1
)
{
return
true
;
}
return
false
;
}
public
boolean
verifyColumn
(
int
x
,
int
column
)
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
if
(
cellValues
[
i
][
column
]
==
x
)
{
count
++;
}
}
if
(
count
==
1
)
{
return
true
;
}
return
false
;
}
public
void
highlightRow
(
int
row
,
GridPane
grid
,
boolean
highlight
)
{
if
(
highlight
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
getCell
(
grid
,
row
,
i
).
setBackground
(
new
Background
(
new
BackgroundFill
(
Color
.
YELLOW
,
CornerRadii
.
EMPTY
,
Insets
.
EMPTY
)));
}
}
else
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
getCell
(
grid
,
row
,
i
).
setBackground
(
new
Background
(
new
BackgroundFill
(
Color
.
TRANSPARENT
,
CornerRadii
.
EMPTY
,
Insets
.
EMPTY
)));
}
}
}
public
void
highlightColumn
(
int
column
,
GridPane
grid
,
boolean
highlight
)
{
if
(
highlight
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
getCell
(
grid
,
i
,
column
).
setBackground
(
new
Background
(
new
BackgroundFill
(
Color
.
YELLOW
,
CornerRadii
.
EMPTY
,
Insets
.
EMPTY
)));
}
}
else
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
getCell
(
grid
,
i
,
column
).
setBackground
(
new
Background
(
new
BackgroundFill
(
Color
.
TRANSPARENT
,
CornerRadii
.
EMPTY
,
Insets
.
EMPTY
)));
}
}
}
public
Cell
getCell
(
GridPane
grid
,
int
i
,
int
j
)
{
for
(
Node
cell
:
grid
.
getChildren
())
{
if
(((
Cell
)
cell
).
getRow
()
==
i
&&
((
Cell
)
cell
).
getColumn
()
==
j
)
{
return
(
Cell
)
cell
;
}
}
return
null
;
}
public
int
[][]
getCellValues
()
{
return
cellValues
;
}
public
int
getN
()
{
return
n
;
}
public
void
setN
(
int
n
)
{
this
.
n
=
n
;
}
}
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