Results 1 to 8 of 8
  1. #1
    helpMe.Java is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Unhappy Need help with a assignment

    Hi i m new to this forum :D and also new to java, seriously need help with a project which is similiar to scrabble game but after trying for so long it does not seem to print out the things i want. Nid some guidance on hw to continue :confused: below is my code and also the output the program is giving which is not what i wanted:( :

    Java Code:
    //----------------------------------------------------import java------------------------------------------------------
    import java.util.Scanner;
    import java.util.Random;
    
    public class FinalTry {
    
    	public static void main(String[] args) {
    		Random rand = new Random();
    		Scanner userInput = new Scanner(System.in);
    		// TODO Auto-generated method stub
    		
    //----------------------------------------------------Player Object----------------------------------------------------
    		Players playerOne = new Players();
    		playerOne.score = 0;
    		playerOne.choice = "@!orWords";
    		playerOne.turn = true;
    
    		Players playerTwo = new Players();
    		playerTwo.score = 0;
    		playerTwo.choice = "@!orWords";
    		playerTwo.turn = false;
    		
    //----------------------------------------------------Letter n score----------------------------------------------------
    		String wordlist[] = FileUtil.readDictFromFile("words.txt");
    		char [] letter = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','e','i','o','u'};
    		int [] letterScore = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
    		
    		//Rand letters stored in this array
    		int[] randomNum = new int[10];//assign numbers
    		char[] ranLetter = new char[10];//assign letters
    		System.out.println("FindYourWords");
    		
    //----------------------------------------------------Player letters----------------------------------------------------
    		boolean playerTurn = true;
    		while (playerTurn) {
    
    			if (playerOne.turn) {
    				System.out.print("Letters of Player 1: ");
    			} else if (!playerOne.turn) {
    				System.out.print("Letters of Player 2: ");
    			}
    			//Generate random letters
    			for (int i = 0; i < randomNum.length; i++) {
    				int rl = rand.nextInt(letter.length); 
    				ranLetter[i] = letter[rl];
    				System.out.print(ranLetter[i] + " ");
    			}
    			
    //----------------------------------------------------Player loop-------------------------------------------------------
    			System.out.println("");
    			System.out.println("Enter your word (or @ to pass or ! to quit): ");
    			
    			if (playerOne.turn) {
    				playerOne.choice = userInput.next();
    				playerOne.turn = false;
    				
    //----------------------------------------------------Player One loop----------------------------------------------------
    				if (playerOne.choice.equals("!")) {
    					break;
    				}else if (playerOne.choice.equals("@")) {
    					playerTurn = true;
    					System.out.println("Total score for word : " +playerOne.score);
    					System.out.println("Total score for game : " +playerOne.score);
    				}
    				else if (playerOne.choice.equals(" "));
    					for (int j = 0; j<letter.length;j++)
    					{
    						for (int k = 0; k < playerOne.choice.length();k++)
    						{
    							if (playerOne.choice.charAt(k) == letter[j])
    							{
    							playerOne.score = playerOne.score + letterScore[j];
    							playerTurn = true;
    						}
    				System.out.println("Total score for word : " +playerOne.score);
    					}
    				}
    			}
    			else if (!playerOne.turn){
    				playerTwo.choice = userInput.next();
    				playerOne.turn = true;
    				
    //----------------------------------------------------Player Two loop----------------------------------------------------			
    				if (playerTwo.choice.equals("@")) {
    					playerTurn = true;
    					System.out.println("Total score for word : " +playerTwo.score);
    					System.out.println("Total score for game : " +playerTwo.score);
    				}
    				else if (playerTwo.choice.equals("!")) {
    				}
    				
    //-------------------------------------------------------Check word-------------------------------------------------------				
    			int valid = 0; // new counter
    			for (int i = 0; i<wordlist.length; i++){
    			    if (playerOne.choice.equalsIgnoreCase(wordlist[i])) {
    			    	//if playerone input == word in wordlist, valid =1
    				break;  //will stop running check since already checked that it is valid
    			    }  
    			}  
    	
     // if only is valid then will add score
    				for (int j = 0; j<letter.length;j++)
    				{
    					for (int k = 0; k < playerOne.choice.length();k++)
    					{
    						if (playerOne.choice.charAt(k) == letter[j])
    						{
    							playerOne.score = playerOne.score + letterScore[j];
    						}
    
    					}
    				}
    				System.out.println("Total score for word : " +playerOne.score);
    				System.out.println("Total score for game : " +playerOne.score);
    
    				for (int j = 0; j<letter.length;j++)
    				{
    					for (int k = 0; k < playerTwo.choice.length();k++)
    					{
    						if (playerTwo.choice.charAt(k) == letter[j])
    						{
    							playerTwo.score = playerTwo.score + letterScore[j];
    						}
    
    					}
    				}
    				System.out.println("Total score for word : " +playerTwo.score);
    				System.out.println("Total score for game : " +playerTwo.score);
    			}			
    		}
    	}
    }

    FindYourWords
    Letters of Player 1: p l o l c p t e v l
    Enter your word (or @ to pass or ! to quit):
    pet
    Total score for word : 0
    Total score for word : 0
    Total score for word : 0
    Total score for word : 0
    Total score for word : 0
    Total score for word : 0
    Total score for word : 0
    Total score for word : 0
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 1
    Total score for word : 4
    Total score for word : 4
    Total score for word : 4
    Total score for word : 4
    Total score for word : 4
    Total score for word : 4
    Total score for word : 4
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Total score for word : 5
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
    at FinalTry.main(FinalTry.java:72)

    Moderator Edit: Code tags added
    Last edited by Fubarable; 06-06-2010 at 11:45 AM. Reason: Moderator Edit: Code tags added

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by helpMe.Java View Post
    char [] letter = {'a','b','c','d','e','f','g','h','i','j','k','l',' m','n','o','p','q','r','s','t','u','v','w','x','y' ,'z','a','e','i','o','u'};
    int [] letterScore = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8, 4,10};

    i first deleted the last 5 chars in the letter array and then the code runs. the reason for the error you get is that the last chars have no letterScore. so, either you delete these letters or you add the score for these letters in letterScore. have fun.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Good debugging code would be to compare the lengths of the two arrays to be the same:
    if(letter.length != letterScore.length) {
    There is a problem
    }

  5. #5
    helpMe.Java is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Fubarable srry i dun quite understand what you are saying is it b4 pasting it to this forum i do a [c0de]my pasted code[/c0de] srry for the zero if put the normal text nth shows up xD than post my thread in?? and also i have given points for my last few letters and the code may seems to have a loop prob i will try chk on it again :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Help me, I'm having a tough time reading your posts as they're full of non-standard abbreviations. Out of respect for us, could you edit it to use standard English? Thanks.

  7. #7
    helpMe.Java is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by helpMe.Java View Post
    Fubarable srry i dun quite understand what you are saying is it b4 pasting it to this forum i do a [c0de]my pasted code[/c0de] srry for the zero if put the normal text nth shows up xD than post my thread in?? and also i have given points for my last few letters and the code may seems to have a loop prob i will try chk on it again :)
    Sorry about my English i understand what you are trying to say already thanks for the help i will continue to work on my errors and if theres any i will be posting on this post again. Thanks all :D

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Help me, I'm having a tough time reading your posts as they're full of non-standard abbreviations. Out of respect for us, could you edit it to use standard English? Thanks.
    My guess is that the OP posted it from a cell phone: a lame combination of L33T SP34K and txt spk. I'm already getting better at reading that mess ;-)

    kind regards,

    Jos

Similar Threads

  1. need help! assignment due by tmr!
    By keycoffee in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 05:45 PM
  2. Need with my assignment ...
    By allergy01 in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 08:33 AM
  3. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 01:27 AM
  4. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 04:23 AM
  5. for Assignment plz help
    By assamhammad in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 08:35 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
  •