Created
November 16, 2023 16:01
-
-
Save youssef3wi/d449c5bba45693f72b69b33d74df3a73 to your computer and use it in GitHub Desktop.
Ascii-art
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Scanner; | |
| public class CodingGame { | |
| private static final int A = 'A'; | |
| private static final int Z = 'Z'; | |
| // static mapping [A-Z] -> ascii-art | |
| private static final Map<Character, String> ARTS = new HashMap<>(); | |
| static { | |
| ARTS.put('A', "\n # \n# # \n### \n# # \n# # "); | |
| ARTS.put('B', "\n## \n# # \n## \n# # \n## "); | |
| ARTS.put('C', "\n ## \n# \n# \n# \n ## "); | |
| ARTS.put('D', "\n## \n# # \n# # \n# # \n## "); | |
| ARTS.put('E', "\n### \n# \n## \n# \n### "); | |
| ARTS.put('F', "\n### \n# \n## \n# \n# "); | |
| ARTS.put('G', "\n ## \n# \n# # \n# # \n ## "); | |
| ARTS.put('H', "\n# # \n# # \n### \n# # \n# # "); | |
| ARTS.put('I', "\n### \n # \n # \n # \n### "); | |
| ARTS.put('J', "\n ## \n # \n # \n# # \n # "); | |
| ARTS.put('K', "\n# # \n# # \n## \n# # \n# # "); | |
| ARTS.put('L', "\n# \n# \n# \n# \n### "); | |
| ARTS.put('M', "\n# # \n### \n### \n# # \n# # "); | |
| ARTS.put('N', "\n### \n# # \n# # \n# # \n# # "); | |
| ARTS.put('O', "\n # \n# # \n# # \n# # \n # "); | |
| ARTS.put('P', "\n## \n# # \n## \n# \n# "); | |
| ARTS.put('Q', "\n # \n# # \n# # \n ## \n # "); | |
| ARTS.put('R', "\n## \n# # \n## \n# # \n# # "); | |
| ARTS.put('S', "\n ## \n# \n # \n # \n## "); | |
| ARTS.put('T', "\n### \n # \n # \n # \n # "); | |
| ARTS.put('U', "\n# # \n# # \n# # \n# # \n### "); | |
| ARTS.put('V', "\n# # \n# # \n# # \n# # \n # "); | |
| ARTS.put('W', "\n# # \n# # \n### \n### \n# # "); | |
| ARTS.put('X', "\n# # \n# # \n # \n# # \n# # "); | |
| ARTS.put('Y', "\n# # \n# # \n # \n # \n # "); | |
| ARTS.put('Z', "\n### \n # \n # \n# \n### "); | |
| } | |
| public static void main(String[] args) { | |
| Scanner scanner = new Scanner(System.in); | |
| String strToPrint = scanner.next().toUpperCase(); | |
| String art = ""; | |
| for (int idx = 0; idx < strToPrint.length(); idx++) { | |
| char currentChar = strToPrint.charAt(idx); | |
| if (currentChar >= A && currentChar <= Z) { | |
| String sketch = ARTS.get(currentChar); | |
| if (art.isEmpty()) { | |
| art += sketch; | |
| continue; | |
| } | |
| String[] sketches = sketch.split("\n"); | |
| String[] rows = art.split("\n"); | |
| art = ""; | |
| for (int jdx = 0; jdx < sketches.length; jdx++) { | |
| art += rows[jdx] + sketches[jdx] + "\n"; | |
| } | |
| } | |
| } | |
| System.out.println("----------------------"); | |
| System.out.println("-----Current ART------"); | |
| System.out.println("----------------------"); | |
| System.out.println(art); | |
| System.out.println("----------------------"); | |
| // Processing arts (like we have two different programs) | |
| List<String> arts = extractArts(art); | |
| // Search in the constants | |
| for (String currentArt : arts) { | |
| for (Map.Entry<Character, String> ART : ARTS.entrySet()) { | |
| if (currentArt.equals(ART.getValue())) { | |
| System.out.println("Found: " + ART.getKey()); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * Processing ascii arts. | |
| * | |
| * @param art the graphic art. | |
| * @return processed arts. | |
| */ | |
| private static List<String> extractArts(String art) { | |
| List<String> arts = new ArrayList<>(); | |
| String[] rows = art.split("\n"); | |
| for (int idx = 0; idx < rows.length; idx++) { | |
| String row = rows[idx]; | |
| int beginArt = 0; | |
| int currentIdxArt = 0; | |
| for (int jdx = 0; jdx < row.length(); jdx++) { | |
| char currentChar = row.charAt(jdx); | |
| // Check whether it has an empty row on the y axe. | |
| if (currentChar == ' ') { | |
| boolean isAnEmptyRow = true; | |
| int currentRow = 1; | |
| while (currentRow < rows.length) { | |
| if (rows[currentRow].charAt(jdx) != ' ') { | |
| isAnEmptyRow = false; | |
| break; | |
| } | |
| currentRow++; | |
| } | |
| // Processing next art | |
| if (isAnEmptyRow) { | |
| if (!arts.isEmpty() && idx > 1 && (jdx - 3) > 0) { | |
| beginArt = jdx - 3; | |
| } | |
| String currentArt = row.substring(beginArt, jdx); | |
| if (idx > 1) { // not first row | |
| // append | |
| arts.set(currentIdxArt, arts.get(currentIdxArt) + "\n" + currentArt + " "); | |
| } else { | |
| // Add to arts | |
| arts.add("\n" + currentArt + " "); | |
| beginArt = jdx + 1; | |
| } | |
| currentIdxArt++; | |
| } | |
| } | |
| } | |
| } | |
| return arts; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment