Results 1 to 20 of 111
Thread: Help with random letters.
- 02-24-2011, 06:42 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Help with random letters.
Hi everyone!
I need help with one of my cs assignments.
The assignment is to basically make a game. In the game, there is basically a 5x5 square, where each square has a letter from the alphabet.
There will be 1 random square with a blank space ' '. I am supposed to move the letters around, and get the inner 3x3 box to look like this
a|b|c
d|e|f
g|h|i
What I need help with is assigning random letters to the squares.
To start off I assigned each square to a random letter, like this
Here's what my board looks like.Java Code:s1 = (char)('a' + randomNumberGenerator.nextInt( 26)); s2 = (char)('a' + randomNumberGenerator.nextInt( 26)); s3 = (char)('a' + randomNumberGenerator.nextInt( 26)); // and so on
My question is how do I assign the letters to a square without repeating the letters? Note: I am not allowed to use arrays or simulate an array with strings.Java Code:// Display the board System.out.printf( "--------------------- \n"); System.out.printf( "| %c | %c | %c | %c | %c | \n", s1, s2 ,s3 , s4, s5) ; System.out.printf( "--------------------- \n"); System.out.printf( "| %c | %c | %c | %c | %c | \n", s6, s7, s8, s9, s10) ; System.out.printf( "--------------------- \n"); System.out.printf( "| %c | %c | %c | %c | %c | \n", s11, s12, s13, s14, s15) ; System.out.printf( "--------------------- \n"); System.out.printf( "| %c | %c | %c | %c | %c | \n", s16, s17, s18, s19, s20) ; System.out.printf( "--------------------- \n"); System.out.printf( "| %c | %c | %c | %c | %c | \n", s21, s22, s23, s24, s25) ; System.out.printf( "--------------------- \n");
the link to my assignment: http://logos.cs.uic.edu/107/assignme...uare%20Up.htmlLast edited by jenxin; 02-24-2011 at 07:22 PM.
- 02-24-2011, 06:50 PM #2
And why are you not allowed to use arrays?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-24-2011, 06:52 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
The teacher doesn't want us to, so I had to individually assign each letter to a variable..
- 02-24-2011, 06:53 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Are you allowed to use a Set?
- 02-24-2011, 06:53 PM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
I'm confused: are you asking how to generate each letter only once? (And if so, which two letters won't get generated, since you're going to generate only 24?)
If you can't use arrays, can you use any of the java.util.Collection classes? If so, then java.util.Set is what you need.
- 02-24-2011, 06:57 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Thanks for the replies, here's what I can and can't do:
And yeah how to generate each letter once. They must be randomized in the board tho.You may not use arrays in this program, but must store the board positions in 25 char variables. (Don't worry if you don't know what arrays are yet - we're studying arrays later.) Similarly you may not simulate arrays by using Strings. 25 variables!
NRitH, i''ll look into what the java.util.Set does.
- 02-24-2011, 07:04 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 02-24-2011, 07:08 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Its worth about 10 % of my grade, I need to do it. :'(. Any guidance will be great. I've never used the java.util.Set , I don't think he wants us to use it.. but if its not against his rules, i'll use it, you can guys give some more info on it? Thanks
- 02-24-2011, 07:10 PM #9
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-24-2011, 07:11 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 02-24-2011, 07:12 PM #11
It would be interesting to see your actual requirements. I don't usually request this, but can we see what your homework assignment actually says?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-24-2011, 07:12 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Oh stop nitpicking, it's part of the riddle, don't you understand that? No arrays, those are the rules of the game and no more than ten curly brackets and no semicolons and just one 'for' keyword. Probably the OP doesn't understand (yet) what arrays are but the OP defines the rules of the game.
kind regards,
Jos ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-24-2011, 07:13 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Sure guys, here's the link to my assignment.
Program 3: Square Up
- 02-24-2011, 07:28 PM #14
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
OK, well to start filling your grid, I would start with a couple of instance variables, initialized like this:
Choose a random letter from letters by generating an index in the range 0 <= index < lettersRemaining, use charAt() to get the letter, then remove that letter from letters and decrement lettersRemaining. I don't see any legalities preventing you from putting that much into a method.Java Code:private String letters = "abcdefghijklmnopqrstuvwxyz"; private int lettersRemaining = 26;
The rest of the code has to be done with huge ugly blocks of switch statements. It seems the whole point is to make you grateful when arrays are introduced.
-Gary-
- 02-24-2011, 07:32 PM #15
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Actually, you don't need lettersRemaining, because it's the same as letters.length(). The brain damage is taking effect already.
-Gary-
- 02-24-2011, 07:33 PM #16
I don't think that's legal either. From the assignment: You may not use arrays in this program, but must store the board positions in 25 char variables. (Don't worry if you don't know what arrays are yet - we're studying arrays later.) Similarly you may not simulate arrays by using Strings. 25 variables!
The assignment does have some clues as to how he wants it done. Convoluted yes, but not impossible. I'd recommend going over the Magic Squares example with a fine toothed comb.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-24-2011, 07:44 PM #17
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Am i supposed to initialize the squares first then?
and instead of making s1 = random letter, make it set to ie. s1 = a?
- 02-24-2011, 08:08 PM #18
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Well thanks for the help. I ended up making 25 while loops for this
microsoft word helped alot for changing the variables.Java Code:while ( s2==s2 || s2==s3 || s2==s4 || s2==s5 || s2==s6 || s2==s7 || s2==s8 || s2==s9 || s2==s10 || s2==s11 || s2==s12 || s2==s13 || s2==s14 || s2==s15 || s2==s16 || s2==s17 || s2==s18 || s2==s19 || s2==s20 || s2==s21 || s2==s22 || s2==s23 || s2==s24 || s2==s25) { s2 = (char)('a' + randomNumberGenerator.nextInt( 26));
- 02-24-2011, 08:45 PM #19
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Alright I need help with changing the square that contains the letter r into a space ' ' .
Can anyone help me with this? thanks
- 02-24-2011, 08:49 PM #20
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
generate random letters inbetween a string
By greg677 in forum New To JavaReplies: 1Last Post: 05-04-2010, 05:06 AM -
What does the letters mean?
By mustachMan in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:50 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Generate a random code 4 letters long
By bl00dr3d in forum New To JavaReplies: 9Last Post: 04-06-2009, 05:32 AM -
need block letters??
By dc2acgsr99 in forum New To JavaReplies: 16Last Post: 01-29-2008, 08:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks