Results 1 to 8 of 8
Thread: Need help with a assignment
- 06-06-2010, 06:00 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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 addedLast edited by Fubarable; 06-06-2010 at 11:45 AM. Reason: Moderator Edit: Code tags added
- 06-06-2010, 10:37 AM #2
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck, and again, welcome!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 06-06-2010, 03:34 PM #4
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
}
- 06-06-2010, 04:22 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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 :)
-
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.
- 06-06-2010, 04:37 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
- 06-06-2010, 04:49 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
need help! assignment due by tmr!
By keycoffee in forum New To JavaReplies: 3Last Post: 02-10-2010, 05:45 PM -
Need with my assignment ...
By allergy01 in forum New To JavaReplies: 1Last Post: 04-25-2009, 08:33 AM -
Can somebody help me in my assignment
By coolstruxx in forum NetBeansReplies: 0Last Post: 03-24-2009, 01:27 AM -
GUI First Assignment-DUE 8/1/08
By ljk8950 in forum AWT / SwingReplies: 2Last Post: 08-01-2008, 04:23 AM -
for Assignment plz help
By assamhammad in forum New To JavaReplies: 1Last Post: 11-06-2007, 08:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks