Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.file;
- import std.random;
- import std.c.stdlib;
- import std.array;
- import std.string;
- import std.conv;
- /* Draw the dude and the lynching pole */
- void draw(int level)
- {
- /* The image is the guy */
- string image;
- /* If we only screwed up once, only add a head */
- if(level >= 1)
- {
- image = "| (_)\n";
- }
- /* If we screwed up twice, add a head and an arm, etc. */
- if(level >= 2)
- {
- image ~= "| \\|";
- }
- if(level >= 3)
- {
- image ~= "/\n";
- }
- if(level >= 4)
- {
- image ~= "| |\n";
- }
- if(level >= 5)
- {
- image ~= "| /";
- }
- if(level >= 6)
- {
- image ~= "\\\n";
- }
- /* We're going to need the top to be drawn every time, no matter what. */
- writeln("_________");
- writeln("|/ |");
- /* If image actually contains something, then draw it */
- if(image)
- {
- writeln(image);
- }
- /* For every level we've screwed up, we need 1 less '|' because a body part is occupying that spot, for a total of 5 levels. So
- 5 - levels = how many extra '|' we need to draw */
- for(int i = 0; i < (5 - level); i++)
- {
- writeln("|");
- }
- /* We'll also need to draw a floor everytime */
- writeln("|__");
- }
- void main(string args[])
- {
- /* Reading in the file specified by argument one. Because it could be binary, we need to specify that we're expecting (and reading in) strings */
- string text = cast(string)read(args[1]);
- text = toLower(text);
- /* Split the text up (into tokens) on each whitespace found and return the tokens in an array */
- auto tokens = text.split();
- /* Getting a uniform random number based on an unpredictableSeed (a built in variable) */
- auto gen = Random(unpredictableSeed);
- auto random = uniform(0, tokens.length, gen);
- /* We'll be editting the word later on, so we need to save it in it's current state */
- string originalWord = tokens[random];
- /* Converting the word we'll be editing to a char array */
- char[] word = to!(char[])(tokens[random]);
- /* The amount of guesses allowed */
- int guesses = 6;
- /* The amount of times you've guessed wrong (for the draw function argument), and the amount of times you've guessed right */
- int wrong = 0;
- int correct = 0;
- /* The display of letters on the screen, '_ _ e _ a' */
- char[] display;
- /* The letters you've used */
- string usedLetters;
- /* For each character in our word */
- foreach(character; word)
- {
- /* Add another underscore and space */
- display ~= "_ ";
- }
- /* While we still have guesses left */
- while(guesses != 0)
- {
- writeln("Guesses left: ", guesses);
- /* Draw our lynching pole based on how many times we were wrong */
- draw(wrong);
- writeln("Word:", display);
- /* Read in input, strip \r\n and space from it, and make it all lower case */
- auto input = readln();
- input = strip(input);
- input = toLower(input);
- /* Hangman is a character based game, so we only need the first character of input */
- char guess = input[0];
- /* If we've already guessed that letter, display a message and move one */
- if(usedLetters.indexOf(guess) != -1)
- {
- writeln("Cannot use a letter you've already used");
- continue;
- }
- /* Add the letter to letters used, and make i tlook pretty */
- usedLetters ~= guess ~ ", ";
- writeln("Used letters: ", usedLetters);
- /* While there's another letter that matches the guess */
- while(indexOf(word, guess) != -1)
- {
- /* Increase the amount of times we were correct */
- correct++;
- /* Get the character position */
- auto index = indexOf(word, guess);
- /* Change the correct underscore to the correct letter */
- display[index * 2] = guess;
- /* 'null' out the letter in the word array */
- word[index] = '*';
- /* If we've guessed all the letters in the word, then we won! */
- if(word.length == correct)
- {
- writeln("You've won!");
- exit(0);
- }
- }
- /* If we guessed wrong, then increase the amount of times we've guessed wrong, and subtract a guess */
- if(indexOf(word, guess) == -1)
- {
- wrong++;
- guesses--;
- }
- }
- /* If we get this far, we must have guessed more than 6 times, and lost :C */
- writeln("Game over, the correct word was: ", originalWord);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement