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
patrick.gustard-sm
battleships
Commits
0fab2dc8
Commit
0fab2dc8
authored
Jul 25, 2019
by
patrick.gustard-sm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update Main.java
parent
a28fb59d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
210 additions
and
88 deletions
+210
-88
Main.java
src/battleship/Main.java
+210
-88
No files found.
src/battleship/Main.java
View file @
0fab2dc8
package
battleship
;
package
battleship
;
import
java.util.Random
;
import
java.util.Random
;
import
javafx.application.Application
;
import
javafx.application.Application
;
import
javafx.geometry.Pos
;
import
javafx.geometry.Pos
;
import
javafx.scene.Parent
;
import
javafx.scene.Parent
;
import
javafx.scene.Scene
;
import
javafx.scene.Scene
;
import
javafx.scene.input.MouseButton
;
import
javafx.scene.input.MouseButton
;
import
javafx.scene.layout.BorderPane
;
import
javafx.scene.layout.BorderPane
;
import
javafx.scene.layout.VBox
;
import
javafx.scene.layout.VBox
;
import
javafx.stage.Stage
;
import
javafx.stage.Stage
;
public
class
Main
extends
Application
{
public
class
Main
extends
Application
{
public
static
int
boardsize
=
10
;
//board size, locks the size to always being a square, if the number is changed will always be square
public
static
boolean
vertical
=
false
;
public
static
int
boardsize
=
10
;
static
Random
random
=
new
Random
();
//board size, locks the size to always being a square, if the number is changed will always be square
public
static
boolean
setup
=
false
;
static
int
[]
ships
=
new
int
[]{
5
,
4
,
4
,
3
,
3
,
2
,
2
,
2
};
//Array with all the ship lengths
public
static
boolean
vertical
=
false
;
static
int
shipnum
=
0
;
static
int
xInteger
;
static
Random
random
=
new
Random
();
static
int
yInteger
;
static
int
HealthPlayer
=
25
;
// Sets the health for both enemy and player, this is the total squares for all ships
public
static
boolean
setup
=
true
;
static
int
HealthEnemy
=
25
;
static
Board
BoardPlayer
=
new
Board
(
boardsize
,
event
->
{
static
int
[]
ships
=
new
int
[]{
5
,
4
,
4
,
3
,
3
,
2
,
2
,
2
};
//Array with all the ship lengths
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
xInteger
=
cell
.
xAxis
;
static
int
shipnum
=
0
;
yInteger
=
cell
.
yAxis
;
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
)
{
static
int
enemyshipnum
=
0
;
vertical
=
true
;
}
static
int
xInteger
;
if
(
setup
&&
!
Board
.
obstructed
(
vertical
,
boardsize
,
xInteger
,
yInteger
))
{
static
int
yInteger
;
Board
.
place
(
boardsize
,
vertical
,
xInteger
,
yInteger
);
static
int
HealthPlayer
=
25
;
// Sets the health for both enemy and player, this is the total squares for all ships
static
int
HealthEnemy
=
25
;
static
Board
BoardPlayer
;
static
Board
BoardEnemy
;
public
static
void
main
(
String
[]
args
)
{
launch
(
args
);
}
@Override
public
void
start
(
Stage
stage
)
throws
Exception
{
BoardPlayer
=
new
Board
(
boardsize
,
event
->
{
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
/* grid coordinates of click */
xInteger
=
cell
.
xAxis
;
yInteger
=
cell
.
yAxis
;
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
)
{
vertical
=
true
;
}
if
(
setup
&&
!
BoardPlayer
.
obstructed
(
vertical
,
ships
[
shipnum
],
xInteger
,
yInteger
))
{
System
.
out
.
println
(
"placing"
);
BoardPlayer
.
place
(
ships
[
shipnum
],
vertical
,
xInteger
,
yInteger
);
shipnum
++;
shipnum
++;
}
});
static
Board
BoardEnemy
=
new
Board
(
boardsize
,
event
->
{
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
// to-do: set setup to false when shipnum is too big? (i.e. when it's 8)
if
(!
cell
.
shoot
&&
!
setup
)
{
//Ignores if the selected cell has already been shot at
}
cell
.
shoot
();
}
else
{
if
(
shipnum
==
8
)
{
return
;
}
startGame
();
});
}
});
@Override
public
void
start
(
Stage
stage
)
throws
Exception
{
BoardEnemy
=
new
Board
(
boardsize
,
event
->
{
Scene
gameWindow
=
new
Scene
(
build
());
//builds the scene, so sets everything up in the window
stage
.
setTitle
(
"Battleship"
);
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
stage
.
setScene
(
gameWindow
);
stage
.
show
();
}
if
(!
cell
.
shoot
&&
!
setup
)
{
public
Parent
build
()
{
//Ignores if the selected cell has already been shot at
BorderPane
workspace
=
new
BorderPane
();
cell
.
shoot
();
}
else
{
VBox
leftPane
=
new
VBox
(
BoardPlayer
);
//Sets where I want each of the boards, in this case next to each other
VBox
rightPane
=
new
VBox
(
BoardEnemy
);
return
;
leftPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
rightPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
}
return
workspace
;
}
});
public
static
void
enemySetup
()
{
shipnum
=
0
;
}
Scene
gameWindow
=
new
Scene
(
build
());
public
static
void
enemyPlace
()
{
//builds the scene, so sets everything up in the window
int
ComputerX
=
random
.
nextInt
(
boardsize
);
//Generates a random location on the board
int
ComputerY
=
random
.
nextInt
(
boardsize
);
stage
.
setTitle
(
"Battleship"
);
boolean
aiVert
=
random
.
nextBoolean
();
if
(
setup
&&
!
Board
.
obstructed
(
vertical
,
boardsize
,
xInteger
,
yInteger
))
{
stage
.
setScene
(
gameWindow
);
if
(!
Board
.
obstructed
(
aiVert
,
ComputerX
,
ComputerY
,
ComputerY
))
{
//prevents placement in obstructed areas
Board
.
place
(
ComputerY
,
aiVert
,
ComputerX
,
ComputerY
);
//Places the ships from the board
stage
.
show
();
}
}
}
}
public
static
void
startGame
(){
public
Parent
build
()
{
setup
=
false
;
enemySetup
();
BorderPane
workspace
=
new
BorderPane
();
}
}
VBox
leftPane
=
new
VBox
(
BoardPlayer
);
//Sets where I want each of the boards, in this case next to each other
VBox
rightPane
=
new
VBox
(
BoardEnemy
);
workspace
.
setLeft
(
leftPane
);
workspace
.
setRight
(
rightPane
);
leftPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
rightPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
return
workspace
;
}
public
static
void
enemySetup
()
{
enemyshipnum
=
0
;
}
public
static
void
enemyPlace
()
{
int
ComputerX
=
random
.
nextInt
(
boardsize
);
//Generates a random location on the board
int
ComputerY
=
random
.
nextInt
(
boardsize
);
boolean
aiVert
=
random
.
nextBoolean
();
System
.
out
.
println
(
"maybe placing enemy ship at "
+
ComputerX
+
" "
+
ComputerY
);
System
.
out
.
println
(
"obstructed is "
+
BoardEnemy
.
obstructed
(
vertical
,
boardsize
,
xInteger
,
yInteger
));
if
(
setup
&&
!
BoardEnemy
.
obstructed
(
vertical
,
ships
[
enemyshipnum
],
xInteger
,
yInteger
))
{
if
(!
BoardEnemy
.
obstructed
(
aiVert
,
ComputerX
,
ComputerY
,
ComputerY
))
{
//prevents placement in obstructed areas
BoardEnemy
.
place
(
ComputerY
,
aiVert
,
ComputerX
,
ComputerY
);
//Places the ships from the board
//For Loop to repeat cycle, and something to cancel cycle when it reaches 8
for
(;
enemyshipnum
<
7
;
enemyshipnum
++);
}
}
}
public
static
void
startGame
(){
enemySetup
();
enemyPlace
();
setup
=
false
;
}
}
\ No newline at end of file
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