Created
April 30, 2020 18:55
-
-
Save ygrenzinger/82c6d175b1981f731edc21d3803de04f to your computer and use it in GitHub Desktop.
Jetbrains Academy AsciiArt
This file contains 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
package signature | |
import java.io.File | |
import java.util.* | |
data class AsciiFontChar(val letter: Char, val width: Int, val lines: List<String>) { | |
operator fun get(index: Int) = lines[index] | |
override fun toString() = lines.joinToString("\n") | |
} | |
class AsciiFont(file: File, spaceSize: Int) { | |
private val letters: Map<Char, AsciiFontChar> | |
private val fontHeight: Int | |
init { | |
val result = readFontFromFile(file) | |
fontHeight = result.first | |
letters = result.second + spaceLetter(spaceSize) | |
} | |
operator fun get(letter: Char) = letters[letter] | |
fun convert(input: String): List<String> { | |
val fontChars = input.map { letters.getValue(it) } | |
val initial = (1..fontHeight).map { "" } | |
val inputInAsciiArt: List<String> = fontChars.fold(initial) { lines, fontChar -> | |
lines.mapIndexed { i, line -> line + fontChar[i] } | |
} | |
return inputInAsciiArt.map { it.dropLast(1) } | |
} | |
private fun readLetterFromFile(letterLines: List<String>): AsciiFontChar { | |
val firstLine = letterLines[0].split(" ") | |
val letter: Char = firstLine[0][0] | |
val width = firstLine[1].toInt() | |
return AsciiFontChar(letter, width, letterLines.drop(1)) | |
} | |
private fun readFontFromFile(file: File): Pair<Int, Map<Char, AsciiFontChar>> { | |
val lines = file.readLines() | |
val height = lines[0].split(" ")[0].toInt() | |
val rawLetters = lines.drop(1).windowed(height + 1, height + 1) | |
val font = rawLetters.map { readLetterFromFile(it) }.map { it.letter to it }.toMap() | |
return Pair(height, font) | |
} | |
private fun spaceLetter(spaceSize: Int): Pair<Char, AsciiFontChar> { | |
val spaceLetter = (1..spaceSize).map { (1..spaceSize).joinToString("") { " " } } | |
return (' ' to AsciiFontChar(' ', spaceSize, spaceLetter)) | |
} | |
} | |
object AsciiArt { | |
private val mediumFont = AsciiFont(File("/Users/ygrenzinger/fonts/medium.txt"), 5) | |
private val romanFont = AsciiFont(File("/Users/ygrenzinger/fonts/roman.txt"), 10) | |
fun printAscii(name: String, status: String) { | |
val nameInAsciiArt = romanFont.convert(name) | |
val statusInAsciiArt = mediumFont.convert(status) | |
val maxLineLength: Int = (nameInAsciiArt + statusInAsciiArt).map { it.length }.max()!! | |
val border = (1..maxLineLength + 9).map { '8' }.joinToString("") | |
println(border) | |
nameInAsciiArt.forEach { | |
printAsciiLine(it, maxLineLength) | |
} | |
statusInAsciiArt.forEach { | |
printAsciiLine(it, maxLineLength) | |
} | |
println(border) | |
} | |
private fun printAsciiLine(line: String, maxLength: Int) { | |
val lineLength = line.length | |
val middle = (maxLength - lineLength) / 2 | |
val leftBlanks = (1..middle).map { ' ' }.joinToString("") | |
val rightBlanks = ((middle + lineLength)..maxLength).map { ' ' }.joinToString("") | |
println("88 $leftBlanks$line$rightBlanks 88") | |
} | |
} | |
fun main() { | |
val scanner = Scanner(System.`in`) | |
print("Enter name and surname: ") | |
val name = scanner.nextLine() | |
if (name.isBlank()) return | |
print("Enter person's status: ") | |
val status = scanner.nextLine() | |
if (status.isBlank()) return | |
AsciiArt.printAscii(name, status) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment