3 Turns Each turn, the user will input a location r, y to attack on the board. Y
ID: 3911203 • Letter: 3
Question
3 Turns Each turn, the user will input a location r, y to attack on the board. You can assume that z and y are integers, but you will need to check if the pair r,y is a valid location on the game board later If there is no boat at the location, print "miss" If the user has already attacked that location or location is out of bounds, print "penalty" If there is a boat, print "hit". If the boat sinks, print "sunk". Do not print "hit" again if the user has already "hit" this location In addition to normal cannon fire, the user will have the option to utilize a recon "drone" to scan a small area of the board. You can implement this option however you wish, as long as it is plain to the user what must be done to use the drone. The drone will allow the player to see the contents of the squares in a 3 × 3 area around the input location, but this will not count as a shot. Instead, the user will lose 4 additional turns while waiting for the drone to return from its mission. Therefore if the user chooses to use the drone, print "recon". If the user sends the drone out of bounds, the user will receive a penalty for a shot out of bounds, wait 4 additional turns for the drone to return but the board wil not be revealed, since the drone wl not return to the user with any valuable information If the user receives a penalty (for attacking the same spot twice or for attacking somewhere out of bounds), the user's next turn will be skipped. The game ends when all the boats have been sunk The game should report how many turns it took to sink all the boats, as well as the total number of cannon shots. Lower scores are better!Explanation / Answer
play around with the code and let me know if you face any issues, seems to work fine for me.
for loop is not possible in this case as we din't know the no of rotaions.
import java.util.Scanner;
public class Lock {
public static final int CLOCKWISE = 0;
public static final int COUNTER_CLOCKWISE = 1;
Scanner in = new Scanner(System.in);
private int x;
private int y;
private int z;
private boolean isLockOpen;
private int noOnTopOfKnob;
public Lock(){
this.x = 0;
this.y = 0;
this.z = 0;
this.isLockOpen = false;
this.noOnTopOfKnob = 0;
}
public void alterLockCombinaiton(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
public void turnKnob(int direction, int noToStop){
int i=noOnTopOfKnob;
int numbersPassed = 0;
System.out.println("Simulating...... Current no on top of knob: ");
do{
if(direction==CLOCKWISE)
i++;
else if(direction==COUNTER_CLOCKWISE)
i--;
if(i>39)
i=0;
if(i<0)
i=39;
this.noOnTopOfKnob = i;
System.out.print(noOnTopOfKnob+" ");
numbersPassed++;
if(numbersPassed>40 && noOnTopOfKnob==noToStop)
break;
}
while(true);
System.out.println(); // enter a blank line
}
public void closeLock(){
this.isLockOpen = false;
}
public boolean openLock(){
// initializing with arbitrary values
int firstStop = -1;
int secondStop = -1;
int thirdStop = -1;
int firstRotation = -1;
int secondRotation = -1;
int thirdRotation = -1;
for(int i=1; i<=3; i++){
System.out.print("Enter the no to stop with on top(0-39) for Rotation"+i+": ");
int noToStop = in.nextInt();
System.out.print("Enter the direction to turn (0 for clockiwse and 1 for counter clock wise) for Rotation"+i+": ");
int direction = in.nextInt();
turnKnob(direction, noToStop); // simulate the knob
if(i==1){
firstStop = noToStop;
firstRotation = direction;
}
else if(i==2){
secondStop = noToStop;
secondRotation = direction;
}
else if(i==3){
thirdStop = noToStop;
thirdRotation = direction;
}
// open lock criteria
if(firstStop==this.x && firstRotation==CLOCKWISE
&& secondStop==this.y && secondRotation==COUNTER_CLOCKWISE
&& thirdStop==this.z && thirdRotation==CLOCKWISE){
this.isLockOpen = true;
}
}
return isLockOpen;
}
public boolean isLockOpen(){
return isLockOpen;
}
public int getNoAtTop(){
return noOnTopOfKnob;
}
}
public class LockDemo {
public static void main(String[] args) {
// initializes with lock closed
Lock lock = new Lock();
System.out.println("Welcome to lock simulator");
lock.alterLockCombinaiton(5, 6, 7); // current lock setting
System.out.println("Before the open attempt....");
System.out.println("No on top: "+lock.getNoAtTop());
System.out.println("Lock is open: "+lock.isLockOpen());
lock.openLock();
System.out.println("-------------------------------------------------");
System.out.println("After lock open attemp....");
System.out.println("No on top: "+lock.getNoAtTop());
System.out.println("Lock is open: "+lock.isLockOpen());
}
}
output:
Welcome to lock simulator
Before the open attempt....
No on top: 0
Lock is open: false
Enter the no to stop with on top(0-39) for Rotation1: 5
Enter the direction to turn (0 for clockiwse and 1 for counter clock wise) for Rotation1: 0
Simulating...... Current no on top of knob:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 0 1 2 3 4 5
Enter the no to stop with on top(0-39) for Rotation2: 6
Enter the direction to turn (0 for clockiwse and 1 for counter clock wise) for Rotation2: 1
Simulating...... Current no on top of knob:
4 3 2 1 0 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
Enter the no to stop with on top(0-39) for Rotation3: 7
Enter the direction to turn (0 for clockiwse and 1 for counter clock wise) for Rotation3: 0
Simulating...... Current no on top of knob:
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 0 1 2 3 4 5 6 7
-------------------------------------------------
After lock open attemp....
No on top: 7
Lock is open: true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.