logo

Desklib - Online Library for Study Material

   

Added on  2022-12-30

8 Pages1880 Words61 Views
 | 
 | 
 | 
To run the application, compile all the source code; this can be done via command
line by navigating to the directory where the source file are store, and running the
command; javac *.java
After successfully compiling, run the command;
java MinePuzzle
To run the GUI version of the program.;
Desklib - Online Library for Study Material_1

Source Code for the application
public class MineTunnel {
//number of miners in blue team (1 to 4)
private int blueGroup;
private int orangeGroup;
//the number of moves made
private int counter;
//0 empty -1 blue in cave , 1 orange in cave
private int caveStatus;
//
private String minerOrder;
//String other_attributes;
private int tunnel_position;
private int cave_offset;
private int miner_width;
private final int left_position = 260;
private int length_of_que;
private String current_position;
/**
* The main constructor to this application
* takes two parameters;
* @param cave_offset; the size of the cave
* @param miner_width : width of a single miner
*/
public MineTunnel(int cave_offset, int miner_width) {
this.cave_offset = cave_offset;
this.miner_width = miner_width;
caveStatus = 0;
counter = 0;
this.Reset();
length_of_que = minerOrder.length();
//we start with the first miner at the left position, just near the
cave
current_position = "Left";
}
/**
* We created an array with O and B a random number is generated between 1
* and 2 the random number is used to pick an option from the miners
array;
* if O is selected the number of blue miners [blueGroup] is incremented
* otherwise orangeGroup is incremented
*/
public void Reset() {
minerOrder = "";
orangeGroup = 0;
blueGroup = 0;
counter = 0;
Desklib - Online Library for Study Material_2

caveStatus = 0;
//An array with both options orange and blue
String miners[] = {"0", "B"};
//choose randomly
for (int i = 0; i < 5; i++) {
int random = (int) ((Math.random() * 2) + 1);
String selected = miners[random - 1];
if (selected.equals("0")) {
//ensure not more than 4
if ((orangeGroup + 1) > 4) {
} else {
orangeGroup++;
minerOrder += selected;
}
} else {
if ((blueGroup + 1) > 4) {
} else {
blueGroup++;
minerOrder += selected;
}
}
}
length_of_que = minerOrder.length();
tunnel_position = left_position - (length_of_que * miner_width);
}
/**
* Returns the current position
* Right or Left of the cave
* @return
*/
public String getCurrentPosition(){
return current_position;
}
/**
* Returns the cave status
*
* @return 0 empty -1 blue in cave 1 orange in cave
*/
public int getCave() {
return caveStatus;
}
/**
* Returns the current position of the miners
*
* @return
*/
public int getPlace() {
return tunnel_position;
Desklib - Online Library for Study Material_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents