Search all array elements at once...[Java]
From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.
| Announcements | Posted on | |
|---|---|---|
| Please change your TSR password | 23-05-2013 | |
| Enter our travel-writing competition for the chance to win a Nikon 1 J3 camera | 20-05-2013 | |
-
Re: Search all array elements at once...[Java]Ok thanks, done it, now the only things left are:(Original post by Chrosson)
...
- the letter guessed should not be added to the wrongAttempts array if it has already been guessed (You mention this problem in your above post where you showed me the check_exists method, can you please explain more about how to overcome this problem?)
- if the wrongAttempts array is full i.e. if the user enters a wrong guess and they already used their limit (which is 6 guesses allowed), the game should end with a commiseration message -
Re: Search all array elements at once...[Java]
I'm going to quote you out of order.
At the point where you are about to add a letter to the wrongattempts array (...which you actually aren't doing yet - see final point below), you simply check if the value is already in said array.(Original post by Prokaryotic_crap)
- the letter guessed should not be added to the wrongAttempts array if it has already been guessed (You mention this problem in your above post where you showed me the check_exists method, can you please explain more about how to overcome this problem?)
So...you're trying to fill in the second half of the while loop condition, yes?- if the wrongAttempts array is full i.e. if the user enters a wrong guess and they already used their limit (which is 6 guesses allowed), the game should end with a commiseration message
You could create an additional variable which simply holds the current number of wrong guesses. You could iterate over the array and check how many are alphabet characters. You could use a list instead of an array and use append methods which means you can use built in methods to get the size of the list.
Sadly, you're wrong. You still have a bit more to do. You aren't actually currently adding any characters at all to the wrongattempts array.Ok thanks, done it, now the only things left are:
The "incorrect" method needs to do something. Specifically, the thing I mentioned above about the checking, and then adding the character.
BUT: the "incorrect" method is not in the correct place.
For example, consider the word hat. If I guess 't', it will call the 'incorrect' with an argument of first 0, and then 1 and then call correct with an argument of 2.Code:for (int i = 0; i < wordChosen.length; i++) { Character.toLowerCase(wordChosen[i]); if (letter == wordChosen[i]) { correct(i); } else { incorrect(i); } }
There are a couple of ways to solve this - it's fairly simple. If you're not immediately sure how, it'll be a good exercise for you to figure it out. -
Re: Search all array elements at once...[Java]My wrongAttempts array is working, since my last post this is what I now have for my check() method:(Original post by Chrosson)
x
As you can see I'm no longer using the correct and incorrect methods.Code:public void check() { if (Character.isLetter(letter)) { // check_exists(letter, wordChosen); if(check_exists(letter, wordChosen) == true) { for(int j = 0; j < wordChosen.length; j++) { if (wordChosen[j] == letter) { dashes[j] = letter; } } } else { wrongAttempts[index] = letter; index++; } } if (!Character.isLetter(letter)) { System.out.println("Letters only please.."); } for(int l = 0; l < dashes.length; l++) { System.out.print(dashes[l]+" "); } System.out.print("\nWrong guesses: \t"); for(int y = 0; y < wrongAttempts.length; y++) { System.out.print(wrongAttempts[y]+" "); } }
I am using the check_exists method you gave, with slight modifications:
And two other methods, winner and loser, which are similar to check_exists:Code:public boolean check_exists(char input, char[] wordChosen) { boolean found = false; for(int i = 0; i < wordChosen.length; i++) { if(wordChosen[i] == letter) { found = true; } } return found; }
Code:public boolean winner() { boolean full = false; for(int i = 0; i < dashes.length; i++) { if(dashes[i] == '_') { full = true; } } return full; }These now ensure that a wrong letter is added to the wrongGuesses array and the right letters are displayed the corresponding dash.Code:public boolean loser() { boolean lost = false; for(int i = 0; i < wrongAttempts.length; i++) { if(wrongAttempts[i] == '*') { lost = true; } } return lost; }
In my main method, I manage to make it repeat until the full word is guessed and start the game again. The things I want to do now are:
1) As you put it, verbally abuse the user when the wrongAttempts array is full
2) Not put the same letter in the wrongAttempts array again if it is already there -
Re: Search all array elements at once...[Java]Hi(Original post by Prokaryotic_crap)
I want to put the letter inputted by the user into an array if it is not an element of another array.
Any ideas?
Check to see if item is in original array (note that the Boolean "exists" must be falsified prior to executing the code below)
FOR c = 0 to SIZE OF ARRAY (construct finite loop)
IF array(c) = input THEN
SET exists = TRUE
NEXT c (or c=c+1 depending on how your programming language increments integers)
END IF
To add an item - if it doesn't exist in original array - to an other array
a = a + 1 (element counter update)
IF exists = FALSE THEN
otherarray(a) = input
END IF
If you don't mind me asking, which programming language do you use for implementation? (just realised that you use Java by the way)
I hope that my pseudo-code is relatively easy to follow 
EDIT: who "negged" me? I happen to have other things that I could spend my Saturday afternoon doing, but instead I'm trying my best to help those who are studying a subject that I happen to be studying at a high level...Last edited by bordercollies10; 28-07-2012 at 18:46. -
Re: Search all array elements at once...[Java]So first you need to check that it's already there.(Original post by Prokaryotic_crap)
please just give me a hint or clue, I'd like to try and figure it out myself
To do that you need to check every element of the array.
There are 2 options while checking the array:
1) If you find it, you do not want to proceed with any more of the method or checking the array.
2) If you find it, you need to store that information so that you can check it in an if statement later.
Does that help?Last edited by Chrosson; 28-07-2012 at 18:57. -
Re: Search all array elements at once...[Java]not really, I'm confused..(Original post by Chrosson)
So first you need to check that it's already there.
To do that you need to check every element of the array.
There are 2 options while checking the array:
1) If you find it, you do not want to proceed with any more of the method or checking the array.
2) If you find it, you need to store that information so that you can check it in an if statement later.
Does that help? -
Re: Search all array elements at once...[Java]You're going to be replacing the following two lines from your else:(Original post by Prokaryotic_crap)
not really, I'm confused..
wrongAttempts[index] = letter;
index++;
To describe the end code:
You haven't found the character in the wrongattempts array (you haven't even looked yet) - set some variable to false.
You need to iterate through every character of the array. You need a for loop.
At each iteration, check if the current character equals the one the user has inputted.
If so, you've found it, so set the variable you set before to true.
If you don't find the character after the for loop is finished (have a look at the variable), it isn't in there. So you need to insert it.
I can give you pseudocode if you need... -
Re: Search all array elements at once...[Java]Thanks - I'll try tomorrow and let you know..(Original post by Chrosson)
You're going to be replacing the following two lines from your else:
wrongAttempts[index] = letter;
index++;
To describe the end code:
You haven't found the character in the wrongattempts array (you haven't even looked yet) - set some variable to false.
You need to iterate through every character of the array. You need a for loop.
At each iteration, check if the current character equals the one the user has inputted.
If so, you've found it, so set the variable you set before to true.
If you don't find the character after the for loop is finished (have a look at the variable), it isn't in there. So you need to insert it.