473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

console hangman problem

94
I am trying to code a little console program (no swing, no awt, just console). But I am having some trouble and I am not sure if its my logic, or my code, or possibly both?!

Anyways heres what I currently have, I have not taken into consideration a user winning by guessing the word yet. Just working on displaying the hidden word and updating it with respect to the users guesses.

Expand|Select|Wrap|Line Numbers
  1. public class whatever {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         List<String> alpha = new ArrayList<String>(); //list of letters user can choose from
  6.         int maxTries = 7;
  7. //        final int maxWordLen = 25;
  8. //        int wrongGuesses = 0;
  9. //        int correctGuess;
  10. //        String guess;
  11.         String hiddenWord ="";
  12.         String correctChars= ""; // previously correctly guessed chars
  13.         String guessX = ""; // the new guess
  14.  
  15.  
  16.         String wordList [] = {"car", "desk"};
  17.         //select a word from array
  18.         int word = 0 + (int) (Math.random() * 2);
  19.  
  20.         String secretWord = wordList[word];
  21.  
  22.         for(int x = 0; x<secretWord.length(); x++)
  23.         {
  24.             hiddenWord += "-";
  25.         }
  26.         System.out.println("This is the hidden word: "+hiddenWord);
  27.  
  28.  
  29.         //System.out.println("The secret word is: "+secretWord);
  30.  
  31.  
  32.     //Add alphabet to list    
  33.         alpha.add("a");
  34.         alpha.add("b");
  35.         alpha.add("c");
  36.         alpha.add("d");
  37.         alpha.add("e");
  38.         alpha.add("f");
  39.         alpha.add("g");
  40.         alpha.add("h");
  41.         alpha.add("i");
  42.         alpha.add("j");
  43.         alpha.add("k");
  44.         alpha.add("l");
  45.         alpha.add("m");
  46.         alpha.add("n");
  47.         alpha.add("o");
  48.         alpha.add("p");
  49.         alpha.add("q");
  50.         alpha.add("r");
  51.         alpha.add("s");
  52.         alpha.add("t");
  53.         alpha.add("u");
  54.         alpha.add("v");
  55.         alpha.add("w");
  56.         alpha.add("x");
  57.         alpha.add("y");
  58.         alpha.add("z");
  59.  
  60.  
  61. //below here is good,kinda        
  62.         while(maxTries > 0 )
  63.         {
  64.  
  65.             System.out.println(hiddenWord);
  66.  
  67.             System.out.println("Here are your letters to chose from\n"+alpha);
  68.  
  69.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  70.             System.out.println("Please make a guess: ");
  71.             try
  72.             {
  73.                 guessX = br.readLine();
  74.             } 
  75.             catch (IOException ioe) 
  76.             {
  77.                 System.out.println("IO error!");
  78.                 System.exit(1);
  79.             }
  80.  
  81. //            if in word use String newSecret and display correct letters
  82.             if(guessX.indexOf(secretWord)!= 0 )
  83.             {
  84.                 alpha.remove(guessX);
  85.                 String newSecret= secretWord.replaceAll("[^"+correctChars+guessX+"]", "-");
  86.                 hiddenWord = newSecret;
  87.                 System.out.println(hiddenWord);
  88.             }
  89.             else
  90.             {
  91.                 System.out.println("Sorry your guess is not in the word");
  92.                 maxTries --;
  93.             }
  94.             System.out.println("You have "+maxTries+" chances remaining");
  95.         }//while
  96.     }
  97. }
  98.  
I have viewed all of the threads revolving around this problem at this site and haven't been able to figure this out yet.

The output now will show: -e-- if the user entered e. But when it goes to the next line it is ----. Any thoughts?
Nov 8 '07 #1
11 3037
dav3
94
whoops

Expand|Select|Wrap|Line Numbers
  1. if(guessX.indexOf(secretWord)!= 0 )
  2.  
fixed that its now

Expand|Select|Wrap|Line Numbers
  1. if(secretWord.indexOf(guessX != -1)
  2.  
Nov 8 '07 #2
r035198x
13,262 8TB
whoops

Expand|Select|Wrap|Line Numbers
  1. if(guessX.indexOf(secretWord)!= 0 )
  2.  
fixed that its now

Expand|Select|Wrap|Line Numbers
  1. if(secretWord.indexOf(guessX != -1)
  2.  
Any reason why your alphabet is made up strings and not chars? Then you can add the alphabet to the list using a loop.
Nov 8 '07 #3
dav3
94
i knew how to add them easily (not eloquently) as strings. Is it incorrect to do it this way? Incorrect in such away that my program will fail, at this point design principles are not a concern. I can always refine once i have a working program.
Nov 8 '07 #4
r035198x
13,262 8TB
i knew how to add them easily (not eloquently) as strings. Is it incorrect to do it this way? Incorrect in such away that my program will fail, at this point design principles are not a concern. I can always refine once i have a working program.
In that case I'll reserve the rest of my comments.
Nov 8 '07 #5
dav3
94
In that case I'll reserve the rest of my comments.

Sorry if I offended you. I have made a very strong effort on this program, but am in need of help. I don't know why my newSecret word is only showing one correct letter at a time.


Ah wasnt adding to my correctChars variable. Problem solved.


Please do post your comments I am interested in becoming a better programmer, but when i spend hours coding something and have someone ask me about something that isnt the problem... i get defensive, sorry.
Nov 8 '07 #6
r035198x
13,262 8TB
Sorry if I offended you. I have made a very strong effort on this program, but am in need of help. I don't know why my newSecret word is only showing one correct letter at a time.


Ah wasnt adding to my correctChars variable. Problem solved.


Please do post your comments I am interested in becoming a better programmer, but when i spend hours coding something and have someone ask me about something that isnt the problem... i get defensive, sorry.
Shouldn't your while condition also test whether the word has now been guessed correctly or not?
Nov 8 '07 #7
JosAH
11,448 Expert 8TB
Again: regular expressions are your friend here; suppose we have this:

Expand|Select|Wrap|Line Numbers
  1. String secretWord= "apple";
  2. String correctChars= "pe";
  3. String displayWord= secretWord.replaceAll("[^"+correctChars+"]", "-");
  4.  
a newChar is correct iff:

Expand|Select|Wrap|Line Numbers
  1. bool correct= secrectWord.indexOf(newChar) >= 0;
  2.  
The word has been guessed correctly iff:

Expand|Select|Wrap|Line Numbers
  1. bool guessed= displayWord.indexOf('-') < 0;
  2.  
kind regards,

Jos
Nov 8 '07 #8
r035198x
13,262 8TB
Again: regular expressions are your friend here; suppose we have this:

Expand|Select|Wrap|Line Numbers
  1. String secretWord= "apple";
  2. String correctChars= "pe";
  3. String displayWord= secretWord.replaceAll("[^"+correctChars+"]", "-");
  4.  
a newChar is correct iff:

Expand|Select|Wrap|Line Numbers
  1. bool correct= secrectWord.indexOf(newChar) >= 0;
  2.  
The word has been guessed correctly iff:

Expand|Select|Wrap|Line Numbers
  1. bool guessed= displayWord.indexOf('-') < 0;
  2.  
kind regards,

Jos
Which basically completes it.
I must say it was a very good effort from dav3.
Nov 8 '07 #9
JosAH
11,448 Expert 8TB
Which basically completes it.
I must say it was a very good effort from dav3.
Yup, but if you use regular expressions the 'business logic' for this little game
doesn't need more than, say, 20 lines or so of code.

kind regards,

Jos
Nov 8 '07 #10
dav3
94
Thank you. I have not completed the program yet, I wanted to be able to produce the word as expected before worrying about whether the person had one,lost, whatever. I also plan on making this a networked game where multiple clients can connect and play.

Project will also contain numerous word lists read from a file, each file containing words about a certain subject. ie Computer Science, Animals, Cities etc....


Thanks for your help guys.
Nov 8 '07 #11
JosAH
11,448 Expert 8TB
Thank you. I have not completed the program yet, I wanted to be able to produce the word as expected before worrying about whether the person had one,lost, whatever. I also plan on making this a networked game where multiple clients can connect and play.

Project will also contain numerous word lists read from a file, each file containing words about a certain subject. ie Computer Science, Animals, Cities etc....


Thanks for your help guys.
You're welcome of course. Don't get over enthousiastic for now. The best thing to
do is to 'decouple' your game's components. A WordListLoader (which is an
interface) would be one part:

Expand|Select|Wrap|Line Numbers
  1. public interface WordsListLoader {
  2.    WordsList load(String identifier);
  3. }
  4.  
A WordsListLoader gives you a WordsList (also an interface). It can do simple
things like this:

Expand|Select|Wrap|Line Numbers
  1. public interface WordsList extends List<String> {
  2.    public String randomWord();
  3.    public String randomWord(String subject);
  4. }
  5.  
etc. etc. Note that I didn't implement anything; if you manage to implement your
'core' of the game using just those interfaces you can implement those interfaces
independent of each other, e.g. a WordListLoader doesn't have to deal with the
production of actual words (either in a subject or not).

The saying is: low coupling, high coherence.

kind regards,

Jos
Nov 8 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: tigrfire | last post by:
So I'm trying to write a hangman game and the output is coming out a little strange. Here's my code thus far: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> ...
18
ilikepython
by: ilikepython | last post by:
Hi I'm new to Python(3 to 4 days) and I'm working on a hangman game and have been having some problems that really annoy me. Here is part of my script: c = input('Would you like to play hangman?...
4
by: princessfrost | last post by:
Hi! I was wondering if someone could please help me with a hangman program that I have to do. I have some ideas, but really don't know what to do or where to start. My program needs to be:...
47
by: araujo2nd | last post by:
Originally Posted by araujo2nd my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by...
2
by: tesa | last post by:
I am not able to figure out how to make this work. I am trying to create a hangman game. I am in a basic javascripting class. I am to only use very basic code as you can see. I am able to use any...
0
by: Madmartigan | last post by:
Hi I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way. I have included my code thus...
8
by: tidiz | last post by:
Hi, I'm trying to make a hangman game that should look like this: Welcome to Hangman ______ Your guess: c Success! __cc__ Your guess: b
1
by: AlexSc | last post by:
Hi to All, I am doing on a hangman project, Where this is a very simple one, where player would guess the word and if it is correct the letter would be from "-" to the correct letter once all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.