Question
This is a homework assignment that I have for my Java coding class. I have completed most of the assignment, but I am having some
This is a homework assignment that I have for my Java coding class. I have completed most of the assignment, but I am having some issue. Could you find my problem and then take my code and add the corrections to it and then display the complete code that works? I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES. I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES!
I have asked this question 3 times and no one has done what I have requested. I don't want a different solution to my problem. It doesn't take a lot of work to search chegg and then copy and paste an answer from another question. I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES!
Objective:
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?
This is the code I have so far. And it works fine. But when you land on a bomb or the ice cream and it ask you to play again, if you say yes then it doesn't reset the game board. Instead it just continues from where the "X" last was on the board. How can I reset the board at the end if the user wants to play again? Again can you add the changes that need to be made to my existing code and then give the answer with the complete code that works? I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES! I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES! I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES!
import java.util.Random; import java.util.Scanner;
public class MineWalker { //Setup enum, Scanner, and final variables enum Spaces {Empty, Player, Mines, Ice_Cream, Walked_Path}; private static final int BOARD_SIZE = 10; private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { //Player's location int pX = 0; int pY = 0; //Game intro System.out.println("Welcome to Mine Walker. Get the ice cream cone and avoid the mines"); int numberOfMoves = 0; //Setup board for the game Spaces[][] board = new Spaces[BOARD_SIZE][BOARD_SIZE]; //Location of the ice cream Random r = new Random(); int gX = r.nextInt(BOARD_SIZE); int gY = r.nextInt(BOARD_SIZE); //Initialize the game board for (int y = 0; y < board.length; y++) { for(int x = 0; x < board[y].length; x++) { board[x][y] = Spaces.Empty; } } board[pX][pY] = Spaces.Player; board[gX][gY] = Spaces.Ice_Cream; int mineMax = 10; int mineCount = 0; //Place mines on the board do { int x = r.nextInt(BOARD_SIZE - 1) + 1; //Makes sure mine isn't 0,0 int y = r.nextInt(BOARD_SIZE - 1) + 1; if(board[x][y] == Spaces.Empty) { board[x][y] = Spaces.Mines; mineCount++; } }while(mineMax > mineCount); boolean gameOver = false; while(gameOver == false) { for(int y = 0; y < board.length; y++) { for(int x = 0; x < board[y].length; x++) { switch(board[x][y]) { case Empty: System.out.print("_"); break; case Player: System.out.print("X"); break; case Walked_Path: System.out.print("#"); break; case Ice_Cream: System.out.print("^"); break; case Mines: System.out.print("o"); break; default: System.out.print("?"); break; } } System.out.println(" "); }
//The player moves System.out.println("Enter either a -1, 0, or 1 in the X direction or 9 to quit"); //Movement in the X direction int dX = scanner.nextInt(); //Or quit if(dX == 9) { System.out.println("Game over"); break; }
System.out.println("Enter either a -1, 0, or 1 in the Y direction"); //Movement in the Y direction int dY = scanner.nextInt();
//Checks to see if the movement is valid if(dX < -1 || dX > 1) { System.out.println("Invalid input X"); dX = 0; } if(dY < -1 || dY > 1) { System.out.println("Invalid input Y"); dY = 0; }
//Sets the player position to a walked path board[pX][pY] = Spaces.Walked_Path; //Moves the player pY += dX; pX += dY;
//Makes sure everything is still in bounds if(pX < 0) { pX = 0; } else if(pX > BOARD_SIZE - 1) { pX = BOARD_SIZE - 1; }
if(pY < 0) { pY = 0; } else if(pY > BOARD_SIZE - 1) { pY = BOARD_SIZE - 1; } //Losing condition if(board[pX][pY] == Spaces.Mines) { System.out.println("Boom! Dead!"); gameOver = true; System.out.println("Would you like to play again? Yes or No"); String playAgain = scanner.next(); if(playAgain.equals("yes")) { gameOver = false; } else if(playAgain.equals("no")) { System.out.println("Thanks for playing!"); gameOver = true; } }
//Winning condition if(board[pX][pY] == Spaces.Ice_Cream) { System.out.println("You made it to the ice cream cone!"); gameOver = true; System.out.println("Would you like to play again? Yes or No"); String playAgain = scanner.next(); if(playAgain.equals("yes")) { gameOver = false; } else if(playAgain.equals("no")) { System.out.println("Thanks for playing!"); gameOver = true; } } board[pX][pY] = Spaces.Player; numberOfMoves++; } } }
Please don't just answer with different code. Use my existing code and fix my problem.
I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES!I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES!I REPEAT CAN YOU LOCATE WHY MY CODE ISN'T WORKING AND THEN USE MY CODE AND ADD THE CHANGES!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started