Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
battleships
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
william.hill1
battleships
Commits
84f7fb66
Commit
84f7fb66
authored
Apr 23, 2019
by
william.hill1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added music
parent
4a78b8e8
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
162 additions
and
203 deletions
+162
-203
cheering.mp3
cheering.mp3
+0
-0
loser.mp3
loser.mp3
+0
-0
Main.java
src/Main.java
+162
-203
No files found.
src/
cheering.mp3
→
cheering.mp3
View file @
84f7fb66
File moved
src/
loser.mp3
→
loser.mp3
View file @
84f7fb66
File moved
src/Main.java
View file @
84f7fb66
...
@@ -12,29 +12,23 @@ import javafx.scene.control.Alert.AlertType;
...
@@ -12,29 +12,23 @@ import javafx.scene.control.Alert.AlertType;
import
javafx.scene.media.Media
;
import
javafx.scene.media.Media
;
import
javafx.scene.media.MediaPlayer
;
import
javafx.scene.media.MediaPlayer
;
public
class
Main
extends
Application
{
public
class
Main
extends
Application
{
int
[]
legnth
=
{
5
,
4
,
4
,
3
,
3
,
2
,
2
,
2
};
int
[]
legnth
=
{
5
,
4
,
4
,
3
,
3
,
2
,
2
,
2
};
int
counter
=
0
;
int
counter
=
0
;
int
EnemyCounter
=
25
;
int
EnemyCounter
=
25
;
int
PlayerCounter
=
25
;
int
PlayerCounter
=
25
;
public
void
start
(
Stage
stage
)
{
public
void
start
(
Stage
stage
)
{
int
Boardsize
=
10
;
int
Boardsize
=
10
;
int
[][]
PlayerShips
=
new
int
[
Boardsize
][
Boardsize
];
int
[][]
PlayerShips
=
new
int
[
Boardsize
][
Boardsize
];
MyRectangle
[][]
playerRects
=
new
MyRectangle
[
Boardsize
][
Boardsize
];
MyRectangle
[][]
playerRects
=
new
MyRectangle
[
Boardsize
][
Boardsize
];
MyRectangle
[][]
enemyRects
=
new
MyRectangle
[
Boardsize
][
Boardsize
];
MyRectangle
[][]
enemyRects
=
new
MyRectangle
[
Boardsize
][
Boardsize
];
Group
group
=
new
Group
();
Group
group
=
new
Group
();
{
{
for
(
int
y
=
0
;
y
<
10
;
++
y
)
for
(
int
y
=
0
;
y
<
10
;
++
y
)
{
{
for
(
int
x
=
0
;
x
<
10
;
++
x
)
{
for
(
int
x
=
0
;
x
<
10
;
++
x
)
MyRectangle
r2
=
new
MyRectangle
();
{
MyRectangle
r2
=
new
MyRectangle
();
r2
.
setX
((
90
*
x
)
+
900
);
r2
.
setX
((
90
*
x
)
+
900
);
r2
.
setY
(
90
*
y
);
r2
.
setY
(
90
*
y
);
r2
.
setWidth
(
90
);
r2
.
setWidth
(
90
);
...
@@ -44,8 +38,7 @@ public class Main extends Application {
...
@@ -44,8 +38,7 @@ public class Main extends Application {
group
.
getChildren
().
add
(
r2
);
group
.
getChildren
().
add
(
r2
);
enemyRects
[
x
][
y
]
=
r2
;
enemyRects
[
x
][
y
]
=
r2
;
MyRectangle
r
=
new
MyRectangle
();
MyRectangle
r
=
new
MyRectangle
();
r
.
setX
(
90
*
x
);
r
.
setX
(
90
*
x
);
r
.
setY
(
90
*
y
);
r
.
setY
(
90
*
y
);
r
.
setWidth
(
90
);
r
.
setWidth
(
90
);
...
@@ -59,153 +52,126 @@ public class Main extends Application {
...
@@ -59,153 +52,126 @@ public class Main extends Application {
final
int
yc
=
y
;
final
int
yc
=
y
;
final
Label
l
=
new
Label
(
""
);
final
Label
l
=
new
Label
(
""
);
int
max
=
9
;
int
max
=
9
;
int
min
=
0
;
int
min
=
0
;
int
range
=
max
-
min
+
1
;
int
range
=
max
-
min
+
1
;
r2
.
setOnMouseClicked
(
event
->
{
r2
.
setOnMouseClicked
(
event
->
{
if
(
counter
==
legnth
.
length
)
{
if
(
counter
==
legnth
.
length
)
{
if
(
enemyRects
[
xc
][
yc
].
getFill
()
==
Color
.
TURQUOISE
||
enemyRects
[
xc
][
yc
].
getFill
()
==
Color
.
BLACK
)
{
if
(
enemyRects
[
xc
][
yc
].
getFill
()
==
Color
.
TURQUOISE
||
enemyRects
[
xc
][
yc
].
getFill
()
==
Color
.
BLACK
)
{
if
(
enemyRects
[
xc
][
yc
].
getNumber
()
>
0
)
{
if
(
enemyRects
[
xc
][
yc
].
getNumber
()
>
0
)
{
enemyRects
[
xc
][
yc
].
setFill
(
Color
.
RED
);
enemyRects
[
xc
][
yc
].
setFill
(
Color
.
RED
);
EnemyCounter
-=
1
;
EnemyCounter
-=
1
;
}
}
else
{
else
{
enemyRects
[
xc
][
yc
].
setFill
(
Color
.
GREY
);
enemyRects
[
xc
][
yc
].
setFill
(
Color
.
GREY
);
}
}
boolean
placed2
=
false
;
boolean
placed2
=
false
;
do
{
do
{
placed2
=
false
;
placed2
=
false
;
int
rand2
=
(
int
)(
Math
.
random
()
*
range
)
+
min
;
int
rand2
=
(
int
)
(
Math
.
random
()
*
range
)
+
min
;
int
rand
=
(
int
)(
Math
.
random
()
*
range
)
+
min
;
int
rand
=
(
int
)
(
Math
.
random
()
*
range
)
+
min
;
if
(
playerRects
[
rand
][
rand2
].
getFill
()
==
Color
.
BLUE
||
playerRects
[
rand
][
rand2
].
getFill
()
==
Color
.
GREEN
){
if
(
playerRects
[
rand
][
rand2
].
getFill
()
==
Color
.
BLUE
||
playerRects
[
rand
][
rand2
].
getFill
()
==
Color
.
GREEN
)
{
placed2
=
true
;
placed2
=
true
;
if
(
playerRects
[
rand
][
rand2
].
getNumber
()
>
0
)
{
if
(
playerRects
[
rand
][
rand2
].
getNumber
()
>
0
)
{
playerRects
[
rand
][
rand2
].
setFill
(
Color
.
RED
);
playerRects
[
rand
][
rand2
].
setFill
(
Color
.
RED
);
PlayerCounter
-=
1
;
PlayerCounter
-=
1
;
}
}
else
{
else
{
playerRects
[
rand
][
rand2
].
setFill
(
Color
.
GREY
);
playerRects
[
rand
][
rand2
].
setFill
(
Color
.
GREY
);
}
}
}
}
System
.
out
.
println
(
rand
);
System
.
out
.
println
(
rand
);
}
while
(
placed2
==
false
&&
(
PlayerCounter
>
0
||
EnemyCounter
>
0
));
}
while
(
placed2
==
false
&&
(
PlayerCounter
>
0
||
EnemyCounter
>
0
));
}
}
}
}
if
(
PlayerCounter
==
0
)
{
if
(
PlayerCounter
==
0
)
{
Alert
alert
=
new
Alert
(
AlertType
.
INFORMATION
);
alert
.
setTitle
(
"You lost"
);
alert
.
setHeaderText
(
null
);
alert
.
setContentText
(
"Better look next time!"
);
alert
.
showAndWait
();
String
DusicFile
=
"loser.mp3"
;
String
DusicFile
=
"loser.mp3"
;
Media
dound
=
new
Media
(
new
File
(
DusicFile
).
toURI
().
toString
());
Media
dound
=
new
Media
(
new
File
(
DusicFile
).
toURI
().
toString
());
MediaPlayer
dediaPlayer
=
new
MediaPlayer
(
dound
);
MediaPlayer
dediaPlayer
=
new
MediaPlayer
(
dound
);
dediaPlayer
.
play
();
dediaPlayer
.
play
();
Alert
alert2
=
new
Alert
(
AlertType
.
INFORMATION
);
alert2
.
setTitle
(
"You lost"
);
alert2
.
setHeaderText
(
null
);
alert2
.
setContentText
(
"Better look next time!"
);
alert2
.
showAndWait
();
System
.
exit
(-
1
);
}
}
if
(
EnemyCounter
==
0
)
{
if
(
EnemyCounter
==
0
)
{
Alert
alert
=
new
Alert
(
AlertType
.
INFORMATION
);
alert
.
setTitle
(
"You Won!!!"
);
alert
.
setHeaderText
(
null
);
alert
.
setContentText
(
"Congratulations!!!"
);
alert
.
showAndWait
();
String
musicFile
=
"cheering.mp3"
;
String
musicFile
=
"cheering.mp3"
;
Media
sound
=
new
Media
(
new
File
(
musicFile
).
toURI
().
toString
());
Media
sound
=
new
Media
(
new
File
(
musicFile
).
toURI
().
toString
());
MediaPlayer
mediaPlayer
=
new
MediaPlayer
(
sound
);
MediaPlayer
mediaPlayer
=
new
MediaPlayer
(
sound
);
mediaPlayer
.
play
();
mediaPlayer
.
play
();
Alert
alert3
=
new
Alert
(
AlertType
.
INFORMATION
);
alert3
.
setTitle
(
"You Won!!!"
);
alert3
.
setHeaderText
(
null
);
alert3
.
setContentText
(
"Congratulations!!!"
);
alert3
.
showAndWait
();
System
.
exit
(-
1
);
}
}
});
});
// Above: Enemy grid. Below: Player Grid
// Above: Enemy grid. Below: Player Grid
r
.
setOnMouseClicked
(
event
->
{
r
.
setOnMouseClicked
(
event
->
{
if
(
counter
<
legnth
.
length
)
{
if
(
counter
<
legnth
.
length
)
{
boolean
colourcheck
=
false
;
boolean
colourcheck
=
false
;
System
.
out
.
println
(
"click at "
+
xc
+
" "
+
yc
);
System
.
out
.
println
(
"click at "
+
xc
+
" "
+
yc
);
if
(
xc
+
legnth
[
counter
]
<=
Boardsize
)
{
if
(
xc
+
legnth
[
counter
]
<=
Boardsize
)
{
for
(
int
q
=
0
;
q
<
legnth
[
counter
];
q
++)
{
for
(
int
q
=
0
;
q
<
legnth
[
counter
];
q
++)
{
if
(
playerRects
[
xc
+
q
][
yc
].
getFill
()
==
Color
.
GREEN
)
{
if
(
playerRects
[
xc
+
q
][
yc
].
getFill
()
==
Color
.
GREEN
)
{
colourcheck
=
true
;
colourcheck
=
true
;
}
}
}
}
if
(
colourcheck
==
false
)
{
if
(
colourcheck
==
false
)
{
for
(
int
m
=
0
;
m
<
legnth
[
counter
];
m
++)
{
for
(
int
m
=
0
;
m
<
legnth
[
counter
];
m
++)
{
System
.
out
.
println
(
legnth
);
System
.
out
.
println
(
legnth
);
playerRects
[
xc
+
m
][
yc
].
setFill
(
Color
.
GREEN
);
playerRects
[
xc
+
m
][
yc
].
setFill
(
Color
.
GREEN
);
playerRects
[
xc
+
m
][
yc
].
setNumber
(
legnth
[
counter
]);
playerRects
[
xc
+
m
][
yc
].
setNumber
(
legnth
[
counter
]);
}
}
System
.
out
.
println
(
"click at "
+
xc
+
" "
+
yc
);
System
.
out
.
println
(
"click at "
+
xc
+
" "
+
yc
);
boolean
numbercheck
=
false
;
boolean
numbercheck
=
false
;
boolean
placed
=
false
;
boolean
placed
=
false
;
do
{
do
{
int
rand2
=
(
int
)
(
Math
.
random
()
*
range
)
+
min
;
int
rand2
=
(
int
)
(
Math
.
random
()
*
range
)
+
min
;
int
rand
=
(
int
)
(
Math
.
random
()
*
range
)
+
min
;
int
rand
=
(
int
)
(
Math
.
random
()
*
range
)
+
min
;
placed
=
false
;
placed
=
false
;
numbercheck
=
false
;
numbercheck
=
false
;
System
.
out
.
println
(
rand
+
" "
+
rand2
);
System
.
out
.
println
(
rand
+
" "
+
rand2
);
if
(
rand
+
legnth
[
counter
]
<
Boardsize
)
{
if
(
rand
+
legnth
[
counter
]
<
Boardsize
)
{
for
(
int
j
=
0
;
j
<
legnth
[
counter
];
j
++)
{
for
(
int
j
=
0
;
j
<
legnth
[
counter
];
j
++)
{
if
(
enemyRects
[
rand
+
j
][
rand2
].
getNumber
()
>
0
)
{
if
(
enemyRects
[
rand
+
j
][
rand2
].
getNumber
()
>
0
)
{
numbercheck
=
true
;
numbercheck
=
true
;
}
}
}
}
if
(
numbercheck
==
false
)
{
if
(
numbercheck
==
false
)
{
for
(
int
p
=
0
;
p
<
legnth
[
counter
];
p
++)
{
for
(
int
p
=
0
;
p
<
legnth
[
counter
];
p
++)
{
enemyRects
[
rand
+
p
][
rand2
].
setFill
(
Color
.
BLACK
);
enemyRects
[
rand
+
p
][
rand2
].
setNumber
(
legnth
[
counter
]);
enemyRects
[
rand
+
p
][
rand2
].
setFill
(
Color
.
BLACK
);
enemyRects
[
rand
+
p
][
rand2
].
setNumber
(
legnth
[
counter
]);
}
}
placed
=
true
;
placed
=
true
;
}
}
}
}
}
while
(
placed
==
false
);
}
while
(
placed
==
false
);
...
@@ -214,30 +180,23 @@ public class Main extends Application {
...
@@ -214,30 +180,23 @@ public class Main extends Application {
counter
++;
counter
++;
}
}
}
}
});
});
}
}
}
}
Scene
scene
=
new
Scene
(
group
,
1800
,
900
);
Scene
scene
=
new
Scene
(
group
,
1800
,
900
);
stage
.
setScene
(
scene
);
stage
.
setScene
(
scene
);
stage
.
show
();
stage
.
show
();
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
launch
(
args
);
launch
(
args
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment