Categories
Writers Solution

Java program that implements a simple game

Please use the simplest form of Java coding

The game
The objective of this assignment is to write a Java program that implements a simple game. The game involves 4 sheep and one dog who exist in a NxN grid. It is a (greatly simplified) representation of sheep herding with a border collie (see https://voutu.be/2Z-FYXXYkBM for some more background. If you are interested, there is a famous local sheep herding competition at the Longshaw estate just outside Sheffield, and the next one will be in September 2021 – see https://www.longshawsheeDdog.co.uk/ ©).
In our game each sheep and the dog are located in one particular square for each turn of the game. The user controls the dog, and the sheep move (mostly) in response to the Dogs position. Note that in English, the word sheep is both singular and plural.
The aim of the game is to herd the sheep into a single square. The game starts with 4 sheep randomly distributed across the grid. In each turn the user enters the co-ordinates (row and column) of one of the squares, and the dog is moved to that square. The sheep should then move 1 square, according to the following rules:
• Each sheep picks a random number between 1 and 6.
• If the number is in the range 1-3, then the sheep moves away from the dog. This should be to the square directly away from the dog, unless this move would take the sheep off the edge of the grid, in which case it should stay still.
• If the number is 4 or 5, the sheep should move beside the dog — i.e., it should move to a square adjacent to the dog. Unless the dog is close to the edge of the grid there are four possible moves, and the sheep should choose one of these randomly.
• If the number is a 6 then the sheep should move to the same square as the dog
• If the dog is on the same square as the sheep at the start of the turn, then the sheep moves randomly to any adjacent square.
• Sheep move either along rows or along columns, they cannot move diagonally.
The task
Your assignment is to implement this game in Java, and you should follow the instructions below exactly:
• The program must include a sheep class that models each individual sheep. This class must contain an instance variable that tracks which square the sheep is on, and
avoid move(int dogRow, int dogCoi) method that takes the Dog’s current square as an argument and updates the sheeps square using the rules provided above.
• A random integer can be generated by importing java.util.Random, creating a Random object, and using the nextint (6) method on the Random object to generate a random integer between 0 to 5 (i.e. from 0 to less than 6), and nextint (4) to generate a random integer between 0 and 3.
• The grid should be of size N x N, where N is an integer between 3 and 9. You can either set N as a constant, or N can be specified by the user.
• During each turn, the program should display a grid that shows the location of each Sheep and the Dog. You do not need to match the example below exactly. Using letters and a simple grid displayed in the command window or terminal, as shown below, is fine. You can use more a imaginative display if you want to. If you would like a challenge, then you can use the updated EasyGraphics class from the Sheffield package for a graphical display. The version of EasyGraphics .java on the Assignment? section on Blackboard has updated capability for displaying colours. However, you should not use other third-party libraries for a graphical display.
• The game should be run from a class called sheepHerding. java which should have a main method. At the beginning of the game, this class should place the 4 sheep randomly on the grid, and then prompt the user for the co-ordinates of the Dog (you can use EasyReader for this. The move (dogRow, dogCoi) method should then be called for each sheep, and the position of each sheep should be updated. The grid should then be re-drawn and the user prompted for the next turn.
• The game should end if (i) all the sheep are in the same square, or (ii) the user enters a number 0. It should print a message to say either -All sheep in one square!- or -User quit- to indicate the event that caused the program to end.
• You should create other classes and methods as you choose to form a structured, Object-Oriented system. Only create additional classes if they are needed. It is possible to write an acceptable solution with only 3 classes, SheepHerding, sheep and DisplayGame.
Example output
Please enter new dog
Please enter new dog
Moving sheep
row: 3
column (0 to quit): 2
12 3
+——+
II I IS S |
Illi
+——+
2 I S | | |
Illi
+——+
31 I S| |
I I D | |
+——+
Please enter new dog row:
In this example the grid has size 3; sheep are indicated by S, and the dog by D. The example below shows the final turn from a game where the dog succeeds in herding the sheep into a single square on a grid with size 5 (this is quite a rare occurrence!).