Desklib is an online library that provides study material with solved assignments, essays, dissertations, and more. Find the resources you need for your courses.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
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.;
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Source Code for the application publicclassMineTunnel{ //number of miners in blue team (1 to 4) privateintblueGroup; privateintorangeGroup; //the number of moves made privateintcounter; //0 empty-1 blue in cave , 1 orange in cave privateintcaveStatus; // privateStringminerOrder; //String other_attributes; privateinttunnel_position; privateintcave_offset; privateintminer_width; privatefinalintleft_position=260; privateintlength_of_que; privateStringcurrent_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 */ publicMineTunnel(intcave_offset,intminer_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 */ publicvoidReset(){ minerOrder=""; orangeGroup=0; blueGroup=0; counter=0;
caveStatus=0; //An array with both options orange and blue Stringminers[]={"0","B"}; //choose randomly for(inti=0;i<5;i++){ intrandom=(int)((Math.random()*2)+1); Stringselected=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 */ publicStringgetCurrentPosition(){ returncurrent_position; } /** * Returns the cave status * * @return 0 empty -1 blue in cave 1 orange in cave */ publicintgetCave(){ returncaveStatus; } /** * Returns the current position of the miners * * @return */ publicintgetPlace(){ returntunnel_position;
} /** * returns String minerOrder with the order of * miners in the tunnel * * @return miners order */ publicStringgetMinersOrder(){ returnthis.minerOrder; } /** * The function takes a miner at the start or end of the * minerOrder string and placesin 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. */ publicbooleanmoveIn(){ 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 newTunnelDrawing(this); counter++; returntrue; }else{ returnfalse; } } /** * Returns the number of moves
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
* @return */ publicintgetMoves(){ returnthis.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 */ publicbooleanmoveOut(){ //check that its occupied if(getCave()!=0){ //if others are on the left, place the miner in the cave at the end charmin='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++; newTunnelDrawing(this); returntrue; } returnfalse; } /** * 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 */ publicbooleanmoveLeft(){ if(current_position.equals("Right")){ current_position="Left"; tunnel_position-=(this.miner_width*length_of_que)+(2* cave_offset); newTunnelDrawing(this); counter++;
returntrue; } returnfalse; } /** * 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 */ publicbooleanmoveRight(){ if(current_position.equals("Left")){ current_position="Right"; //change tunnel position tunnel_position+=(this.miner_width*length_of_que)+(2* cave_offset); newTunnelDrawing(this); counter++; returntrue; } returnfalse; } /** * The function checks if the game has been * won. It checks if blues are following one another or Orange * @return */ publicbooleangameOver(){ Stringorder=getMinersOrder(); intlen=order.length(); charstart=order.charAt(0); if(start=='O'&&order.charAt(len-1)=='B'){ returntrue; } //for (int i = 0; i < order.length(); i++) { //if(order.charAt(i) == order.charAt(i-1)){ //count++; //} // //} returnfalse; } }
importjava.util.Scanner; publicclassDisplay{ MineTunnelmine; publicDisplay(){ mine=newMineTunnel(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 */ publicvoidShowMenu(){ 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: "); Scannerscanner=newScanner(System.in); intchoice=scanner.nextInt(); switch(choice){ case1: mine.moveIn(); ShowTunnel(); break; case2: mine.moveOut(); ShowTunnel(); break; case3: mine.moveRight(); ShowTunnel(); break; case4: mine.moveLeft(); ShowTunnel(); break; case5: mine.Reset();
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.