Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//This is the HexMineMAnager import java.util.*; import java.io.*; public class HexMineManager { private char [][] board; private char [][] mines; int height; int width; int

//This is the HexMineMAnager import java.util.*; import java.io.*; public class HexMineManager {   private char [][] board;   private char [][] mines;   int height;   int width;   int numberofmines;   private int numofBlockUncovered;   private int numofBlockCovered;   public static final int UNKNOWN = 'C';   public static final int MARKED = 'F';   public static final int MINE = 'M';   public HexMineManager(int height, int width, int numofmines) {     this.height = height;     this.width = width;     this.board = create_board(height, width);     this.mines = add_mines(numofmines, height, width);     this.numberofmines = numofmines;     this.numofBlockCovered = height*width;     this.numofBlockUncovered = 0;   }   private char[][] create_board(int height, int width) {     char[][] board = new char[height][width*2];     boolean space = false;     for (int i=0;ifor(int j=0;j2;j++) {         if (!space) {           board[i][j] = 'C';           space = !space;         }         else {           board[i][j] = ' ';           space = !space;         }       }       space = !space;     }     return board;   }   private char[][] add_mines(int numofmines, int height, int width) {     char[][] mines = create_board(height, width);     boolean done = false;     Random rand = new Random(0);     while ( !done ) {       for(int i=0; i< height; i++) {         for (int j=0; j< width;j++) {           if (numofmines<=0) {             done = true;           }           if (mines[i][j] != ' ' && mines[i][j] != 'M' && rand.nextInt(1000) > 800 && numofmines > 0) {             mines[i][j] = 'M';             numofmines--;           }         }       }     }     return mines;   }   public void uncover(int x, int y) {     if (mines[x][(y*2)+(x%2)] == 'M') {       revealall();       printwithmines();       System.out.println("Oops! Uncovered a mine!");     }     char res = reveal(x, (y*2)+(x%2));     if (res == '.') {       revealMore(x, (y * 2) + (x % 2));     }   }   private void revealall() {     for(int i=0;ifor(int j=0;j2;j++) {         if(board[i][j] == 'C') {           reveal(i,j);         }       }     }   }   public char reveal(int x, int y) {     switch (board[x][y]) {       case MARKED:         // If the cell was marked, unmark it         board[x][y] = 'C';       case UNKNOWN:         // One less unknown cell now         numofBlockCovered--;         if (mines[x][y] == 'M') {           board[x][y] = MINE;         }         else {           // How many mines in the vicinity           char res = closeMines(x, y);           board[x][y] = res == '0'?'.': res;         }         break;     }     // Return the revealed value     return board[x][y];   }   private int[][] getadjacentblock(int x, int y) {     int [][] adj = new int[6][2];     if (y-2>=0 && y-22) {       adj[0][0] = x;       adj[0][1] = y-2;     }     else {       adj[0][0] = -1;       adj[0][1] = -1;     }     if (x+1>=0 && x+1< height && y-1>=0 && y-12) {       adj[1][0] = x+1;       adj[1][1] = y-1;     }     else {       adj[1][0] = -1;       adj[1][1] = -1;     }     if (x+1>=0 && x+1< height && y+1>=0 && y+12) {       adj[2][0] = x+1;       adj[2][1] = y+1;     }     else {       adj[2][0] = -1;       adj[2][1] = -1;     }     if (y+2>=0 && y+22) {       adj[3][0] = x;       adj[3][1] = y+2;     }     else {       adj[3][0] = -1;       adj[3][1] = -1;     }     if (x-1>=0 && x-1< height && y+1>=0 && y+12) {       adj[4][0] = x-1;       adj[4][1] = y+1;     }     else {       adj[4][0] = -1;       adj[4][1] = -1;     }     if (x-1>=0 && x-1< height && y-1>=0 && y-12) {       adj[5][0] = x-1;       adj[5][1] = y-1;     }     else {       adj[5][0] = -1;       adj[5][1] = -1;     }     return adj;   }   public void setToggle(int x, int y) {     if (board[x][(y*2)+(x%2)] == 'F' || board[x][(y*2)+(x%2)] == 'C') {       board[x][(y*2)+(x%2)] = board[x][(y*2)+(x%2)] == 'F' ? 'C':'F';     }   }   public void revealMore(int x, int y) {     int [][] adjacentblock = getadjacentblock(x,y);     // Loop over all surrounding cells     for (int i = 0; i < 6; i++) {       if (adjacentblock[i][0] != -1) {         if (mines[adjacentblock[i][0]][adjacentblock[i][1]] != 'M' && board[adjacentblock[i][0]][adjacentblock[i][1]] == 'C') {           reveal(adjacentblock[i][0], adjacentblock[i][1]);           if (board[adjacentblock[i][0]][adjacentblock[i][1]] == '.') {             // Call ourself recursively             revealMore(adjacentblock[i][0], adjacentblock[i][1]);           }         }       }     }   }   private char closeMines(int x, int y) {     int result = 0;     int pos1x = x, pos1y = y - 2;     int pos2x = x+1, pos2y = y -1;     int pos3x = x+1, pos3y = y + 1;     int pos4x = x, pos4y = y + 2;     int pos5x = x-1, pos5y = y + 1;     int pos6x = x-1, pos6y = y - 1;     if (pos1x>=0 && pos1x=0 && pos1y2) {       if (mines[pos1x][pos1y] == 'M') {         result++;       }     }     if (pos2x>=0 && pos2x=0 && pos2y2) {       if (mines[pos2x][pos2y] == 'M') {         result++;       }     }     if (pos3x>=0 && pos3x=0 && pos3y2) {       if (mines[pos3x][pos3y] == 'M') {         result++;       }     }     if (pos4x>=0 && pos4x=0 && pos4y2) {       if (mines[pos4x][pos4y] == 'M') {         result++;       }     }     if (pos5x>=0 && pos5x=0 && pos5y2) {       if (mines[pos5x][pos5y] == 'M') {         result++;       }     }     if (pos6x>=0 && pos6x=0 && pos6y2) {       if (mines[pos6x][pos6y] == 'M') {         result++;       }     }     return Integer.toString(result).charAt(0);   }   public void printboard() {     for(int i=0;ifor(int j=0;j2;j++) {         System.out.print(board[i][j]);       }       System.out.println();     }   }   public void printwithmines() {     for(int i=0;ifor(int j=0;j2;j++) {         if(mines[i][j] == 'M') {           System.out.print(mines[i][j]);         }else{           System.out.print(board[i][j]);         }       }       System.out.println();     }   } } /////////////This is the TestHexMines public class TestHexMines {   public static void main(String args[]) {     System.out.println("Summary of symbols in my string representations");     System.out.println("c : covered cell");     System.out.println("F : flagged cell");     System.out.println("M : uncovered mine");     System.out.println(". : uncovered cell with no neighboring mines");     System.out.println(" : uncovered cell with n neighboring mines");     System.out.println("Test first manager");     System.out.println("initializing hexagon with 6 height and 10 width and mines 10");     HexMineManager hexMineManager = new HexMineManager(6, 10, 10);     testfirstmanager(hexMineManager);     System.out.println("Test second manager");     System.out.println("initializing hexagon with 10 height and 20 width and mines 30");     HexMineManager hexMineManager1 = new HexMineManager(10, 20, 30);     testsecondmanager(hexMineManager1);     System.out.println();     testfirstmanagersecondtime(hexMineManager);   }   public static void testfirstmanager(HexMineManager mgr) {     mgr.printboard();     System.out.println();     System.out.println("uncover(4,7)");     mgr.uncover(4,7);     mgr.printboard();     System.out.println();     System.out.println("Toggle at (1,2)");     mgr.setToggle(1,2);     mgr.printboard();     System.out.println();     System.out.println("uncover(0,3)");     mgr.uncover(0,3);     mgr.printboard();     System.out.println();   }   public static void testsecondmanager(HexMineManager mgr) {     mgr.printboard();     System.out.println();     System.out.println("toggle flag at (3, 0)");     mgr.setToggle(3,0);     mgr.printboard();     System.out.println();     System.out.println("uncover (4, 1)");     mgr.uncover(5,5);     mgr.printboard();     System.out.println();     System.out.println("toggle flag at (3, 0)");     mgr.setToggle(3,0);     mgr.printboard();     System.out.println();     System.out.println("uncover (3, 5)");     mgr.uncover(3,5);     System.out.println();     System.out.println("Go back and test first manager some more");   }   public static void testfirstmanagersecondtime(HexMineManager mgr){     mgr.printboard();     System.out.println();     System.out.println("uncover (2, 3)");     mgr.uncover(2,3);     System.out.println();     mgr.printboard();     System.out.println();     System.out.println("toggle flag at (1, 0)");     mgr.setToggle(1,0);     System.out.println();     mgr.printboard();     System.out.println();     System.out.println("uncover (2, 4)");     mgr.uncover(2,4);     System.out.println();     mgr.printboard();     System.out.println();   } } 

What I am trying to do is print an actual hexagonal grid and not a rectangular grid...

I am don't now how to print a hexagonal grid and I need help with it. I have provided the the two *.java files

Should look something like this instead. initializing hexagon with radius 5 and 10 mines c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c uncover (0, 1) c c c c c c c c c c c c c c c c c c c c c c c c c c 1 1 c 1 c c c c c 2 . 1 1 . c c c c 1 1 . . . . . c c c 1 . . . . . . c c 1 . . . . . . c c 1 . 1 1 . . c c 1 2 c 2 . 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions