diff --git a/01_Acey_Ducey/aceyducey.bas b/01_Acey_Ducey/aceyducey.bas index 2c8b1c3b..0b6f72db 100644 --- a/01_Acey_Ducey/aceyducey.bas +++ b/01_Acey_Ducey/aceyducey.bas @@ -10,7 +10,7 @@ 80 PRINT"IF YOU DO NOT WANT TO BET, INPUT A 0" 100 N=100 110 Q=100 -120 PRINT "YOU NOW HAVE";Q;"DOLLARS." +120 PRINT "YOU NOW HAVE ";Q;" DOLLARS." 130 PRINT 140 GOTO 260 210 Q=Q+M diff --git a/03_Animal/kotlin/Animal.kt b/03_Animal/kotlin/Animal.kt new file mode 100644 index 00000000..bac9af08 --- /dev/null +++ b/03_Animal/kotlin/Animal.kt @@ -0,0 +1,119 @@ +/** + * ANIMAL + * + * + * Converted from BASIC to Kotlin by John Long (@patimen) + * + * Animal is basically a perfect example of a binary tree. Implement it + * as such, with the QuestionNode either having an answer if it is a terminal node + * or a Question + */ + +fun main() { + printIntro() + val rootQuestionNode = + QuestionOrAnswer(question = Question("DOES IT SWIM", QuestionOrAnswer("FISH"), QuestionOrAnswer("BIRD"))) + while (true) { + val choice = ask("ARE YOU THINKING OF AN ANIMAL") + when { + choice == "LIST" -> printKnownAnimals(rootQuestionNode) + choice.startsWith("Q") -> return + choice.startsWith("Y") -> { + // A wrong answer means it's a new animal! + val wrongAnswer = rootQuestionNode.getWrongAnswer() + if (wrongAnswer == null) { + // The computer got the right answer! + println("WHY NOT TRY ANOTHER ANIMAL?") + } else { + // Get a new question to ask next time + wrongAnswer.askForInformationAndSave() + } + } + } + } +} + +// Takes care of asking a question (on the same line) and getting +// an answer or a blank string +fun ask(question: String): String { + print("$question? ") + return readLine()?.uppercase() ?: "" +} + +// Special case for a "yes or no" question, returns true of yes +fun askYesOrNo(question: String): Boolean { + return generateSequence { + print("$question? ") + readLine() + }.firstNotNullOf { yesOrNo(it) } +} + +// If neither Y (true) or N (false), return null, so the above sequence +// will just keep executing until it gets the answer +private fun yesOrNo(string: String): Boolean? = + when (string.uppercase().firstOrNull()) { + 'Y' -> true + 'N' -> false + else -> null + } + +private fun printKnownAnimals(question: QuestionOrAnswer) { + println("\nANIMALS I ALREADY KNOW ARE:") + val animals = question.getAnswers().chunked(4) + animals.forEach { line -> + // The '*' in front of line.toTypedArray() "spreads" the array as a list of parameters instead + System.out.printf("%-15s".repeat(line.size), *line.toTypedArray()) + println() + } +} + +private fun printIntro() { + println(" ANIMAL") + println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + println("\n\n") + println("PLAY 'GUESS THE ANIMAL'") + println("\n") + println("THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT.") +} + +class QuestionOrAnswer(private var answer: String? = null, var question: Question? = null) { + fun getAnswers(): List = answer?.let { listOf(it) } ?: question!!.getAnswers() + fun getWrongAnswer(): QuestionOrAnswer? { + if (answer != null) { + // "takeUnless" will return null if the answer is "yes". In this case + // we will return the "wrong answer", aka the terminal answer that was incorrect + return this.takeUnless { askYesOrNo("IS IT A $answer") } + } + return question?.getWrongAnswer() + } + + fun askForInformationAndSave() { + //Failed to get it right and ran out of questions + //Let's ask the user for the new information + val newAnimal = ask("THE ANIMAL YOU WERE THINKING OF WAS A") + val newQuestion = ask("PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A \n$newAnimal FROM A $answer\n") + val newAnswer = askYesOrNo("FOR A $newAnimal THE ANSWER WOULD BE") + + val trueAnswer = if (newAnswer) newAnimal else answer + val falseAnswer = if (newAnswer) answer else newAnimal + // Replace our answer with null and set the question with the data we just got + // This makes it a question instead of an answer + this.answer = null + this.question = Question(newQuestion, QuestionOrAnswer(trueAnswer), QuestionOrAnswer(falseAnswer)) + } +} + +class Question( + private val question: String, + private val trueAnswer: QuestionOrAnswer, + private val falseAnswer: QuestionOrAnswer +) { + fun getAnswers(): List = trueAnswer.getAnswers() + falseAnswer.getAnswers() + + fun getWrongAnswer(): QuestionOrAnswer? = + if (askYesOrNo(question)) { + trueAnswer.getWrongAnswer() + } else { + falseAnswer.getWrongAnswer() + } +} diff --git a/03_Animal/kotlin/README.md b/03_Animal/kotlin/README.md new file mode 100644 index 00000000..f43a5b70 --- /dev/null +++ b/03_Animal/kotlin/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Kotlin](https://kotlinlang.org/) diff --git a/61_Math_Dice/perl/mathdice.pl b/61_Math_Dice/perl/mathdice.pl index f5a92459..4a2b6f6e 100644 --- a/61_Math_Dice/perl/mathdice.pl +++ b/61_Math_Dice/perl/mathdice.pl @@ -25,24 +25,25 @@ sub game_play { $sum = $sum + $roll; # keeping track of the summary #print "Sum: $sum Roll: $roll\n"; if ($num == 1) { - print " +\n"; # if its the first roll then print an addition sign + print "\n +\n\n"; # if its the first roll then print an addition sign } if ($num == 2) { - print " =\n"; # if its the second roll print the equals sign and wait for an answer + print " =? "; # if its the second roll print the equals sign and wait for an answer my $answer = ; chomp($answer); if ($answer == 0) { die "You input '0', Thanks for playing!\n"; } elsif ($answer == $sum) { - print "RIGHT!\nTHE DICE ROLL AGAIN\n"; + print "RIGHT!\n\nTHE DICE ROLL AGAIN\n\n"; } else { # code execution if they don't get the right answer print "NO,COUNT THE SPOTS AND GIVE ANOTHER ANSWER\n"; + print " =? "; $answer = ; chomp($answer); if ($answer == $sum){ - print "RIGHT!\nTHE DICE ROLL AGAIN\n"; + print "RIGHT!\n\nTHE DICE ROLL AGAIN\n\n"; } else { print "N0, THE ANSWER IS $sum\n"; diff --git a/90 Tower/csharp/Tower.sln b/90_Tower/csharp/Tower.sln similarity index 100% rename from 90 Tower/csharp/Tower.sln rename to 90_Tower/csharp/Tower.sln diff --git a/90 Tower/csharp/Tower/Game.cs b/90_Tower/csharp/Tower/Game.cs similarity index 100% rename from 90 Tower/csharp/Tower/Game.cs rename to 90_Tower/csharp/Tower/Game.cs diff --git a/90 Tower/csharp/Tower/Models/Needle.cs b/90_Tower/csharp/Tower/Models/Needle.cs similarity index 100% rename from 90 Tower/csharp/Tower/Models/Needle.cs rename to 90_Tower/csharp/Tower/Models/Needle.cs diff --git a/90 Tower/csharp/Tower/Models/Towers.cs b/90_Tower/csharp/Tower/Models/Towers.cs similarity index 100% rename from 90 Tower/csharp/Tower/Models/Towers.cs rename to 90_Tower/csharp/Tower/Models/Towers.cs diff --git a/90 Tower/csharp/Tower/Program.cs b/90_Tower/csharp/Tower/Program.cs similarity index 100% rename from 90 Tower/csharp/Tower/Program.cs rename to 90_Tower/csharp/Tower/Program.cs diff --git a/90 Tower/csharp/Tower/Resources/Congratulations.txt b/90_Tower/csharp/Tower/Resources/Congratulations.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/Congratulations.txt rename to 90_Tower/csharp/Tower/Resources/Congratulations.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskCountPrompt.txt b/90_Tower/csharp/Tower/Resources/DiskCountPrompt.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskCountPrompt.txt rename to 90_Tower/csharp/Tower/Resources/DiskCountPrompt.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskCountQuit.txt b/90_Tower/csharp/Tower/Resources/DiskCountQuit.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskCountQuit.txt rename to 90_Tower/csharp/Tower/Resources/DiskCountQuit.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskCountRetry.txt b/90_Tower/csharp/Tower/Resources/DiskCountRetry.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskCountRetry.txt rename to 90_Tower/csharp/Tower/Resources/DiskCountRetry.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskNotInPlay.txt b/90_Tower/csharp/Tower/Resources/DiskNotInPlay.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskNotInPlay.txt rename to 90_Tower/csharp/Tower/Resources/DiskNotInPlay.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskPrompt.txt b/90_Tower/csharp/Tower/Resources/DiskPrompt.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskPrompt.txt rename to 90_Tower/csharp/Tower/Resources/DiskPrompt.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskQuit.txt b/90_Tower/csharp/Tower/Resources/DiskQuit.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskQuit.txt rename to 90_Tower/csharp/Tower/Resources/DiskQuit.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskRetry.txt b/90_Tower/csharp/Tower/Resources/DiskRetry.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskRetry.txt rename to 90_Tower/csharp/Tower/Resources/DiskRetry.txt diff --git a/90 Tower/csharp/Tower/Resources/DiskUnavailable.txt b/90_Tower/csharp/Tower/Resources/DiskUnavailable.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/DiskUnavailable.txt rename to 90_Tower/csharp/Tower/Resources/DiskUnavailable.txt diff --git a/90 Tower/csharp/Tower/Resources/IllegalMove.txt b/90_Tower/csharp/Tower/Resources/IllegalMove.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/IllegalMove.txt rename to 90_Tower/csharp/Tower/Resources/IllegalMove.txt diff --git a/90 Tower/csharp/Tower/Resources/Instructions.txt b/90_Tower/csharp/Tower/Resources/Instructions.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/Instructions.txt rename to 90_Tower/csharp/Tower/Resources/Instructions.txt diff --git a/90 Tower/csharp/Tower/Resources/Intro.txt b/90_Tower/csharp/Tower/Resources/Intro.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/Intro.txt rename to 90_Tower/csharp/Tower/Resources/Intro.txt diff --git a/90 Tower/csharp/Tower/Resources/NeedlePrompt.txt b/90_Tower/csharp/Tower/Resources/NeedlePrompt.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/NeedlePrompt.txt rename to 90_Tower/csharp/Tower/Resources/NeedlePrompt.txt diff --git a/90 Tower/csharp/Tower/Resources/NeedleQuit.txt b/90_Tower/csharp/Tower/Resources/NeedleQuit.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/NeedleQuit.txt rename to 90_Tower/csharp/Tower/Resources/NeedleQuit.txt diff --git a/90 Tower/csharp/Tower/Resources/NeedleRetry.txt b/90_Tower/csharp/Tower/Resources/NeedleRetry.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/NeedleRetry.txt rename to 90_Tower/csharp/Tower/Resources/NeedleRetry.txt diff --git a/90 Tower/csharp/Tower/Resources/PlayAgainPrompt.txt b/90_Tower/csharp/Tower/Resources/PlayAgainPrompt.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/PlayAgainPrompt.txt rename to 90_Tower/csharp/Tower/Resources/PlayAgainPrompt.txt diff --git a/90 Tower/csharp/Tower/Resources/Strings.cs b/90_Tower/csharp/Tower/Resources/Strings.cs similarity index 100% rename from 90 Tower/csharp/Tower/Resources/Strings.cs rename to 90_Tower/csharp/Tower/Resources/Strings.cs diff --git a/90 Tower/csharp/Tower/Resources/TaskFinished.txt b/90_Tower/csharp/Tower/Resources/TaskFinished.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/TaskFinished.txt rename to 90_Tower/csharp/Tower/Resources/TaskFinished.txt diff --git a/90 Tower/csharp/Tower/Resources/Thanks.txt b/90_Tower/csharp/Tower/Resources/Thanks.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/Thanks.txt rename to 90_Tower/csharp/Tower/Resources/Thanks.txt diff --git a/90 Tower/csharp/Tower/Resources/Title.txt b/90_Tower/csharp/Tower/Resources/Title.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/Title.txt rename to 90_Tower/csharp/Tower/Resources/Title.txt diff --git a/90 Tower/csharp/Tower/Resources/TooManyMoves.txt b/90_Tower/csharp/Tower/Resources/TooManyMoves.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/TooManyMoves.txt rename to 90_Tower/csharp/Tower/Resources/TooManyMoves.txt diff --git a/90 Tower/csharp/Tower/Resources/YesNoPrompt.txt b/90_Tower/csharp/Tower/Resources/YesNoPrompt.txt similarity index 100% rename from 90 Tower/csharp/Tower/Resources/YesNoPrompt.txt rename to 90_Tower/csharp/Tower/Resources/YesNoPrompt.txt diff --git a/90 Tower/csharp/Tower/Tower.csproj b/90_Tower/csharp/Tower/Tower.csproj similarity index 100% rename from 90 Tower/csharp/Tower/Tower.csproj rename to 90_Tower/csharp/Tower/Tower.csproj diff --git a/90 Tower/csharp/Tower/UI/Input.cs b/90_Tower/csharp/Tower/UI/Input.cs similarity index 100% rename from 90 Tower/csharp/Tower/UI/Input.cs rename to 90_Tower/csharp/Tower/UI/Input.cs diff --git a/90 Tower/csharp/Tower/UI/Prompt.cs b/90_Tower/csharp/Tower/UI/Prompt.cs similarity index 100% rename from 90 Tower/csharp/Tower/UI/Prompt.cs rename to 90_Tower/csharp/Tower/UI/Prompt.cs diff --git a/90 Tower/csharp/Tower/UI/TowerDisplay.cs b/90_Tower/csharp/Tower/UI/TowerDisplay.cs similarity index 100% rename from 90 Tower/csharp/Tower/UI/TowerDisplay.cs rename to 90_Tower/csharp/Tower/UI/TowerDisplay.cs