C# and VB port of word

This commit is contained in:
Joe Walter
2021-06-06 14:14:53 -04:00
parent ec2d063fb9
commit 6ee857bf2d
6 changed files with 375 additions and 0 deletions

163
96 Word/csharp/Program.cs Normal file
View File

@@ -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" };
/// <summary>
/// Outputs the instructions of the game.
/// </summary>
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!!");
}
/// <summary>
/// This allows the user to enter a guess - doing some basic validation
/// on those guesses.
/// </summary>
/// <returns>The guess entered by the user</returns>
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;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="guess">The user's guess</param>
/// <param name="target">The 'winning' word</param>
/// <param name="progress">A string showing which specific letters have already been guessed</param>
/// <returns>The integer value showing the number of character matches between guess and target</returns>
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;
}
/// <summary>
/// This plays one full game.
/// </summary>
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;
}
}
}
/// <summary>
/// The main entry point for the class - just keeps
/// playing the game until the user decides to quit.
/// </summary>
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();
}
}
}

View File

@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

25
96 Word/csharp/word.sln Normal file
View File

@@ -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

145
96 Word/vbnet/Program.vb Normal file
View File

@@ -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"}
' <summary>
' Outputs the instructions of the game.
' </summary>
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
' <summary>
' This allows the user to enter a guess - doing some basic validation
' on those guesses.
' </summary>
' <returns>The guess entered by the user</returns>
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
' <summary>
' 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.
' </summary>
' <param name="guess">The user's guess</param>
' <param name="target">The 'winning' word</param>
' <param name="progress">A string showing which specific letters have already been guessed</param>
' <returns>The integer value showing the number of character matches between guess and target</returns>
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
' <summary>
' This plays one full game.
' </summary>
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
' <summary>
' The main entry point for the class - just keeps
' playing the game until the user decides to quit.
' </summary>
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

25
96 Word/vbnet/word.sln Normal file
View File

@@ -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

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>word</RootNamespace>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>