mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-08 03:12:26 -08:00
MAINT: Apply pre-commit
Remove byte-order-marker pre-commit check as there would be many adjustments necessary
This commit is contained in:
@@ -9,40 +9,40 @@ import java.lang.Math;
|
||||
* <p>
|
||||
* Note: The idea was to create a version of the 1970's BASIC game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*
|
||||
*
|
||||
* Converted from BASIC to Java by Darren Cardenas.
|
||||
*/
|
||||
|
||||
public class DepthCharge {
|
||||
|
||||
|
||||
public class DepthCharge {
|
||||
|
||||
private final Scanner scan; // For user input
|
||||
|
||||
|
||||
public DepthCharge() {
|
||||
|
||||
|
||||
scan = new Scanner(System.in);
|
||||
|
||||
} // End of constructor DepthCharge
|
||||
|
||||
} // End of constructor DepthCharge
|
||||
|
||||
public void play() {
|
||||
|
||||
|
||||
showIntro();
|
||||
startGame();
|
||||
|
||||
} // End of method play
|
||||
|
||||
|
||||
} // End of method play
|
||||
|
||||
private static void showIntro() {
|
||||
|
||||
|
||||
System.out.println(" ".repeat(29) + "DEPTH CHARGE");
|
||||
System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
} // End of method showIntro
|
||||
|
||||
private void startGame() {
|
||||
|
||||
int searchArea = 0;
|
||||
int shotNum = 0;
|
||||
int shotTotal = 0;
|
||||
int shotTotal = 0;
|
||||
int shotX = 0;
|
||||
int shotY = 0;
|
||||
int shotZ = 0;
|
||||
@@ -52,135 +52,135 @@ public class DepthCharge {
|
||||
int tries = 0;
|
||||
String[] userCoordinates;
|
||||
String userResponse = "";
|
||||
|
||||
|
||||
System.out.print("DIMENSION OF SEARCH AREA? ");
|
||||
searchArea = Integer.parseInt(scan.nextLine());
|
||||
System.out.println("");
|
||||
|
||||
shotTotal = (int) (Math.log10(searchArea) / Math.log10(2)) + 1;
|
||||
|
||||
|
||||
shotTotal = (int) (Math.log10(searchArea) / Math.log10(2)) + 1;
|
||||
|
||||
System.out.println("YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER");
|
||||
System.out.println("AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR");
|
||||
System.out.println("MISSION IS TO DESTROY IT. YOU HAVE " + shotTotal + " SHOTS.");
|
||||
System.out.println("SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A");
|
||||
System.out.println("TRIO OF NUMBERS -- THE FIRST TWO ARE THE");
|
||||
System.out.println("SURFACE COORDINATES; THE THIRD IS THE DEPTH.");
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
|
||||
|
||||
// Begin outer while loop
|
||||
while (true) {
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("GOOD LUCK !");
|
||||
System.out.println("");
|
||||
|
||||
|
||||
targetX = (int) ((searchArea + 1) * Math.random());
|
||||
targetY = (int) ((searchArea + 1) * Math.random());
|
||||
targetZ = (int) ((searchArea + 1) * Math.random());
|
||||
|
||||
|
||||
// Begin loop through all shots
|
||||
for (shotNum = 1; shotNum <= shotTotal; shotNum++) {
|
||||
|
||||
|
||||
// Get user input
|
||||
System.out.println("");
|
||||
System.out.print("TRIAL # " + shotNum + "? ");
|
||||
System.out.print("TRIAL # " + shotNum + "? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
|
||||
// Split on commas
|
||||
userCoordinates = userResponse.split(",");
|
||||
|
||||
|
||||
// Assign to integer variables
|
||||
shotX = Integer.parseInt(userCoordinates[0].trim());
|
||||
shotY = Integer.parseInt(userCoordinates[1].trim());
|
||||
shotZ = Integer.parseInt(userCoordinates[2].trim());
|
||||
|
||||
shotZ = Integer.parseInt(userCoordinates[2].trim());
|
||||
|
||||
// Win condition
|
||||
if (Math.abs(shotX - targetX) + Math.abs(shotY - targetY)
|
||||
if (Math.abs(shotX - targetX) + Math.abs(shotY - targetY)
|
||||
+ Math.abs(shotZ - targetZ) == 0) {
|
||||
|
||||
System.out.println("B O O M ! ! YOU FOUND IT IN" + shotNum + " TRIES!");
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
this.getReport(targetX, targetY, targetZ, shotX, shotY, shotZ);
|
||||
|
||||
System.out.println("");
|
||||
|
||||
|
||||
System.out.println("");
|
||||
|
||||
} // End loop through all shots
|
||||
|
||||
|
||||
if (shotNum > shotTotal) {
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("YOU HAVE BEEN TORPEDOED! ABANDON SHIP!");
|
||||
System.out.println("THE SUBMARINE WAS AT " + targetX + "," + targetY + "," + targetZ);
|
||||
}
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("");
|
||||
System.out.print("ANOTHER GAME (Y OR N)? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
System.out.print("ANOTHER GAME (Y OR N)? ");
|
||||
userResponse = scan.nextLine();
|
||||
|
||||
if (!userResponse.toUpperCase().equals("Y")) {
|
||||
System.out.print("OK. HOPE YOU ENJOYED YOURSELF.");
|
||||
System.out.print("OK. HOPE YOU ENJOYED YOURSELF.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
} // End outer while loop
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
|
||||
} // End of method startGame
|
||||
|
||||
public void getReport(int a, int b, int c, int x, int y, int z) {
|
||||
|
||||
|
||||
System.out.print("SONAR REPORTS SHOT WAS ");
|
||||
|
||||
|
||||
// Handle y coordinate
|
||||
if (y > b) {
|
||||
|
||||
System.out.print("NORTH");
|
||||
|
||||
|
||||
System.out.print("NORTH");
|
||||
|
||||
} else if (y < b) {
|
||||
|
||||
|
||||
System.out.print("SOUTH");
|
||||
}
|
||||
|
||||
|
||||
// Handle x coordinate
|
||||
if (x > a) {
|
||||
|
||||
|
||||
System.out.print("EAST");
|
||||
|
||||
|
||||
} else if (x < a) {
|
||||
|
||||
System.out.print("WEST");
|
||||
|
||||
System.out.print("WEST");
|
||||
}
|
||||
|
||||
|
||||
if ((y != b) || (x != a)) {
|
||||
|
||||
|
||||
System.out.print(" AND");
|
||||
}
|
||||
|
||||
|
||||
// Handle depth
|
||||
if (z > c) {
|
||||
|
||||
|
||||
System.out.println(" TOO LOW.");
|
||||
|
||||
|
||||
} else if (z < c) {
|
||||
|
||||
|
||||
System.out.println(" TOO HIGH.");
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
System.out.println(" DEPTH OK.");
|
||||
|
||||
System.out.println(" DEPTH OK.");
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
|
||||
|
||||
} // End of method getReport
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
DepthCharge game = new DepthCharge();
|
||||
game.play();
|
||||
|
||||
|
||||
} // End of method main
|
||||
|
||||
|
||||
} // End of class DepthCharge
|
||||
|
||||
@@ -12,10 +12,10 @@ function input()
|
||||
{
|
||||
var input_element;
|
||||
var input_str;
|
||||
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
input_element = document.createElement("INPUT");
|
||||
|
||||
|
||||
print("? ");
|
||||
input_element.setAttribute("type", "text");
|
||||
input_element.setAttribute("length", "50");
|
||||
|
||||
@@ -14,4 +14,3 @@ Perl makes life easy.
|
||||
* We use ternarys to generate the message if you miss the sub.
|
||||
* We use join to stitch the pieces of the string together.
|
||||
* If we have a ternary where we don't want to return anything we return an empty list rather than an empty string - if you return the latter you still get the padding spaces.
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ def get_num_charges():
|
||||
|
||||
def ask_for_new_game():
|
||||
answer = input("Another game (Y or N): ")
|
||||
if answer.lower().strip()[0] == 'y':
|
||||
if answer.lower().strip()[0] == "y":
|
||||
start_new_game()
|
||||
else:
|
||||
print("OK. Hope you enjoyed yourself")
|
||||
@@ -58,7 +58,7 @@ def show_shot_result(shot, location):
|
||||
|
||||
if shot[2] > location[2]:
|
||||
result += "too low."
|
||||
elif shot[2] < location [2]:
|
||||
elif shot[2] < location[2]:
|
||||
result += "too high."
|
||||
else:
|
||||
result += "depth OK."
|
||||
@@ -76,7 +76,7 @@ def get_shot_input():
|
||||
print(f"Example: 3 2 1")
|
||||
continue
|
||||
try:
|
||||
x, y, z = [int(num) for num in [x, y, z]]
|
||||
x, y, z = (int(num) for num in [x, y, z])
|
||||
return x, y, z
|
||||
except ValueError:
|
||||
print("Please enter whole numbers only")
|
||||
@@ -92,7 +92,7 @@ def play_game(search_area, num_charges):
|
||||
print("\nGood luck!\n")
|
||||
|
||||
# Generate position for submarine
|
||||
a, b, c = [random.randint(0, search_area) for _ in range(3)]
|
||||
a, b, c = (random.randint(0, search_area) for _ in range(3))
|
||||
|
||||
# Get inputs until win or lose
|
||||
for i in range(num_charges):
|
||||
@@ -116,5 +116,5 @@ def start_new_game():
|
||||
play_game(search_area, num_charges)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
start_new_game()
|
||||
|
||||
Reference in New Issue
Block a user