0% found this document useful (0 votes)
18 views6 pages

ZOHO L3 - MINESWEEPER GAME

The document outlines the implementation of a Minesweeper game using Java, consisting of three main components: the model that manages game logic and state, the view model that mediates between the model and the view, and the view that handles the console user interface. The model initializes the game grid, places mines, computes adjacent mine counts, and manages user actions such as uncovering cells and flagging mines. The main class runs the game loop, allowing players to interact with the game until they win or lose.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

ZOHO L3 - MINESWEEPER GAME

The document outlines the implementation of a Minesweeper game using Java, consisting of three main components: the model that manages game logic and state, the view model that mediates between the model and the view, and the view that handles the console user interface. The model initializes the game grid, places mines, computes adjacent mine counts, and manages user actions such as uncovering cells and flagging mines. The main class runs the game loop, allowing players to interact with the game until they win or lose.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ZOHO L3 - MINESWEEPER GAME

Model: Manages game logic and state

import java.util.Random;
import java.util.Scanner;

class MinesweeperModel {
private char[][] actualGrid; // M=mine, 0-8=numbers
private char[][] visibleGrid; // *=hidden, F=flagged, 0-8 or space=revealed
private int size = 8;
private int mines = 10;
private int revealedCount;
private boolean gameLost;

public MinesweeperModel() {
actualGrid = new char[size][size];
visibleGrid = new char[size][size];
revealedCount = 0;
gameLost = false;
initializeGrids();
placeMines();
computeNumbers();
}

private void initializeGrids() {


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
actualGrid[i][j] = '0';
visibleGrid[i][j] = '*';
}
}
}

private void placeMines() {


Random rand = new Random();
int placed = 0;
while (placed < mines) {
int r = rand.nextInt(size);
int c = rand.nextInt(size);
if (actualGrid[r][c] != 'M') {
actualGrid[r][c] = 'M';
placed++;
}
}
}
private void computeNumbers() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (actualGrid[i][j] == 'M') {
continue;
}
int count = 0;
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
int nr = i + dr;
int nc = j + dc;
if (nr >= 0 && nr < size && nc >= 0 && nc < size && actualGrid[nr][nc] == 'M')
{
count++;
}
}
}
actualGrid[i][j] = (char) ('0' + count);
}
}
}

public boolean uncover(int row, int col) {


if (row < 0 || row >= size || col < 0 || col >= size || visibleGrid[row][col] != '*') {
return false;
}
if (actualGrid[row][col] == 'M') {
gameLost = true;
return false;
}
reveal(row, col);
return true;
}

private void reveal(int row, int col) {


if (row < 0 || row >= size || col < 0 || col >= size || visibleGrid[row][col] != '*') {
return;
}
visibleGrid[row][col] = actualGrid[row][col] == '0' ? ' ' : actualGrid[row][col];
revealedCount++;
if (actualGrid[row][col] == '0') {
reveal(row - 1, col - 1);
reveal(row - 1, col);
reveal(row - 1, col + 1);
reveal(row, col - 1);
reveal(row, col + 1);
reveal(row + 1, col - 1);
reveal(row + 1, col);
reveal(row + 1, col + 1);
}
}

public boolean flag(int row, int col) {


if (row < 0 || row >= size || col < 0 || col >= size) {
return false;
}
if (visibleGrid[row][col] == '*') {
visibleGrid[row][col] = 'F';
return true;
} else if (visibleGrid[row][col] == 'F') {
visibleGrid[row][col] = '*';
return true;
}
return false;
}

public boolean isLost() {


return gameLost;
}

public boolean isWon() {


return revealedCount == (size * size - mines);
}

public char[][] getVisibleGrid() {


return visibleGrid;
}

public char[][] getActualGrid() {


return actualGrid;
}
}

ViewModel: Mediates between Model

class MinesweeperViewModel {
private MinesweeperModel model;
private String status; // playing, won, lost

public MinesweeperViewModel() {
model = new MinesweeperModel();
status = "playing";
}

public char[][] getGrid() {


return model.getVisibleGrid();
}

public boolean doAction(int row, int col, char action) {


boolean valid = false;
if (action == 'U' || action == 'u') {
valid = model.uncover(row, col);
if (!valid && model.isLost()) {
status = "lost";
}
} else if (action == 'F' || action == 'f') {
valid = model.flag(row, col);
}
if (model.isWon()) {
status = "won";
}
return valid;
}

public String getStatus() {


return status;
}

public char[][] getActualGrid() {


return model.getActualGrid();
}
}

View: Handles console UI

class MinesweeperView {
private MinesweeperViewModel viewModel;
private Scanner scanner;

public MinesweeperView(MinesweeperViewModel viewModel) {


this.viewModel = viewModel;
scanner = new Scanner(System.in);
}

public void showGrid() {


char[][] grid = viewModel.getGrid();
System.out.println(" 0 1 2 3 4 5 6 7");
for (int i = 0; i < grid.length; i++) {
System.out.print(i + " ");
for (int j = 0; j < grid[i].length; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}

public void showFullGrid() {


char[][] actual = viewModel.getActualGrid();
char[][] visible = viewModel.getGrid();
System.out.println(" 0 1 2 3 4 5 6 7");
for (int i = 0; i < visible.length; i++) {
System.out.print(i + " ");
for (int j = 0; j < visible[i].length; j++) {
if (actual[i][j] == 'M') {
System.out.print("M ");
} else {
System.out.print(visible[i][j] + " ");
}
}
System.out.println();
}
}

public void showMessage(String msg) {


System.out.println(msg);
}

public String getInput() {


System.out.print("Enter row col action (U=uncover, F=flag, q=quit): ");
return scanner.nextLine();
}

public void close() {


scanner.close();
}
}

// Main class to run the game


public class Minesweeper {
public static void main(String[] args) {
MinesweeperViewModel viewModel = new MinesweeperViewModel();
MinesweeperView view = new MinesweeperView(viewModel);

view.showMessage("Minesweeper: Uncover cells, flag mines.");


while (viewModel.getStatus().equals("playing")) {
view.showGrid();
String input = view.getInput();
if (input.equalsIgnoreCase("q")) {
view.showMessage("Quit game.");
break;
}
String[] parts = input.split(" ");
if (parts.length != 3) {
view.showMessage("Invalid input. Use: row col U/F");
continue;
}
int row, col;
char action;
try {
row = Integer.parseInt(parts[0]);
col = Integer.parseInt(parts[1]);
action = parts[2].charAt(0);
} catch (Exception e) {
view.showMessage("Invalid input. Use: row col U/F");
continue;
}
if (action != 'U' && action != 'u' && action != 'F' && action != 'f') {
view.showMessage("Action must be U or F.");
continue;
}
boolean valid = viewModel.doAction(row, col, action);
if (!valid && !viewModel.getStatus().equals("lost")) {
view.showMessage("Invalid move.");
}
}
if (viewModel.getStatus().equals("won")) {
view.showGrid();
view.showMessage("You win!");
} else if (viewModel.getStatus().equals("lost")) {
view.showFullGrid();
view.showMessage("Game over! Hit a mine.");
}
view.close();
}
}

You might also like