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
Sign in to Reply
  1. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    (Original post by Chrosson)
    ...
    Ok thanks, done it, now the only things left are:

    - 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
  2. Chrosson's Avatar
    • PS Helper
    • Vengeful, Imperial Overlord of The Student Room
    • Posts: 4,216
    Re: Search all array elements at once...[Java]
    I'm going to quote you out of order.

    (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?)
    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.

    - 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
    So...you're trying to fill in the second half of the while loop condition, yes?
    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.

    Ok thanks, done it, now the only things left are:
    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.
    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.
    Code:
               for (int i = 0; i < wordChosen.length; i++)
               {
                   Character.toLowerCase(wordChosen[i]);
                   if (letter == wordChosen[i])
                   {
                       correct(i);
                   }
                   else
                   {
                       incorrect(i);
                   }  
               }
    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.

    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.
  3. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    (Original post by Chrosson)
    x
    My wrongAttempts array is working, since my last post this is what I now have for my check() method:

    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]+" ");
            }
            
        }
    As you can see I'm no longer using the correct and incorrect methods.

    I am using the check_exists method you gave, with slight modifications:

    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;
       }
    And two other methods, winner and loser, which are similar to check_exists:

    Code:
     public boolean winner()
        {
            boolean full = false;
            for(int i = 0; i < dashes.length; i++)
            {
                if(dashes[i] == '_')
                 {
                   full = true;
                 }
            }
            return full;
       }
    Code:
    public boolean loser()
        {
            boolean lost = false;
            
            for(int i = 0; i < wrongAttempts.length; i++)
            {
                if(wrongAttempts[i] == '*')
                 {
                   lost = true;
                 }
            }
            return lost;
           
        }
    These now ensure that a wrong letter is added to the wrongGuesses array and the right letters are displayed the corresponding dash.

    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
  4. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    UPDATE: Managed to do 1)
  5. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    please just give me a hint or clue, I'd like to try and figure it out myself
  6. bordercollies10's Avatar
    • Exalted Member
    • Location: Scotland
    • Posts: 337
    Re: Search all array elements at once...[Java]
    (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?
    Hi

    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.
  7. Chrosson's Avatar
    • PS Helper
    • Vengeful, Imperial Overlord of The Student Room
    • Posts: 4,216
    Re: Search all array elements at once...[Java]
    (Original post by Prokaryotic_crap)
    please just give me a hint or clue, I'd like to try and figure it out myself
    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?
    Last edited by Chrosson; 28-07-2012 at 18:57.
  8. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    (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?
    not really, I'm confused..
  9. Chrosson's Avatar
    • PS Helper
    • Vengeful, Imperial Overlord of The Student Room
    • Posts: 4,216
    Re: Search all array elements at once...[Java]
    (Original post by Prokaryotic_crap)
    not really, I'm confused..
    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.

    I can give you pseudocode if you need...
  10. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    (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.
    Thanks - I'll try tomorrow and let you know..
  11. Prokaryotic_crap's Avatar
    • TSR Demigod
    • Location: Em City, Oz
    Re: Search all array elements at once...[Java]
    (Original post by Chrosson)

    I can give you pseudocode if you need...
    I think that pseudocode will be help..

    Remember I want to check if a letter guessed by the user is already in either of the wrongAttempts array OR the dashes array
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.