Computer Science Canada Help with a hangman game |
Author: | Morice [ Mon Apr 16, 2007 4:37 pm ] |
Post subject: | Help with a hangman game |
ok so im getin rlly stressed trying to figure out how to change a character if it occurs more then once so anyway who knos can tehy tell me hers my code look for the I DONT KNO WHAT TO PUT RIGHT HERE cause thatss wher im havin troubles thanks a lot ![]() // The "HangmanGame" class. import java.awt.*; import hsa.Console; import java.lang.*; import java.io.*; import java.lang.Object; public class HangmanGame { static Console c; // The output console public static void main (String [] args) { c = new Console (); int guessNum = 0, underlines = 0, goodCount = 0, badCount = 0, somecounter = 0; String word = "", wrongGuess = ""; char guess; //*****************************************************************The Words**********************************************************// String [] arrayOfWords = new String [11]; arrayOfWords [0] = new String ("Mathematics"); arrayOfWords [1] = new String ("Canada"); arrayOfWords [2] = new String ("Romania"); arrayOfWords [3] = new String ("Shopping"); arrayOfWords [4] = new String ("Hawkins"); arrayOfWords [5] = new String ("ComputerScience"); arrayOfWords [6] = new String ("Good Marks"); arrayOfWords [7] = new String ("Music"); arrayOfWords [8] = new String ("Boston"); arrayOfWords [9] = new String ("Windsor"); arrayOfWords [10] = new String ("Calculus"); //*************************************************************End of the Words********************************************************// guessNum = (int) (Math.random () * 10); //get a random number c.print (arrayOfWords [guessNum]); underlines = arrayOfWords [guessNum].length (); //getting the length of the word for (int x = 0 ; x < underlines ; x++) //loop to "hide" the word { word = word + "*"; c.print ("*"); } c.println (""); //**********************************************************************Main Loop************************************************************// while (goodCount < underlines) { c.setCursor (5, 1); c.println ("Please enter your letter."); //getting the answer from the user guess = c.readChar (); //getting the answer from the user if (arrayOfWords [guessNum].indexOf (guess) >= 0) //if statement to check if the guess is in the word { c.setCursor (2, 1); if (arrayOfWords [guessNum].indexOf (guess) == arrayOfWords [guessNum].lastIndexOf (guess)) //if statmenet to check more than one of the same letter { c.print (word.substring (0, arrayOfWords [guessNum].indexOf (guess)) + guess + word.substring ((arrayOfWords [guessNum].indexOf (guess)) + 1, underlines)); //display the guess with the hidden letters word = word.substring (0, arrayOfWords [guessNum].indexOf (guess)) + guess + word.substring ((arrayOfWords [guessNum].indexOf (guess)) + 1, underlines); //stores the guess with the hidden letters goodCount++; } else { somecounter++; for (int ii = 0 ; ii < underlines ; ii++) { if (arrayOfWords [guessNum].charAt (ii) == guess) { I DONT KNO WAT TO PUT RIGHT HERE } } c.setCursor (2, 1); c.print (word); } } if (arrayOfWords [guessNum].indexOf (guess) == -1) { badCount++; c.setCursor (10, 1); c.print ("Here are your wrong guesses:"); wrongGuess = wrongGuess + guess + " "; c.print (wrongGuess); if (badCount == 15) { break; } } } //***************************************************************End of Main Loop************************************************************// c.println (""); if (goodCount == underlines) { c.println ("You Win!"); } else { c.println ("You Suck! GAME OVER"); } } // main method } // HangmanGame class [/b] |
Author: | ericfourfour [ Mon Apr 16, 2007 5:48 pm ] | ||
Post subject: | Re: Help with a hangman game | ||
Please use syntax or code tags so you code appears indented. Have you tried writing this out on a piece of paper? It is a pretty simple algorithm: Read in a letter. If the letter has not been guessed, store the letter in a list of guessed letters. Do all of the other stuff with the letter. What is that piece of code supposed to do?
|
Author: | Morice [ Mon Apr 16, 2007 7:41 pm ] |
Post subject: | Re: Help with a hangman game |
tat piece of code is supposed to check if thers an occurence of an word more then once ya i kno i kinda complicated myself however i want to keep it in two different thigns like with only one word and with the guess appereaing more then once in a word like a in Canada so far if i run this program it will only output the first a so it will be *a**** so wat im trying to do with tat through tat if statment is to check if thers more then one occurance so i can put it in cause i dont kno how to do tat so ya hopefully u guys understand wat i mean |
Author: | Morice [ Mon Apr 16, 2007 9:37 pm ] |
Post subject: | Re: Help with a hangman game |
YEY I FIGURED IT OUT all on my own i might add this is teh for loop tat i neded thansk anyway ![]() for (int ii = 0 ; ii < underlines ; ii++) { if (arrayOfWords [guessNum].charAt (ii) == guess) { goodCount++; word = word.substring (0, ii) + guess + word.substring (ii + 1, word.length());<THIS LIEN RITE HERE } } |
Author: | ericfourfour [ Tue Apr 17, 2007 4:28 pm ] | ||
Post subject: | Re: Help with a hangman game | ||
I think I understand what you wanted to do more clearly now. Might I recommend using this function:
It will return the index of a given character in a string. The from index is where it starts the search. It returns -1 if there isn't any. It is just a quick way to find a character in a word. Simply loop through all indexes it finds (starting at 0) until it returns -1. |