A-level Computing/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Skeleton code/2011 Exam/Section D

From Wikibooks, open books for an open world
Jump to: navigation, search

Code Discussion[edit]

When you first run your code you will probably find that everything appears to work. Don't be deceived, try and load the top scores. It probably won't work; we need to get the top scores file in the correct location, so that when we select the load the option from the menu it actually loads the file:

Correct Load Location[edit]

Inside the LoadTopScores() sub routine you'll find the line that is giving you trouble

FileOpen(1, "HiScores.txt", OpenMode.Input)

All this line is doing is trying to find the HiScores.txt file inside the folder where your code is running. On Windows machines this will be:

ConsoleApplication1\bin\Debug

You can fix this by copying the HiScores.txt file into here. Or alternatively you can copy the file into the root of your project, in this case being the poorly named project:

ConsoleApplication1

We then need to make the line we looked at above point to this file:

FileOpen(1, "../../HiScores.txt", OpenMode.Input)

The ../../ means to take the Debug folder where the code is running, and to look two folders up to find the HiScores.txt file:

  • If the code is running in ConsoleApplication1\bin\Debug
  • "../../HiScores.txt" will find the file in ConsoleApplication1\

Potential Questions[edit]

This list is by no means a list of the questions you will be asked in the exam, but by solving these problems you should get to understand the code a little better.

Validating the LoadTopScores(TopScores)[edit]

You might be worried about people accidentally overwriting the top scores from the main menu. Imagine that you have just played the game for an hour and you accidentally click on option 3 on the main menu, you'd overwrite all your scores! How could we validate the entry of the load game to make sure that this didn't happen:

The option menu[edit]

Add a new option to the menu to:

Reset the top scores[edit]

An option that appears to be missing, is the ability for the user to reset the top scores that are currently stored in the TopScores array. We need to add an option (option 5) to the menu so that users can manually reset the top scores. Make sure that you get all the menu option checks to allow for the new menu option number.

Starting the game[edit]

Making sure each player has a different name[edit]

When you start the game the following scenario could occur:

What is player one's name? Aubrey
What is player two's name? Aubrey

This means that you wouldn't know who was who, which Aubrey is which? It would be far better if we forced the users to have different names. To do this you are going to need a loop to check that the second name is different from the first name:

Save current top scores to the CSV file[edit]

A glaring omission from the code is the ability to save the current top scores back to the CSV file. You might well be asked to implement this. Let's first take a look at the code that reads the data from the file:

    Sub LoadTopScores(ByRef TopScores() As TTopScore)
        Dim Count As Integer
        Dim Count2 As Integer
        Dim LineFromFile As String
        Dim ValuesOnLine(2) As String
        FileOpen(1, "../../HiScores.txt", OpenMode.Input)
        For Count = 1 To MaxSize
            ValuesOnLine(1) = ""
            ValuesOnLine(2) = ""
            LineFromFile = LineInput(1)
            Count2 = 0
            Do
                ValuesOnLine(1) = ValuesOnLine(1) + LineFromFile(Count2)
                Count2 = Count2 + 1
            Loop Until LineFromFile(Count2) = ","
            Count2 = Count2 + 1
            Do
                ValuesOnLine(2) = ValuesOnLine(2) + LineFromFile(Count2)
                Count2 = Count2 + 1
            Loop Until Count2 = LineFromFile.Length
            TopScores(Count).Name = ValuesOnLine(1)
            TopScores(Count).Score = CInt(ValuesOnLine(2))
        Next
        FileClose(1)
    End Sub

There are three important things that you need to gain from this.

1. this line opens the file in read only mode and allocates it to file variable 1, we need another mode that writes to the file

FileOpen(1, "../../HiScores.txt", OpenMode.Input)

2. This code reads a line from the file and saves it into the LineFromFile string. Notice the 1 is referring to the file variable. This line is inside a loop that loops from on the variable Count, why is this?

LineFromFile = LineInput(1)

3. Finally we have some house keeping, where we need to close the file to make sure we free up resources

FileClose(1)

Using these ideas we need to create a sub routine to save the files. Let's get some pseudo code together:

Pseudo Code of Save Data to File

sub savefiles(TopScores())

  1. open the text file for writing
  2. loop through 1 to 4 with count
    1. write TopScores(count) to the text file in a CSV format
  3. close the file

end sub

But what real code can we use to do the above?

1. open file for writing, use Output instead of Input

FileOpen(1, "../../HiScores.txt", OpenMode.Output)

2. Print a line to a file instead of reading from it. Remember you will need a loop to write all 4 lines

PrintLine(1, "...") 'write a line to file 1.  Make sure the line you write is in CSV format

When you are writing to the file, remember that each line you are writing must have this format:

<name><comma>

Unable to compile LilyPond input file:
Processing `.../file.ly'
Parsing...
.../file.ly:12:1: error: syntax error, unexpected STRING
 
 Norman,76
.../file.ly:16:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:38:39: error: syntax error, unexpected '.'
You can also achieve this by using a [[
                                       ../../Programming#Reading.2FWriting_Files|stream writer]]:
.../file.ly:38:57: error: GUILE signaled an error for the expression beginning here
You can also achieve this by using a [[../../Programming#
                                                         Reading.2FWriting_Files|stream writer]]:
.../file.ly:20:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:69:0: error: errors found, ignoring music expression

{{ExerciseRobox|title=Pseudo Code of displaying a top score}}
.../file.ly:70:4: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
sub 
    displaytopscore(TopScores())
.../file.ly:71:1: error: GUILE signaled an error for the expression beginning here
#
 create a pointer to the topscore: hiscore
.../file.ly:72:1: error: GUILE signaled an error for the expression beginning here
#
 set hiscore = 1 so that it points to the first element in TopScores
.../file.ly:72:20: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
#set hiscore = 1 so 
                    that it points to the first element in TopScores
.../file.ly:73:1: error: GUILE signaled an error for the expression beginning here
#
 go through each of the TopScores
.../file.ly:74:1: error: GUILE signaled an error for the expression beginning here
#
 #if topscores value is bigger than value being pointed to by hiscores
.../file.ly:76:1: error: GUILE signaled an error for the expression beginning here
#
 print out the TopScores(hiscore)
.../file.ly:78:0: error: errors found, ignoring music expression

{{Robox/Close}}
.../file.ly:79:4: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
Try 
    to code this solution yourself:
.../file.ly:125:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:127:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:131:0: error: syntax error, unexpected STRING

The output should be:
.../file.ly:158:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:160:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:164:0: error: syntax error, unexpected STRING

The output should be:
.../file.ly:168:2: error: syntax error, unexpected $undefined
| 
  &nbsp;
.../file.ly:80:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:174:0: error: errors found, ignoring music expression

{{ExerciseRobox|title=Pseudo Code of displaying a top score}}
.../file.ly:175:4: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
sub 
    displaylowestscore(TopScores())
.../file.ly:176:1: error: GUILE signaled an error for the expression beginning here
#
 create a pointer to the topscore: hiscore
.../file.ly:177:1: error: GUILE signaled an error for the expression beginning here
#
 set hiscore = 1 so that it points to the first element in TopScores
.../file.ly:177:20: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
#set hiscore = 1 so 
                    that it points to the first element in TopScores
.../file.ly:178:1: error: GUILE signaled an error for the expression beginning here
#
 go through each of the TopScores
.../file.ly:179:1: error: GUILE signaled an error for the expression beginning here
#
 #if topscores value is smaller than value being pointed to by hiscores
.../file.ly:181:1: error: GUILE signaled an error for the expression beginning here
#
 print out the TopScores(hiscore)
.../file.ly:183:0: error: errors found, ignoring music expression

{{Robox/Close}}
.../file.ly:184:4: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
Try 
    to code this solution yourself:
.../file.ly:233:2: error: syntax error, unexpected $undefined
| 
  &nbsp;
.../file.ly:185:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:239:205: error: GUILE signaled an error for the expression beginning here
They could well ask you to perform a full sort on the high score table.  A bubble sort is a potential solution to doing this.  Take a look at the following pseudo code to perform a [[../../Problem_Solving#
                                                                                                                                                                                                             Bubble_Sort|bubble sort]].  You need to make your own version of this to sort all the data:
.../file.ly:245:5: error: syntax error, unexpected '\''
     
     '''for each''' i '''in''' 1 '''to''' length(A) - 1 '''inclusive do:'''
.../file.ly:249:7: error: syntax error, unexpected '\''
       
       '''end if'''
.../file.ly:256:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:328:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:330:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:331:48: error: syntax error, unexpected STRING
| True || 3 || 4 || Gerald || 23 || || || || || 
                                                Colin || 75 || Gerald || 23 
.../file.ly:334:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:335:43: error: syntax error, unexpected STRING
| True || 2 || 4 || Michael || 45 || || || 
                                           Colin || 75 || Michael || 45 || || 
.../file.ly:336:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:338:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:340:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:342:1: error: syntax error, unexpected '-'
|
 -
.../file.ly:346:0: error: syntax error, unexpected STRING

The output should be:
.../file.ly:353:2: error: syntax error, unexpected $undefined
| 
  &nbsp;
.../file.ly:270:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:411:2: error: syntax error, unexpected $undefined
| 
  &nbsp;
.../file.ly:363:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:439:85: error: not a duration: 3
We then need to catch each time that they score a four or a six. Remember that the a 
                                                                                     3 on the die means four runs are scored, a 4 means six runs.
.../file.ly:462:31: error: GUILE signaled an error for the expression beginning here
        Dim sixes As Integer '#
                               #
.../file.ly:463:31: error: GUILE signaled an error for the expression beginning here
        Dim fours As Integer '#
                               #
.../file.ly:420:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:473:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:486:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:502:9: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
         
         Console.WriteLine("You have drawn!")
.../file.ly:511:1: error: GUILE signaled an error for the expression beginning here
#
  Neither score is recorded
.../file.ly:512:1: error: GUILE signaled an error for the expression beginning here
#
  Both scores are recorded, adding an extra record (element) to the array
.../file.ly:513:1: error: GUILE signaled an error for the expression beginning here
#
  Concatenate both player names and assign the playerName field of the Topscores array of records
.../file.ly:514:1: error: GUILE signaled an error for the expression beginning here
#
  Have only one score recorded BUT... select which of the players Names is added
.../file.ly:522:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:530:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:562:5: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
They 
     might ask you to keep looping until with an error message until the user inputs a correct answer, to do this you'll need a loop:
.../file.ly:563:0: error: errors found, ignoring music expression

{| class="collapsible collapsed" style="border: none;"
.../file.ly:583:2: error: syntax error, unexpected STRING
{{
  BookCat}}
.../file.ly:583:0: error: errors found, ignoring music expression

{{BookCat}}
.../file.ly:592:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:597:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:599:9: error: syntax error, unexpected '.', expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
  Console
         .WriteLine("Your new score is: " & CurrentPlayerScore) 'this line performs the same function
.../file.ly:604:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:606:30: error: syntax error, unexpected '(', expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
  DisplayCurrentPlayerNewScore
                              (CurrentPlayerScore) 'this line replaces the previous one
.../file.ly:611:2: error: GUILE signaled an error for the expression beginning here
 #
  ##############
.../file.ly:611:4: error: GUILE signaled an error for the expression beginning here
 ###
    ############
.../file.ly:611:6: error: GUILE signaled an error for the expression beginning here
 #####
      ##########
.../file.ly:611:8: error: GUILE signaled an error for the expression beginning here
 #######
        ########
.../file.ly:611:10: error: GUILE signaled an error for the expression beginning here
 #########
          ######
.../file.ly:611:12: error: GUILE signaled an error for the expression beginning here
 ###########
            ####
.../file.ly:611:14: error: GUILE signaled an error for the expression beginning here
 #############
              ##
.../file.ly:611:16: error: GUILE signaled an error for the expression beginning here
 ###############
                
.../file.ly:612:2: error: GUILE signaled an error for the expression beginning here
 #
  Current Score#
.../file.ly:612:16: error: GUILE signaled an error for the expression beginning here
 #Current Score#
                
.../file.ly:613:2: error: GUILE signaled an error for the expression beginning here
 #
  ##############
.../file.ly:613:4: error: GUILE signaled an error for the expression beginning here
 ###
    ############
.../file.ly:613:6: error: GUILE signaled an error for the expression beginning here
 #####
      ##########
.../file.ly:613:8: error: GUILE signaled an error for the expression beginning here
 #######
        ########
.../file.ly:613:10: error: GUILE signaled an error for the expression beginning here
 #########
          ######
.../file.ly:613:12: error: GUILE signaled an error for the expression beginning here
 ###########
            ####
.../file.ly:613:14: error: GUILE signaled an error for the expression beginning here
 #############
              ##
.../file.ly:613:16: error: GUILE signaled an error for the expression beginning here
 ###############
                
.../file.ly:614:16: error: GUILE signaled an error for the expression beginning here
 #    45       #
                
.../file.ly:615:2: error: GUILE signaled an error for the expression beginning here
 #
  ##############
.../file.ly:615:4: error: GUILE signaled an error for the expression beginning here
 ###
    ############
.../file.ly:615:6: error: GUILE signaled an error for the expression beginning here
 #####
      ##########
.../file.ly:615:8: error: GUILE signaled an error for the expression beginning here
 #######
        ########
.../file.ly:615:10: error: GUILE signaled an error for the expression beginning here
 #########
          ######
.../file.ly:615:12: error: GUILE signaled an error for the expression beginning here
 ###########
            ####
.../file.ly:615:14: error: GUILE signaled an error for the expression beginning here
 #############
              ##
.../file.ly:615:16: error: GUILE signaled an error for the expression beginning here
 ###############
                
.../file.ly:616:4: error: syntax error, unexpected STRING, expecting '='
Try 
    and implement this
.../file.ly:621:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:624:15: error: syntax error, unexpected '.', expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
        Console
               .Write("Do you want the game to have a limit on the number of balls?(Y/N) ")
.../file.ly:625:36: error: syntax error, unexpected '.'
        Dim Choice As Char = Console
                                    .ReadLine
.../file.ly:627:12: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
            
            Do
.../file.ly:630:39: error: syntax error, unexpected '.'
                NumberOfBalls = Console
                                       .ReadLine
.../file.ly:633:12: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
            
            NumberOfBalls = 0
.../file.ly:634:12: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
        End 
            If
.../file.ly:636:8: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
    End 
        Function
.../file.ly:644:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:647:25: error: syntax error, unexpected '('
NumberOfBalls = BallLimit
                         ()
.../file.ly:650:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:651:20: error: syntax error, unexpected UNSIGNED, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
For PlayerNo = 1 To 
                    2
.../file.ly:655:19: error: syntax error, unexpected '.', expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
            Console
                   .WriteLine()
.../file.ly:657:16: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
                
                Console.WriteLine(PlayerOneName & " is batting")
.../file.ly:663:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:665:24: error: syntax error, unexpected UNSIGNED, expecting STRING or STRING_IDENTIFIER
                x = x + 
                        1
.../file.ly:666:43: error: syntax error, unexpected '('
                BowlDieResult = RollBowlDie
                                           (VirtualDiceGame)
.../file.ly:671:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:672:44: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
            Loop Until x = NumberOfBalls or 
                                            PlayerOut
.../file.ly:678:181: error: syntax error, unexpected '='
If the player did not set a limit, the number 0 is passed to the variable NumberOfBalls in the PlayDiceGame subroutine. The value of x when it enters the first Do loop is x = 0 + 1 
                                                                                                                                                                                     = 1. It will keep increasing thereafter and so x is always greater than 1. In other words, the condition x = NumberOfBalls will NEVER be satisfied in this case. PlayerOut is the only condition that may work. Therefore, there is no limit on the number of balls
.../file.ly:678:309: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
If the player did not set a limit, the number 0 is passed to the variable NumberOfBalls in the PlayDiceGame subroutine. The value of x when it enters the first Do loop is x = 0 + 1 = 1. It will keep increasing thereafter and so x is always greater than 1. In other words, the condition x = NumberOfBalls will 
                                                                                                                                                                                                                                                                                                                     NEVER be satisfied in this case. PlayerOut is the only condition that may work. Therefore, there is no limit on the number of balls
.../file.ly:687:17: error: syntax error, unexpected >
<source lang="vb"
                 >
.../file.ly:696:24: error: syntax error, unexpected '.'
    nameSought = Console
                        .ReadLine()
.../file.ly:701:8: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
        
        If TopScores(position).Name = nameSought Then
.../file.ly:702:12: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
            
            found = True
.../file.ly:704:12: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
            
            position = position + 1
.../file.ly:704:34: error: syntax error, unexpected UNSIGNED, expecting STRING or STRING_IDENTIFIER
            position = position + 
                                  1
.../file.ly:708:7: error: syntax error, unexpected STRING, expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN or '='
    If 
       found <> True Then
.../file.ly:713:75: error: syntax error, unexpected '.'
Loop While found <> True 'loops back to re-entering a name if found = false
                                                                           .
.../file.ly:724:24: error: syntax error, unexpected '}'
                        
                        }
Unbound variable: Reading.2FWriting_Files|stream
Unbound variable: create
Unbound variable: set
Unbound variable: go
.../file.ly:74:4: unknown # object
Unbound variable: print
Unbound variable: create
Unbound variable: set
Unbound variable: go
.../file.ly:179:4: unknown # object
Unbound variable: print
Unbound variable: Bubble_Sort|bubble
.../file.ly:463:1: Unknown # object: #\newline
.../file.ly:464:1: Unknown # object: #\newline
Unbound variable: Neither
Unbound variable: Both
Unbound variable: Concatenate
Unbound variable: Have
.../file.ly:611:4: Unknown # object: #\#
.../file.ly:611:6: Unknown # object: #\#
.../file.ly:611:8: Unknown # object: #\#
.../file.ly:611:10: Unknown # object: #\#
.../file.ly:611:12: Unknown # object: #\#
.../file.ly:611:14: Unknown # object: #\#
.../file.ly:611:16: Unknown # object: #\#
.../file.ly:612:4: Unknown # object: #\C
Unbound variable: Current
.../file.ly:613:4: Unknown # object: #\#
.../file.ly:613:4: Unknown # object: #\#
.../file.ly:613:6: Unknown # object: #\#
.../file.ly:613:8: Unknown # object: #\#
.../file.ly:613:10: Unknown # object: #\#
.../file.ly:613:12: Unknown # object: #\#
.../file.ly:613:14: Unknown # object: #\#
.../file.ly:613:16: Unknown # object: #\#
.../file.ly:614:4: Unknown # object: #\space
.../file.ly:615:4: Unknown # object: #\#
.../file.ly:615:4: Unknown # object: #\#
.../file.ly:615:6: Unknown # object: #\#
.../file.ly:615:8: Unknown # object: #\#
.../file.ly:615:10: Unknown # object: #\#
.../file.ly:615:12: Unknown # object: #\#
.../file.ly:615:14: Unknown # object: #\#
.../file.ly:615:16: Unknown # object: #\#
Unbound variable: Try
fatal error: failed files: ".../file.ly"

exited with status: 1