A-level Computing/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Skeleton code/2016 Exam/Skeleton code 2016 Problems(section D in comp1 and most likely section c in paper1 this year)

This is Comp1 for the new Computer Science Specification even though it is under the Computing Wiki. Keep in mind that there is another Comp1 exam for Computing (for retakes?) and this is the place for this older exam. There should be a new wiki for Computer Science with links from both wikis to each other.

Welcome to the paper 1 section C (what used to be section D in past COMP1 papers) for the new 2016 AQA Computer Science Exam.

This is where suggestions can be made about what some of the questions might be and how can we solve them.

Please be respectful and do not vandalise or tamper with the page, as this would affect students' preparation for their exams!

I wish you all good luck with the exam, hopefully we can make it a little easier on ourselves by working together.

Checking to see if you're off the board - If the player enters over 9 or under 0 it will crash the game.
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution: Answer :

This is a simple way to check the validity of row and column's entered values to ensure they do not exceed the size of the board. The maximum size of the board being 9 and minimum size being 0.

Another way could be to use the 'Try and Except' function in Python, this would help to cover multiple input errors and include the method above in a slightly more efficient way.

This is some more top-notch code from your friendly neighborhood Oliver James Cooke.

Java Solution

Answer :

Alternative, checking column and then row separately: // Author Asadullah H int[] getRowColumn{ int column = 10; int row = 10; int[] move; move = new int[2]; // make sure column is on the grid while(column < 0 || column > 9){ console.println; column = console.readInteger("Please enter column: "); if((column < 0 || column > 9)){ console.println("That number is not valid."); }   } // make sure row is on the grid while(row < 0 || row > 9){ console.println; row = console.readInteger("Please enter row: "); if((row < 0 || row > 9)){ console.println("That number is not valid."); }   }    move[0] = row; move[1] = column; return move; } Alternative, with no checking, instead invalid values are truncated.

C# Solution

Answer :

p.s. (if you can't do this, you will fail)

Add a feature which tells the player the type of warship they hit.
This feature may defeat the point of the game. But we shall give it a go anyway.

Delphi/Pascal Solution Answer :

VB.NET Solution Answer - Better :

Answer :

Python Solution Answer - Better :

Answer :

Java Solution

Answer :

C# Solution

Answer :

No error checking for erroneous inputs - if a string is entered the program crashes!
This is highly unlikely to come up as it is already implemented in Java but it is good practice nonetheless.

Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Save Game File - The player is able to save an unfinished game.
This would involve adding another menu item between 2 and 9 perhaps to load a saved game?

Saving a game part way through would mean just saving a .txt file and having the option to open it much in the same way a training file works.

We would also need to make sure that when a game is saved it deletes the previous save file, or maybe allow for there to be multiple save slots.

Delphi/Pascal Solution Answer :

VB.NET Solution Answer (works) :

VB.NET Solution Answer:

Answer :

Python Solution Answer :

You will need to add a call to this subroutine into the main PlayGame(Board) subroutine to get it to save the Progress after each turn.

Java Solution

Answer :

C# Solution

Answer :

How would the game be extended to use a bigger board?
Unlikely as all the ship data would need to be changed. It would take too long in the exam.

Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

How could the game be changed to add another ship?
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution Answer :

Game crashes at the main menu if a string is entered or if the menu option does not exist.
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

On menu you can enter any number. But it crashes when you enter a letter.
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Limit the number of moves for the player to use.
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

No score is given (tell user how many moves they took to win).
-Give % accuracy?(user moves/least possible moves) -Could also add a high score list written to a file (Which would mean adding -another option in the menu).

Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Crashes if no input on column or row: (possibly use a try catch statement).
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer : One to use the 'Try and Except' function in Python is shown below, this would help to cover a simple ValueError (string typed in place of an integer input); it also works for the input of a NULL value.

Java Solution

Answer :

C# Solution

Answer :

Add multiple ships of the same type in one game.
Delphi/Pascal Solution Answer :

VB.NET Solution bad Answer :

Python Solution Answer : When I modify the code to show the position of the ships when the game is played, I get this result: I now have two patrol boats on the board.

Java Solution

Answer :

C# Solution

Answer :

Validate Position of Shot
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Add a multiplayer option (another person or the computer).
Delphi/Pascal Solution Answer :

VB.NET Solution

Answer :

Python Solution Answer : This code overhaul allows two players to compete. Player 1 is represented on the game board by pieces labelled 'A'. Player 2 is represented on the game board by pieces labelled 'B'. This retains the neat structure of the game board.

An alternative method to multiplayer. Although Neil's code suffices, this code might be more efficient.

Java Solution

Answer :

C# Solution

Answer :

Add a message for destroying warships (can be coded in a similar way to CheckWin (Board)).
This adds a message to the user as soon as a Ship has been destroyed.

It may quite easily crop up in the exam, as a simple way to get it to work is to copy and modify the existing CheckWin (Board) subroutine.

This works by scanning the entire board to check for the existence of ships. You can then check which exist.

Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer : This subroutine works similarly to the existing CheckWin(Board). It is called in the same position, too.

Probably not the best example that could be coded in, but it works, and in a way that has already been demonstrated in the skeleton code.

The PlayGame(Board, Ships) subroutine has had a simple line added to call the CheckShipsSunk(Board) subroutine when the game is running.

Answer - Alternative :

This solution is simple and uses the lengths of the ships stored in the ships' 2D array.

def MakePlayerMove(Board, Ships): Row, Column = GetRowColumn if Board[Row][Column] == "m" or Board[Row][Column] == "h": print("Sorry, you have already shot at the square (" + str(Column) + "," + str(Row) + "). Please try again.") elif Board[Row][Column] == "-": print("Sorry, (" + str(Column) + "," + str(Row) + ") is a miss.") Board[Row][Column] = "m" else: print("Hit at (" + str(Column) + "," + str(Row) + ").") for Ship in Ships: if Ship[0][0] == Board[Row][Column]:    # Checks which ship has been hit Ship[1] -= 1                          # Subtracts 1 from its length in the array if Ship[1] < 1:                       # Checks if the ship has been hit as many times as its length print('Ship sunk!') Board[Row][Column] = "h"

Java Solution

Answer :

C# Solution

Answer :

Bonus special missile when you sink a ship (such as one with a larger range). (unnecessary)
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Could ask for leaderboard. (very unlikely)
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Clear the environment after every turn has taken place. (pointless)
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Randomize ship positions
VB.NET Solution Answer :

Allow the player to fire a salvo (i.e. simultaneously enter coordinates for three shots and then make the move)
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Python Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Don't subtract a move when a coordinate has been re-entered.
Delphi/Pascal Solution Answer :

VB.NET Solution Answer : This shows how I subtract 1 from the current score every time the same coordinate is entered:

Python Solution Answer : How to add a set number of turns and how to not miss a turn when the same coordinate is re-entered:

Java Solution

Answer :

C# Solution

Answer :

Allow user to enter their own ships on the board
Delphi/Pascal Solution Answer :

VB.NET Solution Answer :

Java Solution

Answer :

C# Solution

Answer :

Python Solution Answer :

Load Text File
VB.NET Solution Answer :

Delphi/Pascal Solution Answer :

Python Solution Answer :

Allow diagonal placement of ships
Java Solution j Answer :

Python

Answer :

Changes have to be made to 3 of the functions; ValidateBoatPoisition, PlaceShip and PlaceRandomShips:

Allow user to enter two-digit co-ordinates, so 35 instead of 3 then 5
VB.NET Solution Answer 1 :

Answer 2 :

Python Solution

Answer:

Java Solution

Answer:

If a miss, scan one element near the miss to see if a ship is present
VB.NET Solution Answer :

Python Solution Answer : Java Solution:

Answer :

Performing Run-length Encoding on the 'Board', possibly useful when saving the game.
Performing some Run-length Encoding on the 'Board', possibly useful when saving the game. This would work in a similar way to the 'Generate FEN' question from last year's COMP1 paper.

Python Solution: Answer :

C# Solution

Answer :

Torpedo Function: fires vertically until a ship is hit or a column has been torpedoed.
Java Solution

Answer :

Python

Answer :

Changes have to be made to the subroutine 'PlayGame' as well as creating a new subroutine 'MakeTorpedoMove'.