Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVAJAVAJAVA PLEASE NOT C OR C SHARP Homework 05 Mine Walker Objective Write a j

ID: 3677805 • Letter: J

Question

JAVAJAVAJAVA PLEASE NOT C OR C SHARP Homework 05 Mine Walker Objective Write a java program MineWalker where you are an X trying to get an ice cream cone in a mine field. Specification: Before the game starts, a field of mines are created. The board has to be first initialized There mines occupy a tenth of the board (IE (BoardSize x BoardSize)/10 = the number of mines) The mines are randomly placed on the board. If a space which is already occupied (either by the player, the ice cream cone, or another mine) is selected then another space must be selected until an empty space is found. The player is placed at 0,0 The ice cream cone is placed at a random location on the board At each turn, the player chooses to move in the X or Y direction by entering either -1, 0, or 1, where -1 is going one space in the negative direction 1 is going one space in the positive direction (remember positive for Y is down) 0 is staying still 9 quits the game Anything other than these values should prompt the player that they have inputted an invalid value and then not move in that direction (IE 0). Before each turn the board is displayed indicating where the player (X) and the goal (^) are located. Unoccupied spaces and mines are denoted by and underscore (_). Remember mines need to be hidden so they are also underscores (_). The board is maximum 10 spaces long and wide. Once the player reaches the ice cream cone the player wins If the player lands on a space with a mine they are killed and the game is over After the game is over, the player should be prompted whether or not they want to play again. Example Dialog: Welcome to Mine Walker. Get the ice cream cone and avoid the mines X_________ __________ __________ __________ __________ __________ __________ __________ __________ _________^ Enter either a -1, 0, or 1 in the X or 9 to quit 1 Enter either a -1,0, or 1 in the Y 1 __________ _X________ __________ __________ __________ __________ __________ __________ __________ _________^ Enter either a -1, 0, or 1 in the X or 9 to quit 0 Enter either a -1,0, or 1 in the Y 1 __________ __________ _X________ __________ __________ __________ __________ __________ __________ _________^ Enter either a -1, 0, or 1 in the X or 9 to quit -1 Enter either a -1,0, or 1 in the Y 1 Boom! Dead! Would you like to play again?

Explanation / Answer

/* MineWalker.java */


import java.util.Scanner;
import java.util.Random;

public class MineWalker{

private Scanner scanner = new Scanner(System.in); // packaged class to read stdin
private boolean playing = true;
private final int SIZEX = 10; // Board size
private final int SIZEY = 10; // Board size
private Game game = new Game(SIZEX,SIZEY); // Woo! New Game

// Constructor to start
private MineWalker(){
System.out.println("Welcome to Mine Walker. Get the ice cream cone and avoid the mines"); // Print out obligatory nonsense
do{
do{
System.out.println(game.render()); // Print the board
game.move(prompt("X", true),prompt("Y", false)); // Move from prompts
}while(!game.gameover); // Did you lose?
System.out.println((game.win? "Mmmm. Icecream." : "Boom! Dead!")); // Print whether win or lose

// Play again or restart?
System.out.println("Do you want to play again? (true/false)");
if(!scanner.nextBoolean()) playing = false;
else game.restart();
}while(playing);
}

// Prompt user for input
private int prompt(String direction, boolean fatal){
System.out.println("Enter either a -1, 0, or 1 in the " + direction + (fatal ? " or 9 to quit":""));
int next = scanner.nextInt();
if(fatal && next == 9) System.exit(1); // Oh no! Exit
if(Math.abs(next) * next == next) return next; // only true for -1,0,1
System.out.println("Didn't move. Bad input"); // Didn't read the instructions did you?
return 0;
}

// Just a class to hold this all together
// Ideally like another file
private static class Game{
private static enum Spot {Mine,Empty,Player,IceCream} // Different types of spots
private final double MINE_PERCENT = 0.1;
private Spot[][] board;
private int[] playerxy = new int[]{0,0}; // Holds player pos
private boolean win = false; // Has I won?
private boolean gameover = false; // Is it over already?
private Random random = new Random(); // Zoidberg
private int height;
private int length;

public Game(int height,int length){
this.height = height; // hold on to these guys
this.length = length;
board = new Spot[height][length]; // create board
restart(); // Set it up
}

// Burn it all!
public void restart(){
int x,y;
int mines = (int)(length * height * MINE_PERCENT);
  
// reset variables
playerxy[0] = playerxy[1] = 0;
win = gameover = false;

// Clear the board
clear();

// Seed Mines
do{
x = random.nextInt(height - 1) + 1; // displaces so not 0,0 or last,last
y = random.nextInt(length - 1) + 1;
if(board[x][y] == Spot.Empty){ // Mined your place isn't already set
board[x][y] = Spot.Mine;
mines--;
}
}while(mines > 0); // set mines

// Set Players
board[0][0] = Spot.Player;
board[height - 1][length - 1] = Spot.IceCream;
}

// Return string, because a void function would be a crime against humanity
// and essentially functionless for anything else but printing
public String render(){
String visual = "";
// For each, string it together
for (int y = 0; y < board.length ; y++ ) {
for (int x = 0; x < board[y].length ; x++ ) {
switch(board[y][x]){
case Player:
visual += "X";
break;
case IceCream:
visual += "^"; // OoOoo0o, Icecream
break;
/* Uncoment for testing
case Mine:
visual += "o";
break;*/
default:
visual += "_"; // Nothing to see here
}
}
visual += " "; // Next line
}
return visual;
}

// Move it!
public void move(int x, int y){
try{
board[playerxy[0]][playerxy[1]] = Spot.Empty; // set old spot to empty
playerxy[0] += y; // update coordinates
playerxy[1] += x; // update coordinates
win = board[playerxy[0]][playerxy[1]] == Spot.IceCream; // has I won?
gameover = win || board[playerxy[0]][playerxy[1]] == Spot.Mine; // has I won or lost?
board[playerxy[0]][playerxy[1]] = Spot.Player; // Update player
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Can't move there"); // There's no escaping the matrix
}
}

// For each iterate and set to empty
private void clear(){
for (int y = 0; y < board.length ; y++ ) {
for (int x = 0; x < board[y].length ; x++ ) {
board[y][x] = Spot.Empty;
}
}
}
}

// Entry point
public static void main(String args[]){
// Create instance
new MineWalker();
}
}