Java Reference
In-Depth Information
public
Board()
{
populateGrid() ;
populateShips () ;
}
public void
populateGrid()
{
for
(
int
row = 0; row
<
BOARD SIZE ;
row++)
{
for
(
int
col = 0; col
<
BOARD SIZE ;
c o l ++)
{
grid [row][ col ] =
new
Cell () ;
}
}
}
public void
populateShips ()
{
for
(
int
i=0; i
<
placement . length ;
i++)
{
ships [ i]=
new
Ship(placement [ i ] ,
this
);
}
}
public boolean
isFree(
int
row ,
int
column ,
boolean
direction ,
int
size)
{
if
( d i r e c t i o n == HORIZONTAL)
{
for
(
int
newColumn = column ; newColumn
<
=column+size
−
1;
newColumn++)
{
if
(getElement(row, newColumn) !=
null
)
{
if
( gr id [ row ] [ newColumn ] . isPartOfShip ( ) )
{
return false
;
}
}
else
{
return false
;
}
return true
;
}
else
{
for
(
int
newRow = row ; newRow
<
=row+size
−
1 ; newRow++)
{
if
(getElement(newRow, column) !=
null
)
{
if
(grid[newRow][column].isPartOfShip())
{
return false
;
}
}
{
return false
;
else
}
return true
;
}
}
public void
populate(
int
row ,
int
column ,
boolean
direction ,
int
size , Ship ship)
{
if
( d i r e c t i o n == HORIZONTAL)
{
for
(
int
newColumn = column ; newColumn
<
=column+size
−
1;
newColumn++)
{
grid [row ] [ newColumn ] . putShip( ship) ;
}
}
else
{