I am nearly complete with my Minesweeper game but the rough idea is done.
My approach is with three classes. One is the tile class which represents a tile on the gameboard. The other is the actual gameboard which is made out of a 2-d array from the tiles. There is no function when you win but wanted to get ideas/ tips from coders. You can play until you lose.
I have my concerns with the current way I have coded things for example, I am unsure if using faceUp
is the best way I can represent an unopened tile but for now that is all I could think of. Furthermore, I implemented nearly every possible edge case I could think of for the board.
The main area I would like more help in is checking whether an given coordinate is within bounds.
public class Tile {
private int numBombsNearBy;
private String faceUp;
private boolean isBomb;
public Tile(){
numBombsNearBy = 0;
faceUp = "X";
isBomb = false;
}
GameBoard class:
public class GameBoard {
/*
TO DO:
Create method to show the rest ofthe bombs when the user loses.
*/
private Tile[][] gameBoard;
private int xdim = 8;
private int ydim = 8;
private List<Pair> listOfBombs = new ArrayList<>();
public GameBoard(){
gameBoard = new Tile[xdim][ydim];
for(int row = 0; row < xdim; row++){
for(int col = 0; col < ydim; col++){
gameBoard[row][col] = new Tile();
}
}
}
public Tile[][] getGameBoard(){
return gameBoard;
}
public void gameOver(Tile[][] gameBoard){
for(int bomb = 0; bomb < listOfBombs.size();bomb++){
Pair temp = listOfBombs.get(bomb);
gameBoard[temp.getX()][temp.getY()].setFaceUp("*");
}
displaygameBoard();
}
public void displaygameBoard(){
for(int row = 0; row < xdim; row++){
for(int col = 0; col < ydim; col++){
System.out.print(gameBoard[row][col].getFaceUp() + "|");
}
System.out.println();
}
}
public int generateRandomNumber(){
Random rn = new Random();
int range = 7 - 0+ 1;
int randomNum = rn.nextInt(range) + 0;
return randomNum;
}
public void fillBoardBombs(){
int row, col = 0;
for(int i = 0; i < 9; i++){
row = generateRandomNumber();
col = generateRandomNumber();
listOfBombs.add(new Pair(row, col));
gameBoard[row][col].setBomb(true);
updateNearByBombs(row, col);
}
}
public boolean checkIfBomb(int x, int y){
if(gameBoard[x][y].isBomb() == true){
return true;
}
return false;
}
public void showTileBomb(int x, int y){
gameBoard[x][y].setFaceUp(String.valueOf(gameBoard[x][y].getNumBombsNearBy()));
}
public void updateNearByBombs(int inputrow, int inputcol){
//For (0,0)
if(inputrow == 0 && inputcol == 0){
for(int row = inputrow; row <= inputrow + 1; row++){
for(int col = inputcol; col <= inputcol + 1; col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (0, (1 - 6))
else if(inputrow == 0 &&(inputcol >= 1 && inputcol <= 6)){
for(int row = inputrow; row <= inputrow + 1; row++){
for(int col = inputcol - 1; col <= inputcol + 1; col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (0,7)
else if(inputrow == 0 && inputcol == 7){
for(int row = inputrow; row <= inputrow + 1; row++){
for(int col = inputcol - 1; col <= inputcol;col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (1 - 6), 0)
else if((inputrow >= 1 && inputrow <= 6) && inputcol == 0){
for(int row = inputrow - 1; row <= inputrow + 1; row++){
for(int col = inputcol; col <= inputcol + 1; col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (7,0)
else if(inputrow == 7 && inputcol == 0){
for(int row = inputrow - 1; row<= inputrow;row++){
for(int col = inputcol; col <= inputcol + 1;col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (7, (1 - 6))
else if(inputrow == 7 && (inputcol >= 1 && inputcol <= 6)){
for(int row = inputrow - 1; row <= inputrow; row++){
for(int col = inputcol - 1; col <= inputcol + 1; col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (7,7)
else if(inputrow == 7 && inputcol == 7){
for(int row = inputrow - 1; row <= inputrow; row++){
for(int col = inputcol - 1; col <= inputcol; col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//For (1 - 6), 7)
else if((inputrow >= 1 && inputrow <= 6) && inputcol == 7){
for(int row = inputrow - 1; row<= inputrow + 1; row++){
for(int col = inputcol - 1; col <= inputcol;col++){
if(row == inputrow && col == inputcol){
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
//for the rest
else for(int row = inputrow - 1; row <= inputrow + 1; row++){
for(int col = inputcol - 1; col<= inputcol + 1;col++){
if(row == inputrow && col == inputcol) {
gameBoard[row][col].setNumBombsNearBy(0);
}else{
gameBoard[row][col].setNumBombsNearBy(gameBoard[row][col].getNumBombsNearBy() + 1);
}
}
}
}
}
Main:
public class JMineSweeper {
final static String regex = "^[0-7]\\s+[0-7]$";
public static String askForUserInput() {
Scanner input = new Scanner(System.in);
String xy = null;
System.out.print("Enter x and y coordinates seperated by a space: ");
return xy = input.nextLine();
}
public static boolean isCorrectInput(String input){
if(input.matches(regex)){
return true;
}
return false;
}
public static List<Integer> convertUserInput(String input){
String [] splited = input.split("\\s+");
List<Integer> coordinates = new ArrayList<>();
for(int i = 0; i < splited.length; i++){
coordinates.add(Integer.parseInt(splited[i]));
}
return coordinates;
}
public static void main(String[] args) {
Boolean gameOver = false;
GameBoard gameBoard = new GameBoard();
gameBoard.fillBoardBombs();
while(!gameOver){
try{
gameBoard.displaygameBoard();
String userInput = askForUserInput();
if(isCorrectInput(userInput)){
List<Integer> coordinates = convertUserInput(userInput);
System.out.println(coordinates.toString());
if(gameBoard.checkIfBomb(coordinates.get(0), coordinates.get(1))){
System.out.println("You lose, tile is a bomb");
gameBoard.gameOver(gameBoard.getGameBoard());
gameOver = true;
}else{
gameBoard.showTileBomb(coordinates.get(0), coordinates.get(1));
}
}
}catch (Exception e){
System.out.println("Incorrect input");
}
}
}
}'