Advertisement
Guest User

Untitled

a guest
Sep 21st, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.77 KB | None | 0 0
  1. import std.stdio;
  2. import std.file;
  3. import std.random;
  4. import std.c.stdlib;
  5. import std.array;
  6. import std.string;
  7. import std.conv;
  8.  
  9. /* Draw the dude and the lynching pole */
  10. void draw(int level)
  11. {
  12.     /* The image is the guy */
  13.     string image;
  14.     /* If we only screwed up once, only add a head */
  15.     if(level >= 1)
  16.     {
  17.         image = "|      (_)\n";
  18.     }
  19.     /* If we screwed up twice, add a head and an arm, etc. */
  20.     if(level >= 2)
  21.     {
  22.         image ~= "|      \\|";
  23.     }
  24.     if(level >= 3)
  25.     {
  26.         image ~= "/\n";
  27.     }
  28.     if(level >= 4)
  29.     {
  30.         image ~= "|       |\n";
  31.     }
  32.     if(level >= 5)
  33.     {    
  34.         image ~= "|      /";
  35.     }
  36.     if(level >= 6)
  37.     {
  38.         image ~= "\\\n";
  39.     }
  40.  
  41.     /* We're going to need the top to be drawn every time, no matter what. */
  42.     writeln("_________");
  43.     writeln("|/      |");
  44.  
  45.     /* If image actually contains something, then draw it */
  46.     if(image)
  47.     {
  48.         writeln(image);
  49.     }
  50.  
  51.     /* 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
  52.        5 - levels = how many extra '|' we need to draw */
  53.     for(int i = 0; i < (5 - level); i++)
  54.     {
  55.         writeln("|");
  56.     }
  57.  
  58.     /* We'll also need to draw a floor everytime */
  59.     writeln("|__");
  60. }
  61.            
  62. void main(string args[])
  63. {
  64.     /* 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 */
  65.     string text = cast(string)read(args[1]);
  66.     text = toLower(text);
  67.    
  68.     /* Split the text up (into tokens) on each whitespace found and return the tokens in an array */
  69.     auto tokens = text.split();
  70.  
  71.     /* Getting a uniform random number based on an unpredictableSeed (a built in variable) */
  72.     auto gen = Random(unpredictableSeed);
  73.     auto random = uniform(0, tokens.length, gen);
  74.  
  75.     /* We'll be editting the word later on, so we need to save it in it's current state */
  76.     string originalWord = tokens[random];
  77.  
  78.     /* Converting the word we'll be editing to a char array */
  79.     char[] word = to!(char[])(tokens[random]);
  80.  
  81.     /* The amount of guesses allowed */
  82.     int guesses = 6;
  83.  
  84.     /* The amount of times you've guessed wrong (for the draw function argument), and the amount of times you've guessed right */
  85.     int wrong = 0;
  86.     int correct = 0;
  87.    
  88.  
  89.     /* The display of letters on the screen, '_ _ e _ a' */
  90.     char[] display;
  91.  
  92.     /* The letters you've used */
  93.     string usedLetters;
  94.  
  95.     /* For each character in our word */
  96.     foreach(character; word)
  97.     {
  98.         /* Add another underscore and space */
  99.         display ~= "_ ";
  100.     }
  101.  
  102.     /* While we still have guesses left */
  103.     while(guesses != 0)
  104.     {
  105.  
  106.         writeln("Guesses left: ", guesses);
  107.  
  108.         /* Draw our lynching pole based on how many times we were wrong */
  109.         draw(wrong);
  110.  
  111.         writeln("Word:", display);
  112.  
  113.         /* Read in input, strip \r\n and space from it, and make it all lower case */
  114.         auto input = readln();
  115.         input = strip(input);
  116.         input = toLower(input);
  117.  
  118.         /* Hangman is a character based game, so we only need the first character of input */
  119.         char guess = input[0];
  120.  
  121.         /* If we've already guessed that letter, display a message and move one */
  122.         if(usedLetters.indexOf(guess) != -1)
  123.         {
  124.             writeln("Cannot use a letter you've already used");
  125.             continue;
  126.         }
  127.  
  128.         /* Add the letter to letters used, and make i tlook pretty */
  129.         usedLetters ~= guess ~ ", ";
  130.         writeln("Used letters: ", usedLetters);
  131.  
  132.         /* While there's another letter that matches the guess */
  133.         while(indexOf(word, guess) != -1)
  134.         {
  135.             /* Increase the amount of times we were correct */
  136.             correct++;
  137.  
  138.             /* Get the character position */
  139.             auto index = indexOf(word, guess);
  140.  
  141.             /* Change the correct underscore to the correct letter */
  142.             display[index * 2] = guess;
  143.  
  144.             /* 'null' out the letter in the word array */
  145.             word[index] = '*';
  146.  
  147.             /* If we've guessed all the letters in the word, then we won! */
  148.             if(word.length == correct)
  149.             {
  150.                 writeln("You've won!");
  151.                 exit(0);
  152.             }
  153.         }
  154.        
  155.         /* If we guessed wrong, then increase the amount of times we've guessed wrong, and subtract a guess */
  156.         if(indexOf(word, guess) == -1)
  157.         {
  158.             wrong++;
  159.             guesses--;
  160.         }
  161.     }
  162.     /* If we get this far, we must have guessed more than 6 times, and lost :C */
  163.     writeln("Game over, the correct word was: ", originalWord);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement