In this assignment you make a simple pacman-like game with


Programming Fundamentals - Assignment

Note: Any student who is participating in the software studio should email Raymond Lister and tell him whether or not they intend to do the assignment. They should include their student number and their full name (as shown on their student card) in their email.

Amendment: To be eligible for a mark on Part A of the assignment, a student need NOT have completed the final additional lab test on each thread (i.e. "Additional ListOfNV3PartA" on thread 1 and "Additional ListOfNV3PartB" on thread 2). A student must complete those two lab tests to be eligible for a mark on Part B of the assignment.

Assignment: Part A and Part B (released May 19).

There are two parts to the assignment, parts A and B. Both parts are due on the same date. You have to use your solution to Part A to do Part B, so you have to do Part A first. If you wish, you need only do Part A. Immediately below are the details of Part A. Scroll down a few pages to see the details of Part B. Please note that at the end of Part B, there are important instructions and rules which are applicable to both parts.

Introduction
In this assignment you make a simple Pacman-like game with one player, 3 dots, an exit and an enemy. The player can move left, right, up or down and must collect all 3 dots and reach the exit without being killed by the enemy. The enemy is programmed to chase down the player. The game finishes when either:

(1) all the dots have been collected or the exit has been reached, or
(2) when the enemy has killed the player.

Sample output is shown below. Your program should produce input/output in exactly the following format, with user input shown underlined and in bold:

Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 3
Initial x: 5
Initial y: 0
Player[](5,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(5,5)
Move (l/r/u/d): l
Player[](4,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(5,4)
Move (l/r/u/d): l
Player[](3,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(5,3)
Move (l/r/u/d): l
Player[](2,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(5,2)
Move (l/r/u/d): l
Player[](1,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(5,1)
Move (l/r/u/d): d
Player[*](1,1) Dot(-,-) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(4,1)
Move (l/r/u/d): r
Player[*](2,1) Dot(-,-) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(3,1)
Move (l/r/u/d): d
Player[**](2,2) Dot(-,-) Dot(-,-) Dot(3,3) Exit(4,4) Enemy(2,1)
Move (l/r/u/d): r
Player[**](3,2) Dot(-,-) Dot(-,-) Dot(3,3) Exit(4,4) Enemy(2,1)
Move (l/r/u/d): d
Player[***](3,3) Dot(-,-) Dot(-,-) Dot(-,-) Exit(4,4) Enemy(2,2)
Move (l/r/u/d): r
Player[***](4,3) Dot(-,-) Dot(-,-) Dot(-,-) Exit(4,4) Enemy(2,3)
Move (l/r/u/d): d
Player[***](4,4) Dot(-,-) Dot(-,-) Dot(-,-) Exit(-,-) Enemy(2,4)

Explanation of the above sample output

The program begins by asking the user to input the initial position for the player. The x coordinate increases from left to right and the y-coordinate increases from top to bottom.

The position of each Player, each Dot, the Exit and the Enemy are shown in the format (x,y).

Once a dot has been collected, or the exit has been successfully reached or the player has been killed, its position is instead shown as (-,-) with coordinates replaced by the - symbol. The user plays the game by entering move commands (l/r/u/d) indicating move left, right up or down, until the game is over.

In Part A, you will build only a fragment of this game. In Part B, you make it all functionality.

Submission to PLATE

This assignment is divided into separate tasks described below. You must submit your progress to plate regularly while you are working on each task. This will provide us with a record that you have been doing your own work. If two students submit the same solution, your submission record may be used by the Academic Misconduct Committee to determine who did Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 4 the work and who copied. For more details on assignment submission, return and other important rules, scroll down to the last few pages.

Solution requirements
To receive any marks, your solution must meet the following minimum requirements:

1. The tasks must be implemented in the order specified in section "Tasks" below.
2. Your solution must use only the features of Java that are taught in this subject. For example, you must not use inheritance, exceptions, varargs (e.g. printf), interfaces, or generics. Furthermore, your solution must NOT use arrays.

3. Your program's output must exactly match the output given by PLATE.

Part A
Due date: 11:59pm Sunday June 11. (i.e. the same date and time as Part B).
Value: 10%. That is, Part A is worth 1/3 of the total of 30 marks for the entire assignment.
Topics: Data flow, OO programming basics, Control flow.
Objectives: This assignment supports objectives 1-4.

Tasks
These tasks must be completed in the order listed below.

Task 1: Classes and fields

In a new BlueJ project write code to produce the following three classes (written in italics) and their "fields" / "private data members" / "variables" (enclosed in double quotes):
- A Game has 1 instance of the Player class, called "player", and 3 instances of the Dot class (called "dot1", "dot2" and "dot3"). (An instance of a class is an object.)

- A Player has an "x" and "y" value (both integers) which together describe the position of the player. The coordinate system of the x and y position is as follows: the xcoordinate increases from left to right, and the y-coordinate increases from top to bottom. The player also keeps a counter (an integer) of the number of dots collected (called "collectedDots").

- A Dot has an "x" and "y" position with the same coordinate system as the player, and also has a boolean status (i.e. a variable which has either the value true or the value NOT June 5 as printed incorrectly in the outline.

Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 5 false; look it up) called "exists" which is initially true and is set to false whenever the dot has been collected by the player.

You should also add a fourth class, called Util, which for Part A is given to you and is as follows:
public class Util {
public static String coordStr(int x, boolean shown) {
return shown ? (""+x) : "-";
}
public static String objectStr(int x, int y, boolean shown) {
return "(" + coordStr(x, shown) + "," + coordStr(y, shown) +
")";
}
}
The "?" operator in the Util method called "coordStr" has not been discussed in lectures, but you can look it up yourself.

The names of your classes and fields should exactly match those in the above system description, and must follow Java's naming conventions (i.e. class names begin with an uppercase letter, field names begin with a lowercase letter, compound nouns (google it) are joined without a space and the second word begins with an uppercase letter).

All classes must be public and all fields must be private.

Task 2: Constructors

In this task, you will add constructors to each class to initialise the program.

2.1 Dot
Define a constructor for class Dot that takes the initial x and y values as parameters, and copies these initial values into the x and y fields. The Dot constructor should also initialise the "exists" field to true.

2.2 Player
Define a constructor for class Player that takes the initial x and y values as parameters, and copies these initial values into the x and y fields. The collectedDots fields should also be Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 6 initialised to 0.
Submit your code to PLATE to receive marks for this part and do the same for each subsequent part below. You are required to submit your progress to plate while you are making progress.

2.3 Game
Define a constructor for class Game that takes the initial x and y positions of the player as parameters and creates and initialises the player, dot1, dot2 and dot3 fields. Hint: creating and initialising player, dot1, dot2 and dot3 is analogous to creating and initialising:
- "scanner" in the lab test class "ThreeNumbers"
- "summer1" or "summer2" in the main method of the lab test class "SummerOO"

The player should be created at the position given by the parameters. dot1, dot2 and dot3 must be created at positions (1,1), (2,2) and (3,3) respectively.

Preamble to Task 3 and Task 4
Tasks 3 and 4 can mostly be completed in any order. It is recommended that you do the easy
parts first and leave any hard parts (labelled "advanced") until after you have completed the
easy parts.

Task 3: move() method
Add the following method "move" to the class Game:
public void move(int dx, int dy) {
player.move(dx, dy);
}
Then add a "move()" method to the Player class that moves a player by the given relative distance (dx,dy). That is, if the player is currently at position (x,y), the player should be moved to position (x+dx, y+dy). For example, if the player is currently at position (3,4) and the move method is used with parameters dx=2 and dy=3, then the player should move to position (5,7). Advanced: (You can try this after completing Task 4)

Add the following lines of code to the move() method in class Game:
player.collect(dot1);
player.collect(dot2);
player.collect(dot3);

Then add a "collect()" method to the Player class to collect the dot specified as the parameter if Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 7 that is possible. The player can collect a dot only if the player is at the same position as the dot.

When the player collects a dot, the player's "collectedDots" count should be increased by 1. When the player collects a dot, the dot should disappear. To implement that, as part of the "collect()" method in the class Player, there should be a call to a "disappear()" method in the class Dot. That method in Dot should contain a single line of code in the body:
exists = false;

Task 4: toString()
Add "toString()" methods to each of the classes Game, Player and Dot containing the following code in the body of the method:
 Class Game:
o return player + " " + dot1 + " " + dot2 + " " + dot3;
 Class Player:
o return "Player[" + collectedDots + "]" + Util.objectStr(x, y, true);
 Class Dot:
o return "Dot" + Util.objectStr(x, y, exists);
(Do not vary from the lines of code specified above; a returned string must exactly match the above format in order to receive the marks from PLATE.)

Thus a call to the toString() method of class Game returns (but does not print) a string of the form:
Player[0](0,0) Dot(1,1) Dot(2,2) Dot(3,3)
The above string indicates the positions of the player and 3 dots, as well as the number of dots collected by the player. Here, [0] indicates the player has collected 0 dots, and each (x,y) indicates the position of a player or dot.

If a player moves to position (2,2) and collects dot2, the string should appear as follows:
Player[1](2,2) Dot(1,1) Dot(-,-) Dot(3,3)
Showing the dot's position as (-,-) indicates that the dot has been collected and has disappeared.

Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 8

Part B

Topics: Data flow, OO programming basics, Control flow.

Objectives: This assignment supports objectives 1-5.

Introduction

In Part B of the assignment, you will finish the game that you started building in Part A, using your solution to Part A as a starting point.

Note!! Your Part B solution should be submitted on PLATE under the link "Assignment 1 Part B". Be careful not to submit under the link "Assignment 1 Part A".

In Part B, you are free to redesign any aspect of your code except for the following:

- Certain class names must be as specified:
? From Part A: Game, Player, Dot.
? Introduced in Part B: Enemy, Exit, Main.
- Certain method headers must be as specified:
? From Part A: Game.move(dx,dy).
? Introduced in Part B: Game.input() and Game.start(). Also, each object must provide an appropriate toString() function.

Apart from these requirements, you may add, remove or rename fields, add, remove or rename methods, and add classes. Your design choices will be reflected in your design mark (see the section "Marking Scheme" below).

NOT June 5 as printed in the outline.
Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 10

Tasks

Task 1: Exit
Add 1 "exit" object to the game which has the position (4,4). Design the class "Exit" such that its position can be specified by constructor parameters. Your Game's toString() function should now produce a string in the following format:
Player[](0,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4)
A player reaches the exit when the player's position is the same as the exit's position. Note that the number of dots collected by the player is now represented as [] if no dots have been collected. These square brackets are to be filled with a * symbol for each dot collected. For example, if two dots have been collected, this is represented as [**].
If the player reaches the exit and has already collected all 3 dots, the exit is "opened" and the game's toString() function displays the exit as "Exit(-,-)" instead of "Exit(4,4)". That is, the game's toString() function returns:
Player[***](4,4) Dot(-,-) Dot(-,-) Dot(-,-) Exit(-,-)
The suggested development sequence for submitting to PLATE is:
1. Define class Exit with appropriate fields and an appropriate constructor, and create the Exit.
2. Define an appropriate toString() function for Exit and submit to PLATE.
3. Modify your program so that the player performs actions in this order: (1) the player moves, (2) the player collects a dot if possible, (3) the player reaches an exit if possible.
You may define new methods as appropriate for the task, but the design choice is yours.
Submit to PLATE.
4. Modify the Player's toString() to display collectedDots as a series of * symbols. Submit to PLATE.

Task 2: input() method
Define a method called input() in class Game that takes no parameters. This method should present the user with the following menu:
Move (l/r/u/d):
This prompt should be printed with a single space following the colon, and should be printed in a way that allows the user typpea movement command on the same line.
Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 11
If the user types l, r, u or d at the prompt, you should invoke the move() method with appropriate directional arguments that cause the player to move one step left, right, up or down respectively. If the user enters an invalid movement command, you should print the error message "Invalid move".
HINT: You should probably use a switch statement to implement this movement menu.The switch statement
The switch statement is more clear than a if/else statement when a variable is compared to a series of different
constant values:
If Statement Equivalent Switch Statement
if (place == 1)
{
System.out.println("Gold medal!");
}
else if (place == 2)
{
System.out.println("Silver medal!");
}
else if (place == 3)
{
System.out.println("Bronze medal!");
}
else
{
System.out.println(
"Sorry, you didn't place.");
}
switch (place)
{
case 1:
System.out.println("Gold medal!");
break;
case 2:
System.out.println("Silver medal!");
break;
case 3:
System.out.println("Bronze medal!");
break;
default:
System.out.println(
"Sorry, you didn't place.");
break;
}
An example of using a switch statement to process menu of commands
System.out.print("Would you like to quit? (Y/N) ");
String line = keyboard.nextLine();
char answer = line.charAt(0);
switch (answer)
{
case 'y':
case 'Y':
System.out.println("Bye.");
break;
case 'n':
case 'N':
System.out.println("Excellent!");
break;
default:
System.out.println("Invalid answer.");
break;
}
At this point, submit your solution to PLATE to receive marks for this task. Then continue reading to earn the remainder of the marks.
Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 12

Task 3: start() method
Define a start() method in class Game taking no parameters. This method should repeatedly print the game's string representation and invoke the game's input() method until the game is over. The game is over when the exit is open. After the game is over, the game's string representation should be printed once more followed on a separate line by the message "You win!".

Define a class called Main with a public static void main(String [] args)method. The main() method should ask the user for the initial (x,y) position of the player according to the following sample I/O:
Initial x: 2
Initial y: 0
The numbers underlined and in bold represent input typed by the user. This is "sample" input. Your program should read actual numbers from the user via the Scanner class. The user must not enter negative numbers and is given 3 chances to input a valid number for each coordinate. To handle this, your program must follow the sample I/O below:
Initial x: -3
Must not be negative.
Initial x: -1
Must not be negative.
Initial x: 4
Initial y: -7
Must not be negative.
Initial y: -7
Must not be negative.
Initial y: -7
Must not be negative.
Too many errors. Exiting.
Note that the user made only 2 mistakes when entering the x position, and the third input was finally accepted. However, the user made 3 mistakes when entering the y position, and so the program was aborted.
NOTE: due to a quirk in the behaviour of Scanner's nextInt() method, you must follow each use of nextInt() by an invocation of nextLine() as shown in the example below:
int number = keyboard.nextInt(); keyboard.nextLine();
Without adding the extra nextLine(), your program might generate the error
"StringIndexOutOfBoundsException"
If the user enters valid initial x and y position, create the Game with the user's chosen initial and (x,y) position for the player, and invoke the game's start() method.
Test your program by running the main() method in BlueJ.
At this point, submit your solution to PLATE to receive marks for this task. Then continue reading to earn the remainder of the marks.
Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 13

Task 4: Enemy
If the user specifies the player's initial x-position to be 5, the game will be started in "advanced mode". In advanced mode, one more object is introduced into the game called the "Enemy". The Enemy is created at position (5,5) and the game's toString() function displays as follows:
Player[](5,0) Dot(1,1) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(5,5)
Each time after the player moves one step, the enemy also moves one step in the general direction of the player. If the enemy catches up with the player, the enemy kills the player and the game's toString() functions displays, for example, as follows:
Player[*](-,-) Dot(-,-) Dot(2,2) Dot(3,3) Exit(4,4) Enemy(2,1)
Displaying the Player's position as (-,-) indicates that the player was killed. In such a case, the main game loop should also terminate and display the message "You lose!" instead of "You win!".

To the user, it will appear as though the player and enemy move simultaneously in each move of the game. However, the computer must still carry out actions in a particular sequence. In this game, the player should be asked to move, collect dots and potentially reach an exit before the enemy is asked to move. If the player reaches an exit and the enemy attempts to kill the player in the same move, the player exits and wins the game without being killed. An interesting case occurs when the player and enemy are adjacent, the enemy takes one step toward the player and the player takes one step toward the enemy. As a result, the enemy and player swap places and neither before nor after the step do the two coincide at the same position. If not programmed carefully, the player might pass right through the enemy without dying. You need to make sure that the enemy does in this case catch and kill the player in passing.

Each time the enemy is about to move one step, it either decides to continue moving in the same direction that it moved in its previous step, or if that direction will not bring the enemy closer to the player, the enemy will decide to change direction. The enemy will also invoke this decision process on its first step because it has no previous direction to remember.

When deciding to change direction, the enemy considers the distance between the enemy's and player's x positions (the "x distance") and the distance between the enemy's and player's y positions (the "y distance"). If the "x distance" is further than the "y distance", the enemy will change direction toward the player along the x axis (i.e. either left or right). If the "y distance" is further than the "x distance", the enemy will change direction toward the player along the y axis (i.e. either up or down). Otherwise, the enemy will choose a special neutral direction which has the effect of causing the enemy to not move.

Programming Fundamentals (48023) Autumn 2017 Assignment: Part A and Part B (released May 19). Page 14

This task is complex but it is possible in some cases to receive partial marks for partial completeness. Make sure that you submit regularly to PLATE to see what scenarios are tested and marked first.

At this point, submit your solution to PLATE to receive marks for this task.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: In this assignment you make a simple pacman-like game with
Reference No:- TGS02361176

Now Priced at $20 (50% Discount)

Recommended (94%)

Rated (4.6/5)