Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created May 25, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbilson/33da8c1b9600b60248a77fc11faf3af0 to your computer and use it in GitHub Desktop.
Save cbilson/33da8c1b9600b60248a77fc11faf3af0 to your computer and use it in GitHub Desktop.
Maze-L1
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class MineField {
public static int row;
public static int col;
public static void main(String[] args) {
// Project: Escape the MineField
char[][] array = new char[5][5];
// fill array
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
array[row][col] = '-';
} // end row
} // end col
// Add the player in position 2,2
array[2][2] = 'P';
row=2;
col=2;
// Add 3 Bombs
placeBomb(array);
placeBomb(array);
placeBomb(array);
// Add 2 exits in the margins
placeExit(array);
placeExit(array);
System.out.println("Original Board: ");
printBoard(array);
int count=0;
while (count<8)
{
// This changes row and col and erases the player from board
movePlayer(array,row,col);
if(checkBomb(array, row, col))
{
System.out.println("You hit a bomb!");
break;
}
else if(checkExit(array, row, col))
{
System.out.println("You win! You found the Exit!!");
break;
}
// Draw the player's new location
array[row][col] = 'P';
printBoard(array);
count++;
}
}
public static void printBoard(char[][] board)
{
// print array
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.print(board[row][col]+" ");
} // end row
System.out.println();
}
// end col
}
//===================================================
public static void placeBomb(char[][] board) {
Random rnd = new Random();
int y = rnd.nextInt(board.length);
int x = rnd.nextInt(board[y].length);
while (board[y][x] != '-') {
y = rnd.nextInt(board.length);
x = rnd.nextInt(board[y].length);
}
board[y][x] = 'B';
}
//=====================================================
public static void placeExit(char[][] board) {
Random rnd = new Random();
int y = 0;
int x = 0;
y = rnd.nextInt(board.length);
x = rnd.nextInt(board[y].length - 1);
while (board[y][x] != '-'
|| (y >= 1 && y < board.length - 1
&& x >= 1 && x < board[y].length - 1))
{
y = rnd.nextInt(board.length);
x = rnd.nextInt(board[y].length - 1);
}
board[y][x] = 'E';
}
//=====================================
public static boolean checkBomb(char[][] board,
int row, int col) {
if (board[row][col]=='B')
return true;
else
return false;
}
//=====================================
public static boolean checkExit(char[][] board,
int row, int col) {
if (board[row][col]=='E')
return true;
else
return false;
}
//=============================================
public static void setRow(int myRow) {
row=myRow;
}
//====================================
public static void setCol(int myCol) {
col=myCol;
}
// =======================================
public static void movePlayer(char[][] board,
int row, int col) {
Scanner kb = new Scanner(System.in);
String allowedDirections = "";
System.out.println("row: " + row + ", col: " + col);
if (row > 0) allowedDirections += " N";
if (row < board.length - 1) allowedDirections += " S";
if (col > 0) allowedDirections += " W";
if (col < board[row].length - 1) allowedDirections += " E";
String move = "X";
while (!allowedDirections.contains(move.toUpperCase())) {
System.out.print("\nEnter a direction to move: " +
"\n" + allowedDirections + ": ");
move = kb.next();
}
board[row][col]='-';
if (move.equalsIgnoreCase("N")) {
// move north
setRow(--row);
} else if (move.equalsIgnoreCase("S")) {
// move south
setRow(++row);
} else if (move.equalsIgnoreCase("E")) {
// move east
setCol(++col);
} else if (move.equalsIgnoreCase("W")) {
// move west
setCol(--col);
}
else {
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment