From 36588fc1dbce2c7a6dd95f19bfa1eef7823e11b8 Mon Sep 17 00:00:00 2001 From: Darren Cardenas <53984972+darrencardenas@users.noreply.github.com> Date: Sat, 20 Mar 2021 12:00:27 -0400 Subject: [PATCH 1/2] Ported Diamond to Java. --- 32 Diamond/java/Diamond.java | 163 +++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 32 Diamond/java/Diamond.java diff --git a/32 Diamond/java/Diamond.java b/32 Diamond/java/Diamond.java new file mode 100644 index 00000000..bbad33a7 --- /dev/null +++ b/32 Diamond/java/Diamond.java @@ -0,0 +1,163 @@ +import java.util.Scanner; + +/** + * Game of Diamond + *
+ * Based on the BASIC game of Diamond here + * https://github.com/coding-horror/basic-computer-games/blob/main/32%20Diamond/diamond.bas + *
+ * 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 Diamond { + + private static final int LINE_WIDTH = 60; + + private static final String PREFIX = "CC"; + + private static final char SYMBOL = '!'; + + private final Scanner scan; // For user input + + + public Diamond() { + + scan = new Scanner(System.in); + + } // End of constructor Diamond + + + public void play() { + + showIntro(); + startGame(); + + } // End of method play + + + private void showIntro() { + + System.out.println(" ".repeat(32) + "DIAMOND"); + System.out.println(" ".repeat(14) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println("\n\n"); + + } // End of method showIntro + + + private void startGame() { + + int body = 0; + int column = 0; + int end = 0; + int fill = 0; + int increment = 2; + int numPerSide = 0; + int prefixIndex = 0; + int row = 0; + int start = 1; + int userNum = 0; + + String lineContent = ""; + + // Get user input + System.out.println("FOR A PRETTY DIAMOND PATTERN,"); + System.out.print("TYPE IN AN ODD NUMBER BETWEEN 5 AND 21? "); + userNum = scan.nextInt(); + System.out.println(""); + + // Calcuate number of diamonds to be drawn on each side of screen + numPerSide = (int) (LINE_WIDTH / userNum); + + end = userNum; + + // Begin loop through each row of diamonds + for (row = 1; row <= numPerSide; row++) { + + // Begin loop through top and bottom halves of each diamond + for (body = start; increment < 0 ? body >= end : body <= end; body += increment) { + + lineContent = ""; + + // Add whitespace + while (lineContent.length() < ((userNum - body) / 2)) { + lineContent += " "; + } + + // Begin loop through each column of diamonds + for (column = 1; column <= numPerSide; column++) { + + prefixIndex = 1; + + // Begin loop that fills each diamond with characters + for (fill = 1; fill <= body; fill++) { + + // Right side of diamond + if (prefixIndex > PREFIX.length()) { + + lineContent += SYMBOL; + + } + // Left side of diamond + else { + + lineContent += PREFIX.charAt(prefixIndex - 1); + prefixIndex++; + + } + + } // End loop that fills each diamond with characters + + // Column finished + if (column == numPerSide) { + + break; + + } + // Column not finishd + else { + + // Add whitespace + while (lineContent.length() < (userNum * column + (userNum - body) / 2)) { + lineContent += " "; + } + + } + + } // End loop through each column of diamonds + + System.out.println(lineContent); + + } // End loop through top and bottom half of each diamond + + if (start != 1) { + + start = 1; + end = userNum; + increment = 2; + + } + else { + + start = userNum - 2; + end = 1; + increment = -2; + row--; + + } + + } // End loop through each row of diamonds + + } // End of method startGame + + + public static void main(String[] args) { + + Diamond diamond = new Diamond(); + diamond.play(); + + } // End of method main + +} // End of class Diamond \ No newline at end of file From 6b3afaea526f01f37140afc24dc9f36f3f4b87d7 Mon Sep 17 00:00:00 2001 From: Darren Cardenas <53984972+darrencardenas@users.noreply.github.com> Date: Sat, 20 Mar 2021 12:03:19 -0400 Subject: [PATCH 2/2] Add newline to end of Diamond.java. --- 32 Diamond/java/Diamond.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/32 Diamond/java/Diamond.java b/32 Diamond/java/Diamond.java index bbad33a7..da56efe4 100644 --- a/32 Diamond/java/Diamond.java +++ b/32 Diamond/java/Diamond.java @@ -160,4 +160,4 @@ public class Diamond { } // End of method main -} // End of class Diamond \ No newline at end of file +} // End of class Diamond