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

c. Minesweeper Minesweeper [3] is a single-­­player puzzle video game. The objec

ID: 3869151 • Letter: C

Question

c. Minesweeper Minesweeper [3] is a single-­­player puzzle video game. The objective of the game is to clear a rectangular board containing hidden "mines" without detonating any of them, with help from clues about the number of neighboring mines in each field. The game originates from the 1960s, and has been written for many computing platforms in use today. Write a VBA Sub Program to create a Minesweeper game. Create a 5 X 5 board for the game. Remember you are the person who creates the game board for others to play on. ENGR 1412 Introduction to Computer Programming Dr. Jennifer Glenn, Summer ‘17 Here are the rules that you need to keep in mind while building the game: a. Out of the 25 cells, 10 cells should contain mines and others contain no mines. (Hint: Generate 10 random non-­­repetitive numbers and place the mines in those cells). b. If a player selects the cell that contains a mine, the player loses. If the cell contains no mine then the cell should display the number of mines surrounding the cell (any cell which has at least one corner touching the currently selected cell is said to be surrounding the current cell). c. If there are no mines in any of the cells surrounding the cell the player selected, then the player’s selected cell plus all the surrounding cells should open up. Any game board encourages people to play if it looks creative and attractive. Design your game using your best creative skills so people will want to play your game!.

12 13 14 15 16 17 18 3 Count 3 Score 15 Start Ready

Explanation / Answer

public class Minesweeper extends World {
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
public static final int CELLSIZE = 30;

private int fieldSize;
private int bombs;
private boolean firstPlay;

private Block[][] field;

public Minesweeper() {
super(WIDTH, HEIGHT, CELLSIZE, false);

firstPlay = true;
fieldSize = 10;
bombs = 10;

field = new Block[fieldSize][fieldSize];

for(int i = 0; i < fieldSize; i++) {
for(int j = 0; j < fieldSize; j++) {
field[i][j] = new Block(i, j);
addObject(field[i][j], i, j);
}
}
}

/* Get and Set methods */
}


public class Block extends Actor
{
private Minesweeper game;
private GreenfootImage blockImage;
private GreenfootImage bombImage;
private boolean bomb;
private int number;
private int i, j;

public Block(int i, int j) {
blockImage = new GreenfootImage("Block.png");
bombImage = new GreenfootImage("Bomb.png");

bomb = false;
number = 0;

this.i = i;
this.j = j;
setImage(blockImage);
}

@Override
protected void addedToWorld(World game) {
this.game = (Minesweeper) game;
}

/* Get and Set methods */
}

private char [] scramble (char [] word)
{
char [] scramWord = new char [word.length];
System.arraycopy (word, 0, scramWord, 0, word.length);

mainloop:
do
{
for (int i = 0; i < 100; i++)
{
int x = rnd (word.length);
int y = rnd (word.length);

char ch = scramWord [x];
scramWord [x] = scramWord [y];
scramWord [y] = ch;
}


for (int i = 0; i < word.length; i++)
if (word [i] != scramWord [i] &&
word [i] != scramWord [word.length-1-i])
break mainloop;
}
while (true);

return scramWord;
}

{
playWAV (audioFile);

int tileID = mouseX/TILE_SIZE;

if (!tileSelected)
{
tileSelected = true;
selectedTileID = tileID;
repaint ();
return;
}

char ch = scramWord [selectedTileID];
scramWord [selectedTileID] = scramWord [tileID];
scramWord [tileID] = ch;
tileSelected = false;

repaint ();

for (int i = 0; i < word.length; i++)
if (word [i] != scramWord [i])
return;

lblClue.setIcon (imageIcon);
lblClue.setHorizontalTextPosition (JLabel.CENTER);
lblClue.setText ("You got it!");

pnlGlass.setVisible (true);

if (al == null)
al = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
int index;
while ((index = rnd (wordsClues.size ())) == prevIndex);
prevIndex = index;

WordClue wc = wordsClues.get (index);
word = wc.word;
scramWord = scramble (word);
lblClue.setText ("Clue: "+wc.clue);

Dimension dim;
dim = new Dimension (word.length*TILE_SIZE, TILE_SIZE);
setPreferredSize (dim);
setMaximumSize (dim);
revalidate ();

lblClue.setIcon (null);

pnlGlass.setVisible (false);
}
};


javax.swing.Timer t = new javax.swing.Timer (DELAY, al);
t.setRepeats (false);

// Start the timer.

t.start ();
}

public void init ()
{
// Read the words-and-clues text file's contents into the wordsClues
// ArrayList. The file must exist.

InputStream is = getClass ().getResourceAsStream (WORDS_CLUES_FILE);
if (is == null)
{
System.err.println (WORDS_CLUES_FILE+" does not exist");
return;
}

InputStreamReader isr = new InputStreamReader (is);
BufferedReader br = new BufferedReader (isr);

try
{
String line;
while ((line = br.readLine ()) != null)
{
// Each line contains two parts separated by a colon character.

String [] parts = line.toUpperCase ().split (":");

// parts.length returns 1 for blank lines. This if statement
// prevents an exception that occurs if you accidentally insert a
// blank line into the file (typically at the end of the file).

if (parts.length != 2)
break;

WordClue wc = new WordClue ();
wc.word = parts [0].trim ().toCharArray ();
wc.clue = parts [1].trim ();
wordsClues.add (wc);
}
}
catch (IOException ioe)
{
System.err.println (ioe);
return;
}
finally
{
try
{
br.close ();
}
catch (IOException ioe)
{
}
}

PuzzleBoard (ArrayList<WordClue> wordsClues, URL audioFile, URL imageFile,
Component pnlGlass, JLabel lblClue)
{
this.wordsClues = wordsClues;
this.audioFile = audioFile;
imageIcon = new ImageIcon (imageFile);
this.pnlGlass = pnlGlass;
this.lblClue = lblClue;

WordClue wc = wordsClues.get (prevIndex = rnd (wordsClues.size ()));
word = wc.word;
scramWord = scramble (word);
lblClue.setText ("Clue: "+wc.clue);

font = new Font ("Arial", Font.BOLD, 20);

addMouseListener (new MouseAdapter ()
{
public void mouseClicked (MouseEvent e)
{
doSelection (e.getX (), e.getY ());
}
});

// The Box container's BoxLayout manager requires a component's maximum
// size to be set equal to its preferred size (for proper layout).

setPreferredSize (new Dimension (word.length*TILE_SIZE, TILE_SIZE));
setMaximumSize (new Dimension (word.length*TILE_SIZE, TILE_SIZE));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote