From 6ee857bf2df1bbefc81b6d25cbe9f0fd7f8e942c Mon Sep 17 00:00:00 2001 From: Joe Walter Date: Sun, 6 Jun 2021 14:14:53 -0400 Subject: [PATCH] C# and VB port of word --- 96 Word/csharp/Program.cs | 163 +++++++++++++++++++++++++++++++++++++ 96 Word/csharp/word.csproj | 8 ++ 96 Word/csharp/word.sln | 25 ++++++ 96 Word/vbnet/Program.vb | 145 +++++++++++++++++++++++++++++++++ 96 Word/vbnet/word.sln | 25 ++++++ 96 Word/vbnet/word.vbproj | 9 ++ 6 files changed, 375 insertions(+) create mode 100644 96 Word/csharp/Program.cs create mode 100644 96 Word/csharp/word.csproj create mode 100644 96 Word/csharp/word.sln create mode 100644 96 Word/vbnet/Program.vb create mode 100644 96 Word/vbnet/word.sln create mode 100644 96 Word/vbnet/word.vbproj diff --git a/96 Word/csharp/Program.cs b/96 Word/csharp/Program.cs new file mode 100644 index 00000000..464787c5 --- /dev/null +++ b/96 Word/csharp/Program.cs @@ -0,0 +1,163 @@ +using System; +using System.Linq; +using System.Text; + +namespace word +{ + class Word + { + // Here's the list of potential words that could be selected + // as the winning word. + private string[] words = { "DINKY", "SMOKE", "WATER", "GRASS", "TRAIN", "MIGHT", "FIRST", + "CANDY", "CHAMP", "WOULD", "CLUMP", "DOPEY" }; + + /// + /// Outputs the instructions of the game. + /// + private void intro() + { + Console.WriteLine("WORD".PadLeft(37)); + Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".PadLeft(59)); + + Console.WriteLine("I am thinking of a word -- you guess it. I will give you"); + Console.WriteLine("clues to help you get it. Good luck!!"); + } + + /// + /// This allows the user to enter a guess - doing some basic validation + /// on those guesses. + /// + /// The guess entered by the user + private string get_guess() + { + string guess = ""; + + while (guess.Length == 0) + { + Console.WriteLine($"{Environment.NewLine}Guess a five letter word. "); + guess = Console.ReadLine().ToUpper(); + + if ((guess.Length != 5) || (guess.Equals("?")) || (!guess.All(char.IsLetter))) + { + guess = ""; + Console.WriteLine("You must guess a give letter word. Start again."); + } + } + + return guess; + } + + /// + /// This checks the user's guess against the target word - capturing + /// any letters that match up between the two as well as the specific + /// letters that are correct. + /// + /// The user's guess + /// The 'winning' word + /// A string showing which specific letters have already been guessed + /// The integer value showing the number of character matches between guess and target + private int check_guess(string guess, string target, StringBuilder progress) + { + // Go through each letter of the guess and see which + // letters match up to the target word. + // For each position that matches, update the progress + // to reflect the guess + int matches = 0; + string common_letters = ""; + + for (int ctr = 0; ctr < 5; ctr++) + { + // First see if this letter appears anywhere in the target + // and, if so, add it to the common_letters list. + if (target.Contains(guess[ctr])) + { + common_letters.Append(guess[ctr]); + } + // Then see if this specific letter matches the + // same position in the target. And, if so, update + // the progress tracker + if (guess[ctr].Equals(target[ctr])) + { + progress[ctr] = guess[ctr]; + matches++; + } + } + + Console.WriteLine($"There were {matches} matches and the common letters were... {common_letters}"); + Console.WriteLine($"From the exact letter matches, you know......... {progress}"); + return matches; + } + + /// + /// This plays one full game. + /// + private void play_game() + { + string guess_word, target_word; + StringBuilder guess_progress = new StringBuilder("-----"); + Random rand = new Random(); + int count = 0; + + Console.WriteLine("You are starting a new game..."); + + // Randomly select a word from the list of words + target_word = words[rand.Next(words.Length)]; + + // Just run as an infinite loop until one of the + // endgame conditions are met. + while (true) + { + // Ask the user for their guess + guess_word = get_guess(); + count++; + + // If they enter a question mark, then tell them + // the answer and quit the game + if (guess_word.Equals("?")) + { + Console.WriteLine($"The secret word is {target_word}"); + return; + } + + // Otherwise, check the guess against the target - noting progress + if (check_guess(guess_word, target_word, guess_progress) == 0) + { + Console.WriteLine("If you give up, type '?' for your next guess."); + } + + // Once they've guess the word, end the game. + if (guess_progress.Equals(guess_word)) + { + Console.WriteLine($"You have guessed the word. It took {count} guesses!"); + return; + } + } + } + + /// + /// The main entry point for the class - just keeps + /// playing the game until the user decides to quit. + /// + public void play() + { + intro(); + + bool keep_playing = true; + + while (keep_playing) + { + play_game(); + Console.WriteLine($"{Environment.NewLine}Want to play again? "); + keep_playing = Console.ReadLine().StartsWith("y", StringComparison.CurrentCultureIgnoreCase); + } + } + } + + class Program + { + static void Main(string[] args) + { + new Word().play(); + } + } +} diff --git a/96 Word/csharp/word.csproj b/96 Word/csharp/word.csproj new file mode 100644 index 00000000..c73e0d16 --- /dev/null +++ b/96 Word/csharp/word.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/96 Word/csharp/word.sln b/96 Word/csharp/word.sln new file mode 100644 index 00000000..59bdec97 --- /dev/null +++ b/96 Word/csharp/word.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31321.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "word", "word.csproj", "{E2CF183B-EBC3-497C-8D34-32EBEE4E2B73}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E2CF183B-EBC3-497C-8D34-32EBEE4E2B73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2CF183B-EBC3-497C-8D34-32EBEE4E2B73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2CF183B-EBC3-497C-8D34-32EBEE4E2B73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2CF183B-EBC3-497C-8D34-32EBEE4E2B73}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B4D37881-6972-406B-978F-C1B60BA42638} + EndGlobalSection +EndGlobal diff --git a/96 Word/vbnet/Program.vb b/96 Word/vbnet/Program.vb new file mode 100644 index 00000000..cc807e56 --- /dev/null +++ b/96 Word/vbnet/Program.vb @@ -0,0 +1,145 @@ +Imports System +Imports System.Text +Imports System.Text.RegularExpressions + +Module Word + ' Here's the list of potential words that could be selected + ' as the winning word. + Dim words As String() = {"DINKY", "SMOKE", "WATER", "GRASS", "TRAIN", "MIGHT", "FIRST", + "CANDY", "CHAMP", "WOULD", "CLUMP", "DOPEY"} + + ' + ' Outputs the instructions of the game. + ' + Private Sub intro() + Console.WriteLine("WORD".PadLeft(37)) + Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".PadLeft(59)) + + Console.WriteLine("I am thinking of a word -- you guess it. I will give you") + Console.WriteLine("clues to help you get it. Good luck!!") + End Sub + + ' + ' This allows the user to enter a guess - doing some basic validation + ' on those guesses. + ' + ' The guess entered by the user + Private Function get_guess() As String + Dim guess As String = "" + + While (guess.Length = 0) + Console.WriteLine($"{Environment.NewLine}Guess a five letter word. ") + guess = Console.ReadLine().ToUpper() + + If ((guess.Length <> 5) Or guess.Equals("?") Or Not Regex.IsMatch(guess, "^[A-Z]+$")) Then + guess = "" + Console.WriteLine("You must guess a give letter word. Start again.") + End If + End While + + Return guess + End Function + + ' + ' This checks the user's guess against the target word - capturing + ' any letters that match up between the two as well as the specific + ' letters that are correct. + ' + ' The user's guess + ' The 'winning' word + ' A string showing which specific letters have already been guessed + ' The integer value showing the number of character matches between guess and target + Private Function check_guess(guess As String, target As String, progress As StringBuilder) As Integer + ' Go through each letter of the guess And see which + ' letters match up to the target word. + ' For each position that matches, update the progress + ' to reflect the guess + Dim matches As Integer = 0 + Dim common_letters As String = "" + + For ctr As Integer = 0 To 4 + + ' First see if this letter appears anywhere in the target + ' And, if so, add it to the common_letters list. + If (target.Contains(guess(ctr))) Then + common_letters.Append(guess(ctr)) + End If + ' Then see if this specific letter matches the + ' same position in the target. And, if so, update + ' the progress tracker + If (guess(ctr).Equals(target(ctr))) Then + progress(ctr) = guess(ctr) + matches += 1 + End If + Next + + Console.WriteLine($"There were {matches} matches and the common letters were... {common_letters}") + Console.WriteLine($"From the exact letter matches, you know......... {progress}") + Return matches + End Function + + ' + ' This plays one full game. + ' + Private Sub play_game() + Dim guess_word As String, target_word As String + Dim guess_progress As StringBuilder = New StringBuilder("-----") + Dim rand As Random = New Random() + Dim count As Integer = 0 + + Console.WriteLine("You are starting a new game...") + + ' Randomly select a word from the list of words + target_word = words(rand.Next(words.Length)) + + ' Just run as an infinite loop until one of the + ' endgame conditions are met. + While (True) + ' Ask the user for their guess + guess_word = get_guess() + count += 1 + + ' If they enter a question mark, then tell them + ' the answer and quit the game + If (guess_word.Equals("?")) Then + Console.WriteLine($"The secret word is {target_word}") + Return + End If + + ' Otherwise, check the guess against the target - noting progress + If (check_guess(guess_word, target_word, guess_progress) = 0) Then + Console.WriteLine("If you give up, type '?' for your next guess.") + End If + + ' Once they've guess the word, end the game. + If (guess_progress.Equals(guess_word)) Then + Console.WriteLine($"You have guessed the word. It took {count} guesses!") + Return + End If + + End While + End Sub + + ' + ' The main entry point for the class - just keeps + ' playing the game until the user decides to quit. + ' + Public Sub play() + intro() + + Dim keep_playing As Boolean = True + + While (keep_playing) + play_game() + Console.WriteLine($"{Environment.NewLine}Want to play again? ") + keep_playing = Console.ReadLine().StartsWith("y", StringComparison.CurrentCultureIgnoreCase) + End While + + End Sub +End Module + +Module Program + Sub Main(args As String()) + Word.play() + End Sub +End Module diff --git a/96 Word/vbnet/word.sln b/96 Word/vbnet/word.sln new file mode 100644 index 00000000..16584104 --- /dev/null +++ b/96 Word/vbnet/word.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31321.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "word", "word.vbproj", "{F0D2422C-983F-4DF3-9D17-D2480839DF07}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F0D2422C-983F-4DF3-9D17-D2480839DF07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0D2422C-983F-4DF3-9D17-D2480839DF07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0D2422C-983F-4DF3-9D17-D2480839DF07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0D2422C-983F-4DF3-9D17-D2480839DF07}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {179D39EB-C497-4336-B795-49CC799929BB} + EndGlobalSection +EndGlobal diff --git a/96 Word/vbnet/word.vbproj b/96 Word/vbnet/word.vbproj new file mode 100644 index 00000000..9868dd3e --- /dev/null +++ b/96 Word/vbnet/word.vbproj @@ -0,0 +1,9 @@ + + + + Exe + word + netcoreapp3.1 + + +