Development of Mine Puzzle Game using OOP (NIT2112 Assignment)

Verified

Added on  2022/12/30

|8
|1880
|61
Practical Assignment
AI Summary
This document presents the complete Java source code for the Mine Puzzle game, along with instructions on how to compile and run it. The game involves miners of two colors, blue and orange, moving through a tunnel with a central cave. The goal is to navigate the miners, one at a time, through the tunnel, using methods to move miners into and out of the cave, and to shift the miners' position in the tunnel. The code includes classes for the game logic, display and user interaction, featuring methods to simulate the miners' movements, track the game's state, and determine when the game is won or lost. The application also incorporates a GUI for a more interactive experience and a command line interface. The assignment emphasizes object-oriented programming principles, including encapsulation, inheritance, and polymorphism, to create a functional and engaging game environment.
Document Page
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.;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
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;
Document Page
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;
Document Page
}
/**
* returns String minerOrder with the order of
* miners in the tunnel
*
* @return miners order
*/
public String getMinersOrder() {
return this.minerOrder;
}
/**
* The function takes a miner at the start or end of the
* minerOrder string and places in the cave. it then updates the
* cave status to either -1 or 1. The minerOrder is then trancated
* by taking a substring of the original string. This is done to ensure
* the number of miners displayed is consistent and accurate.
* Only the miner at the front or back can get into the cave.
*
* Modified to return a boolean in order to be able to update the
* message field;
* @return true if moved, false if not moved.
*/
public boolean moveIn() {
if (getCave() == 0) {
if (current_position.equals("Left")) {
if (minerOrder.charAt(length_of_que - 1) == 'B') {
caveStatus = -1;
minerOrder = minerOrder.substring(0, length_of_que - 1);
} else {
caveStatus = 1;
minerOrder = minerOrder.substring(0, length_of_que - 1);
}
} else {
if (minerOrder.charAt(0) == 'B') {
caveStatus = -1;
minerOrder = minerOrder.substring(1, length_of_que);
} else {
caveStatus = 1;
minerOrder = minerOrder.substring(1, length_of_que);
}
}
//call repainter
new TunnelDrawing(this);
counter++;
return true;
}else{
return false;
}
}
/**
* Returns the number of moves
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
* @return
*/
public int getMoves() {
return this.counter;
}
/**
* Removes the miner in the cave and places at the start or end
* of the minerOrder string, depending on whether the rest of the
* miners are on the left or right.
* The function first checks that the cave is occupied
* After removing the miner, the number of moves are increased
* then a call is made to the Tunnel Drawing to re-draw the GUI
* this is followed by a repaint(); which ensures the GUI is refreshed
*/
public boolean moveOut() {
//check that its occupied
if (getCave() != 0) {
//if others are on the left, place the miner in the cave at the
end
char min = 'O';
//IF BLUE was in the cave
if (caveStatus == -1) {
min = 'B';
}
if (current_position.equals("Left")) {
caveStatus = 0;
//append at the end
minerOrder += min;
} else {
caveStatus = 0;
//append at the beginning
minerOrder = min + minerOrder;
}
//increment moves
counter++;
new TunnelDrawing(this);
return true;
}
return false;
}
/**
* Moving left entails deducting the length covered by the miners
* from the position of tunnel
* shift all miners to the left of tunnel
* (return False if miners already on left)
* @return
*/
public boolean moveLeft() {
if (current_position.equals("Right")) {
current_position = "Left";
tunnel_position -= (this.miner_width * length_of_que) + (2 *
cave_offset);
new TunnelDrawing(this);
counter++;
Document Page
return true;
}
return false;
}
/**
* The function shift all miners to the right of tunnel
* it returns False if miners already on right.
* To move to the right the tunnel position is changed by adding
* the product of miner width * number of miners and twice the size of the
cave
* to the tunnel position.
* @return
*/
public boolean moveRight() {
if (current_position.equals("Left")) {
current_position = "Right";
//change tunnel position
tunnel_position += (this.miner_width * length_of_que) + (2 *
cave_offset);
new TunnelDrawing(this);
counter++;
return true;
}
return false;
}
/**
* The function checks if the game has been
* won. It checks if blues are following one another or Orange
* @return
*/
public boolean gameOver(){
String order = getMinersOrder();
int len = order.length();
char start = order.charAt(0);
if(start == 'O' && order.charAt(len -1) == 'B'){
return true;
}
// for (int i = 0; i < order.length(); i++) {
// if(order.charAt(i) == order.charAt(i-1)){
// count++;
// }
//
// }
return false;
}
}
Document Page
import java.util.Scanner;
public class Display {
MineTunnel mine;
public Display(){
mine = new MineTunnel(27, 21);
}
/**
* The function displays the menu to the command line
* it runs in an infinite loop that can only be terminated
* by option 6
*/
public void ShowMenu() {
ShowTunnel();
while(true){ //run in an infinite loop
System.out.println("Press 1 to move miner from tunnel to cave");
System.out.println("Press 2 to move miner from cave to tunnel");
System.out.println("Press 3 to move all tunnel miners to the right of
cave");
System.out.println("Press 4 to move all tunnel miners to the left of
cave");
System.out.println("Press 5 to start a new game");
System.out.println("Press 6 to quit");
System.out.println("");
System.out.println("");
System.out.print("Select your next move: ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
mine.moveIn();
ShowTunnel();
break;
case 2:
mine.moveOut();
ShowTunnel();
break;
case 3:
mine.moveRight();
ShowTunnel();
break;
case 4:
mine.moveLeft();
ShowTunnel();
break;
case 5:
mine.Reset();
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
ShowTunnel();
break;
case 6:
System.exit(0);
default:
System.out.println("Invalid Option");
break;
}
}
}
public void ShowTunnel() {
System.out.println(" __ ");
if(mine.getCave() == 0 ){ //empty
System.out.println("_______________| |________________");
}else if(mine.getCave() == 1 ){
System.out.println("_______________|O|________________");
}else if(mine.getCave() == -1 ){
System.out.println("_______________|B|________________");
}
if(mine.getCurrentPosition() == "Left"){
System.out.println(" " + mine.getMinersOrder());
}else{
System.out.println(" " + mine.getMinersOrder());
}
System.out.println("____________________________________");
}
/**
* The entry point to the application
*
* @param args
*/
public static void main(String args[]){
new Display().ShowMenu();
}
}
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]