Let's Play Hangman Challenge

This is the place for ALL of the user submitted challenges. If you create a little challenge/mission/riddle/whatever, post it here.
Forum rules
Do not post missions that you did NOT create without proper citing.

Let's Play Hangman Challenge

Post by -Ninjex- on Wed Aug 14, 2013 4:31 am
([msg=76840]see Let's Play Hangman Challenge[/msg])

This is part of the continuous programming challenges!

The Challenge:
Level: Moderate

Points: 175
Create a program that simulates a game of hangman.
The program must use a word list.
The program should take the word list as a command line argument.
The word for the game needs to be a randomly selected word from the word list.
In the game of hangman the user gets 6 guesses. Each wrong guess will add a part to the hangman, use the following order: head, body, left arm, right arm, left leg, then right leg (after right leg, it's game over)
You will show the user how many guesses they have remaining.
You will show the user which letters they have used, and issue an error message if they try to use a duplicate character
You will place underscores "_" separated by spaces as a template for the word to be guessed.
You will fill in the underscores with correctly guessed characters into their appropriate position.
If the user runs out of guesses tell them it's game over and display the word.
If the user guesses the word, tell them they won.
The amount of guesses should be subtracted by 1 if they guess a wrong character, otherwise it remains static.

Example Game:
Code: Select all
Guesses Left: 0
      _________
      |/      |
      |      (_)
      |      \|/
      |       |
      |      / \
      |
   ___|___
Used Letters: a, r, b, e, h, o,  s, m, t
Word: h e _ _ o
Game over, the correct word was: hello


Note: You do need to have the ascii hangman art as well.
Last edited by -Ninjex- on Sat Sep 21, 2013 2:52 am, edited 1 time in total.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by caracarn001 on Thu Aug 15, 2013 9:44 am
([msg=76853]see Re: Let's Play Hangman Challenge[/msg])

This is done in java
Code: Select all
package hangman;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Main {
   private ArrayList<String>wordlist;
   private String currentword;
   private String guessedword;
   private ArrayList<Character>fails;
   private int attempts=0;
   private Scanner scanner;
   
   public Main(ArrayList<String> wordlist) {
      this.wordlist=wordlist;
      fails=new ArrayList<Character>();
      currentword = selectWord();
      scanner= new Scanner(System.in);
      while (attempts <6) {
         drawField();
         showFails();
         showWord();
         if(!guessedword.contains("_")){
            System.out.println("we have a winner");
            break;
         }
         makeGuess();
      }
      drawField();
      showFails();
      showWord();
   }
   private void makeGuess() {
      System.out.print("Make a guess: ");
      char character = scanner.next().charAt(0);
      if( fails.contains(character)){
         System.out.println("Already guessed and failed, try again");
         makeGuess();
      } else if (!currentword.contains(character+"")){
         fails.add(character);
         attempts++;
      } else {
         char[]stringtoarray = guessedword.toCharArray();
         for(int i=0; i < currentword.length(); i++){
            if(currentword.charAt(i)==character){
               stringtoarray[2*i]=character;
            }
         }
         guessedword = new String(stringtoarray);
      }
      
   }
   private void showWord() {
      System.out.println(guessedword);
      
   }
   private void showFails() {
      StringBuilder sb = new StringBuilder("wrong guesses: ");
      for (Character c : fails) {
         sb.append(c);
         sb.append(" ");
      }
      System.out.println(sb.toString());
   }
   private void drawField() {
      if(attempts == 0){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("   ___|___");      
      }else if(attempts == 1){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |      (_)");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("   ___|___");               
      }else if(attempts == 2){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |      (_)");
          System.out.println("      |       |");
          System.out.println("      |       |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("   ___|___");               
      }else if(attempts == 3){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |      (_)");
          System.out.println("      |       |/");
          System.out.println("      |       |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("   ___|___");               
      }else if(attempts == 4){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |      (_)");
          System.out.println("      |      \\|/");
          System.out.println("      |       |");
          System.out.println("      |");
          System.out.println("      |");
          System.out.println("   ___|___");               
      }else if(attempts == 5){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |      (_)");
          System.out.println("      |      \\|/");
          System.out.println("      |       |");
          System.out.println("      |        \\");
          System.out.println("      |");
          System.out.println("   ___|___");               
      }else if(attempts == 6){
          System.out.println("      _________");
          System.out.println("      |/      |");
          System.out.println("      |      (_)");
          System.out.println("      |      \\|/");
          System.out.println("      |       |");
          System.out.println("      |      / \\");
          System.out.println("      |");
          System.out.println("   ___|___");               
      }
      
   }
   private String selectWord() {
      Random r = new Random();
      String nextword=wordlist.get(r.nextInt(wordlist.size()));
      StringBuilder sb = new StringBuilder();
      for (int i=0; i<nextword.length();i++) {
         sb.append("_ ");
      }
      guessedword=sb.toString();
      return nextword;
      
   }

   public static void main(String args[]){
      ArrayList<String> words = new ArrayList<String>(Arrays.asList(args));
      new Main(words);
   }
}

Last edited by caracarn001 on Thu Aug 15, 2013 3:41 pm, edited 2 times in total.
User avatar
caracarn001
New User
New User
 
Posts: 42
Joined: Thu Nov 04, 2010 5:23 am
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by -Ninjex- on Thu Aug 15, 2013 3:16 pm
([msg=76857]see Re: Let's Play Hangman Challenge[/msg])

Please fix your post and add your language.
I am getting an error.

Code: Select all
hangman.java:18: illegal start of type
      fails=new ArrayList<>();
                          ^
1 error
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by caracarn001 on Thu Aug 15, 2013 3:42 pm
([msg=76858]see Re: Let's Play Hangman Challenge[/msg])

i edited the code and hope it runs smoothly for you now too. In eclipse the code did not give problems for me.
User avatar
caracarn001
New User
New User
 
Posts: 42
Joined: Thu Nov 04, 2010 5:23 am
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by -Ninjex- on Thu Aug 15, 2013 4:21 pm
([msg=76859]see Re: Let's Play Hangman Challenge[/msg])

Tried in the code in Eclipse and with javac from the command line, both are still giving errors. Maybe a third party wouldn't mind checking this as well.

Code: Select all
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
   at java.util.Random.nextInt(Random.java:265)
   at hangman.Main.selectWord(Main.java:136)
   at hangman.Main.<init>(Main.java:19)
   at hangman.Main.main(Main.java:148)
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by Assassian360 on Thu Aug 15, 2013 9:15 pm
([msg=76861]see Re: Let's Play Hangman Challenge[/msg])

Here's a lazy C++ version.

Code: Select all
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;

bool showVisibleString(string selectedWord, vector<char> usedLetters);
void showUsedLetters(vector<char> usedLetters);
void showHangMan(int guesses);

int main(int argc, char* argv[]) {
   srand(time(NULL));
   int selectedWord = rand() % (argc - 1) + 1;
   string solutionWord = string(argv[selectedWord]);
   vector<char> usedLetters = vector<char>();
   int guesses = 6;
   char guessInput;

   while(true) {
      showUsedLetters(usedLetters);
      bool complete = showVisibleString(solutionWord, usedLetters);
      if(complete){
         cout << "You guessed the word " << solutionWord
            << " correctly! Well done!" << endl;
         break;
      } else if(!guesses) {
         cout << "Game over, the correct word was: "
            << solutionWord << endl;
         break;
      } else {
         cout << "Enter your guess: ";
         cin >> guessInput;
         if(find(usedLetters.begin(), usedLetters.end(),
                  guessInput) != usedLetters.end()) {
            cout << "You already guessed that!" << endl;
         } else {
            usedLetters.push_back(guessInput);
            if(find(solutionWord.begin(), solutionWord.end(), guessInput)
                     == solutionWord.end())
            {
               cout << "Guesses left: " << --guesses << endl;
               showHangMan(guesses);
            }
         }
      }
   }

   cin >> selectedWord;
   return 0;
}

bool showVisibleString(string selectedWord, vector<char> usedLetters) {
   bool complete = true;
   cout << "Word: ";
   for (auto i = selectedWord.begin(); i != selectedWord.end(); ++i) {
      if(find(usedLetters.begin(), usedLetters.end(), (char)(*i))
               != usedLetters.end()) {
         cout << *i << " ";
      } else {
         cout << "_ ";
         complete = false;
      }
   }
   cout << endl;
   return complete;
}

void showUsedLetters(vector<char> usedLetters) {
   cout << "Used Letters: ";
   if(usedLetters.size() == 0)
      cout << "You have not used any letters yet.";

   for (auto i = usedLetters.begin(); i != usedLetters.end(); ++i) {
      if(i != usedLetters.begin())
         cout << ", ";
      cout << *i;
   }
   cout << endl;
}

void showHangMan(int guesses) {
   cout << "\t_________\n\t|/      |\n\t|      (_)";
   cout << "\n\t|      " << ((guesses<4)?"\\":" ");
   cout << ((guesses<5)?"|":" ") << ((guesses<3)?"/":" ");
   cout << "\n\t|       " << ((guesses<5)?"|":" ");
   cout << "\n\t|      " << ((guesses<2)?"/ ":" ");
   cout << ((guesses<1)? "\\":" ") << "\n\t|\n     ___|___\n";
}




-Ninjex- wrote:Tried in the code in Eclipse and with javac from the command line, both are still giving errors. Maybe a third party wouldn't mind checking this as well.


I tested it and got the error when I ran it without supplying arguments. You need to modify the projects arguments that it supplies for debugging if you want to run it directly in Eclipse. I tested it in Netbeans and as soon as it had a series of words supplied as arguments to it the code ran.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by -Ninjex- on Thu Aug 15, 2013 10:31 pm
([msg=76863]see Re: Let's Play Hangman Challenge[/msg])

Assassian360 wrote:
-Ninjex- wrote:Tried in the code in Eclipse and with javac from the command line, both are still giving errors. Maybe a third party wouldn't mind checking this as well.


I tested it and got the error when I ran it without supplying arguments. You need to modify the projects arguments that it supplies for debugging if you want to run it directly in Eclipse. I tested it in Netbeans and as soon as it had a series of words supplied as arguments to it the code ran.


@assassian360, I appreciate the review. I have never used Eclipse, and managed to get the code to work. Your code is giving me compilation errors:

Code: Select all
hangman.cpp: In function ‘bool showVisibleString(std::string, std::vector<char>)’:
hangman.cpp:56:12: error: ‘i’ does not name a type
hangman.cpp:56:38: error: expected ‘;’ before ‘i’
hangman.cpp:56:38: error: ‘i’ was not declared in this scope
hangman.cpp: In function ‘void showUsedLetters(std::vector<char>)’:
hangman.cpp:74:12: error: ‘i’ does not name a type
hangman.cpp:74:37: error: expected ‘;’ before ‘i’
hangman.cpp:74:37: error: ‘i’ was not declared in this scope


@caracarn001, your code works but does not use a randomly selected word from a word list, as the rules state. You will get 125 points, until the code is fixed. Then you may receive the other 50 points.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by Assassian360 on Thu Aug 15, 2013 10:46 pm
([msg=76865]see Re: Let's Play Hangman Challenge[/msg])

-Ninjex- wrote:@assassian360, I appreciate the review. I have never used Eclipse, and managed to get the code to work. Your code is giving me compilation errors:

Looks like your compiler doesn't support newer C++ syntax. (eg, the auto keyword). I'll make some changes later to make it compile for you. The reason it isn't compiling though is you're using an outdated version of C++ (compiles with VS2010).
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by pretentious on Thu Aug 15, 2013 11:01 pm
([msg=76867]see Re: Let's Play Hangman Challenge[/msg])

Assassin360, i like your use of the conditional operator things for the ascii art. I'm thinking of using a multiline string array and i could actually relate the index directly the the number of guesses. Wow I'm good :P
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: Let's Play Hangman Challenge

Post by -Ninjex- on Thu Aug 15, 2013 11:15 pm
([msg=76871]see Re: Let's Play Hangman Challenge[/msg])

Assassian360 wrote:
-Ninjex- wrote:@assassian360, I appreciate the review. I have never used Eclipse, and managed to get the code to work. Your code is giving me compilation errors:

Looks like your compiler doesn't support newer C++ syntax. (eg, the auto keyword). I'll make some changes later to make it compile for you. The reason it isn't compiling though is you're using an outdated version of C++ (compiles with VS2010).


http://stackoverflow.com/questions/1006 ... ion-of-gcc

Seems like auto is actually a legend keyword, which compilers are still attempting to support, have a look at the link above.

The code compiled when adding the following options: -std=c++11
Also, your code works but does not use a randomly selected word from a word list, as the rules state. You will get 125 points, until the code is fixed. Then you may receive the other 50 points.
Last edited by -Ninjex- on Thu Aug 15, 2013 11:34 pm, edited 1 time in total.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Next

Return to User Submitted

Who is online

Users browsing this forum: No registered users and 0 guests