1 Replies - 274 Views - Last Post: 06 June 2014 - 05:47 PM Rate Topic: -----

#1 mochajava  Icon User is offline

  • New D.I.C Head

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

does my linked list look right? linked list implementation of Hangman

Posted 06 June 2014 - 05:22 PM

My assignment was to create a linked list implementation of a Hangman game. I had to use linked lists and was not allow to use any pre-created structures like arraylists. I created three linked lists (one to store the word to be guessed, one to store all the guesses I've made, and one to store the game board progress: _ _ _ _ .

I'm having issues with the linked lists I've created - I tried to add indexes that would refer to the last node in the linked lists and I'm wondering if this is where I went wrong. My debugging shows me that my guesses are not being added to my AllGuess linked list that is supposed to store guesses, also my IsCorrect method is not returning when I have correct guesses. I'd appreciate any pointers you might be contribute.


public class GameBoardLinkedList implements GameBoard, HangManConstants {
	
	/** hung state */
	private int		state;
	
	private int numGuesses;
	
	private LLCharacterNode first; // Reference to first link of stored guess word
	private LLCharacterNode last; // Reference to last link of stored guess word
	
	private LLCharacterNode guessProgressFirst; // Reference to the first link of the linked list storing game board progress
	private LLCharacterNode guessProgressLast; // Reference to the last link of the linked list storing game board progress
	
	private LLCharacterNode allGuessFirst; // Reference to first link of linked list storing all guesses made
	private LLCharacterNode allGuessLast; // Reference to last link of linked list storing all guesses made
	
	// Constructs a new linked list with no links yet
	
	// ************************************CREATE LINKED LIST TO STORE GUESS WORD*******************************//
	
	public GameBoardLinkedList(String guessWord) { 
	System.out.println(guessWord);
	
	
			first = null;
			last = null;	
	}
	
	// Returns true if no links on list yet
	public boolean isEmpty(){ 
		return first == null;}
	
	// Method to insert at front of list/onto "first"
	public void insertFirst(char f){
		LLCharacterNode newLink = new LLCharacterNode(f);
		
		if(isEmpty())//{ // If isEmpty() is true, or list is empty
			last = newLink;
			//newLink.next = first;			//???Where does 2nd "if" bracket go?
			newLink.setLink(first);
			first = newLink;
		}
		
	// Method to insert at end of list/onto "last"
	public void insertLast(char f){
		LLCharacterNode newLink = new LLCharacterNode(f);
		
		if(isEmpty())//{ // If is isEmpty() is true, or if list is empty
			first = newLink;
		else
			//last.next = newLink;
			newLink.setLink(last);
			last = newLink;
	}
	
	// Creates a linked list to store the word to be guessed
	public void createStoreGuessWord(String guessWord){
		GameBoardLinkedList storeGuessWord = new GameBoardLinkedList(guessWord);
		
		int index =0;
		char charGuessWord;
		while (index<guessWord.length()){
			charGuessWord = guessWord.charAt(index);
			storeGuessWord.insertFirst(charGuessWord);}
	}
	
	// Creates a linked list to store the progress of the game board
	public void createGameBoard(String guessWord){
		GameBoardLinkedList guessProgress = new GameBoardLinkedList(guessWord); 
		
		int index = 0;
		
		while (index<guessWord.length()){			//////DEBUG: not entering this loop
			
			guessProgress.insertFirst('_');
		}
	}
	

	//******************************CREATES LINKED LIST TO STORE GAME BOARD PROGRESS**************************//
	
	// Constructs a new linked list with no links yet
	public void guessProgress(String guessWord) { 
		
				guessProgressFirst = null;
				guessProgressLast = null;	
		}
		
		// Returns true if no links on list yet
		public boolean isGuessProgressEmpty(){ 
			return allGuessFirst == null;}
		
		// Method to insert at front of list/onto "first"
		public void insertFirstGuessProgress(char f){
			LLCharacterNode newLink = new LLCharacterNode(f);
			
			if(isEmpty())//{ // If isEmpty() is true, or list is empty
				guessProgressLast = newLink;
				//newLink.next = first;			//???Where does 2nd "if" bracket go?
				newLink.setLink(guessProgressFirst);
				guessProgressFirst = newLink;
			}
			
		// Method to insert at end of list/onto "last"
		public void insertLastGuessProgress(char f){
			LLCharacterNode newLink = new LLCharacterNode(f);
			
			if(isEmpty())//{ // If is isEmpty() is true, or if list is empty
				guessProgressFirst = newLink;
			else
				//last.next = newLink;
				newLink.setLink(guessProgressLast);
				guessProgressLast = newLink;}
	
		
	//*****************CREATES LINKED LIST TO STORE ALL GUESSES (CORRECT AND INCORRECT) MADE SO FAR********************//
	
	// Constructs a new linked list with no links yet
	public void allGuess(char guess) { 
		
				allGuessFirst = null;
				allGuessLast = null;	
		}
		
		// Returns true if no links on list yet
		public boolean isAllGuessEmpty(){ 
			return allGuessFirst == null;}
		
		// Method to insert at front of list/onto "first"
		public void insertFirstAllGuess(char f){
			LLCharacterNode newLink = new LLCharacterNode(f);
			
			if(isEmpty())//{ // If isEmpty() is true, or list is empty
				allGuessLast = newLink;
				//newLink.next = first;			//???Where does 2nd "if" bracket go?
				newLink.setLink(allGuessFirst);
				allGuessFirst = newLink;
			}
			
		// Method to insert at end of list/onto "last"
		public void insertLastAllGuess(char f){
			LLCharacterNode newLink = new LLCharacterNode(f);
			
			if(isEmpty())//{ // If is isEmpty() is true, or if list is empty
				allGuessFirst = newLink;
			else
				//last.next = newLink;
				newLink.setLink(allGuessLast);
				allGuessLast = newLink;}
	
	
		
	// Returns true if the guess is a prior guess.
	public boolean isPriorGuess(char guess) {
		LLCharacterNode node;
		node = allGuessFirst;
		//allGuessFirst.getLink();
		
		while (node != null){
			if (guess==(node.getInfo())){
				return true;}
			else
				node = node.getLink();
			}
		return false;}
	
	
	
				
	// Returns the number of correct and incorrect guesses made.
	public int numberOfGuesses() {
		return numGuesses;
	}

	
	
	
	public boolean isCorrectGuess(char guess) {
		LLCharacterNode node;
		node = first;
	
		while (node != null){
			if (guess==(node.getInfo())){
				return true;}
			else
				node = node.getLink();
			}
		return false;}
	
	
	public boolean doMove(char guess) {
		
		// Check to see if guess has been guessed previously 
			char move = guess; 
					
		if (isPriorGuess(move)==true){
				return false;}
		
		while (isPriorGuess(move)== false){
					
		// Increment the number of guesses that has been made	
				numGuesses++; 							//////DEBUGGING: working as planned
				
		// Add guess to linked list of all previous guesses if it is not a prior guess, want to set guesses to tail end
				insertFirstAllGuess(guess);
				
		// Updates gameboard from "_" to current guess if guess is correct
				if (isCorrectGuess(move)==true){
				
				//****Write Linked list version of this
					insertLastGuessProgress(guess);
					return true;}
			
		// Adds a piece to the hangman drawing/increments incorrect guesses if guess is incorrect
					if (isCorrectGuess(move)==false){
						state++;
						return false;}}
					return false;}
		

	public boolean inWinningState() {
		LLCharacterNode node;
		node = guessProgressFirst;
		
		while (node != null){     ///////Debugging: not entering this loop
			if ('_'!=(node.getInfo())){
				return true;}
			else
				node = node.getLink();
				//return false;
			}
		return false;
		
	}

	public boolean inLosingState() {
		return state == NUMBER_OF_STATES;
	}

	public int currentHungState() {
		return state;
	}
	
	
	
	// Returns string representation of board
	
	
	public String toString() {
		String boardString = " "; 
		
		LLCharacterNode node;
		node = guessProgressFirst;
		//guessProgressFirst.getLink();
		
		while (node !=null){
			//uessProgressFirst.getLink();
			//count++;
			boardString = node.getInfo()+"";
			node = node.getLink();}
		
		return boardString;
		}
		
		

	// Returns string representation of prior guesses
	public String previousGuessString() {
		String guessesString = " "; 
		/*LLCharacterNode node;
		node = logAllGuess;
		int count = 0;
		
		while (node != null){
			count++;
			guessesString = guessesString + count + "." + node.getInfo();
			node = node.getLink();}*/
		return guessesString;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: does my linked list look right? linked list implementation of Hangman

#2 mochajava  Icon User is offline

  • New D.I.C Head

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

Re: does my linked list look right? linked list implementation of Hangman

Posted 06 June 2014 - 05:47 PM

Update: I went through and made sure I had bracketed all my "else" statements and that fixed the first issue I was having, it is now storing my previous guesses into the linked list. However, it is still not recognizing when I've made a correct guess.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1