Page 1 of 6 123 ... LastLast
Results 1 to 20 of 111
  1. #1
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Question 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

    Java Code:
    s1 = (char)('a' + randomNumberGenerator.nextInt( 26));
    s2 = (char)('a' + randomNumberGenerator.nextInt( 26));
    s3 = (char)('a' + randomNumberGenerator.nextInt( 26));
    // and so on
    Here's what my board looks like.

    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");
    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.

    the link to my assignment: http://logos.cs.uic.edu/107/assignme...uare%20Up.html
    Last edited by jenxin; 02-24-2011 at 07:22 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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!

  3. #3
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    The teacher doesn't want us to, so I had to individually assign each letter to a variable..

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Are you allowed to use a Set?

  5. #5
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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.

  6. #6
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Thanks for the replies, here's what I can and can't do:
    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!
    And yeah how to generate each letter once. They must be randomized in the board tho.

    NRitH, i''ll look into what the java.util.Set does.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by jenxin View Post
    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!
    That sounds like an exercise designed to induce brain damage. I wouldn't do it.

    -Gary-

  8. #8
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    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

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by jenxin View Post
    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
    If he wants you to use 25 variables, then he doesn't want you to use a Set.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by jenxin View Post
    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
    It seems to be against his rules. He's pretty specific about requiring 25 char variables.

    -Gary-

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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!

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    And why are you not allowed to use arrays?
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Sure guys, here's the link to my assignment.

    Program 3: Square Up

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, well to start filling your grid, I would start with a couple of instance variables, initialized like this:
    Java Code:
            private String letters = "abcdefghijklmnopqrstuvwxyz";
            private int lettersRemaining = 26;
    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.

    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-

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Actually, you don't need lettersRemaining, because it's the same as letters.length(). The brain damage is taking effect already.

    -Gary-

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by gcalvin View Post
    OK, well to start filling your grid, I would start with a couple of instance variables, initialized like this:
    Java Code:
            private String letters = "abcdefghijklmnopqrstuvwxyz";
            private int lettersRemaining = 26;
    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.

    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-
    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!

  17. #17
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Am i supposed to initialize the squares first then?

    and instead of making s1 = random letter, make it set to ie. s1 = a?

  18. #18
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Well thanks for the help. I ended up making 25 while loops for this

    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));
    microsoft word helped alot for changing the variables.

  19. #19
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Alright I need help with changing the square that contains the letter r into a space ' ' .

    Can anyone help me with this? thanks

  20. #20
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by jenxin View Post
    Alright I need help with changing the square that contains the letter r into a space ' ' .

    Can anyone help me with this? thanks
    Check every single character to see if it is the char 'r' and if it is change it to space going to need a switch statement, a big one...

Page 1 of 6 123 ... LastLast

Similar Threads

  1. generate random letters inbetween a string
    By greg677 in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 05:06 AM
  2. What does the letters mean?
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-11-2010, 09:50 PM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Generate a random code 4 letters long
    By bl00dr3d in forum New To Java
    Replies: 9
    Last Post: 04-06-2009, 05:32 AM
  5. need block letters??
    By dc2acgsr99 in forum New To Java
    Replies: 16
    Last Post: 01-29-2008, 08:31 AM

Posting Permissions

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