D version of the word game.

This commit is contained in:
Bastiaan Veelo
2022-01-05 01:51:34 +01:00
parent 3bb82b7cf7
commit f7b4b999dd
3 changed files with 102 additions and 0 deletions

2
96_Word/d/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*.exe
*.obj

15
96_Word/d/README.md Normal file
View File

@@ -0,0 +1,15 @@
Original source downloaded from [Vintage Basic](http://www.vintage-basic.net/games.html)
Converted to [D](https://dlang.org/) by [Bastiaan Veelo](https://github.com/veelo).
The Basic original required words to be exactly five letters in length for the program to behave correctly.
This version does not replicate that limitation, and the test for that requirement is commented out.
## Running the code
Assuming the reference [dmd](https://dlang.org/download.html#dmd) compiler:
```shell
dmd -dip1000 -run word.d
```
[Other compilers](https://dlang.org/download.html) also exist.

85
96_Word/d/word.d Normal file
View File

@@ -0,0 +1,85 @@
@safe: // Make @safe the default for this file, enforcing memory-safety.
import std;
void main()
{
enum width = 80;
writeln(center("Word", width));
writeln(center("(After Creative Computing Morristown, New Jersey)\n\n\n", width));
writeln(wrap("I am thinking of a word -- you guess it. I will give you " ~
"clues to help you get it. Good luck!!\n\n", width));
string[] words = ["dinky", "smoke", "water", "grass", "train", "might", "first",
"candy", "champ", "would", "clump", "dopey"];
playLoop: while (true)
{
writeln("\n\nYou are starting a new game...");
string word = words[uniform(0, $-1)]; // $ is a short-hand for words.length.
int guesses = 0;
string knownLetters = '-'.repeat(word.length).array;
while (true)
{
writeln("Guess a ", word.length, " letter word");
string guess = readString.toLower;
if (guess == "?")
{
writeln("The secret word is ", word, "\n");
continue playLoop; // Start a new game.
}
/* Uncomment this for equivalence with Basic.
if (guess.length != 5)
{
writeln("You must guess a 5 letter word. Start again.");
continue; // Ask for new guess.
}
*/
guesses++;
if (guess == word)
break; // Done guessing
string commonLetters;
foreach (i, wordLetter; word)
foreach (j, guessLetter; guess)
if (guessLetter == wordLetter)
{
commonLetters ~= guessLetter;
if (i == j)
knownLetters.replaceInPlace(i, i + 1, [guessLetter]);
}
writeln("There were ", commonLetters.length, " matches and the common letters were... ", commonLetters);
writeln("From the exact letter matches, you know................ ", knownLetters);
if (knownLetters == word)
break; // Done guessing
if (commonLetters.length < 2)
writeln("If you give up, type '?' for your next guess.");
writeln;
}
writeln("You have guessed the word. It took ", guesses, " guesses!");
write("\n\nWant to play again? ");
if (readString.toLower != "yes")
break; // Terminate playLoop
}
}
/// Read a string from standard input, stripping newline and other enclosing whitespace.
string readString() nothrow
{
try
return trustedReadln.strip;
catch (Exception) // readln throws on I/O and Unicode errors, which we handle here.
return "";
}
/** An @trusted wrapper around readln.
*
* This is the only function that formally requires manual review for memory-safety.
* [Arguably readln should be safe already](https://forum.dlang.org/post/rab398$1up$1@digitalmars.com)
* which would remove the need to have any @trusted code in this program.
*/
string trustedReadln() @trusted
{
return readln;
}