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
25c8bc5d
Commit
25c8bc5d
authored
Jul 25, 2019
by
patrick.gustard-sm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated, stopped the crashes on the loop that counts up enemyship num
parent
0fab2dc8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
90 deletions
+102
-90
.gitignore
.gitignore
+1
-0
Board.java
src/battleship/Board.java
+5
-1
Main.java
src/battleship/Main.java
+96
-89
No files found.
.gitignore
0 → 100644
View file @
25c8bc5d
/bin/
src/battleship/Board.java
View file @
25c8bc5d
...
@@ -289,9 +289,13 @@ System.exit(0);
...
@@ -289,9 +289,13 @@ System.exit(0);
}
}
}
}
//Cell.setfill put in if statement, to tell if player or enemy, if enemy do nothing, create variable in board, but set player or enemy in main
//Cell.setfill put in if statement, to tell if player or enemy, if enemy do nothing, create variable in board, but set player or enemy in main
\ No newline at end of file
src/battleship/Main.java
View file @
25c8bc5d
...
@@ -24,188 +24,195 @@ import javafx.stage.Stage;
...
@@ -24,188 +24,195 @@ import javafx.stage.Stage;
public
class
Main
extends
Application
{
public
class
Main
extends
Application
{
public
static
int
boardsize
=
10
;
public
static
int
boardsize
=
10
;
//board size, locks the size to always being a square, if the number is changed will always be square
//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
boolean
vertical
=
false
;
static
Random
random
=
new
Random
();
static
Random
random
=
new
Random
();
public
static
boolean
setup
=
true
;
public
static
boolean
setup
=
true
;
static
int
[]
ships
=
new
int
[]{
5
,
4
,
4
,
3
,
3
,
2
,
2
,
2
};
//Array with all the ship lengths
static
int
[]
ships
=
new
int
[]{
5
,
4
,
4
,
3
,
3
,
2
,
2
,
2
};
//Array with all the ship lengths
static
int
shipnum
=
0
;
static
int
shipnum
=
0
;
static
int
enemyshipnum
=
0
;
static
int
enemyshipnum
=
0
;
static
int
xInteger
;
static
int
xInteger
;
static
int
yInteger
;
static
int
yInteger
;
static
int
HealthPlayer
=
25
;
// Sets the health for both enemy and player, this is the total squares for all ships
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
int
HealthEnemy
=
25
;
static
Board
BoardPlayer
;
static
Board
BoardPlayer
;
static
Board
BoardEnemy
;
static
Board
BoardEnemy
;
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
launch
(
args
);
launch
(
args
);
}
}
@Override
@Override
public
void
start
(
Stage
stage
)
throws
Exception
{
public
void
start
(
Stage
stage
)
throws
Exception
{
BoardPlayer
=
new
Board
(
boardsize
,
event
->
{
BoardPlayer
=
new
Board
(
boardsize
,
event
->
{
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
/* grid coordinates of click */
/* grid coordinates of click */
xInteger
=
cell
.
xAxis
;
xInteger
=
cell
.
xAxis
;
yInteger
=
cell
.
yAxis
;
yInteger
=
cell
.
yAxis
;
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
)
{
if
(
event
.
getButton
()
==
MouseButton
.
PRIMARY
)
{
vertical
=
true
;
vertical
=
true
;
}
}
if
(
setup
&&
!
BoardPlayer
.
obstructed
(
vertical
,
ships
[
shipnum
],
xInteger
,
yInteger
))
{
if
(
setup
&&
!
BoardPlayer
.
obstructed
(
vertical
,
ships
[
shipnum
],
xInteger
,
yInteger
))
{
System
.
out
.
println
(
"placing"
);
System
.
out
.
println
(
"placing"
);
BoardPlayer
.
place
(
ships
[
shipnum
],
vertical
,
xInteger
,
yInteger
);
BoardPlayer
.
place
(
ships
[
shipnum
],
vertical
,
xInteger
,
yInteger
);
shipnum
++;
shipnum
++;
// to-do: set setup to false when shipnum is too big? (i.e. when it's 8)
}
// to-do: set setup to false when shipnum is too big? (i.e. when it's 8)
if
(
shipnum
==
8
)
{
}
startGame
();
if
(
shipnum
==
8
)
{
}
startGame
();
});
}
});
BoardEnemy
=
new
Board
(
boardsize
,
event
->
{
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
BoardEnemy
=
new
Board
(
boardsize
,
event
->
{
Board
.
Cell
cell
=
(
Board
.
Cell
)
event
.
getSource
();
if
(!
cell
.
shoot
&&
!
setup
)
{
//Ignores if the selected cell has already been shot at
cell
.
shoot
();
if
(!
cell
.
shoot
&&
!
setup
)
{
//Ignores if the selected cell has already been shot at
}
else
{
cell
.
shoot
();
return
;
}
else
{
}
return
;
}
});
});
Scene
gameWindow
=
new
Scene
(
build
());
//builds the scene, so sets everything up in the window
stage
.
setTitle
(
"Battleship"
);
Scene
gameWindow
=
new
Scene
(
build
());
//builds the scene, so sets everything up in the window
stage
.
setScene
(
gameWindow
);
stage
.
setTitle
(
"Battleship"
);
stage
.
show
(
);
stage
.
setScene
(
gameWindow
);
}
stage
.
show
();
public
Parent
build
()
{
}
BorderPane
workspace
=
new
BorderPane
();
public
Parent
build
()
{
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
);
VBox
leftPane
=
new
VBox
(
BoardPlayer
);
//Sets where I want each of the boards, in this case next to each other
workspace
.
setLeft
(
leftPane
);
VBox
rightPane
=
new
VBox
(
BoardEnemy
);
workspace
.
setRight
(
righ
tPane
);
workspace
.
setLeft
(
lef
tPane
);
leftPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
workspace
.
setRight
(
rightPane
);
righ
tPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
lef
tPane
.
setAlignment
(
Pos
.
CENTER_LEFT
);
return
workspace
;
rightPane
.
setAlignment
(
Pos
.
CENTER_LEFT
)
;
}
return
workspace
;
}
public
static
void
enemySetup
()
{
enemyshipnum
=
0
;
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
);
public
static
void
enemyPlace
()
{
boolean
aiVert
=
random
.
nextBoolean
();
System
.
out
.
println
(
"maybe placing enemy ship at "
+
ComputerX
+
" "
+
ComputerY
);
int
ComputerX
=
random
.
nextInt
(
boardsize
);
//Generates a random location on the board
System
.
out
.
println
(
"obstructed is "
+
BoardEnemy
.
obstructed
(
vertical
,
boardsize
,
xInteger
,
yInteger
)
);
int
ComputerY
=
random
.
nextInt
(
boardsize
);
boolean
aiVert
=
random
.
nextBoolean
();
if
(
setup
&&
!
BoardEnemy
.
obstructed
(
vertical
,
ships
[
enemyshipnum
],
xInteger
,
yInteger
))
{
System
.
out
.
println
(
"maybe placing enemy ship at "
+
ComputerX
+
" "
+
ComputerY
);
if
(!
BoardEnemy
.
obstructed
(
aiVert
,
ComputerX
,
ComputerY
,
ComputerY
))
{
System
.
out
.
println
(
"obstructed is "
+
BoardEnemy
.
obstructed
(
aiVert
,
boardsize
,
ComputerX
,
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
if
(
setup
&&
!
BoardEnemy
.
obstructed
(
aiVert
,
ships
[
enemyshipnum
],
ComputerX
,
ComputerY
))
{
for
(;
enemyshipnum
<
7
;
enemyshipnum
++);
if
(!
BoardEnemy
.
obstructed
(
aiVert
,
ships
[
enemyshipnum
],
ComputerX
,
ComputerY
))
{
//prevents placement in obstructed areas
}
System
.
out
.
println
(
"calling place at "
+
ComputerX
+
" "
+
ComputerY
);
BoardEnemy
.
place
(
ships
[
enemyshipnum
],
aiVert
,
ComputerX
,
ComputerY
);
//Places the ships from the board
//so the for Loop will count up by one
enemyshipnum
++;
System
.
out
.
println
(
enemyshipnum
);
}
}
}
public
static
void
startGame
(){
//Repeat enemy place till it reaches 8, use a loop to repeat the statement enemyPlace
enemySetup
();
}
if
(
enemyshipnum
==
8
)
{
//Stops the loop when the variable is reached starting the game
startGame
();
}
enemyPlace
();
}
setup
=
false
;
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