Page 3 of 6 FirstFirst 12345 ... LastLast
Results 41 to 60 of 111
  1. #41
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    No i believe i hard coded it for those. I'm trying to just hard code the entire way.. :|

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

    Default

    I know it feels like time wasted, but it isn't. Write void setValueAtSquare(int square, char value), char getValueAtSquare(int square), and write a int getSquareWithValue(char value) method for good measure. You still need to be able to validate moves, but you could write it as boolean moveValid(int letterSquare, int spaceSquare) instead of what I suggested earlier. Doing it that way would allow you to do it as a switch instead of if/else.

    -Gary-

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

    Default

    how do I choose the square if the squares are char's tho? My brain is has been exhausted.. =[

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

    Default

    I'm pretty sure I have it almost done, i'm just wondering why the check script isn't working correctly. I believe its the way I typed it..

  5. #45
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Squares aren't chars. Squares are your 25 variables, and you can have a one-to-one correspondence with ints by using switch statements. Squares hold chars. The Magic Squares example demonstrates how to do the set and get methods. You have to do some thinking to write getSquareWithValue() but it's pretty simple.

    -Gary-

  6. #46
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by jenxin View Post
    I'm pretty sure I have it almost done, i'm just wondering why the check script isn't working correctly. I believe its the way I typed it..
    The moveValid() method you posted only seems to be looking at one space on the board. You need to consider every square on the board.

    -Gary-

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

    Default

    Here's my moveValid method..
    Some squares work normally while some don't and some diagonal squares are also able to be moved.
    Java Code:
        private void moveValid(char d) {
            if (s1==d && (d == s2 || d == s6)){
            	moveLetter(d);}
            	
            if (s2==' ' && (d == s1 || d == s3 || d == s7)){
            	moveLetter(d);}
            	
            if (s3==' ' && (d == s2 || d == s4 || d == s8)){
            	moveLetter(d);}
            
            if (s4==' ' && (d == s3 || d == s5 || d == s9)){
            	moveLetter(d);}
            	
            if (s5==' ' && d == s4 || d == s10){
            	moveLetter(d);}
            	
            if (s6==' ' && (d == s1 || d == s7 || d == s11)){
            	moveLetter(d);}
            	
            if (s7==' ' && d == s2 || d == s6 || d == s8 || d == s12){
            	moveLetter(d);}
            	
            if (s8==' ' && (d == s3 || d == s7 || d == s9 || d == s13)){
            	moveLetter(d);}
            	
            if (s9==' ' && (d == s4 || d == s8 || d == s10 || d == s14)){
            	moveLetter(d);}
    
            if (s10==' ' && (d == s5 || d == s9 || d == s15)){
            	moveLetter(d);}
            	
            if (s11==' ' && (d == s6 || d == s12 || d == s16)){
            	moveLetter(d);}
            	
            if (s12==' ' && (d == s7 || d == s11 || d == s13 || d == s17)){
            	moveLetter(d);}
            	
            if (s13==' ' && (d == s8 || d == s12 || d == s14 || d == s18)){
            	moveLetter(d);}
            	
            if (s14==' ' && (d == s13 || d == s9 || d == s15 || d == s19)){
            	moveLetter(d);}
            	
            if (s15==' ' && (d == s10 || d == s14 || d == s20)){
            	moveLetter(d);}
            	
            if (s16==' ' && (d == s11 || d == s17 || d == s21)){
            	moveLetter(d);}
            	
            if (s17==' ' && (d == s12 || d == s16 || d == s18 || d == s22)){
            	moveLetter(d);}
            	
            if (s18==' ' && (d == s13 || d == s17 || d == s19 || d == s23)){
            	moveLetter(d);}
            	
            if (s19==' ' && (d == s14 || d == s18 || d == s20 || d == s24)){
            	moveLetter(d);}
            	
            if (s20==' ' && (d == s15 || d == s19 || d == s25)){
            	moveLetter(d);}
            	
            if (s21==' ' && (d == s16 || d == s22)){
            	moveLetter(d);}
            	
            if (s22==' ' && (d == s21 || d == s17 || d == s23)){
            	moveLetter(d);}
            	
            if (s23==' ' && (d == s22 || d == s18 || d == s24)){
            	moveLetter(d);}
            	
            if (s24==' ' && (d == s23 || d == s19 || d == s25)){
            	moveLetter(d);}
            	
            if (s25==' ' && (d == s20 || d == s19)){
            	moveLetter(d);}
            	
            
            		
            }

  8. #48
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    moveValid() needs to be a boolean, not a void. You need to check if a move is valid, and print a message if it isn't.

    Think like a computer scientist. Your variables should be s0 - s24, not s1 - s25.

    You've clearly got a problem here:
    Java Code:
            if (s1==d && (d == s2 || d == s6)){
            	moveLetter(d);}
    Again, if you implement the methods I advised you to, you can do this one as a pretty simple switch statement.

    -Gary-

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

    Default

    I'll try to,
    can you clarify on what your methods do?
    void setValueAtSquare(int square, char value)?
    char getValueAtSquare(int square)?
    getSquareWithValue(char value)?

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

    Default

    setValueAtSquare() and getValueAtSquare() are pretty much exactly like the ones in the Magic Squares example. getSquareWithValue() does what you would expect. If you have a letter 'm' in variable s18, then getSquareWithValue('m') will return 18. If there's a space in variable s9 then getSquareWithValue(' ') will return 9.

    -Gary-

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

    Default

    By the way, I still think this entire exercise is a perversion. You're forbidden from using arrays, but then encouraged to develop methods that sort of let you treat your set of discrete variables as if they are an array. I believe the whole thing is to make you say "wow! that's much better!" when you're finally allowed to use arrays.

    -Gary-

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

    Default

    Haha.. arrays will be my life saver. Thanks I'll let you know what I did.

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

    Default

    I'm still stuck. I'm just not good with methods. :[

    I think I wrote the getvalueAtsquare,
    bunch of if statements, but if i type a and that is in square 17

    it will set returnValue = 17; is that right?

    What should I do now?(brain dead)

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

    Default

    "I'm just not good with methods"? No, sorry, that's not an acceptable attitude. It's totally fine to be a beginner, and we all have things to learn, but you can't study Java and avoid methods. You're going to get good with methods and you're going to start now.

    getValueAtSquare() does what it says. It takes a square number (an int) and returns a value (a char). getSquareWithValue() does the opposite. It takes a value (a char) and returns the number of the square that holds that value (an int). The conditions of the assignment guarantee that each square will hold a unique value.

    Post your code for these methods and we'll work from there.

    -Gary-

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

    Default

    >__> I'll be embarrassed, it's.. really long, and I didn't follow the magic thingy.. but i'm really lost, and i'd love to learn methods instantly, but this is due tomorrow lol!

    It's too long i believe, i'll put it in a .java file.
    Attached Files Attached Files

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

    Default

    Why didn't you look at the method in the Magic Squares example? I know you're tired and you're stressed, but you're making this so much harder for yourself.
    Java Code:
        //---------------------------------------------------------------------------------------
        // getBoardValueAtSquare
        //    Return the board variable value corresponding to "index".
        int getBoardValueAtSquare( int index)
        {
        	int returnValue = 0;
        	
        	// find the value corresponding to the index
        	switch( index) {
    	    	case 1: returnValue =  p1; break;
    	    	case 2: returnValue =  p2; break;
    	    	case 3: returnValue =  p3; break;
    	    	case 4: returnValue =  p4; break;
    	    	case 5: returnValue =  p5; break;
    	    	case 6: returnValue =  p6; break;
    	    	case 7: returnValue =  p7; break;
    	    	case 8: returnValue =  p8; break;
    	    	case 9: returnValue =  p9; break;
    	    	// Sanity check, should never get to the code below
    	    	default: System.out.println("*** Invalid index value of: " + index +
        								" in getBoardValueAtSquare(). " +
        							    " Exiting program...");
        			 System.exit( -1);	// exit program
        	}//end switch( index...
        	
        	return returnValue;
        }//end getBoardValueAtSquare()
    Java Code:
        char getValueAtSquare(int square) {
            char result = '*'; // just a placeholder
            switch(square) {
            case 0: result = s0; break;
            case 1: result = s1; break;
            ...
            case 24: result = s24; break;
        	// Sanity check, should never get to the code below
        	default: System.out.println("*** Invalid square value of: " + square +
        					" in getValueAtSquare(). " +
        					" Exiting program...");
        		System.exit( -1);	// exit program
            }
            return result;
        }
    You see? Much simpler than what you've written. Try again.

    -Gary-

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

    Default

    Thing is I checked, but truth is i don't know how to write that. I tried the index thing, but I couldn't figure out what it was referring to, since these are chars.
    Does the code that I wrote work? I got the get position of letter script finished up there..
    Can you explain what you meant with the Boolean check move method?

  18. #58
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, I kind of don't blame you for some of that confusion, because like I said at first, this seems like an exercise designed to induce brain damage. But you understand that you have 25 squares, right? And that you can number them, 0-24 (or 1-25, but start thinking like a computer scientist and get in the habit of starting at zero instead of one)? So "index" is just another way of saying "square number".

    The Magic Squares example is storing ints in each variable, so its getBoardValueAtSquare() method returns an int. You're storing chars in your variables, so your getValueAtSquare() method will return a char. So all you need to do is use the square number to look up the char value from one of your 25 variables. If the square number is 0, you return the char value in s0. If the square number is 5, you return the char value in s5. If the square number is 12 you return the char value in s12. You don't need to look at what the char value is, or compare it to anything -- you just return it. Does that make sense?

    -Gary-

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

    Default

    Ok so I think I got what you meant, but what will call upon this script? (not sure if i'm using the correct words)

    Like I wrote the script, here it is..

    Java Code:
     int getBoardValueAtSquare( int index)
        {
        	int returnValue = 0;
        	
        	// find the value corresponding to the index
        	switch( index) {
    	    	case 1: returnValue =  s1; break;
    	    	case 2: returnValue =  s2; break;
    	    	case 3: returnValue =  s3; break;
    	    	case 4: returnValue =  s4; break;
    	    	case 5: returnValue =  s5; break;
    	    	case 6: returnValue =  s6; break;
    	    	case 7: returnValue =  s7; break;
    	    	case 8: returnValue =  s8; break;
    	    	case 9: returnValue =  s9; break;
    	    	case 10: returnValue =  s10; break;
    	    	case 11: returnValue =  s11; break;
    	    	case 12: returnValue =  s12; break;
    	    	case 13: returnValue =  s13; break;
    	    	case 14: returnValue =  s14; break;
    	    	case 15: returnValue =  s15; break;
    	    	case 16: returnValue =  s16; break;
    	    	case 17: returnValue =  s17; break;
    	    	case 18: returnValue =  s18; break;
    	    	case 19: returnValue =  s19; break;
    	    	case 20: returnValue =  s20; break;
    	    	case 21: returnValue =  s21; break;
    	    	case 22: returnValue =  s22; break;
    	    	case 23: returnValue =  s23; break;
    	    	case 24: returnValue =  s24; break;
    	    	case 25: returnValue =  s25; break;
    	    	// Sanity check, should never get to the code below
    	    	default: System.out.println("*** Invalid index value of: " + d +
        								" in getBoardValueAtSquare(). " +
        							    " Exiting program...");
        			 System.exit( -1);	// exit program
        	}//end switch( index...
        	
        	return returnValue;
        }//end getBoardValueAtSquare()

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

    Default

    You mean "what will call this method?" We'll cross that bridge very soon. First show me code for the other two methods: void setValueAtSquare(int square, char value), and int getSquareWithValue(char value).

    -Gary-

Page 3 of 6 FirstFirst 12345 ... LastLast

Similar Threads

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