Update README and add comments

This commit is contained in:
Zev Spitz
2022-01-21 04:52:44 +02:00
parent 74480a80b2
commit 0a4fe29ebe
2 changed files with 29 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
Public Property Yes As Branch Public Property Yes As Branch
Public Property No As Branch Public Property No As Branch
' Allows walking all the descendants recursively
Public Iterator Function DescendantTexts() As IEnumerable(Of String) Public Iterator Function DescendantTexts() As IEnumerable(Of String)
If Yes IsNot Nothing Then If Yes IsNot Nothing Then
Yield Yes.Text Yield Yes.Text

View File

@@ -1,6 +1,8 @@
Option Compare Text Option Compare Text
Public Class Game Public Class Game
' This Dictionary holds the corresponding value for each of the variants of "YES" and "NO" we accept
' Note that the Dictionary is case-insensitive, meaning it maps "YES", "yes" and even "yEs" to True
Private Shared ReadOnly YesNoResponses As New Dictionary(Of String, Boolean)(StringComparer.InvariantCultureIgnoreCase) From { Private Shared ReadOnly YesNoResponses As New Dictionary(Of String, Boolean)(StringComparer.InvariantCultureIgnoreCase) From {
{"yes", True}, {"yes", True},
{"y", True}, {"y", True},
@@ -15,6 +17,8 @@ Public Class Game
} }
ReadOnly console As ConsoleAdapterBase ReadOnly console As ConsoleAdapterBase
' The pre-initialized root branch
ReadOnly root As New Branch With { ReadOnly root As New Branch With {
.Text = "DOES IT SWIM?", .Text = "DOES IT SWIM?",
.Yes = New Branch With {.Text = "FISH"}, .Yes = New Branch With {.Text = "FISH"},
@@ -41,6 +45,7 @@ Public Class Game
Sub BeginLoop() Sub BeginLoop()
' Print the program heading
console.WriteCenteredLine("ANIMAL") console.WriteCenteredLine("ANIMAL")
console.WriteCenteredLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") console.WriteCenteredLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
console.Write( console.Write(
@@ -58,10 +63,16 @@ THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT.
Dim response = console.ReadLine Dim response = console.ReadLine
If response = "list" Then If response = "list" Then
' List all the stored animals
console.WriteLine( console.WriteLine(
" "
ANIMALS I ALREADY KNOW ARE:") ANIMALS I ALREADY KNOW ARE:")
' We're using a ForEach extension method instead of the regular For Each loop to provide the index alongside the text
root.DescendantTexts.ForEach(Sub(text, index) root.DescendantTexts.ForEach(Sub(text, index)
' We want to move to the next line after every four animals
' But for the first animal, where the index is 0, 0 Mod 4 will also return 0
' So we have to explicitly exclude the first animal
If index > 0 AndAlso index Mod 4 = 0 Then console.WriteLine() If index > 0 AndAlso index Mod 4 = 0 Then console.WriteLine()
console.Write($"{text.MaxLength(15),-15}") console.Write($"{text.MaxLength(15),-15}")
End Sub) End Sub)
@@ -76,10 +87,14 @@ ANIMALS I ALREADY KNOW ARE:")
Dim currentBranch = root Dim currentBranch = root
Do While Not currentBranch.IsEnd Do While Not currentBranch.IsEnd
' Branches can either be questions, or end branches
' We have to walk the questions, prompting each time for "yes" or "no"
console.Write($"{currentBranch.Text} ") console.Write($"{currentBranch.Text} ")
Do Do
ynResponse = GetYesNo() ynResponse = GetYesNo()
Loop While ynResponse Is Nothing Loop While ynResponse Is Nothing
' Depending on the answer, we'll follow either the branch at "Yes" or "No"
currentBranch = If( currentBranch = If(
ynResponse, ynResponse,
currentBranch.Yes, currentBranch.Yes,
@@ -87,31 +102,42 @@ ANIMALS I ALREADY KNOW ARE:")
) )
Loop Loop
'at end ' Now we're at an end branch
console.Write($"IS IT A {currentBranch.Text}? ") console.Write($"IS IT A {currentBranch.Text}? ")
ynResponse = GetYesNo() ynResponse = GetYesNo()
If ynResponse Then ' No difference between False or Nothing If ynResponse Then ' Only if ynResponse = True will we go into this If Then
console.WriteLine("WHY NOT TRY ANOTHER ANIMAL?") console.WriteLine("WHY NOT TRY ANOTHER ANIMAL?")
Continue Do Continue Do
End If End If
' Get the new animal
console.Write("THE ANIMAL YOU WERE THINKING OF WAS A ? ") console.Write("THE ANIMAL YOU WERE THINKING OF WAS A ? ")
Dim newAnimal = console.ReadLine Dim newAnimal = console.ReadLine
' Get the question used to distinguish the new animal from the current end branch
console.WriteLine( console.WriteLine(
$"PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A $"PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A
{newAnimal} FROM A {currentBranch.Text}") {newAnimal} FROM A {currentBranch.Text}")
Dim newQuestion = console.ReadLine Dim newQuestion = console.ReadLine
' Get the answer to that question, for the new animal
' for the old animal, the answer would be the opposite
console.Write( console.Write(
$"FOR A {newAnimal} THE ANSWER WOULD BE ? ") $"FOR A {newAnimal} THE ANSWER WOULD BE ? ")
Do Do
ynResponse = GetYesNo() ynResponse = GetYesNo()
Loop While ynResponse Is Nothing Loop While ynResponse Is Nothing
' Create the new end branch for the new animal
Dim newBranch = New Branch With {.Text = newAnimal} Dim newBranch = New Branch With {.Text = newAnimal}
' Copy over the current animal to another new end branch
Dim currentBranchCopy = New Branch With {.Text = currentBranch.Text} Dim currentBranchCopy = New Branch With {.Text = currentBranch.Text}
' Make the current branch into the distinguishing question
currentBranch.Text = newQuestion currentBranch.Text = newQuestion
' Set the Yes and No branches of the current branch according to the answer
If ynResponse Then If ynResponse Then
currentBranch.Yes = newBranch currentBranch.Yes = newBranch
currentBranch.No = currentBranchCopy currentBranch.No = currentBranchCopy