Page 2 of 6 FirstFirst 1234 ... LastLast
Results 21 to 40 of 111
  1. #21
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Do you think you can help me start it off? I just learned switch statements recently but I'm not sure how I can check. Thanks

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

    Default

    I don't think a switch is going to work here. A switch is branching based on the value of a single variable. You want to look at 25 variables to see which one matches your value. You want a block of if/else if.

    -Gary-

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

    Default

    I was hoping I didn't need one of those.. =[. How will I be able to move these variables later on?

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

    Default

    Quote Originally Posted by gcalvin View Post
    I don't think a switch is going to work here. A switch is branching based on the value of a single variable. You want to look at 25 variables to see which one matches your value. You want a block of if/else if.

    -Gary-
    :o Yup my bad.

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

    Default

    In the assignment page, it says
    Rather than 25 if-else-if or switch statements everytime you need to decide which board square you are dealing with, I recommend you write two methods for this, one to retrieve the square at position n and another to store a character into the board square at position n.
    I'm lookng at the sample code given as a hint, but I'm not sure where to go..

    see the Fall 2010 solution to the Magic Square program, in particular paying attention to how the getBoardValueAtSquare(...) and setBoardValueAtSquare(...) methods are used.

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

    Default

    When you are assigning the random letters perform a check to see if it is the letter 'r' (after it has passed uniqueness) and if it is assign it to an empty space.

  7. #27
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Hint- The Magic Squares program linked in your assignment does most of this already.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default

    I don't really understand the Magic squares program tho. I've just finished assigning r to ' '. I have to do the same with z.. Can you help me with moving the letters around?

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

    Default

    If you're hard-coding based on character values, you're doing it wrong. In other words, you shouldn't be doing anything specifically with 'r' or with 'z'.

    If you don't understand what the Magic Squares example is doing, you're going to be quite lost. Work with that code some more.

    -Gary-

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

    Default

    Oh god, I might have to start all over. :[

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

    Default

    Hint: You'll need to write a boolean moveValid(char letter) method, which will be called from your void moveLetter(char letter) method. Both of these methods will be long, ugly if blocks. Pseudocode for moveLetter():
    Java Code:
        if moveValid
            if letter is at s0
                store ' ' at s0
            else if ' ' is at s0
                store letter at s0
            if letter is at s1
                store ' ' at s1
            else if ' ' is at s1
                store letter at s1
            ...
        else
            print invalid move message
    Last edited by gcalvin; 02-24-2011 at 10:23 PM. Reason: Italics were wrong

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

    Default

    How Would I write thee move valid script? Will it have to check if the surrounding 4 squares are next to each other?

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

    Default

    I'm sorry, special handling for 'r' does seem to be OK. I hadn't seen that 'r' is supposed to re-randomize the board. I don't see anything special about 'z' though.

    -Gary-

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

    Default

    Quote Originally Posted by jenxin View Post
    How Would I write thee move valid script? Will it have to check if the surrounding 4 squares are next to each other?
    It has to figure out which square holds the letter, and then check if one of the surrounding four squares has a space. I don't think there's a clever way to do this -- you have to hard-code if statements for each of your 25 variables. (I suppose the clever thing would be to write a program that writes the program you need, but I'm not going to recommend that.)

    -Gary-

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

    Default

    Ok now, i'm lost. lol. I got the script kind of working.. as in moving the letters works fine, but is there no way to check the blocks around it? If i do have to hard code it, can you help me start it off like the earlier method? Thanks This is due tomorrow morning :[.

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

    Default

    Well, if the letter's in variable s0, which variables are valid for the space? How about if the letter's in s1?
    Java Code:
            private boolean moveValid(char letter) {
                boolean result = false;
                // do your checking here, and set result to true if appropriate
                return result;
            }
    -Gary-

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

    Default

    Thanks a lot, but the blank square is always random, so how can I base it off that then?

  18. #38
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    I haven't read the entire thread so apologies if not upto speed.

    You declare 25 char variables all with an initial value of a space. Your instructions even say that. Then have a loop that iterates 24 times. Each time around generate a random number which represents which variable you will change. If that variable is a space assign it a (then b, then c etc). At the end you will have one variable that is still a space. Note only increment your loop variable if you actually change a variable.

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

    Default

    I've finished the random letters part, now I am onto moving the pieces. Gary, I didn't use the method you supplied, cause I didn't really understand it;

    instead what i've done is:
    Java Code:
    private void moveValid(char d) {
            if (' '==s1 && (d == s2 || d == s6)){
            	moveLetter(d);
    // and so on.}
    It doesn't work correctly tho..

    my method for moveLetter is
    Java Code:
    private void moveLetter(char c) {
        	if (c==s1){
        		s1=' ';
        	}
        	else if(' '==s1){
        		s1=c;
        	}
        	if (c==s2){
        		s2=' ';
        	}
        	else if(' '==s2){
        		s2=c;
        	//and so on}

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

    Default

    Have you written setValueAtSquare() or getValueAtSquare() yet?

    -Gary-

Page 2 of 6 FirstFirst 1234 ... 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
  •