SlideShare a Scribd company logo
1 of 8
Download to read offline
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_________
__________
__________
__________
__________
__________
__________
__________
__________
_________^
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?
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++;
}
}
}
Solution
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.

More Related Content

Similar to This is for an homework assignment using Java code. Here is the home.pdf

Project 2
Project 2Project 2
Project 2
umtkrc
 
19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf
NayanOza
 
Description For this part of the assignment, you will create a Grid .pdf
Description For this part of the assignment, you will create a Grid .pdfDescription For this part of the assignment, you will create a Grid .pdf
Description For this part of the assignment, you will create a Grid .pdf
formicreation
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfPlease follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
info382133
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
kavithaarp
 
Node meetup feb_20_12
Node meetup feb_20_12Node meetup feb_20_12
Node meetup feb_20_12
jafar104
 
C++You will design a program to play a simplified version of war, .pdf
C++You will design a program to play a simplified version of war, .pdfC++You will design a program to play a simplified version of war, .pdf
C++You will design a program to play a simplified version of war, .pdf
ezzi97
 

Similar to This is for an homework assignment using Java code. Here is the home.pdf (12)

0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx
 
Yet another building metaphor
Yet another building metaphorYet another building metaphor
Yet another building metaphor
 
Project 2
Project 2Project 2
Project 2
 
19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf
 
Description For this part of the assignment, you will create a Grid .pdf
Description For this part of the assignment, you will create a Grid .pdfDescription For this part of the assignment, you will create a Grid .pdf
Description For this part of the assignment, you will create a Grid .pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdfPlease follow the data 1) For Line 23 In the IF - Condition yo.pdf
Please follow the data 1) For Line 23 In the IF - Condition yo.pdf
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
2nd National ArithmetEQ Challenge Rules
2nd National ArithmetEQ Challenge Rules2nd National ArithmetEQ Challenge Rules
2nd National ArithmetEQ Challenge Rules
 
National ArithmetEQ Challenge Rules 2010
National ArithmetEQ Challenge Rules 2010National ArithmetEQ Challenge Rules 2010
National ArithmetEQ Challenge Rules 2010
 
Node meetup feb_20_12
Node meetup feb_20_12Node meetup feb_20_12
Node meetup feb_20_12
 
C++You will design a program to play a simplified version of war, .pdf
C++You will design a program to play a simplified version of war, .pdfC++You will design a program to play a simplified version of war, .pdf
C++You will design a program to play a simplified version of war, .pdf
 

More from rajkumarm401

Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
rajkumarm401
 
eee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdfeee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdf
rajkumarm401
 
Essay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdfEssay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdf
rajkumarm401
 
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdfDescribe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
rajkumarm401
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
Click the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdfClick the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdf
rajkumarm401
 
What was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdfWhat was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdf
rajkumarm401
 
Program Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdfProgram Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdf
rajkumarm401
 

More from rajkumarm401 (20)

Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
 
Explain the characterstic of web service techonolgy.SolutionT.pdf
Explain the characterstic of web service techonolgy.SolutionT.pdfExplain the characterstic of web service techonolgy.SolutionT.pdf
Explain the characterstic of web service techonolgy.SolutionT.pdf
 
Executive Summary i. Provide a succinct overview of your strategic p.pdf
Executive Summary i. Provide a succinct overview of your strategic p.pdfExecutive Summary i. Provide a succinct overview of your strategic p.pdf
Executive Summary i. Provide a succinct overview of your strategic p.pdf
 
eee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdfeee230 Instruction details Answer the following questions in a typed.pdf
eee230 Instruction details Answer the following questions in a typed.pdf
 
Essay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdfEssay questionPorter Combining business strategy What is itmeani.pdf
Essay questionPorter Combining business strategy What is itmeani.pdf
 
During oogenesis, will the genotypes of the first and second polar b.pdf
During oogenesis, will the genotypes of the first and second polar b.pdfDuring oogenesis, will the genotypes of the first and second polar b.pdf
During oogenesis, will the genotypes of the first and second polar b.pdf
 
Does personal information available on the Internet make an employee.pdf
Does personal information available on the Internet make an employee.pdfDoes personal information available on the Internet make an employee.pdf
Does personal information available on the Internet make an employee.pdf
 
Determine whether each series is convergent or divergent..pdf
Determine whether each series is convergent or divergent..pdfDetermine whether each series is convergent or divergent..pdf
Determine whether each series is convergent or divergent..pdf
 
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdfDescribe the primary differences between WEP, WPA, and WPA2 protocol.pdf
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
 
Click the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdfClick the desktop shortcut icon that you created in this module, and .pdf
Click the desktop shortcut icon that you created in this module, and .pdf
 
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdfYOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
 
What was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdfWhat was the causes of the Vietnam war What was the causes of .pdf
What was the causes of the Vietnam war What was the causes of .pdf
 
Transactions costs are zero in financial markets. zero in financial i.pdf
Transactions costs are zero in financial markets. zero in financial i.pdfTransactions costs are zero in financial markets. zero in financial i.pdf
Transactions costs are zero in financial markets. zero in financial i.pdf
 
Question 34 (1 point) D A check is 1) not money because it is not off.pdf
Question 34 (1 point) D A check is 1) not money because it is not off.pdfQuestion 34 (1 point) D A check is 1) not money because it is not off.pdf
Question 34 (1 point) D A check is 1) not money because it is not off.pdf
 
Program Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdfProgram Structure declare ButtonState Global variable holding statu.pdf
Program Structure declare ButtonState Global variable holding statu.pdf
 
Need help in assembly. Thank you Implement the following expression .pdf
Need help in assembly. Thank you Implement the following expression .pdfNeed help in assembly. Thank you Implement the following expression .pdf
Need help in assembly. Thank you Implement the following expression .pdf
 
mine whether the following s are true or false False A parabola has e.pdf
mine whether the following s are true or false False A parabola has e.pdfmine whether the following s are true or false False A parabola has e.pdf
mine whether the following s are true or false False A parabola has e.pdf
 
Locate an article where the technique of Polymerase chain reaction (.pdf
Locate an article where the technique of Polymerase chain reaction (.pdfLocate an article where the technique of Polymerase chain reaction (.pdf
Locate an article where the technique of Polymerase chain reaction (.pdf
 
It is said that the CEO and other corporate leaders are keepers of t.pdf
It is said that the CEO and other corporate leaders are keepers of t.pdfIt is said that the CEO and other corporate leaders are keepers of t.pdf
It is said that the CEO and other corporate leaders are keepers of t.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

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.