This document discusses code for a Mine Walker game where the player moves around a board trying to reach an ice cream cone while avoiding mines. It provides the game rules and an example dialog. The code works but does not properly reset the board when the player chooses to play again. To fix this, the board initialization code needs to be moved to a separate function that can be called again when restarting.
This is for an homework assignment using Java code. Here is the home.pdf
1. This is for an homework assignment using Java code. Here is the homework:
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_________
__________
__________
__________
__________
__________
__________
__________
2. __________
_________^
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?
3. 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?
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++)
{
4. 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");
5. 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;
}
6. 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();
7. 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++;
}
}
}
Solution
8. Your board initialization code needs to be in another function. If the play wants to play again,
just call the initialize function and continue the loop.
Add everything in your main method that is always true eg:
You will also need to mark when the player restarts the game:
Or below is the another psuedocode also you can add:
Thank you.