Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP1206-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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nimrod Abramovich
COMP1206-Mathdoku
Commits
696eaff2
Commit
696eaff2
authored
Apr 1, 2020
by
Nimrod Abramovich
Browse files
Options
Downloads
Patches
Plain Diff
regex for input & change N added for game load
parent
52327c69
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
bin/coursework/Main.class
+0
-0
0 additions, 0 deletions
bin/coursework/Main.class
src/coursework/Main.java
+107
-39
107 additions, 39 deletions
src/coursework/Main.java
with
107 additions
and
39 deletions
bin/coursework/Main.class
+
0
−
0
View file @
696eaff2
No preview for this file type
This diff is collapsed.
Click to expand it.
src/coursework/Main.java
+
107
−
39
View file @
696eaff2
...
...
@@ -8,6 +8,9 @@ import java.io.IOException;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
javafx.application.*
;
import
javafx.geometry.Insets
;
import
javafx.geometry.Pos
;
...
...
@@ -174,12 +177,12 @@ public class Main extends Application {
topHBox
.
setAlignment
(
Pos
.
BOTTOM_CENTER
);
topHBox
.
setSpacing
(
10
);
Button
loadGameFileButton
=
new
Button
(
"Load
g
ame -
f
ile"
);
Button
loadGameFileButton
=
new
Button
(
"Load
G
ame -
F
ile"
);
CheckBox
showMistakesCheck
=
new
CheckBox
(
"Show mistakes"
);
if
(
showMistakes
)
{
showMistakesCheck
.
setSelected
(
true
);
}
Button
loadGameTextInputButton
=
new
Button
(
"Load
g
ame -
t
ext
input
"
);
Button
loadGameTextInputButton
=
new
Button
(
"Load
G
ame -
T
ext"
);
loadGameFileButton
.
setPrefWidth
(
200
);
loadGameTextInputButton
.
setPrefWidth
(
loadGameFileButton
.
getPrefWidth
());
...
...
@@ -348,21 +351,7 @@ public class Main extends Application {
}
}
///////////////////////////////////////////////////////////
private
void
generateRandomGameEvent
()
{
// new popup selecting difficultly and n
Stage
popupStage
=
new
Stage
();
popupStage
.
initModality
(
Modality
.
APPLICATION_MODAL
);
popupStage
.
initOwner
(
stage
);
VBox
vbox
=
new
VBox
();
vbox
.
setSpacing
(
5
);
vbox
.
setAlignment
(
Pos
.
TOP_CENTER
);
HBox
hboxBottom
=
new
HBox
();
hboxBottom
.
setSpacing
(
86
);
hboxBottom
.
setPadding
(
new
Insets
(
10
));
private
Slider
setupSlider
()
{
Slider
slider
=
new
Slider
(
2
,
8
,
1
);
slider
.
setPrefWidth
(
150
);
slider
.
setMaxWidth
(
150
);
...
...
@@ -374,7 +363,10 @@ public class Main extends Application {
slider
.
setSnapToTicks
(
true
);
slider
.
setValue
(
N
);
slider
.
setPadding
(
new
Insets
(
10
,
0
,
0
,
0
));
return
slider
;
}
private
HBox
setupRadioHBox
()
{
HBox
hboxMiddle
=
new
HBox
();
ToggleGroup
tg
=
new
ToggleGroup
();
RadioButton
easy
=
new
RadioButton
(
"Easy"
);
...
...
@@ -389,14 +381,31 @@ public class Main extends Application {
radioVBox
.
setSpacing
(
5
);
hboxMiddle
.
getChildren
().
addAll
(
new
Label
(
"Difficulty:"
),
radioVBox
);
return
hboxMiddle
;
}
///////////////////////////////////////////////////////////
private
void
generateRandomGameEvent
()
{
Stage
popupStage
=
new
Stage
();
popupStage
.
initModality
(
Modality
.
APPLICATION_MODAL
);
popupStage
.
initOwner
(
stage
);
VBox
vbox
=
new
VBox
();
vbox
.
setSpacing
(
5
);
vbox
.
setAlignment
(
Pos
.
TOP_CENTER
);
HBox
hboxBottom
=
new
HBox
();
hboxBottom
.
setSpacing
(
86
);
hboxBottom
.
setPadding
(
new
Insets
(
10
));
Slider
slider
=
setupSlider
();
Button
generateButton
=
new
Button
(
"Generate"
);
generateButton
.
setAlignment
(
Pos
.
TOP_RIGHT
);
generateButton
.
setOnAction
(
e
->
{
popupStage
.
close
();
if
((
int
)
slider
.
getValue
()
!=
N
)
{
N
=
(
int
)
slider
.
getValue
();
resetGrid
();
}
RandomGenerator
rg
=
new
RandomGenerator
(
N
);
gridCages
=
rg
.
generate
();
});
...
...
@@ -411,10 +420,8 @@ public class Main extends Application {
hboxTop
.
setSpacing
(
3
);
hboxTop
.
setAlignment
(
Pos
.
CENTER
);
hboxBottom
.
getChildren
().
addAll
(
cancelButton
,
generateButton
);
vbox
.
getChildren
().
addAll
(
hboxTop
,
hboxMiddle
,
hboxBottom
);
vbox
.
getChildren
().
addAll
(
hboxTop
,
setupRadioHBox
()
,
hboxBottom
);
popupStage
.
setScene
(
new
Scene
(
vbox
,
250
,
250
));
popupStage
.
show
();
...
...
@@ -427,16 +434,39 @@ public class Main extends Application {
File
selectedFile
=
fileChooser
.
showOpenDialog
(
stage
);
BufferedReader
file
=
new
BufferedReader
(
new
FileReader
(
selectedFile
));
String
line
;
while
((
line
=
file
.
readLine
())
!=
null
)
{
List
<
String
>
fileLines
=
file
.
lines
().
collect
(
Collectors
.
toList
());
int
max
=
-
1
;
for
(
String
line
:
fileLines
)
{
String
[]
rightHalfLine
=
line
.
split
(
" "
)[
1
].
split
(
","
);
int
[]
lineIds
=
new
int
[
rightHalfLine
.
length
];
for
(
int
i
=
0
;
i
<
lineIds
.
length
;
i
++)
{
lineIds
[
i
]
=
Integer
.
parseInt
(
rightHalfLine
[
i
]);
if
(
lineIds
[
i
]
>
max
)
{
max
=
lineIds
[
i
];
}
}
}
if
(
Math
.
sqrt
(
max
)
!=
N
)
{
N
=
(
int
)
Math
.
sqrt
(
max
);
resetGrid
();
}
for
(
String
line
:
fileLines
)
{
createNewCage
(
line
);
}
file
.
close
();
}
catch
(
FileNotFoundException
e1
)
{
}
catch
(
FileNotFoundException
e1
)
{
e1
.
printStackTrace
();
}
catch
(
IOException
e2
)
{
}
catch
(
IOException
e2
)
{
e2
.
printStackTrace
();
}
catch
(
NullPointerException
e3
)
{}
}
private
void
loadTextButtonClickEvent
()
{
...
...
@@ -446,10 +476,17 @@ public class Main extends Application {
VBox
vbox
=
new
VBox
();
vbox
.
setPadding
(
new
Insets
(
10
));
vbox
.
setSpacing
(
10
);
vbox
.
setSpacing
(
5
);
HBox
hboxBottom
=
new
HBox
();
hboxBottom
.
setSpacing
(
163
);
HBox
hbox
=
new
HBox
();
hbox
.
setSpacing
(
162
);
HBox
hboxTop
=
new
HBox
();
hboxTop
.
setSpacing
(
5
);
hboxTop
.
setAlignment
(
Pos
.
CENTER
);
Slider
slider
=
setupSlider
();
TextArea
textArea
=
new
TextArea
();
Button
cancelButton
=
new
Button
(
"Cancel"
);
...
...
@@ -460,6 +497,10 @@ public class Main extends Application {
Button
loadButton
=
new
Button
(
"Load"
);
loadButton
.
setAlignment
(
Pos
.
TOP_RIGHT
);
loadButton
.
setOnAction
(
e
->
{
if
((
int
)
slider
.
getValue
()
!=
N
)
{
N
=
(
int
)
slider
.
getValue
();
resetGrid
();
}
String
[]
lines
=
textArea
.
getText
().
split
(
"\n"
);
for
(
String
line
:
lines
)
{
if
(!
line
.
equals
(
null
)
&&
!
line
.
equals
(
""
))
{
...
...
@@ -469,8 +510,10 @@ public class Main extends Application {
popupStage
.
hide
();
});
hbox
.
getChildren
().
addAll
(
cancelButton
,
loadButton
);
vbox
.
getChildren
().
addAll
(
textArea
,
hbox
);
Label
sizeLabel
=
new
Label
(
"Size (NxN):"
);
hboxTop
.
getChildren
().
addAll
(
sizeLabel
,
slider
);
hboxBottom
.
getChildren
().
addAll
(
cancelButton
,
loadButton
);
vbox
.
getChildren
().
addAll
(
hboxTop
,
textArea
,
hboxBottom
);
popupStage
.
setScene
(
new
Scene
(
vbox
,
300
,
300
));
popupStage
.
show
();
...
...
@@ -612,11 +655,36 @@ public class Main extends Application {
private
void
textFieldDataChange
(
int
i
,
int
j
)
{
if
(!
clearing
&&
!
undoing
)
{
boolean
validInput
=
true
;
try
{
int
textInput
=
Integer
.
parseInt
(
gridNumbers
[
i
*
N
+
j
].
getText
());
if
(
textInput
<
1
)
{
validInput
=
false
;
gridNumbers
[
i
*
N
+
j
].
setText
(
""
);
}
else
if
(
textInput
>
N
)
{
validInput
=
false
;
gridNumbers
[
i
*
N
+
j
].
setText
(
gridNumbers
[
i
*
N
+
j
].
getText
().
substring
(
0
,
1
));
}
}
catch
(
NumberFormatException
ex
)
{
validInput
=
false
;
if
(
gridNumbers
[
i
*
N
+
j
].
getText
().
length
()
==
2
)
{
gridNumbers
[
i
*
N
+
j
].
setText
(
gridNumbers
[
i
*
N
+
j
].
getText
().
substring
(
0
,
1
));
}
else
{
gridNumbers
[
i
*
N
+
j
].
setText
(
""
);
}
}
if
(
validInput
)
{
undoStack
.
push
(
new
GameState
(
GameState
.
getCurrentGameState
(
gridNumbers
)));
if
(
undoButton
.
isDisabled
())
{
undoButton
.
setDisable
(
false
);
}
}
}
constraints
.
checkConstraints
(
j
,
i
,
showMistakes
);;
constraints
.
checkWinCondition
();
}
...
...
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