diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 3908207..0000000 --- a/.gitattributes +++ /dev/null @@ -1,8 +0,0 @@ -# Check out all text files in UNIX format, with LF as end of line -# Don't change this file. If you have any ideas about it, please -# submit a separate issue about it and we'll discuss. - -* text=auto eol=lf -*.java ident -*.xml ident -*.png binary diff --git a/.gitignore b/.gitignore index f6a2cb2..3c4efe2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,261 @@ -target/ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider .idea/ -*.iml -.DS_Store -.classpath -.project -.settings +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1675217..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: java -sudo: false -cache: - directories: - - $HOME/.m2 -script: - - set -e - - mvn clean package --errors --batch-mode -jdk: - - oraclejdk8 diff --git a/Hangman/App.config b/Hangman/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/Hangman/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Hangman/Game/Game.cs b/Hangman/Game/Game.cs new file mode 100644 index 0000000..d2ea6ed --- /dev/null +++ b/Hangman/Game/Game.cs @@ -0,0 +1,37 @@ +using HangmanGame.Hangman; +using System; +using System.IO; + +namespace HangmanGame.Game +{ + public class Game + { + private IHangman _hangman; + private TextReader _tr; + private TextWriter _tw; + + public Game(IHangman hangman, TextReader tr, TextWriter tw) + { + _hangman = hangman; + _tr = tr; + _tw = tw; + } + + public void Start() + { + _tw.WriteLine(_hangman.Tell()); + while (_hangman.CanPlay()) + { + string raw = _tr.ReadLine(); + char guess = raw.Length > 0 ? raw[0] : ' '; + _tw.WriteLine($"Your guess is: '{guess}'"); + _hangman = _hangman.Step(guess); + + _tw.WriteLine(_hangman.Tell()); + _tw.WriteLine("***************"); + } + _tw.WriteLine("Press any key to exit"); + _tr.ReadLine(); + } + } +} diff --git a/Hangman/GuessStep/FailStep.cs b/Hangman/GuessStep/FailStep.cs new file mode 100644 index 0000000..b8f5f2a --- /dev/null +++ b/Hangman/GuessStep/FailStep.cs @@ -0,0 +1,12 @@ +using HangmanGame.Hangman; + +namespace HangmanGame.GuessStep +{ + internal class FailStep : IGuessStep + { + public IHangman Visit(IStepAcceptor hangman) + { + return hangman.Accept(this); + } + } +} diff --git a/Hangman/GuessStep/IGuessStep.cs b/Hangman/GuessStep/IGuessStep.cs new file mode 100644 index 0000000..0fdda80 --- /dev/null +++ b/Hangman/GuessStep/IGuessStep.cs @@ -0,0 +1,9 @@ +using HangmanGame.Hangman; + +namespace HangmanGame.GuessStep +{ + internal interface IGuessStep + { + IHangman Visit(IStepAcceptor hangman); + } +} diff --git a/Hangman/GuessStep/SuccessStep.cs b/Hangman/GuessStep/SuccessStep.cs new file mode 100644 index 0000000..6edeb9a --- /dev/null +++ b/Hangman/GuessStep/SuccessStep.cs @@ -0,0 +1,12 @@ +using HangmanGame.Hangman; + +namespace HangmanGame.GuessStep +{ + internal class SuccessStep : IGuessStep + { + public IHangman Visit(IStepAcceptor hangman) + { + return hangman.Accept(this); + } + } +} diff --git a/Hangman/Hangman/AliveHangman.cs b/Hangman/Hangman/AliveHangman.cs new file mode 100644 index 0000000..c5bca19 --- /dev/null +++ b/Hangman/Hangman/AliveHangman.cs @@ -0,0 +1,73 @@ +using HangmanGame.GuessStep; +using System; +using HangmanGame.Word; + +namespace HangmanGame.Hangman +{ + public class AliveHangman : IHangman, IStepAcceptor + { + private readonly TargetWord _secret; + private readonly int _max; + private readonly int _mistakes; + private readonly string _message; + + public AliveHangman(TargetWord secret, int max) + { + _secret = secret; + _max = max; + _message = "Guess a letter: "; + _mistakes = 0; + } + + private AliveHangman(TargetWord secret, int max, string message, int mistakes) + { + _secret = secret; + _max = max; + _mistakes = mistakes; + _message = message; + } + + public IHangman Step(char guess) + { + IGuessStep step = _secret.GuessLetter(guess); + return step.Visit(this); + } + + public bool CanPlay() + { + return true; + } + + public string Tell() + { + return _message; + } + + IHangman IStepAcceptor.Accept(SuccessStep step) + { + if (_secret.Visible()) + { + return new WonHangman(); + } + else + { + string msg = $"Hit! {Environment.NewLine} {string.Join(Environment.NewLine, _secret.Print())}"; + return new AliveHangman(_secret, _max, msg, _mistakes); + } + } + + IHangman IStepAcceptor.Accept(FailStep step) + { + int mistakes = _mistakes + 1; + if (mistakes >= _max) + { + return new DeadHangman(); + } + else + { + string msg = $"Missed, mistake {mistakes} out of {_max}{Environment.NewLine} {string.Join(Environment.NewLine, _secret.Print())}"; + return new AliveHangman(_secret, _max, msg, mistakes); + } + } + } +} diff --git a/Hangman/Hangman/DeadHangman.cs b/Hangman/Hangman/DeadHangman.cs new file mode 100644 index 0000000..91fe8ab --- /dev/null +++ b/Hangman/Hangman/DeadHangman.cs @@ -0,0 +1,20 @@ +namespace HangmanGame.Hangman +{ + public class DeadHangman : IHangman + { + public bool CanPlay() + { + return false; + } + + public string Tell() + { + return "You lost."; + } + + public IHangman Step(char guess) + { + return this; + } + } +} diff --git a/Hangman/Hangman/IHangman.cs b/Hangman/Hangman/IHangman.cs new file mode 100644 index 0000000..13b629e --- /dev/null +++ b/Hangman/Hangman/IHangman.cs @@ -0,0 +1,9 @@ +namespace HangmanGame.Hangman +{ + public interface IHangman + { + IHangman Step(char guess); + string Tell(); + bool CanPlay(); + } +} diff --git a/Hangman/Hangman/IStepAcceptor.cs b/Hangman/Hangman/IStepAcceptor.cs new file mode 100644 index 0000000..d536a1a --- /dev/null +++ b/Hangman/Hangman/IStepAcceptor.cs @@ -0,0 +1,10 @@ +using HangmanGame.GuessStep; + +namespace HangmanGame.Hangman +{ + internal interface IStepAcceptor + { + IHangman Accept(SuccessStep step); + IHangman Accept(FailStep step); + } +} diff --git a/Hangman/Hangman/WonHangman.cs b/Hangman/Hangman/WonHangman.cs new file mode 100644 index 0000000..04417f8 --- /dev/null +++ b/Hangman/Hangman/WonHangman.cs @@ -0,0 +1,20 @@ +namespace HangmanGame.Hangman +{ + public class WonHangman : IHangman + { + public bool CanPlay() + { + return false; + } + + public string Tell() + { + return "You won"; + } + + public IHangman Step(char guess) + { + return this; + } + } +} diff --git a/Hangman/HangmanGame.csproj b/Hangman/HangmanGame.csproj new file mode 100644 index 0000000..beb79de --- /dev/null +++ b/Hangman/HangmanGame.csproj @@ -0,0 +1,74 @@ + + + + + Debug + AnyCPU + {A16B9FB0-2635-4385-B12B-8722B86446F7} + Exe + HangmanGame + HangmanGame + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Hangman/Letter/HiddenLetter.cs b/Hangman/Letter/HiddenLetter.cs new file mode 100644 index 0000000..3fb1eff --- /dev/null +++ b/Hangman/Letter/HiddenLetter.cs @@ -0,0 +1,38 @@ +using System; + +namespace HangmanGame.Letter +{ + public class HiddenLetter : ILetter + { + private readonly char _l; + + public HiddenLetter(char l) + { + _l = l; + } + + public char Value() + { + return _l; + } + + public char Print() + { + return '?'; + } + + public bool Hidden() => true; + + public ILetter Open(char guess) + { + if (guess == _l) + { + return new VisibleLetter(_l); + } + else + { + return this; + } + } + } +} diff --git a/Hangman/Letter/ILetter.cs b/Hangman/Letter/ILetter.cs new file mode 100644 index 0000000..f5bd5a3 --- /dev/null +++ b/Hangman/Letter/ILetter.cs @@ -0,0 +1,10 @@ +namespace HangmanGame.Letter +{ + public interface ILetter + { + char Value(); + char Print(); + bool Hidden(); + ILetter Open(char guess); + } +} diff --git a/Hangman/Letter/LetterComparer.cs b/Hangman/Letter/LetterComparer.cs new file mode 100644 index 0000000..c698536 --- /dev/null +++ b/Hangman/Letter/LetterComparer.cs @@ -0,0 +1,26 @@ +using System.Collections; +using System; +using System.Reflection; +using System.Linq; + +namespace HangmanGame.Letter +{ + public class LetterComparer : IComparer + { + public int Compare(object x, object y) + { + if (x is VisibleLetter xv && y is VisibleLetter yv) + { + return xv.Value().CompareTo(yv.Value()); + } + else if (x is HiddenLetter xh && y is HiddenLetter yh) + { + return xh.Value().CompareTo(yh.Value()); + } + else + { + return -1; + } + } + } +} diff --git a/Hangman/Letter/VisibleLetter.cs b/Hangman/Letter/VisibleLetter.cs new file mode 100644 index 0000000..de7c342 --- /dev/null +++ b/Hangman/Letter/VisibleLetter.cs @@ -0,0 +1,31 @@ +using System; + +namespace HangmanGame.Letter +{ + public class VisibleLetter : ILetter + { + private readonly char _l; + + public VisibleLetter(char l) + { + _l = l; + } + + public char Value() + { + return _l; + } + + public char Print() + { + return _l; + } + + public bool Hidden() => false; + + public ILetter Open(char guess) + { + return this; + } + } +} diff --git a/Hangman/Letters/GuessedLetters.cs b/Hangman/Letters/GuessedLetters.cs new file mode 100644 index 0000000..d8418ad --- /dev/null +++ b/Hangman/Letters/GuessedLetters.cs @@ -0,0 +1,32 @@ +using HangmanGame.Letter; +using System.Collections.Generic; +using System.Linq; + +namespace HangmanGame.Letters +{ + public class GuessedLetters : ILetters + { + private readonly ILetter[] _letters; + private readonly List _cache; + + public GuessedLetters(ILetter[] letters) + { + _letters = letters; + _cache = new List(); + } + + public ILetter[] Show() + { + return _letters; + } + + public int HiddensCount() + { + if (_cache.Count == 0) + { + _cache.Add(_letters.Where(x => x.Hidden()).Count()); + } + return _cache[0]; + } + } +} diff --git a/Hangman/Letters/ILetters.cs b/Hangman/Letters/ILetters.cs new file mode 100644 index 0000000..7490a16 --- /dev/null +++ b/Hangman/Letters/ILetters.cs @@ -0,0 +1,10 @@ +using HangmanGame.Letter; + +namespace HangmanGame.Letters +{ + public interface ILetters + { + ILetter[] Show(); + int HiddensCount(); + } +} diff --git a/Hangman/Letters/InitialLetters.cs b/Hangman/Letters/InitialLetters.cs new file mode 100644 index 0000000..3f128f4 --- /dev/null +++ b/Hangman/Letters/InitialLetters.cs @@ -0,0 +1,26 @@ +using HangmanGame.Letter; +using HangmanGame.WordSource; +using System.Linq; + +namespace HangmanGame.Letters +{ + public class InitialLetters : ILetters + { + private readonly IWordSource _source; + + public InitialLetters(IWordSource source) + { + _source = source; + } + + public ILetter[] Show() + { + return _source.Load().Select(x => new HiddenLetter(x)).ToArray(); + } + + public int HiddensCount() + { + return Show().Length; + } + } +} diff --git a/Hangman/Program.cs b/Hangman/Program.cs new file mode 100644 index 0000000..830c7b8 --- /dev/null +++ b/Hangman/Program.cs @@ -0,0 +1,98 @@ +using HangmanGame.Game; +using HangmanGame.Hangman; +using HangmanGame.Letters; +using HangmanGame.Word; +using HangmanGame.WordSource; +using System; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("HangmanGameTests")] +namespace Hangman +{ + class Program + { + private static readonly int max = 5; + private static readonly string[] WORDS = { + "simplicity", "equality", "grandmother", + "neighborhood", "relationship", "mathematics", + "university", "explanation" + }; + + static void Main(string[] args) + { + new Game(new AliveHangman(new TargetWord(new InitialLetters(new RandomWordSource(WORDS, new Random()))), 5), Console.In, Console.Out).Start(); + } + + #region Old procedural-style version + [Obsolete] + private static void exec() + { + string word = WORDS[new Random().Next(WORDS.Length)]; + bool[] visible = new bool[word.Length]; + int mistakes = 0; + try + { + bool done = true; + while (mistakes < max) + { + done = true; + for (int i = 0; i < word.Length; ++i) + { + if (!visible[i]) + { + done = false; + } + } + if (done) + { + break; + } + Console.WriteLine("Guess a letter: "); + char chr = Console.ReadLine()[0]; + bool hit = false; + for (int i = 0; i < word.Length; ++i) + { + if (word[i] == chr && !visible[i]) + { + visible[i] = true; + hit = true; + } + } + if (hit) + { + Console.WriteLine("Hit!\n"); + } + else + { + Console.WriteLine($"Missed, mistake {mistakes + 1} out of {max}"); + ++mistakes; + } + Console.Write("The word: "); + for (int i = 0; i < word.Length; ++i) + { + if (visible[i]) + { + Console.WriteLine(word[i]); + } + else + { + Console.WriteLine("?"); + } + } + } + if (done) + { + Console.WriteLine("You won"); + } + else + { + Console.WriteLine("You lost."); + Console.ReadLine(); + } + } + catch + { } + } + #endregion + } +} diff --git a/Hangman/Properties/AssemblyInfo.cs b/Hangman/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0394b11 --- /dev/null +++ b/Hangman/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Hangman")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Hangman")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a16b9fb0-2635-4385-b12b-8722b86446f7")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Hangman/Word/TargetWord.cs b/Hangman/Word/TargetWord.cs new file mode 100644 index 0000000..9c7b7e1 --- /dev/null +++ b/Hangman/Word/TargetWord.cs @@ -0,0 +1,43 @@ +using HangmanGame.GuessStep; +using HangmanGame.Letter; +using HangmanGame.Letters; +using System.Linq; + +namespace HangmanGame.Word +{ + public class TargetWord + { + private ILetters _letters; + + public TargetWord(ILetters letters) + { + _letters = letters; + } + + public char[] Print() + { + return _letters.Show().Select(x => x.Print()).ToArray(); + } + + internal IGuessStep GuessLetter(char guess) + { + int oldHidden = _letters.HiddensCount(); + ILetter[] afterOpen = _letters.Show().Select(x => x.Open(guess)).ToArray(); + _letters = new GuessedLetters(afterOpen); + + if (_letters.HiddensCount() < oldHidden) + { + return new SuccessStep(); + } + else + { + return new FailStep(); + } + } + + public bool Visible() + { + return _letters.HiddensCount() == 0; + } + } +} diff --git a/Hangman/WordSource/ConcreteWordSource.cs b/Hangman/WordSource/ConcreteWordSource.cs new file mode 100644 index 0000000..7424233 --- /dev/null +++ b/Hangman/WordSource/ConcreteWordSource.cs @@ -0,0 +1,17 @@ +namespace HangmanGame.WordSource +{ + public class ConcreteWordSource : IWordSource + { + private readonly string _word; + + public ConcreteWordSource(string word) + { + _word = word; + } + + public string Load() + { + return _word; + } + } +} diff --git a/Hangman/WordSource/IWordSource.cs b/Hangman/WordSource/IWordSource.cs new file mode 100644 index 0000000..f18ef82 --- /dev/null +++ b/Hangman/WordSource/IWordSource.cs @@ -0,0 +1,7 @@ +namespace HangmanGame.WordSource +{ + public interface IWordSource + { + string Load(); + } +} diff --git a/Hangman/WordSource/RandomWordSource.cs b/Hangman/WordSource/RandomWordSource.cs new file mode 100644 index 0000000..d3a17c0 --- /dev/null +++ b/Hangman/WordSource/RandomWordSource.cs @@ -0,0 +1,34 @@ +using System; +using System.Linq; + +namespace HangmanGame.WordSource +{ + public class RandomWordSource : IWordSource + { + private readonly string[] _words; + private readonly Random _random; + private string _state; + + public RandomWordSource(string[] words, Random random) + { + _words = words; + _random = random; + _state = string.Empty; + } + + public string Load() + { + if (string.IsNullOrEmpty(_state)) + { + Reload(); + } + return _state; + } + + public void Reload() + { + string[] excluded = _words.Where(x => x != _state).ToArray(); + _state = excluded[_random.Next(excluded.Length)]; + } + } +} diff --git a/HangmanGameSolution.sln b/HangmanGameSolution.sln new file mode 100644 index 0000000..59c241b --- /dev/null +++ b/HangmanGameSolution.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.421 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HangmanGame", "Hangman\HangmanGame.csproj", "{A16B9FB0-2635-4385-B12B-8722B86446F7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HangmanGameTests", "HangmanGameTests\HangmanGameTests.csproj", "{D3DA7FEB-5A6A-475D-9DF1-DC790BC581F5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A16B9FB0-2635-4385-B12B-8722B86446F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A16B9FB0-2635-4385-B12B-8722B86446F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A16B9FB0-2635-4385-B12B-8722B86446F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A16B9FB0-2635-4385-B12B-8722B86446F7}.Release|Any CPU.Build.0 = Release|Any CPU + {D3DA7FEB-5A6A-475D-9DF1-DC790BC581F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D3DA7FEB-5A6A-475D-9DF1-DC790BC581F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D3DA7FEB-5A6A-475D-9DF1-DC790BC581F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D3DA7FEB-5A6A-475D-9DF1-DC790BC581F5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {686F3153-4DBC-4C28-B601-DD4631B91EA3} + EndGlobalSection +EndGlobal diff --git a/HangmanGameTests/Game/GameTests.cs b/HangmanGameTests/Game/GameTests.cs new file mode 100644 index 0000000..c0adc66 --- /dev/null +++ b/HangmanGameTests/Game/GameTests.cs @@ -0,0 +1,64 @@ +using HangmanGame.Hangman; +using HangmanGame.Letters; +using HangmanGame.Word; +using HangmanGame.WordSource; +using HangmanGameTests; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.IO; +using System.Linq; + +namespace HangmanGame.Game.Tests +{ + [TestClass()] + public class GameTests + { + [TestMethod()] + public void CanLost() + { + string l = Environment.NewLine; + char i = ' '; // incorrect char + using (TextReader tr = new StringReader($"{i}{l}{i}{l}{i}{l}{i}{l}{i}{l}")) + { + using (TextWriter tw = new StringWriter()) + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + + Game game = new Game(new AliveHangman(new TargetWord(new InitialLetters(new ConcreteWordSource(test))), 5), tr, tw); + + game.Start(); + + string result = tw.ToString(); + bool lost = result.Contains("You lost."); + + Assert.IsTrue(lost); + } + } + } + + [TestMethod()] + public void CanWin() + { + string l = Environment.NewLine; + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + string rightInputs = string.Join(l, test.Distinct().Select(x => x.ToString())) + l; + + using (TextWriter tw = new StringWriter()) + { + using (TextReader tr = new StringReader(rightInputs)) + { + Game game = new Game(new AliveHangman(new TargetWord(new InitialLetters(new ConcreteWordSource(test))), 5), tr, tw); + + game.Start(); + + string result = tw.ToString(); + bool won = result.Contains("You won"); + + Assert.IsTrue(won); + } + } + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/Hangman/AliveHangmanTests.cs b/HangmanGameTests/Hangman/AliveHangmanTests.cs new file mode 100644 index 0000000..80d08b8 --- /dev/null +++ b/HangmanGameTests/Hangman/AliveHangmanTests.cs @@ -0,0 +1,99 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using HangmanGame.Hangman; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HangmanGame.Word; +using HangmanGame.Letters; +using HangmanGame.WordSource; +using HangmanGameTests; +using HangmanGame.GuessStep; +using HangmanGame.Letter; + +namespace HangmanGame.Hangman.Tests +{ + [TestClass()] + public class AliveHangmanTests + { + private Random rnd = new Random(); + private int RandomVal => rnd.Next(2, 10); + + [TestMethod()] + public void WonStepTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + char correctGuess = test[rnd.Next(0, test.Length)]; + ILetters letters = new GuessedLetters(test.Select(x => x == correctGuess ? (ILetter)new HiddenLetter(x) : new VisibleLetter(x)).ToArray()); + AliveHangman hangman = new AliveHangman(new TargetWord(letters), RandomVal); + + IHangman result = hangman.Step(correctGuess); + + Assert.IsInstanceOfType(result, typeof(WonHangman)); + } + + [TestMethod()] + public void DeadStepTest() + { + RandomChar rc = new RandomChar(); + int maxAttempts = 0; + AliveHangman hangman = HangmanFactory(maxAttempts, out string test); + + IHangman result = hangman.Step(rc.GetAnother(test.ToCharArray())); + + Assert.IsInstanceOfType(result, typeof(DeadHangman)); + } + + [TestMethod()] + public void StepTest() + { + RandomChar rc = new RandomChar(); + AliveHangman hangman = HangmanFactory(RandomVal, out string test); + + IHangman result = hangman.Step(rc.Load()); + + Assert.IsInstanceOfType(result, typeof(AliveHangman)); + } + + [TestMethod()] + public void CanPlayTest() + { + AliveHangman hangman = HangmanFactory(RandomVal, out _); + + Assert.IsTrue(hangman.CanPlay()); + } + + [TestMethod()] + public void TellTest() + { + RandomChar rc = new RandomChar(); + int maxAttempts = RandomVal; + AliveHangman hangman = HangmanFactory(maxAttempts, out string test); + + char correct = test[rnd.Next(0, test.Length)]; + char incorrect = rc.GetAnother(test.ToCharArray()); + + + IHangman hangmanGuess1 = hangman.Step(correct); + IHangman hangmanGuess2 = hangmanGuess1.Step(incorrect); + + string start = hangman.Tell(); + string success = hangmanGuess1.Tell(); + string fail = hangmanGuess2.Tell(); + + Assert.AreEqual(start, "Guess a letter: "); + Assert.IsTrue(success.StartsWith("Hit! ")); + Assert.IsTrue(fail.StartsWith($"Missed, mistake 1 out of {maxAttempts}")); + } + + private AliveHangman HangmanFactory(int maxAttempts, out string test) + { + RandomWord rw = new RandomWord(5, 20); + test = rw.Word(); + AliveHangman hangman = new AliveHangman(new TargetWord(new InitialLetters(new ConcreteWordSource(test))), maxAttempts); + return hangman; + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/Hangman/DeadHangmanTests.cs b/HangmanGameTests/Hangman/DeadHangmanTests.cs new file mode 100644 index 0000000..09641a8 --- /dev/null +++ b/HangmanGameTests/Hangman/DeadHangmanTests.cs @@ -0,0 +1,44 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using HangmanGame.Hangman; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HangmanGameTests; + +namespace HangmanGame.Hangman.Tests +{ + [TestClass()] + public class DeadHangmanTests + { + [TestMethod()] + public void CanPlayTest() + { + DeadHangman hangman = new DeadHangman(); + + Assert.IsFalse(hangman.CanPlay()); + } + + [TestMethod()] + public void TellTest() + { + DeadHangman hangman = new DeadHangman(); + + string msg = hangman.Tell(); + + Assert.AreEqual(msg, "You lost."); + } + + [TestMethod()] + public void StepTest() + { + DeadHangman hangman = new DeadHangman(); + RandomChar rc = new RandomChar(); + + IHangman newHangman = hangman.Step(rc.Load()); + + Assert.IsInstanceOfType(newHangman, typeof(DeadHangman)); + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/Hangman/WonHangmanTests.cs b/HangmanGameTests/Hangman/WonHangmanTests.cs new file mode 100644 index 0000000..11a8c3b --- /dev/null +++ b/HangmanGameTests/Hangman/WonHangmanTests.cs @@ -0,0 +1,44 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using HangmanGame.Hangman; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HangmanGameTests; + +namespace HangmanGame.Hangman.Tests +{ + [TestClass()] + public class WonHangmanTests + { + [TestMethod()] + public void CanPlayTest() + { + WonHangman hangman = new WonHangman(); + + Assert.IsFalse(hangman.CanPlay()); + } + + [TestMethod()] + public void TellTest() + { + WonHangman hangman = new WonHangman(); + + string msg = hangman.Tell(); + + Assert.AreEqual(msg, "You won"); + } + + [TestMethod()] + public void StepTest() + { + WonHangman hangman = new WonHangman(); + RandomChar rc = new RandomChar(); + + IHangman newHangman = hangman.Step(rc.Load()); + + Assert.IsInstanceOfType(newHangman, typeof(WonHangman)); + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/HangmanGameTests.csproj b/HangmanGameTests/HangmanGameTests.csproj new file mode 100644 index 0000000..55c00c4 --- /dev/null +++ b/HangmanGameTests/HangmanGameTests.csproj @@ -0,0 +1,87 @@ + + + + + + Debug + AnyCPU + {D3DA7FEB-5A6A-475D-9DF1-DC790BC581F5} + Library + Properties + HangmanGameTests + HangmanGameTests + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + {a16b9fb0-2635-4385-b12b-8722b86446f7} + HangmanGame + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/HangmanGameTests/Letter/HiddenLetterTests.cs b/HangmanGameTests/Letter/HiddenLetterTests.cs new file mode 100644 index 0000000..5b95cea --- /dev/null +++ b/HangmanGameTests/Letter/HiddenLetterTests.cs @@ -0,0 +1,67 @@ +using HangmanGame.Letter; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace HangmanGameTests.Letter +{ + [TestClass] + public class HiddenLetterTests + { + private const char _questionMark = '?'; + + [TestMethod] + public void ReturnCorrectValue() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + HiddenLetter letter = new HiddenLetter(target); + + char val = letter.Value(); + + Assert.AreEqual(target, val); + } + + [TestMethod] + public void DontPrintValue() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + HiddenLetter letter = new HiddenLetter(target); + + char val = letter.Print(); + + Assert.AreNotEqual(target, val); + Assert.AreEqual(_questionMark, val); + } + + [TestMethod] + public void CanOpenValue() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + char incorrect = rc.GetAnother(target); + HiddenLetter letter = new HiddenLetter(target); + + ILetter success = letter.Open(target); + ILetter fail = letter.Open(incorrect); + + Assert.IsFalse(success.Hidden()); + Assert.IsTrue(fail.Hidden()); + + Assert.AreEqual(target, success.Value()); + Assert.AreEqual(target, fail.Value()); + Assert.AreEqual(_questionMark, fail.Print()); + } + + [TestMethod] + public void IsHidden() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + HiddenLetter letter = new HiddenLetter(target); + + bool hidden = letter.Hidden(); + + Assert.IsTrue(hidden); + } + } +} diff --git a/HangmanGameTests/Letter/VisibleLetterTests.cs b/HangmanGameTests/Letter/VisibleLetterTests.cs new file mode 100644 index 0000000..c87f944 --- /dev/null +++ b/HangmanGameTests/Letter/VisibleLetterTests.cs @@ -0,0 +1,63 @@ +using HangmanGame.Letter; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace HangmanGameTests.Letter +{ + [TestClass] + public class VisibleLetterTests + { + [TestMethod] + public void ReturnCorrectValue() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + VisibleLetter letter = new VisibleLetter(target); + + char val = letter.Value(); + + Assert.AreEqual(target, val); + } + + [TestMethod] + public void PrintCorrectValue() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + VisibleLetter letter = new VisibleLetter(target); + + char val = letter.Print(); + + Assert.AreEqual(target, val); + } + + [TestMethod] + public void ValueAlreadyOpenedInAnyCase() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + char incorrect = rc.GetAnother(target); + VisibleLetter letter = new VisibleLetter(target); + + ILetter success = letter.Open(target); + ILetter fail = letter.Open(incorrect); + + Assert.IsFalse(success.Hidden()); + Assert.IsFalse(fail.Hidden()); + + Assert.AreEqual(target, success.Value()); + Assert.AreEqual(target, fail.Value()); + } + + [TestMethod] + public void IsNotHidden() + { + RandomChar rc = new RandomChar(); + char target = rc.Load(); + VisibleLetter letter = new VisibleLetter(target); + + bool hidden = letter.Hidden(); + + Assert.IsFalse(hidden); + } + } +} diff --git a/HangmanGameTests/Letters/GuessedLettersTests.cs b/HangmanGameTests/Letters/GuessedLettersTests.cs new file mode 100644 index 0000000..98d4b6d --- /dev/null +++ b/HangmanGameTests/Letters/GuessedLettersTests.cs @@ -0,0 +1,37 @@ +using HangmanGame.Letter; +using HangmanGame.Letters; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Linq; + +namespace HangmanGameTests.Letters +{ + [TestClass] + public class GuessedLettersTests + { + [TestMethod] + public void ShowTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + ILetter[] initial = test.Select(x => new HiddenLetter(x)).ToArray(); + GuessedLetters guessedLetters = new GuessedLetters(initial); + + ILetter[] shown = guessedLetters.Show(); + + CollectionAssert.AreEqual(initial, shown); + } + + [TestMethod] + public void HiddensCountTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + int index = new Random().Next(0, test.Length); + ILetter[] initial = test.Select((e, i) => i == index ? (ILetter)new VisibleLetter(e) : new HiddenLetter(e)).ToArray(); + GuessedLetters guessedLetters = new GuessedLetters(initial); + + Assert.AreEqual(guessedLetters.HiddensCount(), test.Length - 1); + } + } +} diff --git a/HangmanGameTests/Letters/InitialLettersTests.cs b/HangmanGameTests/Letters/InitialLettersTests.cs new file mode 100644 index 0000000..0fd0e9c --- /dev/null +++ b/HangmanGameTests/Letters/InitialLettersTests.cs @@ -0,0 +1,37 @@ +using HangmanGame.Letter; +using HangmanGame.Letters; +using HangmanGame.WordSource; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; + +namespace HangmanGameTests.Letters +{ + [TestClass] + public class InitialLettersTests + { + [TestMethod] + public void ShowTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + ConcreteWordSource cws = new ConcreteWordSource(test); + InitialLetters guessedLetters = new InitialLetters(cws); + ILetter[] reference = test.Select(x => new HiddenLetter(x)).ToArray(); + + ILetter[] shown = guessedLetters.Show(); + + CollectionAssert.AreEqual(reference, shown, new LetterComparer()); + } + + [TestMethod] + public void HiddensCountTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + ConcreteWordSource cws = new ConcreteWordSource(test); + InitialLetters guessedLetters = new InitialLetters(cws); + + Assert.AreEqual(guessedLetters.HiddensCount(), test.Length); + } + } +} diff --git a/HangmanGameTests/Properties/AssemblyInfo.cs b/HangmanGameTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0352e5c --- /dev/null +++ b/HangmanGameTests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("HangmanGameTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("HangmanGameTests")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("d3da7feb-5a6a-475d-9df1-dc790bc581f5")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/HangmanGameTests/RandomChar.cs b/HangmanGameTests/RandomChar.cs new file mode 100644 index 0000000..2543979 --- /dev/null +++ b/HangmanGameTests/RandomChar.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HangmanGameTests +{ + internal class RandomChar + { + private const string _alphabet = "abcdefghijklmnopqrstuvwxyz"; + private readonly Random rnd; + + public RandomChar() + { + rnd = new Random(); + } + + public RandomChar(int seed) + { + rnd = new Random(seed); + } + + public char Load() + { + int randomInd = rnd.Next(0, _alphabet.Length); // a-z + return _alphabet[randomInd]; + } + + public char GetAnother(char a) + { + return GetAnother(new char[] { a }); + } + + public char GetAnother(char[] range) + { + HashSet set = new HashSet(range); + char[] others = _alphabet.Where(x => !set.Contains(x)).ToArray(); + int randomInd = rnd.Next(0, others.Length); + return others[randomInd]; + } + } +} diff --git a/HangmanGameTests/RandomWord.cs b/HangmanGameTests/RandomWord.cs new file mode 100644 index 0000000..864d9ca --- /dev/null +++ b/HangmanGameTests/RandomWord.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HangmanGameTests +{ + internal class RandomWord + { + private readonly int _minLength; + private readonly int _maxLength; + private readonly RandomChar _rc; + + public RandomWord(int minLength, int maxLength) + { + _minLength = minLength; + _maxLength = maxLength; + _rc = new RandomChar(); + } + + public string Word() + { + int length = new Random().Next(_maxLength, _maxLength); + return string.Concat(new char[length].Select(x => _rc.Load())); + } + } +} diff --git a/HangmanGameTests/Word/TargetWordTests.cs b/HangmanGameTests/Word/TargetWordTests.cs new file mode 100644 index 0000000..bf96062 --- /dev/null +++ b/HangmanGameTests/Word/TargetWordTests.cs @@ -0,0 +1,79 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using HangmanGame.Word; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HangmanGame.Letters; +using HangmanGame.WordSource; +using HangmanGame.Letter; +using HangmanGame.GuessStep; +using HangmanGameTests; + +namespace HangmanGame.Word.Tests +{ + [TestClass()] + public class TargetWordTests + { + [TestMethod()] + public void PrintTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + string questions = string.Concat(test.Select(x => '?')); + TargetWord invisible = CreateInvisible(test); + TargetWord visible = CreateVisible(test); + + string invisibleStr = string.Concat(invisible.Print()); + string visibleStr = string.Concat(visible.Print()); + + Assert.AreEqual(test, visibleStr); + Assert.AreEqual(questions, invisibleStr); + } + + [TestMethod()] + public void GuessLetterTest() + { + RandomChar rc = new RandomChar(); + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + TargetWord tw = CreateInvisible(test); + char correct = test[0]; + char incorrect = rc.GetAnother(test.ToCharArray()); + + IGuessStep fail = tw.GuessLetter(incorrect); + IGuessStep success = tw.GuessLetter(correct); + + Assert.IsInstanceOfType(fail, typeof(FailStep)); + Assert.IsInstanceOfType(success, typeof(SuccessStep)); + } + + [TestMethod()] + public void VisibleTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + TargetWord invisible = CreateInvisible(test); + TargetWord visible = CreateVisible(test); + + Assert.IsFalse(invisible.Visible()); + Assert.IsTrue(visible.Visible()); + } + + private TargetWord CreateVisible(string test) + { + ILetter[] letters = test.Select(x => new VisibleLetter(x)).ToArray(); + GuessedLetters gl = new GuessedLetters(letters); + + return new TargetWord(gl); + } + + private TargetWord CreateInvisible(string test) + { + InitialLetters il = new InitialLetters(new ConcreteWordSource(test)); + + return new TargetWord(il); + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/WordSource/ConcreteWordSourceTests.cs b/HangmanGameTests/WordSource/ConcreteWordSourceTests.cs new file mode 100644 index 0000000..ac0b5c4 --- /dev/null +++ b/HangmanGameTests/WordSource/ConcreteWordSourceTests.cs @@ -0,0 +1,21 @@ +using HangmanGameTests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace HangmanGame.WordSource.Tests +{ + [TestClass()] + public class ConcreteWordSourceTests + { + [TestMethod()] + public void LoadTest() + { + RandomWord rw = new RandomWord(5, 20); + string test = rw.Word(); + ConcreteWordSource cws = new ConcreteWordSource(test); + + string result = cws.Load(); + + Assert.AreEqual(test, result); + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/WordSource/RandomWordSourceTests.cs b/HangmanGameTests/WordSource/RandomWordSourceTests.cs new file mode 100644 index 0000000..fedbdac --- /dev/null +++ b/HangmanGameTests/WordSource/RandomWordSourceTests.cs @@ -0,0 +1,33 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace HangmanGame.WordSource.Tests +{ + [TestClass()] + public class RandomWordSourceTests + { + private readonly string[] _words = { "test", "another", "something", "else" }; + + [TestMethod()] + public void LoadTest() + { + RandomWordSource rws = new RandomWordSource(_words, new Random()); + + string loaded = rws.Load(); + + Assert.AreEqual(loaded, rws.Load()); + } + + [TestMethod()] + public void ReloadTest() + { + RandomWordSource rws = new RandomWordSource(_words, new Random()); + + string loaded = rws.Load(); + rws.Reload(); + string reloaded = rws.Load(); + + Assert.AreNotEqual(loaded, reloaded); + } + } +} \ No newline at end of file diff --git a/HangmanGameTests/packages.config b/HangmanGameTests/packages.config new file mode 100644 index 0000000..102a45c --- /dev/null +++ b/HangmanGameTests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 28f3514..0000000 --- a/README.md +++ /dev/null @@ -1,31 +0,0 @@ - - -[![Build Status](https://img.shields.io/travis/yegor256/hangman/master.svg)](https://travis-ci.org/yegor256/hangman) -[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/hangman/blob/master/LICENSE.txt) - -[Hangman](https://en.wikipedia.org/wiki/Hangman_%28game%29) is a words -guessing game for one player. The computer guesses a word and the user -has to suggest letters one by one. Every time the word contains a letter, -the computer opens it (all of them, if there are a few). Every time the -word doesn't contain a letter, the computer gives a penalty point for -the user. If there are five penalty points, the user looses. If there -are no hidden letters anymore, the computer looses. - -First, build it: - -``` -$ mvn clean package -``` - -Then, run: - -``` -$ java -classpath target/classes hangman.Main -``` - -This Java code is very procedural, imperative and not elegant at all. - -Please, try to refactor it and improve. You're welcome to create new classes, -new methods, anything. Just make sure it still builds. - -When ready, please submit a pull request. diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 945f7dd..0000000 --- a/pom.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - 4.0.0 - - com.jcabi - parent - 0.42 - - com.yegor256 - hangman - 1.0-SNAPSHOT - jar - hangman - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - diff --git a/src/main/java/hangman/Main.java b/src/main/java/hangman/Main.java deleted file mode 100644 index d870fbb..0000000 --- a/src/main/java/hangman/Main.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - */ -package hangman; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintStream; -import java.util.Iterator; -import java.util.Random; -import java.util.Scanner; - -public class Main { - - private final InputStream input; - private final OutputStream output; - private final int max; - private static final String[] WORDS = { - "simplicity", "equality", "grandmother", - "neighborhood", "relationship", "mathematics", - "university", "explanation" - }; - - public Main(final InputStream in, final OutputStream out, final int m) { - this.input = in; - this.output = out; - this.max = m; - } - - public static void main(final String... args) { - new Main(System.in, System.out, 5).exec(); - } - - public void exec() { - String word = WORDS[new Random().nextInt(WORDS.length)]; - boolean[] visible = new boolean[word.length()]; - int mistakes = 0; - try (final PrintStream out = new PrintStream(this.output)) { - final Iterator scanner = new Scanner(this.input); - boolean done = true; - while (mistakes < this.max) { - done = true; - for (int i = 0; i < word.length(); ++i) { - if (!visible[i]) { - done = false; - } - } - if (done) { - break; - } - out.print("Guess a letter: "); - char chr = scanner.next().charAt(0); - boolean hit = false; - for (int i = 0; i < word.length(); ++i) { - if (word.charAt(i) == chr && !visible[i]) { - visible[i] = true; - hit = true; - } - } - if (hit) { - out.print("Hit!\n"); - } else { - out.printf( - "Missed, mistake #%d out of %d\n", - mistakes + 1, this.max - ); - ++mistakes; - } - out.append("The word: "); - for (int i = 0; i < word.length(); ++i) { - if (visible[i]) { - out.print(word.charAt(i)); - } else { - out.print("?"); - } - } - out.append("\n\n"); - } - if (done) { - out.print("You won!\n"); - } else { - out.print("You lost.\n"); - } - } - } - -} diff --git a/src/test/java/hangman/MainTest.java b/src/test/java/hangman/MainTest.java deleted file mode 100644 index d0cc008..0000000 --- a/src/test/java/hangman/MainTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - */ -package hangman; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import org.junit.Test; - -public final class MainTest { - - @Test - public void failsAfterManyWrongAttempts() throws Exception { - final ByteArrayInputStream input = new ByteArrayInputStream( - "a\na\na\na\na\n".getBytes() - ); - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - new Main(input, output, 1).exec(); - assertThat(output.toString(), containsString("You lost")); - } - -}