Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2007, 01:07 AM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
Create the game Hangman
Hi, I have to create the game Hangman. It has to run in the command prompt. I have drawn scaffold and created an array with words.

I can randomly generate a word from that list. My first problem comes in getting the length of that word. I can't seem to figure out how to get the length of the word.
When I get the length of the word i can loop the length to draw __'s for the length.
My next problem would be, how do you get user inputs as guesses from the command prompt as guesses, then determine if they are in the actual word generated, then return a wrong if wrong, display correct letters in place, and add the list of guessed letters to the settings.
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 08:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
how to get the length of the word
Use the String method length
Code:
int len = word.length();
Code:
import java.util.*; public class UserInput { public static void main(String[] args) { String[] words = { "hello", "world" }; Random rand = new Random(); Scanner scanner = new Scanner(System.in); String choice; do { String word = words[rand.nextInt(words.length)]; System.out.println("Guess word"); String guess = scanner.nextLine(); if(word.equals(guess)) { System.out.println("correct"); } else { System.out.println("wrong"); // add the list of guessed letters to the settings // Do you mean to the words array? words = addGuess(words, guess); System.out.printf("words = %s%n", Arrays.toString(words)); } System.out.println("More? \"y\" or any key"); choice = scanner.nextLine(); } while(choice.equals("y")); scanner.close(); } private static String[] addGuess(String[] array, String element) { int len = array.length; String[] temp = new String[len+1]; System.arraycopy(array, 0, temp, 0, len); temp[len] = element; return temp; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TicTacToe Game Ebtihal New To Java 0 01-09-2008 01:01 PM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-16-2007 01:02 AM
Need help with random game! silverq_82 New To Java 4 08-07-2007 04:58 PM
Help with my game in java lenny New To Java 1 07-23-2007 06:40 PM
Help with pong game Eric New To Java 2 07-03-2007 09:02 PM


All times are GMT +3. The time now is 10:56 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org