Java programming question... see sample outputs first please. Please also read t
ID: 669620 • Letter: J
Question
Java programming question...
see sample outputs first please.
Please also read the assignment carefully!!
The bomb needs to be visible on the board AT ALL TIMES.
If I get the same answer I've gotten two times before I will triple thumbs down it.
This is a simplified minesweeper game. The player moves from the starting position on the board to the finish without stepping on a bomb.
Your program must work like this...
Player starts in upper left
Player moves one space at a time
Player can only move right or down. Ignore any other input.
Player cannot move off the board
Player wins when he/she reaches lower right corner
Player loses if he/she steps on a bomb
Your program should prompt for the size of the board. Ensure that input is at least size 3. Place one bomb for a 3X3 board. Add an additional bomb for each row above 3.
Bombs must be placed randomly. Your instructor will check this thoroughly.
Insure that bombs are not placed at the start or the finish.
Game board should be printed out in a user friendly fashion at the start and after each move, except the last. Since you are doing this as a homework exercise rather than a real game, your board display should show where the bombs are. This will make it easier for your instructor to grade your program.
See the 4 pages of sample output included in this document.
You are free to design your program any way you wish, with one stipulation. The intent of this exercise is to give you experience with arrays in Java. The game board must implemented in your program as an array.
Submit a single .java file. Programs that do not compile with receive a grade of 0.
I know it's a big assignment. Sorry about that.
Explanation / Answer
Answer:
Program code:
Board1.java:
import java.util.Random;
import java.util.Scanner;
public class Board1
{
private int[][] mines1;
private char[][] boardgame1;
private int Line1, Column1;
Random randomVal = new Random();
Scanner inputValue = new Scanner(System.in);
public Board1(){
mines1 = new int[3][3];
boardgame1 = new char[3][3];
startmines1();
randomValmines1();
fillTips();
startBoard();
}
public boolean win(){
int count=0;
for(int Line1 = 1 ; Line1 <2 ; Line1++)
for(int Column1 = 1 ; Column1 <2; Column1++)
if(boardgame1[Line1][Column1]=='_')
count++;
if(count == 3)
return true;
else
return false;
}
public void openNeighbors(){
for(int i=-1 ; i<2 ; i++)
for(int j=-1 ; j<2 ; j++)
if( (mines1[Line1+i][Column1+j] != -1) && (Line1 != 0 && Line1 != 2 && Column1 != 0 && Column1 != 2) )
boardgame1[Line1+i][Column1+j]=Character.forDigit(mines1[Line1+i][Column1+j], 3);
}
public int getPosition(int Line1, int Column1){
return mines1[Line1][Column1];
}
public boolean setPosition(){
do{
System.out.print(" Line1: ");
Line1 = inputValue.nextInt();
System.out.print("Column1: ");
Column1 = inputValue.nextInt();
if( (boardgame1[Line1][Column1] != '_') && ((Line1 < 2 && Line1 > 0) && (Column1 < 2 && Column1 > 0)))
System.out.println("Field already shown");
if( Line1 < 1 || Line1 > 1 || Column1 < 1 || Column1 > 1)
System.out.println("Choose a number between 1 and 2");
}while((Line1 < 1 || Line1 > 1|| Column1 < 1 || Column1 >1) || (boardgame1[Line1][Column1] != '_') );
if(getPosition(Line1, Column1)== -1)
return true;
else
return false;
}
public void show(){
System.out.println(" Line1s");
for(int Line1 = 1 ; Line1 > 0 ; Line1--){
System.out.print(" "+Line1 + " ");
for(int Column1 = 1 ; Column1 <2 ; Column1++){
System.out.print(" "+ boardgame1[Line1][Column1]);
}
System.out.println();
}
System.out.println(" 1 2 3 ");
System.out.println(" Column1s");
}
public void fillTips(){
for(int Line1=1 ; Line1 <2 ; Line1++)
for(int Column1=1 ; Column1 <= 3 ; Column1++){
for(int i=-1 ; i<=1 ; i++)
for(int j=-1 ; j<=1 ; j++)
if(mines1[Line1][Column1] != -1)
if(mines1[Line1+i][Column1+j] == -1)
mines1[Line1][Column1]++;
}
}
public void showmines1(){
for(int i=1 ; i <2; i++)
for(int j=1 ; j <2 ; j++)
if(mines1[i][j] == -1)
boardgame1[i][j]='*';
show();
}
public void startBoard(){
for(int i=1 ; i<mines1.length ; i++)
for(int j=1 ; j<mines1.length ; j++)
boardgame1[i][j]= '_';
}
public void startmines1(){
for(int i=0 ; i<mines1.length ; i++)
for(int j=0 ; j<mines1.length ; j++)
mines1[i][j]=0;
}
public void randomValmines1(){
boolean raffled;
int Line1, Column1;
for(int i=0 ; i<3; i++){
do{
Line1 = randomVal.nextInt(2) + 1;
Column1 = randomVal.nextInt(2) + 1;
if(mines1[Line1][Column1] == -1)
raffled=true;
else
raffled = false;
}while(raffled);
mines1[Line1][Column1] = -1;
}
}
}
Game.java:
import java.util.*;
public class Game
{
private Board1 board;
boolean finish = false;
boolean win = false;
int turn=0;
public Game()
{
board = new Board1();
Play(board);
}
public void Play(Board1 board){
do{
turn++;
System.out.println("Turn "+turn);
board.show();
finish = board.setPosition();
if(!finish)
{
board.openNeighbors();
finish = board.win();
}
}while(!finish);
if(board.win()){
System.out.println("Congratulations, you let the 10 fields with the mines in "+turn+" turns");
board.showMines();
} else {
System.out.println("Mine! You lost!");
board.showMines();
}
}
}
minesweeper.java:
import java.util.*;
public class MineSweeper1
{
public static void main(String[] args) {
Game game = new Game();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.