Results 1 to 2 of 2
Thread: Hangman Game
- 02-17-2011, 02:36 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Hangman Game
So right now I am working on a very simple hangman game for class. Right now, the program runs great but I have run into a few problems... I know it is a few lines of code here and there but I don't know how to do it!
The problem is:
1. I don't know how to replace the "-" with letters they guessed right
2. If I enter a correct letter over and over the game will eventually end saying I have won.
3. I have no idea how to store the wrong guesses.. I want to store them and order them alphabetically and then print them out after every guess. What kind of storing process should I use?
Code and hints are VERY appreciated!
package Project3;
import java.util.*;
public class project3
{
int errors;
int gameover;
int counter;
public project3(String name)
{
Dictionary dictionary = new Dictionary();
String chsnWrd = dictionary.getRandomWord();
//This makes the amount of characters in chsnWrd and makes it a number
int lineSize = chsnWrd.length();
System.out.println(lineSize);
char blankLine [];
//this makes a new char array = to the length of the randomly chosen word
blankLine = new char[lineSize];
for(int i=0;i<lineSize;i++)
{blankLine[i] = ' ';}
drawFigure();
for(int underlineCounter = 0; underlineCounter < lineSize; underlineCounter++)
{
System.out.print("_ ");
}
System.out.println("\n\nTry and Guess the Word. Good Luck!\n");
Scanner guessScan = new Scanner (System.in);
int correct = 0;
while(errors < 6)
{
String guess = guessScan.next();
char guessChar = guess.charAt(0);
boolean checkMe = false;
System.out.println();
for(int charCounter = 0; charCounter < lineSize; charCounter++)
{
if(guessChar == chsnWrd.charAt(charCounter))
{
System.out.println("Correct!\n");
correct++;
blankLine[charCounter] = guessChar;
checkMe = true;
}
}
if(!checkMe)
{
System.out.println("Incorrect...Try Again");
errors++;
}
drawFigure();
for(int i=0; i<lineSize; i++)
{
System.out.print(blankLine[i]);
}
System.out.println();
for(int i=0;i<lineSize;i++)
{
System.out.print("-");
}
if(correct == lineSize)
{
System.out.println("\nYou Won!");
break;
}
}
}
void drawFigure ()
{
if(errors == 6)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| 0");
System.out.println("|- + -");
System.out.println("| / \\ ");
System.out.println();
}
if(errors == 5)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| 0");
System.out.println("|- + -");
System.out.println("| / ");
System.out.println();
}
if(errors == 4)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| 0");
System.out.println("|- + -");
System.out.println("| ");
System.out.println();
}
if(errors == 3)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| 0");
System.out.println("|- + ");
System.out.println("| ");
System.out.println();
}
if(errors == 2)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| 0");
System.out.println("| + ");
System.out.println("| ");
System.out.println();
}
if(errors == 1)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| 0");
System.out.println("| ");
System.out.println("| ");
System.out.println();
}
if(errors == 0)
{
System.out.println("+–-+");
System.out.println("| |");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println();
}
}
}
- 02-26-2011, 04:01 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 18
- Rep Power
- 0
2.
3. you should use an array and add each incorrect guess to it. sort itJava Code:for(int charCounter = 0; charCounter < lineSize; charCounter++) { [COLOR="Red"] if(guessChar != blankLine[charCounter]) {[/COLOR] if(guessChar == chsnWrd.charAt(charCounter)) { System.out.println("Correct!\n"); correct++; blankLine[charCounter] = guessChar; checkMe = true; } [COLOR="Red"]} else System.out.println("You have already guessed that letter"); [/COLOR] } if(!checkMe) { System.out.println("Incorrect...Try Again"); errors++; }to order the letters alphabeticallyJava Code:myArray.sort()
Last edited by mr_guy; 02-26-2011 at 04:12 AM.
Similar Threads
-
Hangman Game // HELP //
By K-Scale in forum New To JavaReplies: 4Last Post: 05-27-2010, 12:01 AM -
Hangman Game Help Please
By 9tjh in forum New To JavaReplies: 4Last Post: 12-04-2009, 03:19 AM -
Hangman Game..
By iPetey in forum New To JavaReplies: 4Last Post: 05-07-2009, 02:24 PM -
Need help with hangman game
By kurt in forum New To JavaReplies: 4Last Post: 04-25-2009, 06:47 PM -
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 01:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks