Start New Discussion within our Software Development Community

I'm trying to implement a hangman game using linked lists but not getting anywhere. I want guessed characters to be inserted in a Linked list of chars at a position that corresponds to the character's position in the word they are trying to guess.(See example at bottom.) For some reason my play() method doesn't do what it's supposed to do.

  1. I have a gameLinkedList class that takes in a word; creates a charNode reference (character node) and a LinkedList of charNodes with their info set to the '_' character. the size of the linked list is equal to the size of the word.

  2. I have a play method that takes in a character and determines whether the character exists in the word. If it does, then the play method is supposed to replace the '_' in the charNode linked list that corresponds to the position of that char in the word.

  3. For example, a word like drone would result in a newNode linked list of _ _ _ _ _ If a gameLinkedList object gll is created and it calls play('o') i.e. gll.play('o') This would change the newNode to _ _ o _ _

    Here's how my class looks like

    public class gameLinkedList{
    private charNode game;
    private String newString;
    private int size;
    public gameLiknedList(String word){
    newString=word;
    size=word.length();
    for(int i=0;i<size;i++){charNode newNode = new charNode('_');
       if(game==null){game=newNode;}
       else{newNode.setLink(game);game=newNode;}
        }
      }
    
     public boolean charExists(guess){return newString.indexOf(guess)>0;}    
     public boolean play(guess){charNode kNode=game;
     if(charExists(guess)){
       for(int j=0;j==newString.indexOf(guess);j++){
         if(j==newString.indexOf(guess)){game.setInfo(guess);}
         else{game=game.getLink();}
        } 
       }
      }
     }
    Any help is appreciated
    
This question has already been answered. Start a new discussion instead.