import java.util.Scanner; public class V2Skeleton { public static void main(Stri
ID: 3768169 • Letter: I
Question
import java.util.Scanner;
public class V2Skeleton {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
char [][] grid = new char [10][10];
// initialize vampire
System.out.print("Enter (i, j) for vampire: ");
int newI = input.nextInt();
int newJ = input.nextInt();
Creature vampire = new Creature('V', newI, newJ);
// initialize human
// check whether human can move
System.out.print("Would you like human to move? (0: no, 1: yes): ");
int isMove = input.nextInt();
// update and display grid
clearGrid(grid);
vampire.display(grid);
drawGrid(grid);
System.out.println("Vampire at: " + vampire.getI() +
" " + vampire.getJ());
// get next user command
System.out.print("Enter command (0 to quit): ");
int command = input.nextInt();
while (command != 0) { // while not quit
clearGrid(grid);
vampire.update(command);
vampire.display(grid);
// if vampire and human are on same square,
// vampire bites human, game ends
// if game does not end
// human makes random move
// display human on grid
// if vampire and human are on same square,
// human sacrificed himself, game ends
drawGrid(grid);
System.out.println("Vampire at: " + vampire.getI() +
" " + vampire.getJ());
System.out.print("Enter command (0 to quit): ");
command = input.nextInt();
} // while (command != 0)
}
public static void clearGrid(char [][] g) {
for (int i=0; i<g.length; i++) {
for (int j=0; j<g.length; j++) {
g[i][j] = '.';
}
}
}
public static void drawGrid(char [][] g) {
System.out.println("0123456789");
for (int i=0; i<g.length; i++) {
for (int j=0; j<g.length; j++) {
System.out.print(g[i][j]);
}
System.out.println(i);
}
}
public static boolean sameSquare(Creature c1, Creature c2) {
// if c1 and c2 have identical (i, j) coordinates, return true
// else return false
return false;
}
}
class Creature {
// display character for creature
private char pic;
// (i, j) coordinates for creature
private int i = 0;
private int j = 0;
private boolean canMove = true; // can creature move?
Creature(char c, int nI, int nJ) {
// set display character to c
pic = c;
// set position to (nI, nJ)
}
public void setIJ(int nI, int nJ) {
// set (i, j) coordinates for creature
// if new coordinates are invalid, leave current position unchanged
}
public void setMoving(int n) {
if (n == 0)
canMove = false;
else
canMove = true;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public void update() {
// random position update; call update with random argument 1-4
}
public void update(int c) {
// if canMove, update position according to user command c
// 1: j-- (left)
// 2: i++ (down)
// 3: i-- (up)
// 4: j++ (right)
}
public void display(char [][] g) {
g[i][j] = pic;
}
}
Your task is to fill in code in various places in this file, following the steps
given below. If you follow the instructions in this handout, this will be a fairly
straightforward project. Otherwise, you may spend lots of time working on unnecessary
tasks.
The action takes place on a 10 x 10 display grid, similar to the one you used for the Game
of Life project. When the game begins, the user is prompted for the (i, j) coordinates of
the vampire, and the (i, j) coordinates of the human. i and j are the row and column
numbers of the position of an element in a 2dimensional array; hence, the order is
reversed from the usual (x, y) coordinates.
Then the user is asked if s/he would like the human to move. (It’s easier to specify that
the human does not move, for testing the program; normally we would have the human
move, of course!) If so, the human will move in a random direction each turn.
The user types in commands to move the vampire. There are four commands:
1 means go left (j)
2 means go down (i++)
3 means go up (i)
4 means go right (j++)
The vampire is not allowed to move outside the grid. You (the vampire) will try to catch
the human. The human makes random moves, chosen from the ones corresponding to the
four user commands. The game ends when the vampire moves onto the human’s grid
square and bites the human, or when the human accidentally moves onto the vampire’s
grid square and sacrifices himself.
Before you start writing any code, read the skeleton program
carefully and make sure you understand it. You have to add code that adds to the
functionality of the vampire hunt game, according to the steps below.
Both the vampire and the human are objects of the Creature class. Each Creature has a
position, indicated by its i and j private instance variables, a pic variable which contains
the character to be displayed, and a boolean canMove variable, which indicates whether
the Creature is allowed to move. Follow the steps to add to the program.
Step 1:
Allow a Creature to be placed in the requested position on the grid. First, in the
Creature class, fill in the setIJ() method to set i and j. In setIJ(), you should add
code to prevent the Creature from moving outside the grid. Then, add code to the
constructor method Creature() to set the position of the Creature by calling setIJ().
After Step 1, the vampire will be placed at the requested position in the grid. Run
your program now to test this.
Step 2:
Enable a Creature to move, based on the user commands 1 to 4 described above.
In the Creature class, you have to fill in the method
public void update(int c)
The argument c is the number from 1 to 4 that the user typed in.
After Step 2, the vampire will move around the grid, following the user command.
Run your program now to test this.
Step 3:
Declare and initialize a new Creature, the human, in the main method of the
public class. Allow the user to enter the human’s position (see sample run). Use
the character 'H' to represent the human. Add code in the main method to display
the human, after the line that displays the vampire. (Note: this just one tiny line of
code!)
After Step 3, the human will be displayed in the grid. Run your program now to
test this.
Step 4:
Enable a Creature to make random moves, using the method
public void update()
This overloaded update() method simply calls the earlier update() method, with a
random integer 14 as the argument. (Remember the formula to generate random
integers within a desired range.)
Next, in the main method, after reading the user's input for whether the human
can move, call the setMoving() method of the human, passing the user's input as
its argument.
Finally, do the following to test the update() method: In the while loop of the
main method, after the vampire is updated and displayed, update and display the
human. If you run the program, the human should now make a random move each
turn. Once you have this working, remove the testing code.
Step 5:
In the public class, fill in the method
public static boolean sameSquare(Creature c1, Creature c2)
The sameSquare() method returns true if the two creatures are on the same
square, or false if they are not on the same square.
Next, in the main while loop, after the vampire's position is updated and
displayed, use the sameSquare() method to check if the vampire has caught the
human. If so, print "You bit the human!" and break out of the loop to end the
game.
Otherwise, update and display the human, and if the human moves into the same
square as the vampire, print "The human sacrificed himself!" and break out of the
loop to end the game. After this point, the rest of the code that was already in the
while loop (starting with drawGrid(grid)) should be moved inside this “else”
block. Don't forget to print out the position of the human.
Explanation / Answer
// Creature.java
class Creature {
// display character for creature
private char pic;
// (i, j) coordinates for creature
private int i = 0;
private int j = 0;
private boolean canMove = true; // can creature move?
Creature(char c, int nI, int nJ) {
// set display character to c
pic = c;
// set position to (nI, nJ)
setIJ(nI, nJ);
}
public void setIJ(int nI, int nJ) {
// set (i, j) coordinates for creature
// if new coordinates are invalid, leave current position unchanged
if(nI > 10 || nJ > 10 || nI < 0 || nJ < 0){
return;
}
this.i = nI;
this.j = nJ;
}
public void setMoving(int n) {
if (n == 0)
canMove = false;
else
canMove = true;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public void update() {
// random position update; call update with random argument 1-4
update((int)(Math.random() * 4 + 1));
}
public void update(int c) {
// if canMove, update position according to user command c
// 1: j-- (left)
// 2: i++ (down)
// 3: i-- (up)
// 4: j++ (right)
if(canMove){
switch(c){
case 1:
setIJ(i, j - 1);
break;
case 2:
setIJ(i + 1, j);
break;
case 3:
setIJ(i - 1, j);
break;
case 4:
setIJ(i, j + 1);
break;
}
}
}
public void display(char [][] g) {
g[i][j] = pic;
}
}
// V2Skeleton.java
import java.util.Scanner;
public class V2Skeleton {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
char [][] grid = new char [10][10];
// initialize vampire
System.out.print("Enter (i, j) for vampire: ");
int newI = input.nextInt();
int newJ = input.nextInt();
Creature vampire = new Creature('V', newI, newJ);
// initialize human
System.out.print("Enter (i, j) for human: ");
newI = input.nextInt();
newJ = input.nextInt();
Creature human = new Creature('H', newI, newJ);
// check whether human can move
System.out.print("Would you like human to move? (0: no, 1: yes): ");
int isMove = input.nextInt();
human.setMoving(isMove);
// update and display grid
clearGrid(grid);
vampire.display(grid);
human.display(grid);
drawGrid(grid);
System.out.println("Vampire at: " + vampire.getI() +
" " + vampire.getJ());
System.out.println("Human at: " + human.getI() +
" " + human.getJ());
// get next user command
System.out.print("Enter command (0 to quit): ");
int command = input.nextInt();
while (command != 0) { // while not quit
clearGrid(grid);
vampire.update(command);
vampire.display(grid);
// if vampire and human are on same square,
// vampire bites human, game ends
if(sameSquare(vampire, human)){
System.out.println("You bit the human!");
break;
}
// if game does not end
// human makes random move
// display human on grid
// if vampire and human are on same square,
// human sacrificed himself, game ends
human.update();
human.display(grid);
drawGrid(grid);
if(sameSquare(vampire, human)){
System.out.println("The human sacrificed himself!");
break;
}
System.out.println("Vampire at: " + vampire.getI() +
" " + vampire.getJ());
System.out.print("Enter command (0 to quit): ");
command = input.nextInt();
} // while (command != 0)
}
public static void clearGrid(char [][] g) {
for (int i=0; i<g.length; i++) {
for (int j=0; j<g.length; j++) {
g[i][j] = '.';
}
}
}
public static void drawGrid(char [][] g) {
System.out.println("0123456789");
for (int i=0; i<g.length; i++) {
for (int j=0; j<g.length; j++) {
System.out.print(g[i][j]);
}
System.out.println(i);
}
}
public static boolean sameSquare(Creature c1, Creature c2) {
// if c1 and c2 have identical (i, j) coordinates, return true
// else return false
if(c1.getI() == c2.getI() && c1.getJ() == c2.getJ()) return true;
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.