aspose file tools*
The moose likes Game Development and the fly likes Function not returning values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Function not returning values." Watch "Function not returning values." New topic
Author

Function not returning values.

Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125


This is a method which is written for returning proper values,according to conditions.
here the variable score is for seeing three in a row.

With this code,even if I allow computer to get three in a row, it does not play that move.
but instead if I write this way, it works.



Why does this happen?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

put System.out.println() statements throughout your method, and then capture them to a file. Your code is probably not doing what you think it is. By using print statements, you can see what it's doing.

At the very least, put on in right before your final if-else, to see what eval really is.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I'm assuming this is still for Connect 4: Is there any code missing here as the supplied code isn't checking for all the variations of 3 in a row - remember there are 4 possible directions for a line and that the hole in the line of 4 can be anywhere on the line. Also your final check is on the variable eval which isn't set anywhere within the loops - wouldn't it be better to check for eval == 1000 before doing the loops as this will be faster?


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@Mich.

Yes this is still for connect 4.
and yes the some code is missing as well.
Here is the entire code.
Code
I am trying this step by step so that I create less bugs(one day I will reach to a stage where I create no bugs!(dream...)).
I tried with following move order- 4-4 4-4 4-4 5-3 (Now it should play 6 because it gets 3 in the bottom row, but it plays 5 returning the value 0 instead of 999.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Looking at your code (lines 90-113). It checks for lines of 3 pieces where the hole is either at the start or at the end of the line. It doesn't check for holes in the middle which is what's happening in the game you supplied. I suggest you rewrite the code to look for holes anywhere in the line and I'd use a more data driven approach rather than hard coding everything.

+++X+++
+++O+++
+++X+++
+++O+++
+++X+++
++XO+O+

Mike
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@Mich
I think you have misread the post and my game as well.
The game is 4-4 4-4 4-4 5-3


so now reply should be


but it plays in 5.

but I have alreaded wrote condtion for YYY_ which is applicable here,then why is it not following?
and what you mean by data driven program?
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Gurudas Bhandarkar wrote:I think you have misread the post and my game as well.
The game is 4-4 4-4 4-4 5-3
...
what you mean by data driven program?

I just went quickly through your code and reported the first bug I found that seemed related to your issue. The bug seemed serious because it misses half the situations that you're trying to cover.

Data driving your code means controlling the action of your program through data rather than through hundreds of if statements. In this particular case you have loads of lines of code that check for lines of 3 in each direction and then again for each different position a hole can be found in - in your case half the checks are missing so the code would be twice as long if it was complete. Better would be to have a small loop for each of the different directions and simply count the pieces from each square and out in each direction. If there's 3 of your pieces and a gap on that line then you get the bonus score.

You should also read up on how to debug code - the general idea is to print out debugging info that helps you work out what's wrong. You won't learn much by dumping your program on a forum and asking others to debug your code for you and you certainly won't get much respect if you then point out your helpers mistakes An added benefit of data driving the above code would be a much smaller amount of code which would then be easier to debug.

Mike
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
Data driving your code means controlling the action of your program through data rather than through hundreds of if statements. In this particular case you have loads of lines of code that check for lines of 3 in each direction and then again for each different position a hole can be found in - in your case half the checks are missing so the code would be twice as long if it was complete. Better would be to have a small loop for each of the different directions and simply count the pieces from each square and out in each direction. If there's 3 of your pieces and a gap on that line then you get the bonus score.


I was reading on that so now I made some provision of checking in all possible directions.

You should also read up on how to debug code - the general idea is to print out debugging info that helps you work out what's wrong. You won't learn much by dumping your program on a forum and asking others to debug your code for you


Well again I think you are not getting the point. I posted (well 'dump' is not a correct word here,it shows the attitude that you have towards beginner programmers.) the code in this forum after trying a lot from my side,but failed every time.So I thought of asking whether I am making mistake in approach of coding(I am new to game programing and its easy to make mistakes in recursion).
I have never asked any one to debug my code, but to just let me know why was the code behaving in such a way,(a piece of advice for you, please read the posts carefully.),even after putting print statements, I was unable to see the reason.

and you certainly won't get much respect if you then point out your helpers mistakes

Obviously If 'helpers' try to help some one without actually bothered to read the question, then the 'helper' is not being helpful.
So its important to clarify and make it clear as what the person is really thinking of. But for that, the helper should read the post carefully


An added benefit of data driving the above code would be a much smaller amount of code which would then be easier to debug.

Ya, I can see that now, thanks for this point.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I simply get frustrated pointing out the same issues in your code each time and seeing very little effort on your part to fix anything. In fact many of the issues I pointed out on the 5 April in this thread are still issues in your code. There is also no obvious attempt at debugging in your code and I feel that even a minimum amount would highlight where your program has gone wrong.

I think I'll just leave you in Fred's capable hands.

Mike
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@Mich
I simply get frustrated pointing out the same issues in your code each time and seeing very little effort on your part to fix anything. In fact many of the issues



Well in that case, You are completely wrong, I try my best but then too if I am unable to fix it up then I post it here.
I pointed out on the 5 April in this thread are still issues in your code. There is also no obvious attempt at debugging in your code and I feel that even a minimum amount would highlight where your program has gone wrong.


In that post it was about negamax and not of evaluations. I fixed it up long time before.

I think I'll just leave you in Fred's capable hands.

Fred was a very helpful guy.
If given a chance,I still want to work with Fred,only difference is now I can devote more time to programing and also can experiment with my code so that only final things I verify with Fred.

I don't know what conversation has taken place between you and fred and what has he told about me.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
The following points (made on the 5th April) relate to the code you've just posted:

  • Without a border around your board you'll find it difficult to write generic code and instead be writing confusing code that needs to search for the edges of the board using special cases rather than just using the data.
  • You have no debugging code in your program - I'm guessing you're just assuming the program is playing correctly?

  • I only commented about the debugging again because there is no debugging in your code. If there had been debugging and it didn't spot any issues then you need to improve how your debugging skills before progressing. No conversation has taken place between Fred and I, I was simply pointing out that I was out of here.
    Kabir Shah
    Ranch Hand

    Joined: Aug 04, 2009
    Posts: 125
    @mich

    what exactly is a border?
    I have made an array called as x and y.
    It contains parameters which will help me search in all 8 directions.


    I wrote print statements where ever I think its going to be confusing,typically the alpha and beta values.
    I remove the print statements before posting the code here.so that the length decreases and people can draw their own conclusion.
    Basically the length decreases dramatically. and it helps to understand the code better to people over here. when they cannot compile and see the output themselves.


    # You have no debugging code in your program - I'm guessing you're just assuming the program is playing correctly?

    I only commented about the debugging again because there is no debugging in your code. If there had been debugging and it didn't spot any issues then you need to improve how your debugging skills before progressing.


    I guess by debugging code you mean writing print statements, my program works correct as of now.
    You too tried to play against my applet, though the applet is still in premature state, I am pretty sure it followed basic criteria of game i.e connect4 or stop opponent from connecting 4. so I guess my program is playing correctly.

    Am I missing something?

    paul wheaton
    Trailboss

    Joined: Dec 14, 1998
    Posts: 20582
        ∞

    Gurudas,

    I think that it is good to want things. You can want people to come back with the perfect words to make everything clear in your head. There is nothing wrong with wanting that.

    However, if you expect those things from a stranger that is trying to help you ... well, I think you are going to end up frustrated and sad. And I think you will end up making those around you frustrated too.

    At one point you said "helper should read the post carefully". This is my site. Please don't tell people how to behave on my site.

    Mich,

    Thanks for trying to help somebody. And when that somebody traveled an awkward path, thanks for tying to make things smoother rather than worse.

    --------

    If y'all have further questions about proper etiquette on JavaRanch, please take it up in the ranch office forum.



    permaculture Wood Burning Stoves 2.0 - 4-DVD set
    Kabir Shah
    Ranch Hand

    Joined: Aug 04, 2009
    Posts: 125
    .
    paul wheaton
    Trailboss

    Joined: Dec 14, 1998
    Posts: 20582
        ∞

    Gurudas,

    You don't follow instructions very well do you. Are you having a hard time finding the ranch office forum?

    I'll repeat what I said:

    If y'all have further questions about proper etiquette on JavaRanch, please take it up in the ranch office forum.


    If you have more to say about me, or mich or how things work here, or what is appropriate or what is less than appropriate, you really need to take it up there.

    I think you need to follow your own advice and "read the post carefully".

    And finally, I think it is fair to say that you are still struggling with a problem. And, it is entirely unfair to suggest that anybody on JavaRanch is anything less than perfect. Such as they are not doing things they way you want them to do things. I don't give a damn how many times you say what you are or are not doing: I'll be the judge, jury, and, if need be, the executioner. There is only one rule on this site: be nice. And I am the guy that decides what that means. And right now, I think I would like to see you do a lot more apologizing, and no more cheap excuses to justify your poor behavior.

    Enough of this. If you have anything more to say, take it up in the ranch office forum.




    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11313
        
      16

    back to the actual thread...

    I honestly can't tell if you still have a problem and need help, or if it's been resolved. Can you clarify if you do need help, where and what exactly?
    Ashish Schottky
    Ranch Hand

    Joined: Dec 29, 2009
    Posts: 93
    @Kabir

    Even I was facing this problem when I was programing this game.
    I can suggest that instead of searching whole board,just search the relevant position.
    I broke the board into individual rows,cols and diagonals.
    There are 6 rows,7 columns and 12 diagonals(6/ and 6\)
    Then you can write another method which will take these arrays as parameters and check whether its three in a row,for both the players.

    Here is roughly what I remember
    initialize an array which will contain values for 3inrow 4inrow.(Interesting part is to fine tune the values to get best results.)
    eval method

    eval += CheckLine(row, number);
    eval += CheckLine(col, number);
    eval += CheckLine(diagonalLR number);
    eval += CheckLine(diagonalRL, number);

    CheckLine method, accepts the array parameter,number(player)
    and returns a score.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Function not returning values.