Subroutines Flashcards

1
Q

ResetDataStructures

A

Clears all arrays and the grid:
- Empties the puzzle[] array
- resets the 2D array puzzle grid
- empties the solution[] array
- clears answer[] - clears the record of answers you input

Called when:
- At the start of static void LoadPuzzle()
- In an IF statement in LoadPuzzle()
- Called in static void NumberPuzzle when the main game is started

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

LoadPuzzleFile

A

Reads and loads the PUZZLE file:
- tries to read the input file, adding ‘.txt’ to the end
- the puzzle[] array is populated by reading the file - e.g. puzzle[0] contains the information in the first line of the file
- checks if the given file actually contains data
- uses a try-catch statement: the catch part is used if the program cannot locate the file
returns true if it works

Called when:
- static void LoadPuzzle(), loaded as true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

LoadSolution

A

Reads and loads the SOLUTION file:
- This file cannot be loaded at the starter
- asks you to enter the name of the file, ‘Puzzle1’, and adds ‘S’ onto the end
- the solution file contains the correct values of each coord
- in the first line, it contains the values of each coord on the first row
- e.g. the first row is 865192437, so in [1,1], the value stored is 8, etc

Called when:
- static void LoadPuzzle(), checks if LoadSolution returns true (this is done only if LoadPuzzleFile returns true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ResetAnswer

A

Used when you enter a number. It resets the grid and adds your value at the desired coordinate
- answer[] is a 162 long array where the first three items are taken up by puzzle name, score, and number of attempts
- therefore max input is 159
- uses a FOR loop to fill the rest of the 159 spaces with nothing

Called when:
- static bool TransferPuzzleIntoGrid()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

TransferPuzzleIntoGrid

A

Takes the file and puts it into an empty grid:
- sets int ‘row’ to be first digit of the three digits in the cell
- sets int ‘column’ to be the second digit of the three digits in the cell
- sets char ‘digit’ to be the third digit of the three digits in the cell
- repeats this for each number in the puzzle[] array
- writes ‘digit’ to the ‘puzzleGrid’ 2D array using puzzleGrid[row][column]

Called when:
- static void LoadPuzzle(), checks if TransferPuzzleIntoGrid returns true (this is done only if LoadSolution returns true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

LoadPuzzle

A

Loads the puzzle:
- checks to make sure LoadPuzzleFile() is true
- if LoadPuzzleFile() is true, it then checks to make sure LoadSolution() is true
- if any is false, it runs ResetDataStructures()

Called when:
- static void LoadPartSolvedPuzzle(), to… load the partially solved puzzle
- static void NumberPuzzle(), when option ‘L’ is pressed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

TransferAnswerIntoGrid

A

Transfers the answer[] array into the 9x9 grid:
- uses a FOR loop to convert each item in the answer[] array
- cellInfo[0] = row, cellInfo[1] = column, cellInfo[2] = digit to be input
- the FOR loop repeats until line < Convert.ToInt32(answer[2].ToString()) + 3
- you must +3 since if answer[2] is 0 (0 attempts), you still need to do the loop once

Called when:
- static void LoadPartSolvedPuzzle(), to put the answer array in the partially solved puzzle into the 9x9 grid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

LoadPartSolvedPuzzle

A

Loads a semi-completed puzzle file:
- adds ‘P.txt’ to the puzzle name to recognise it as a partially solved puzzle
- checks if the first line of the file says ‘puzzle1’
- if the file is valid, it adds the 3 digits of each number in the text document into the answer[] array, until the entire file has been read
- then puts this answer[] array into the grid using TransferAnswerIntoGrid()

Called when:
- static void NumberPuzzle(), when option P is pressed to load a partially solved puzzle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

DisplayGrid

A

Prints out the 9x9 grid:
- uses a FOR loop, skipping (0,0) because that is where X is stored as a check

Called when:
- static void SolvePuzzle(), to print out the puzzle grid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

SolvePuzzle

A

Run when the user wants to attempt to solve the puzzle:
- uses [0,0] as a check to ensure that the puzzle is loaded
- takes the user’s row column digit input (the three digits), and sets the first digit to row, second to column and third to the value
- has bool inputError set to False (true under certain conditions e.g. the input isn’t 3 long, if the input cannot be converted into an integer)
- puts the coords and value into the puzzle grid
- increments the attempts item in answer[] (answer[2]) by 1
- asks for the next entry and prints the updated grid

Called when:
- static void NumberPuzzle(), when the user presses ‘S’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

DisplayMenu

A

Prints out the options:
- just a bunch of console.writeline()

Called when:
- static void NumberPuzzle(), showing the user what options they have

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

GetMenuOption

A

Takes in userinput for the menu option:
- uses a WHILE loop to ensure that the input string is only one character long
- returns the userinput
- (it returns the first character choice[0])

Called when:
- static void NumberPuzzle(), after DisplayMenu() is called to print the options

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

KeepPuzzle

A

Saves your answers to a text doc:
- uses [0,0] to check that a puzzle has been loaded
- checks answer[2] (attempts) is > 0 to make sure that the user actually made attempts to store
- stores the partially solved puzzle as “puzzle1P”
- records the answer[] array into the file
- uses a FOR loop for this, going up to the number of attempts + 3, since the first three items in the answer[] array are used for info

Called when:
- static void NumberPuzzle(), when option ‘K’ is pressed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

CheckSolution

A

Checks the values that have been put on the grid (i.e. checks to make sure that the user’s answers are right);
- has two bools, incomplete and correct
- uses two nested FOR loops to cycle through each coord
- if a coord has an empty space, incomplete is set to true
- it checks the coord against the solution[] array
- this is a 1D array, however this subroutine uses solution[row][column]
- it takes the row from the text file (here called [row]) and then the character from that row (here called [column])
- the solution file contains the correct values of each coord. in the first line, it contains the values of each coord on the first row
- if correct is false, it states how many errors are made and continues the game. if correct is true, the game is complete

Called when:
- static void NumberPuzzle(), when option ‘C’ is pressed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

CalculateScore

A

Calculates your score based on how many errors you made:
- score is stored in answer[1], and is set to 0 at the start
- so the maximum score you can get is 0
- it takes the error count from CheckSolution() and takes it away from answer[1] (0)

Called when:
- static void NumberPuzzle(); when option ‘C’ is pressed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

DisplayResults

A

Called when you exit the game, to show your statistics:
- checks answer[2] if you made any attempts
- if you didn’t make any attempts, it outputs “you didn’t make a start”
- else, outputs your score from answer[1]
- outputs your solutions for the puzzle, by using a FOR loop starting at 3, ending at number of attempts + 3, printing answer[]

Called when:
- static void NumberPuzzle(), after the subroutine ends the menu option IF statement, if the number of attempts is greater than 0, it prints DisplayResults()
- (i.e. when you exit)

17
Q

NumberPuzzle

A

The main body where all things are called and stuff:
- initialises the variables used throughout the code
- prints DisplayMenu() and gets the menu option input from user, calling different subroutines depending on their input
- uses random when an invalid menu option is input, to generate 1 of 6 error messages
- prints DisplayResults once the subroutine ends (when the user is exiting)

Called when:
- static void Main()