Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

l am working on battleship game(Matlab). How can i save the scores in the text f

ID: 3690162 • Letter: L

Question

l am working on battleship game(Matlab). How can i save the scores in the text file?

here are the instructions:

Recording
the
score:
For
each
session
(current
user),
after
each
game,
and
before
starting
a
new
game
(or
before
ending
the
session),
record
the
game
stats
in
a
file
named
firstName_lastName_results.txt:
-­ Who
won/lost
-­ Number
of
moves
(only
if
the
computer
wins)
-­ Difficulty
level
-­ Overall
score
between
the
user
and
the
computer.
If
there
are
previous
games
recorded
in
the
file,
the
current
game’s
score
should
be
appended
at
the
end
of
the
file.
Sample text file:
User Computer No. Of Moves Level Overall Score
---- -------- ------------ ----- -------------
L W 23 EP 0 - 1
W L - MS 1 – 1
L W 19 WD 1 - 2

Explanation / Answer

You can use fprintf. The syntax is:

THe first parameter is the fileID. The second parameter is the format spec that you want to apply to all elements of the array A. This function writes the elements of array A to text file in column order

fprintf(fileID,formatSpec,A1,...,An)