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

Write a game where you are an X trying to get an ice cream cone in a mine field

ID: 670797 • Letter: W

Question

Write a game where you are an X trying to get an ice cream cone in a mine field

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

Code is given below:

NOTE :            System.out.println(mpos[i]); This line is given for testing purpose. It'will give you the position of mines, so that you can check the functionality of your code.

Here is your Code:

package chegg;

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

public class MineGame {

   public static void main(String[] args) {
       StringBuilder[] arr = new StringBuilder[10];
       int[] mpos = new int[10];
       for (int i = 0; i < arr.length; i++) {
           arr[i] = new StringBuilder("");
           for (int j = 0; j < 10; j++) {
               arr[i].append('_');
           }
           mpos[i] = randInt(0, 98);
           System.out.println(mpos[i]);
       }
       arr[0].replace(0, 1, "X");
       int tpos = randInt(90, 99);
       arr[9].replace(tpos%10, (tpos%10)+1, "^");
      
       int xpos = -1, ypos = -1, xmove = 0, ymove = 0;
      
       do {
           display(arr);
           Scanner scanner = new Scanner(System.in);
System.out.println("Enter either a -1, 0, or 1 in the X or 9 to quit");
xmove=scanner.nextInt();
scanner.nextLine();
System.out.println("Enter either a -1,0, or 1 in the Y");
ymove = scanner.nextInt();
scanner.nextLine();
  
xpos = xpos+xmove;
ypos = ypos+ymove;
       } while (null != generateView(arr, xpos, ypos, mpos, xmove, ymove, tpos));
       System.out.println("Game Over");
   }
  
   public static int randInt(int min, int max) {

   Random rand = new Random();
   int randomNum = rand.nextInt((max - min) + 1) + min;
   return randomNum;
   }
  
   public static void initialise(StringBuilder[] arr, int xpos, int ypos, int tpos)
   {
       for (int i = 0; i < arr.length; i++) {
           for (int j = 0; j < 10; j++) {
               arr[i].replace(j, j+1, "_");
           }
       }
       arr[ypos].replace(xpos, xpos+1, "X");
       arr[9].replace(tpos%10, (tpos%10)+1, "^");
   }
  
   public static void display(StringBuilder[] arr)
   {
       for (int i = 0; i < 10; i++)
           System.out.println(arr[i]);
   }
  
   public static StringBuilder[] generateView(StringBuilder[] arr, int xpos, int ypos,
           int[] mines, int xmove, int ymove, int tpos) {

       if (9 == xmove) {
           System.out.println("You quit. So you lost.");
           return null;
       } else {
           xpos = xpos + xmove;
           ypos = ypos + ymove;

           for (int mpos : mines) {
               if (mpos == (xpos * 10 + ypos)) {
                   System.out.println("Boom! Dead!");
                   return null;
               }
           }
           if (ypos == 9 && xpos == tpos%10)
           {
               System.out.println("You Won");
               initialise(arr, xpos, ypos, tpos);
               arr[9].replace(tpos%10, (tpos%10)+1, "X");
           }
           arr[xpos].replace(ypos, ypos+1, "X");
           initialise(arr, xpos, ypos, tpos);
           return arr;
       }
   }

}

Language is JAVA.

Have Fun. Enjoy :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote