Results 1 to 2 of 2
  1. #1
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default need help with my java assignment

    okay i have an assignment due in for 5th may and i have been struggling and grinding to do as much as possible but i still has lots to do can anybody help me

    Java Code:
    public class Dict {
    
        private String numberofDict;
        private String english;
        //private String pirate;
        
        public Dict(String n, String a ) {
            numberofDict=n;
            english=a;
        }
        
        public String toString() {
            return numberofDict+"  "+english;
        }
     
        public String getName() {
            return english;
        }
    }

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    public class DictionaryList {
    	
    	private ArrayList<String> words = new ArrayList<String>();
        private ArrayList<String> dicts = new ArrayList<String>();
    	private Random r = new Random();
        
        public DictionaryList() {
        }
        
        public String getRandomWord(){
    		return words.get(r.nextInt(words.size()));
    	}
    
    	public String getRandomPhrase(){
    		return dicts.get(r.nextInt(dicts.size()));
    	}
        
    	public boolean addWord(String word){
    		for(String w : words)
    			if(w.equals(word))
    				return false;
    		words.add(word);
    		return true;
    	}
    	
    	public boolean addDict(String dict){
    		for(String d : dicts)
    			if(d.equals(dict))
    				return false;
    		dicts.add(dict);
    		return true;
    	}
    	
    	public boolean add(String t) {
    		if(t == null || t.equals("")) return false;
    		if(t.split("\\t+").length > 1){
    			for(String d: dicts)
    				if(t.toLowerCase().equals(d.toLowerCase()))
    					return false;
    			this.addDict(t);
    			return true;
    		} else {
    			for(String w : words)
    				if(t.toLowerCase().equals(w.toLowerCase()))
    					return false;
    			this.addWord(t);
    			return true;
    		}
    	}
    	
    	public boolean remove(String t){
    		if(t == null || t.equals("")) return false;
    		if(t.split("\\t+").length > 1){
    			for(String d: dicts){
    				if(t.toLowerCase().equals(d.toLowerCase())){
    					dicts.remove(d);
    					return true;
    				}
    			}
    			return false;
    		} else {
    			for(String w: words){
    				if(t.toLowerCase().equals(w.toLowerCase())){
    					words.remove(w);
    					return true;
    				}
    			}
    			return false;
    		}
    	}
    	public String getWords() {
    		String returnWords = "";
    		for(String w: words)
    			returnWords+=w+"\n";
    		return returnWords.trim();
    	}
    	
        
        public int size(){
            return dicts.size();
        }
        public ArrayList<String> getWordsAsList(){
            return words;
        }
        
    	public String getDict(){
    		String returnDict = "";
    		for(String d: dicts)
    			returnDict+=d+"\n";
    		return returnDict.trim();
    	}
    
        public ArrayList<String> getPhrasesAsList(){
            return dicts;
        }
        
     
        public String toString(){
    		String out = "DictionaryList {\n\tSingle Words:[\n";
    		for(String w : words)
    			out += "\t\t" + w + "\n";
    		out += "\t],\n\tDicts:[\n";
    		for(String d : dicts)
    			out += "\t\t" + d + "\n";
    		out += "\t]\n}";
    		return out;
    	
    	}
    
        public ArrayList<String> getAllItems() {
            ArrayList<String> toReturn = new ArrayList<String>();
            toReturn.addAll(words);
            toReturn.addAll(dicts);
            return toReturn;
        }
    	
    }

    Java Code:
    /**
    * This interface describes the behaviour of a hangman type of game
    * To be used in CS124 individual assignment
    */
    public interface GameModelInterface
    {
    /**
    * this method returns what the user is allowed to see
    */
    public String getVisible();
    /**
    * this method returns the words to guess
    */
    public String getHidden() ;
    /**
    * this method tells how many guesses are left
    */
    public int guessLeft() ;
    /**
    * this method returns the letters that have already been guessed
    */
    public String getLetters() ;
    /**
    * this method sees if the letter is in the words
    * if it is it updates what the user can see (visible)
    * if not it removes a guess
    *
    * @param letter the letter to try
    * @return whether there is winner
    */
    public boolean tryThis(char letter);
    /**
    * this method sees if the word guess is correct
    * if it is it updates what the user can see (visible)
    * if not it removes 5 guesses
    *
    * @param guess the word to guess
    * @return whether there is winner
    */
    public boolean tryWord(String guess) ;
    }


    Java Code:
    import java.io.*;
    public class runApplication{
        public static void main(String args[]) throws IOException{
            TextModel app=new TextModel();
            app.runTextModel();
        }
    }

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TextModel implements GameModelInterface {
    
    	private String word;
    	private int guesses;
    	private int lives;
    	private static boolean inGame = false;
        private String visible;
    	private ArrayList<Character> guessedChars = new ArrayList<Character>();
        private DictionaryList dictionaryList;
         
        public TextModel(){           
    }
        public String showVisible() {
            return visible;
         }
        
    	private void updateVisible() {
    		String newVisible = "";
    		for(char w: word.toCharArray()){
    			for(char g: guessedChars){
    				if(g == w)
    					newVisible+=w;
    				else newVisible+='*';
    			}
    		}
    		if(visible.length() != newVisible.length())
    			System.out.println("We've got a problem here!");
    		visible = newVisible;
    	}
        public void runTextModel() throws FileNotFoundException {
            printMenu();   
        	char letter;
        	
        	@SuppressWarnings("resource")
    		Scanner kb = new Scanner(System.in);
            char again = 'n';
            String secret;
            StringBuffer gaps;
            final int MAXPARTS = 6;
            int bodyparts;
            boolean done;
            String guess;
            String guesses;
         	
            @SuppressWarnings("resource")
    		Scanner infile = new Scanner(new FileReader("piratewords.txt"));
        	
        	do {
                // play a game
                   secret = infile.next();
                   guesses = "";
                   done = false;
                   bodyparts = MAXPARTS;
                
                // make gaps
                   gaps = makeDashes(secret);
                  
                   while (! done)
                   {
                      System.out.println("Here is your word: " + gaps);
                      System.out.println("Guesses so far: " + guesses);
                      System.out.print("enter a guess (letter or word): ");
                      guess = kb.next();
                   // process the guess
                   
                      if (guess.length() > 1)  
                      {
                         if (guess.equals(secret))
                            System.out.println("you win!");
                         else
                            System.out.println("you lose");
                         done=true;
                      }
                      else // process single letter guess
                      {
                         letter = guess.charAt(0);
                         guesses += letter;
                         if (secret.indexOf(letter) < 0)  
                         {	--bodyparts;
                            System.out.print("bad guess - ");
                         }
                         else // letter is in the secret
                         {
                         	
                            matchLetter(secret, gaps, letter);                                   
                         }
                         System.out.println(bodyparts + " bodyparts are left");
                         if (bodyparts == 0)
                         {	System.out.println("you lose");
                            done = true;
                         }
                         if (secret.equals(gaps.toString()))
                         {	System.out.println("you win!");
                            done = true;
                         }
                      } // process single letter guess
                   
                   } // process whole guess
                
                   if (infile.hasNext())
                   {
                      System.out.print("play again (y/n)?: ");
                      again = kb.next().charAt(0);
                   }
                   else
                      System.out.println("thanks for playing (no more words)");
                } while (infile.hasNext() && (again == 'Y' || again == 'y'));
             } // end of main
    
        public static StringBuffer makeDashes(String s)
       {
          StringBuffer gaps = new StringBuffer(s.length());
          for (int count=0; count < s.length(); count++)
             gaps.append('-');
          return gaps;
       }
    	private void printMenu() {
            System.out.println("What would you like to do:");
            System.out.println("1 - create new pirate phrase and add");     
    		
    	}
    	
        public static void matchLetter(String secret, StringBuffer gaps, char letter)
       {
          for (int index = 0; index < secret.length(); index++)
             if (secret.charAt(index) == letter)
                gaps.setCharAt(index, letter);
          System.out.print("good guess - ");
       }
    	@Override
    	public String getVisible() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	public String getHidden() {
            String wordHints = "";
            for(String w: dictionaryList.getAllItems()){
                for(char c: guessedChars)
                    if(w.contains(""+c)){ //There has to be a better way??!?!?!?!?
                        wordHints+=wordHints + ", ";
                        break;
                    }
            }
    		return null;
    	}
    
    	@Override
    	public int guessLeft() {
    
    		return lives-guesses;
    	}
    	@Override
    	public String getLetters() {
    		
    		String guessed = "";
    		for(char c: guessedChars)
    			guessed+=c;
    		return guessed;
    	}
    	@Override
    	public boolean tryThis(char letter) {
    		guessedChars.add(letter);
    		for(char c: word.toCharArray())
    			if(c == letter){
    				updateVisible();
    				return true;
    			}
    		return false;
    	}
    	@Override
    	public boolean tryWord(String guess) {
    		return guess.equals(word);
    	}
    	public boolean isInGame() {
    		return inGame;
    	}
    
    	public static void setInGame() {
    		inGame = true;
    	}
    
        public static void setOutOfGame() {inGame = false; }
        
    }


    the assignment:

    This project is a variation of hangman. It involves programming both a text based, and
    a graphics based version of the same game. The graphics based version should be
    pirate themed. The words to be guessed must be pirate themed and must be read from
    the provided file called piratewords.txt that has the format.


    You have been told that you must program your system to interfaces.
    You need to use JUnit to check your implementation of this interface.
    In addition to your testing, you should write a main program application that
    runs from the command line by typing: java PirateApp
    Alternatively, if you are using a JAR file make sure you specify the commandline
    command we should use.
    The interface you are coding the Model of the game to is this: the game model interface class

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: need help with my java assignment

    That is waaay too much code for us to debug. And you've posted your assignment, but not a question or an explanation of exactly what you're confused by.

    I recommend reading the link in my signature on asking questions the smart way, then trying again.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Java Assignment
    By roladejo in forum Advanced Java
    Replies: 2
    Last Post: 05-09-2013, 01:25 AM
  2. JAVA assignment help
    By ScarabVenom in forum New To Java
    Replies: 1
    Last Post: 10-15-2012, 07:07 AM
  3. java assignment
    By drewjordan in forum New To Java
    Replies: 9
    Last Post: 01-16-2012, 12:44 AM
  4. Need Help with Java Assignment
    By smurf67 in forum New To Java
    Replies: 4
    Last Post: 03-26-2011, 11:25 AM
  5. My java assignment -- please help me !
    By java_beginner1 in forum New To Java
    Replies: 11
    Last Post: 05-20-2010, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •