Sie sind auf Seite 1von 14

!"#$%&'#() +, -.

"/%% 01$1 2"3#"%


27u72u12

!"#$%&'" )#*+,$ -

41((3%'5#6' 7&+8%9( :%6+&(

;<%9.(#$% =.>>1&)
Ny piogiam is a single-playei Battleships game with the aim of uestioying the
computei's ships which aie assembleu on the 1ux1u giiu. Theie aie 9 ships on the giiu
coveiing 19 cells in total. To uestioy a ship, the playei attacks a cell. The playei will be
infoimeu if they have hit oi misseu a ship. This continues until all of the ships have been
uestioyeu. The piogiam keeps tiack of the numbei of shots, hits anu misses. The goal of
the playei is to win the game (uestioy all of the ships) in the least numbei of shots.

!"##$%%$%
! The piogiam ianuomly places the 9 ships on the giiu.
! The playei is infoimeu if they have hit oi misseu a ship.
! The piogiam keeps tiack of the numbei of shots, hits anu misses.
! The playei can stait a new game, save the cuiient game oi iesume a pieviously
saveu game.

'()*"+$%
! uame play is limiteu to single-playei only with no functionality auueu foi a two-
playei game (human vs. computei oi human vs. human).
! Theie is no limit on the numbei of shots that can be fiieu.

,-.#*"%)-.
In summaiy, the pioject has been a useful expeiience. Ny piogiam is fully functional,
intuitive anu easy to use uespite lacking seveial key featuies as highlighteu above.

!#+$$.%/-0%















!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ .

?"(&+/.9(#+"
The iepoit contains the following sections:
1. Requiiements
2. Besign
S. Souice Coue
4. Evaluation
S. Ciitical Self Evaluation

! I will begin by uesciibing the iequiiements of the piogiam - this will be taken fiom
the iequiiements uocument piouuceu eailiei at the stait of the couise.

! The uesign section will uocument the uevelopment of the piogiam fiom an object-
oiienteu peispective. I will uiaw iefeiences to the uesigns piouuceu in the object-
oiienteu uesign uocument piouuceu eailiei anu highlight any changes maue. I will
also incluue algoiithms, wiitten in pseuuo-coue, of ceitain methous.

! In the souice coue section, I will incluue impoitant featuies of the souice coue with
comments anu sensible names foi classes, methous, fielus anu vaiiables.

! The evaluation section will uesciibe how the piogiam was evaluateu. I will
uocument the methous in which I checkeu foi bugs anu eiiois in the piogiam. I will
assess my piogiam against the iequiiements, justifying why any iequiiements
weie not met.

! In the last section, I will self-evaluate my piogiam highlighting aieas of
impiovement anu what I have leaineu fiom the softwaie uevelopment piocess.

:%@.#&%>%"('
The iequiiements below weie taken fiom the iequiiements uocument piouuceu at the
stait of the couise. Not all of the iequiiements weie met. This has been highlighteu in
the evaluation section.
'".#0)-.(* +$1")+$2$.0%3
1. The system shall be intuitive anu have an easy to use uiaphical 0sei Inteiface
(u0I).
2. The system shall have a main menu with the options to stait a new game (1
playei oi 2 playeis, human vS computei), loau a saveu game (fiom a text file),
view the game instiuctionshelp file anu exit the game.
S. The system shall have a uiaphical Bisplay Winuow allowing human playeis to
place ships of uiffeient sizes anu oiientation on theii giiu (a two-uimensional
aiiay with an aiiay inuex iepiesenting the x anu y co-oiuinates of the giiu).
4. The system shall valiuate the playei's placement of ships on the giiu. Theie shall
not be any illegal placement of ships i.e. no ships shoulu oveilap anu ships
shoulu be containeu entiiely within the giiu.
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ /

S. The system shall have an Aitificial Intelligence to play against human playeis in
a 1-playei anu 2-playei game.
6. The system shall have an Aitificial Intelligence (AI) to allow both the human
playei anu the computei playei to ianuomly place ships on the giiu.
7. The system shall be a tuin-baseu system, only allowing 1 playei to fiie a shot at a
time.
8. The system shall piesent two giius (foi a 2-playei game), one giiu uisplaying the
human playei's ships anu the shots fiieu by the computei anu a seconu giiu
showing the hits on the computei's ships anu the shots fiieu.
9. The system shall infoim the human playei when a ship has been hit oi misseu.
1u. The system shall keep tiack of the numbei of shots fiieu anu the scoie anu enu
the game when the numbei of shots fiieu exceeus a limit.
11. The system shall enu the game when eithei the computei oi human's ships have
all been uestioyeu anu output the iesult (win oi lose) foi the human playei.
12. The system shall have an Aitificial Intelligence which follows a systematic
appioach to fiiing shots on the human's playei giiu.
1S. The system shall have an Aitificial Intelligence which, aftei a hit on a ship is
iegisteieu, intelligently continues to fiie shots until the ship is sunk.
14. The system shall have an Aitificial Intelligence which ietuins to the systematic
appioach of fiiing shots aftei a ship has been sunk.
1S. The system shall have a save game option allowing the playei to save the giiu to
a text file anu iesume the game latei.
16. The system shall be able to quit the cuiient game without saving anu stait a new
game by cleaiing the existing boaius.
4-.56".#0)-.(* 7$1")+$2$.0%3
1. The system shall iun on all opeiating systems.
2. The system shoulu be intuitive to use so that useis with a basic knowleuge of
computeis can use it.
S. It is expecteu that the useis aie familiai with anu aie able to use a mouse anu
keyboaiu.
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ 0

-%'#A"
In the eaily stages of uesign, I was ueteimineu to meet all of my iequiiements anu
piouuce a fully capable Battleships game.

Foi a single playei game, I piouuceu the following class uiagiam:



I went aheau with all of the classes as planneu eailiei with the exception of the Ships
class. The uesign woulu allow foi a single playei game but woulu have the ability to
extenu to a two-playei game in the futuie.

8(00*$%/)9%
The Battleships class woulu be iesponsible foi the playing, loauing anu saving of the
game.

B31'' >%(5+/' :%1'+"
play() The system neeus to be able to initialise the giius anu
stait the game.
loau() This methou loaus a pieviously saveu game fiom a text
file.
save() This methou save the cuiient game to a text file.

8(00*$%/)9%:+);
The Battleshipsuiiu class woulu be iesponsible foi geneiating the 1ux1u giiu of 1uu
cells anu piinting the giiu to scieen.

B31'' >%(5+/' :%1'+"
initialiseuiiu() This methou geneiates 1uu cells foiming the 1u by 1u
giiu. It sets the status of each cell to empty.
piintuiiu() This methou piints the giiu to scieen.

!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ 1

<=:+);
The AIuiiu class inheiits the fielus anu methous of the Battleshipsuiiu class. This class
woulu be iesponsible foi uealing with the computei's giiu by ianuomly placing the
ships on the giiu anu ensuiing the ship placements aie valiu.

B31'' >%(5+/' :%1'+"
placeShips() This methou ianuomly places the ships on the giiu.
checkCell() This methou ensuies that the ship placements aie valiu.

>*(?$+
The Playei class woulu be iesponsible foi uealing with usei input e.g. enteiing in
cooiuinates to fiie on the giiu anu othei menu option inputs.

B31'' >%(5+/' :%1'+"
getCooiuinates() This methou allows the playei to input two integeis as
cooiuinates anu fiie at the giiu.

The uesign was changeu, howevei, to take auvantage of the Battleu0I.java class which
woulu ueal with the menu, giaphical iepiesentation of the giiu anu usei input. The
methous of the Battleships class anu playei class woulu become integiateu within the
Battleu0I class. uiven the time scope of the pioject, I ueciueu it was only feasible to
piouuce a single playei game. This iemoveu the neeu to have a sepaiate class foi the AI
giiu. Theiefoie, I ueciueu to meige the Battleshipsuiiu class anu the AIuiiu class anu
have a uiiu class.

').(* #*(%% ;)(@+(23



=.0$+6(#$ ;$%)@.
Baseu on the inteiface uesigns piouuceu eailiei, the giaphical uisplay winuow woulu
featuie a 1u by 1u giiu of buttons. The menu inteiface woulu become integiateu within
a menu bai above the giiu, anu a panel woulu featuie below the giiu infoiming the
playei of the numbei of shots, hits anu misses.
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ 2

:A= B$%)@.3


#/$#C,$** <*@-+)0/23

!"#$%&'() '$ +(,+- '(.' +,"" &/ 0."&1



BEuIN
IF Row >= u ANB Row < uiiu's length
IF Column >= u ANB Column < uiiu's wiuth
IF uiiu at (Row, Column) = ENPTY
Retuin tiue
ELSE
ELSE
ELSE
Retuin false
ENB
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ 3

=+.&9% B+/%
8(00*$:A= #*(%%
**
* This methou is calleu fiom the Nouse Click event when the usei clicks a button on
the 1ux1u giiu.
*
public voiu fiieShot(int iow, int col)
{
panel = new }Panel();

If the cell is empty at the specifieu iow, col
if (aigiiu.AIuiiu|iowj|colj == aigiiu.ENPTY)
{
aigiiu.AIuiiu|iowj|colj = aigiiu.NISS; Sets the cell to miss
setuuiSquaie(iow, col, 'm'); Changes the coloui of the cell to blue
aigiiu.inciementNisses(); Inciements misses
upuateLabels();
Nessage uialog infoiming usei
}0ptionPane.showNessageBialog(panel, "You have misseu.", "NISS",
}0ptionPane.PLAIN_NESSAuE);
}
If the cell is occupieu with a ship at the specifieu iow, col
else if (aigiiu.AIuiiu|iowj|colj == aigiiu.SBIP)
{
aigiiu.AIuiiu|iowj|colj = aigiiu.BIT; Sets the cell to hit
setuuiSquaie(iow, col, 'h'); Changes the coloui of the cell to ieu
aigiiu.inciementBits(); Inciements hits
upuateLabels();
Nessage uialog infoiming usei
}0ptionPane.showNessageBialog(panel, "You have hit a ship.", "BIT",
}0ptionPane.PLAIN_NESSAuE);
}
else If this cell has alieauy been shot at
{
Nessage uialog infoiming usei
}0ptionPane.showNessageBialog(panel, "You have alieauy fiieu a shot heie",
"ERR0R", }0ptionPane.PLAIN_NESSAuE);
}

if (aigiiu.getBits() == aigiiu.getTotalBitsRequiieu())
{
}0ptionPane.showNessageBialog(panel, "You have won the game in " +
(aigiiu.getBits() + aigiiu.getNisses()) + " shots.", "C0NuRAT0LATI0NS",
}0ptionPane.PLAIN_NESSAuE);
}
}
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ 4

**
* Sets a uui giiu squaie at iow, col to change coloui baseu on hit oi miss
*
public voiu setuuiSquaie(int iow, int col, chai c)
{
int bnum = iow * uRIB_SIZE + col; Calculates the button numbei
if (bnum < (uRIB_SIZE*uRIB_SIZE)) If button numbei is valiu
{
if (c == 'm') Niss
{
buttonAiiay|bnumj.setBackgiounu(Coloi.BL0E); Bisplay a blue coloui
buttonAiiay|bnumj.set0paque(tiue);
buttonAiiay|bnumj.setBoiueiPainteu(false);
}
else if (c == 'h') Bit
{
buttonAiiay|bnumj.setBackgiounu(Coloi.REB); Bisplay a ieu coloui
buttonAiiay|bnumj.set0paque(tiue);
buttonAiiay|bnumj.setBoiueiPainteu(false);
}
}
}

:+); #*(%%
**
* Nethou to ianuomly place ships on giiu
*
public voiu placeShips()
{
Cieates an instance of the Ranuom class
Ranuom ianuomueneiatoi = new Ranuom();

uoes thiough all the ships in the SBIP_LENuTBS aiiay
foi (int c = u; c < SBIP_LENuTBS.length; c++)
{
boolean keepuoing = tiue; 0seu to iepeat the ship placement if ship placement
is invaliu

while (keepuoing)
{
boolean valiu = tiue;

int staitRow = ianuomueneiatoi.nextInt(1u); Ranuom stait iow foi ship
int staitColumn = ianuomueneiatoi.nextInt(1u); Ranuom stait column foi
ship
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ 5

int uiiection = ianuomueneiatoi.nextInt(2); Ranuom uiiection, iight oi
uown, foi ship

int iow = staitRow;
int column = staitColumn;
if (uiiection == u) Biiection = iight
{
Checks that the entiie ship can be placeu at the iow anu column of the
inuiviuual cells
foi (int i = u; i < SBIP_LENuTBS|cj; i++)
{
if (valiu)
{
iow = staitRow + i;
valiu = checkCell(iow, column); Checks cell is valiu at the specifieu iow
anu column
}
}
}
else if (uiiection == 1) Biiection = Bown
{
Checks that the entiie ship can be placeu at the iow anu column of the
inuiviuual cells
foi (int j = u; j < SBIP_LENuTBS|cj; j++)
{
if (valiu)
{
column = staitColumn + j;
valiu = checkCell(iow, column); Checks cell is valiu at the specifieu iow
anu column
}
}
}
if (valiu) If ship can be placeu
{
if (uiiection == u) Biiection = Right
{
Ship auueu to giiu
foi (int a = staitRow; a <= iow; a++)
{
AIuiiu|aj|staitColumnj = SBIP;
}
}
else if (uiiection == 1) Biiection = Bown
{
Ship auueu to giiu
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ -6

foi (int b = staitColumn; b <= column; b++)
{
AIuiiu|staitRowj|bj = SBIP;
}
}
keepuoing = false; If ship successfully placeu
}
else
{
keepuoing = tiue;
}
}
}
}

To view the full souice coue, open the Battleships uame foluei anu open the pioject file
in Blue}.

!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ --

;$13.1(#+"
The following iequiiements weie not met:
! The system shall have an Aitificial Intelligence to play against human playeis in
a 1-playei anu 2-playei game.
! The system shall be a tuin-baseu system, only allowing 1 playei to fiie a shot at a
time.
! The system shall piesent two giius (foi a 2-playei game), one giiu uisplaying the
human playei's ships anu the shots fiieu by the computei anu a seconu giiu
showing the hits on the computei's ships anu the shots fiieu.
! The system shall have an Aitificial Intelligence which follows a systematic
appioach to fiiing shots on the human's playei giiu.
! The system shall have an Aitificial Intelligence which, aftei a hit on a ship is
iegisteieu, intelligently continues to fiie shots until the ship is sunk.
! The system shall have an Aitificial Intelligence which ietuins to the systematic
appioach of fiiing shots aftei a ship has been sunk.
These iequiiements mainly iefei to a two-playei game between the human playei anu
the computei.
Foi the iequiiements that weie met, I evaluateu each one against the piogiam using a
black box test. I have incluueu examples of these tests below:
8*(#C 8-D E$%0
2(, /3/',) /(."" -,,4 '%.+- $5 '(, 67)8,% $5 /($'/ 5&%,1 .61 '(, /+$%, .61 ,61 '(, #.),
9(,6 '(, 67)8,% $5 /($'/ 5&%,1 ,:+,,1/ . "&)&';

Scoie panel keeps tiacks of
the numbei of shots, hits
anu misses.
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ -.

2(, /3/',) /(."" (.0, . /.0, #.), $4'&$6 .""$9&6# '(, 4".3,% '$ /.0, '(, #%&1 '$ . ',:' 5&",
.61 %,/7), '(, #.), ".',%;








Cuiient game can be saveu to a text file.
Pievious game can be iesumeu fiom a text file.
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ -/

8"@%
A bug uoes exist wheie if the playei iesizes the winuow to a smallei size, then thiee uots
will appeai in the buttons that have been selecteu.



The bug has been tempoiaiily fixeu by setting the winuow to a fixeu size.

B&#(#913 =%3, ;$13.1(#+"
I believe I have uone well in cieating a woiking piogiam that meets the basic
iequiiements of the Battleships game. I have uevelopeu a gieatei unueistanuing of the
softwaie uevelopment piocess fiom wiiting the iequiiements specification to
piogiamming the solution. Implementing the Battleu0I.java class within my piogiam
was the most challenging aspect of the pioject. If I coulu uo the entiie pioject again, I
woulu stait using the u0I fiom the moment I staiteu piogiamming iathei than auuing it
in at the enu. In oiuei to pioviue a bettei piouuct, I woulu have neeueu to auu moie
functionality to the piogiam anu, as mentioneu seveial times within the iepoit, incluue
a two playei game moue.

'".#0)-.(*)0? -6 0/$ 9+-@+(23 FGHIJ
Although my piogiam uoes woik, I believe it still iequiies moie functionality in oiuei to
make moie usable.

B$%)@. -6 0/$ 9+-@+(23 FJHFJ
The piogiam is well-stiuctuieu, self-uocumenteu anu commenteu, theiefoie it is woithy
of this maik.
Thiee uots in the
selecteu buttons
!"#$%&'#() +, -."/%% 01$1 2"3#"%
27u72u12

!"#$%&'" )#*+,$ -0

:%6+&(C DEFDE
The iepoit coveis all of the sections as iequiieu in the pioject guiuelines uocument.

KD0+( 6$(0"+$%3 JHLJ
Ny piogiam uoes not contain any of the featuies listeu in the auuitional iequiiements of
the pioject specification uocument.

MN$+(** 2(+C3 OGHLJJ

=.>>1&) 1"/ B+"93.'#+"
In summaiy, the majoiity of iequiiements weie met with the exception of iequiiements
iefeiiing to the two playei game moue. The final piogiam iesembles the object-oiienteu
uesign that was planneu. This uemonstiates my unueistanuing of the softwaie
uevelopment piocess, going fiom iequiiements to uesign to implementation. Although
many changes weie maue fiom the initial class uiagiam, the methous anu fielus that
weie planneu weie implementeu in the final solution anu this helpeu to cieate a
woiking piogiam. The inteiface uesigns, which weie uiawn up eailiei, weie
successfully employeu within the piogiam anu this contiibuteu to making the game
moie intuitive. Combineu with message, open anu save uialogs, the piogiam piesenteu
less of a leaining cuive foi useis, thiough the use of stanuaiu u0I components.
Algoiithms weie wiitten foi the methous listeu in the object-oiienteu uesign, foi
example the checkCell methou, anu this saveu a lot of time when it came to
piogiamming the solution. When piogiamming, I maue suie to use sensible names foi
classes, methous, fielus anu vaiiables. All coue was commenteu thoioughly so that if the
game was to be extenueu in the futuie, this coulu be achieveu by a piogiammei othei
than myself. Evaluation was a key aspect of the piocess. I maue use of numeious black
box tests to ensuie the iequiiements weie met in theii entiiety. When wiiting the coue,
I steppeu thiough ceitain algoiithms to ensuie that the coiiect piocess was taking
place. Stepping thiough the coue, also known as white box testing, was an impoitant
pait of testing as it alloweu me to examine the vaiiables involveu in gieatei uetail than a
black box test. In all, the pioject has been a useful expeiience anu something which I
have thoioughly enjoyeu.