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. -
public class whatever {
-
-
public static void main(String[] args) {
-
-
List<String> alpha = new ArrayList<String>(); //list of letters user can choose from
-
int maxTries = 7;
-
// final int maxWordLen = 25;
-
// int wrongGuesses = 0;
-
// int correctGuess;
-
// String guess;
-
String hiddenWord ="";
-
String correctChars= ""; // previously correctly guessed chars
-
String guessX = ""; // the new guess
-
-
-
String wordList [] = {"car", "desk"};
-
//select a word from array
-
int word = 0 + (int) (Math.random() * 2);
-
-
String secretWord = wordList[word];
-
-
for(int x = 0; x<secretWord.length(); x++)
-
{
-
hiddenWord += "-";
-
}
-
System.out.println("This is the hidden word: "+hiddenWord);
-
-
-
//System.out.println("The secret word is: "+secretWord);
-
-
-
//Add alphabet to list
-
alpha.add("a");
-
alpha.add("b");
-
alpha.add("c");
-
alpha.add("d");
-
alpha.add("e");
-
alpha.add("f");
-
alpha.add("g");
-
alpha.add("h");
-
alpha.add("i");
-
alpha.add("j");
-
alpha.add("k");
-
alpha.add("l");
-
alpha.add("m");
-
alpha.add("n");
-
alpha.add("o");
-
alpha.add("p");
-
alpha.add("q");
-
alpha.add("r");
-
alpha.add("s");
-
alpha.add("t");
-
alpha.add("u");
-
alpha.add("v");
-
alpha.add("w");
-
alpha.add("x");
-
alpha.add("y");
-
alpha.add("z");
-
-
-
//below here is good,kinda
-
while(maxTries > 0 )
-
{
-
-
System.out.println(hiddenWord);
-
-
System.out.println("Here are your letters to chose from\n"+alpha);
-
-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
System.out.println("Please make a guess: ");
-
try
-
{
-
guessX = br.readLine();
-
}
-
catch (IOException ioe)
-
{
-
System.out.println("IO error!");
-
System.exit(1);
-
}
-
-
// if in word use String newSecret and display correct letters
-
if(guessX.indexOf(secretWord)!= 0 )
-
{
-
alpha.remove(guessX);
-
String newSecret= secretWord.replaceAll("[^"+correctChars+guessX+"]", "-");
-
hiddenWord = newSecret;
-
System.out.println(hiddenWord);
-
}
-
else
-
{
-
System.out.println("Sorry your guess is not in the word");
-
maxTries --;
-
}
-
System.out.println("You have "+maxTries+" chances remaining");
-
}//while
-
}
-
}
-
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?
11 3069
whoops -
if(guessX.indexOf(secretWord)!= 0 )
-
fixed that its now -
if(secretWord.indexOf(guessX != -1)
-
whoops -
if(guessX.indexOf(secretWord)!= 0 )
-
fixed that its now -
if(secretWord.indexOf(guessX != -1)
-
Any reason why your alphabet is made up strings and not chars? Then you can add the alphabet to the list using a loop.
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.
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.
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.
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?
JosAH 11,448
Recognized Expert MVP
Again: regular expressions are your friend here; suppose we have this: -
String secretWord= "apple";
-
String correctChars= "pe";
-
String displayWord= secretWord.replaceAll("[^"+correctChars+"]", "-");
-
a newChar is correct iff: -
bool correct= secrectWord.indexOf(newChar) >= 0;
-
The word has been guessed correctly iff: -
bool guessed= displayWord.indexOf('-') < 0;
-
kind regards,
Jos
Again: regular expressions are your friend here; suppose we have this: -
String secretWord= "apple";
-
String correctChars= "pe";
-
String displayWord= secretWord.replaceAll("[^"+correctChars+"]", "-");
-
a newChar is correct iff: -
bool correct= secrectWord.indexOf(newChar) >= 0;
-
The word has been guessed correctly iff: -
bool guessed= displayWord.indexOf('-') < 0;
-
kind regards,
Jos
Which basically completes it.
I must say it was a very good effort from dav3.
JosAH 11,448
Recognized Expert MVP
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>
int hangman()
|
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? Type y or n ')
word = sample(('soccer', 'robbery', 'antibiotics'), 1)
word = str(word)
spaces = len(word) - 4
def hangman():
print '-------- '
|
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: interactive with the user, menu based(like Menu: 1. Play hangman 2. Exit program), have a dictionaryand use a random method(create arrays of Strings or ListArray of Strings. e.g. : String dictionary =
“apple”, “ball”, “cat”, “dog”, …..} minimum words...
|
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 the 15th which i have just started now, i have always struggled with java and would rele appreciate all the help that i can get. Pls could some1 help me as much as u can, im nt asking u to do the program for me but if u could pls take me steps through...
|
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 online resources to help me.
I have added alot of comments for what should be happening however is not happening.
I also have a teacher that teaches us one way but then asks us to do things he has not taught. I am trying my very best.
Any...
| |
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 far and at this stage would like to know how I can get the RandomWordManager, which I found on another site, to display a newly generated word as the textBox1 text when the user enters a new game.
I have enclosed tags around the two forms I have...
|
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
|
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 letter is correct they system will appear out good job u clear the game.
I had done up the game and it work however i find mine is rather impractical, looking for better solution for it. please enlighten me in it.
public static void...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |