4 Replies - 174 Views - Last Post: 05 June 2014 - 03:01 PM Rate Topic: -----

#1 mochajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 14

replacing underscores in array implementation of Hangman game

Posted 05 June 2014 - 02:29 PM

Hello,

For an array implementation of a Hangman game I have created an array to hold the game board progress. It is initialized as "_ _ _ " where the underscores represent the number of letter in the word to be guessed. I have written the following method to replace underscores with a correct guess. It is functioning correctly in that it is replacing the underscore with a correct character guess, but it is only replacing the first time this letter appears in the word. I.e. for "greed" guessing "e" would only replace the first e: _ _ e _ _

I would greatly appreciate any pointers on how I may be able to fix this issue.

// Updates gameboard from "_" to current guess if guess is correct
			if (isCorrectGuess(move)==true){
				if(inWinningState()==false){
				guessProgress[charLocation] = guess;}				
				return true;}


Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: replacing underscores in array implementation of Hangman game

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,475
  • Joined: 12-June 08

Re: replacing underscores in array implementation of Hangman game

Posted 05 June 2014 - 02:32 PM

Perhaps use a loop to examine all of the characters.
Was This Post Helpful? 0
  • +
  • -

#3 mochajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 14

Re: replacing underscores in array implementation of Hangman game

Posted 05 June 2014 - 02:37 PM

That's what I was trying to accomplish by adding in my inWinningState() method (code below), but it doesn't seem to be doing the trick...
public boolean inWinningState() {
		
		for (int i = 0; i< storeGuessWord.length; i++){
			if (guessProgress[i]=='_'){
				return false;}}
			return true;}

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,475
  • Joined: 12-June 08

Re: replacing underscores in array implementation of Hangman game

Posted 05 June 2014 - 02:43 PM

Wouldn't you want to be checking the array with the word not hidden to find the indexes that correspond to the display 'guess' array? So you know.. you can swap out the underscores for characters?


Hidden 		Shown
--------	------
[0] = C		= _
[1] = A		= _
[2] = T		= _


User guesses M.
Use a loop to look through hidden to see if any [x] match M.
Nothing does so wait for next input.

User guesses T.
use a loop to look through hidden to see if any [x] match T
One does.. at index 2.

That means copy the value from hidden[2] to array shown[2]

Hidden 		Shown
--------	------
[0] = C		= _
[1] = A		= _
[2] = T		= T

Check to see if all the shown letters are.. well.. shown.  

No, there are still some hidden so repeat.


User guesses C.
use a loop to look through hidden to see if any [x] match C
One does.. at index 0.

That means copy the value from hidden[0] to array shown[0]
Hidden 		Shown
--------	------
[0] = C		= C
[1] = A		= _
[2] = T		= T


Check to see if all the shown letters are.. well.. shown.  

No, there are still some hidden so repeat.

.... etc ...

Was This Post Helpful? 1
  • +
  • -

#5 mochajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 14

Re: replacing underscores in array implementation of Hangman game

Posted 05 June 2014 - 03:01 PM

I got it to work by adding a for-loop:
// Updates gameboard from "_" to current guess if guess is correct
			for (int i=0; i<storeGuessWord.length; i++){
				if(guess==storeGuessWord[i]){
				guessProgress[i]=guess;}
				}
				

Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1