File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes array out of bounds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "array out of bounds" Watch "array out of bounds" New topic
Author

array out of bounds

Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
I am writing tictactoe in java, in which I am facing problem
Basically I have put the tic-tac-toe board as a 2-d array. Two different methods for human move and for compute move.Also included a minimax algorithm.. but still cannot do it as error is popping out. Its a console application.



kindly help me with this.
Also if you know how to develop a perfect tictactoe playing AI in a simpler way, please share it.
thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToeOtherClass.computer(Main.java:181)
at TicTacToeOtherClass.main(Main.java:39)


Basically, at line 181, of the main.java file, which isin the computer() method -- you are accessing index 3 of an array (ie. the fourth item) and there is no such element.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

for(int y=0;y<3;y++)

After this loop has ended, y has become 3.

If you indent a bit better you'll see exactly why:
If value > prevValue, an inner loop changes y, and it will become 3.

Also, this code cannot surely work well. You use the same for-loop variables (x and y) in both an outer and an inner loop. The inner loop will modify the behaviour of the outer loop. Instead use fresh new variables:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
Thanks for answering my question.

I have changed the variables of inner loop and replaced with p and m.
but still I get array out of bounds error.
Also is there any free java code for tictactoe(This is not my home-work assignment.. i am student of electronics but Programming is my hobby),so that i can take a look at it and based on those outlines, can program one.
Here is the modified code.



Here is the entire code:
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Index out of bounds gave me fits for a long long time. Happened every time my chess engine tried to move a piece off the board. I don't have any answers for you, this is a logic error on your part, and pure grunt work to solve. Find the line where the error occurred, then do a manual trace backwards from there. Throw in a few print statements to tell you what is going on. Maybe someone else has a better answer.

p.s. it is only in the last few weeks that I got my alphabeta working. What I had before was a working negamax, but it wasn't quite alphabeta. how about your connect 4?
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@fred
good to hear from you. About my connect four, its still the same, without alpha-beta.
so started a new game of tic tac toe. as usual it threw many errors.
If you don't mind and have time, can you just check the code for connect four and let me know my mistakes in alpha-beta pruning?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Gurudas Bhandarkar wrote:@fred
good to hear from you. About my connect four, its still the same, without alpha-beta.
so started a new game of tic tac toe. as usual it threw many errors.
If you don't mind and have time, can you just check the code for connect four and let me know my mistakes in alpha-beta pruning?


Well, personally, I find your code difficult to read. What I will do is offer a few design suggestions that will make your job easier. And also make it easier for others who are looking at yuor code.

#1, clearly separate your alphabeta code from your MoveGeneration code and your evaluation code. Is it clear what I mean by that? If you do that step properly, then it will be much easier to differentiate between alphabeta problems and evaluation problems. Also, as I see it, your alphabeta code should be pretty much independant of the type of game. It should be pretty much plug and play for different type of games. It's the evaluation and move generation functions which will differ from game to game. Again, keep these separate and clearly identifiable. As far as I can see, you haven't done that.

If you'd care to reconfigure your connect four code based on these suggestions, I can take another look.

I ended up using the following page as a guide for my alphabeta I found it quite helpful. Notice how MpveGeneration, alphaBeta, and Evaluate() are clearly separated as different methods.

http://web.archive.org/web/20040403211728/brucemo.com/compchess/programming/index.htm

also, at the website in my sig, you will see a programmer's page that has a link to the Engine class which has complete alphabeta and Evaluate code.

regards.

p.s. please feel free to ask for assistance for some kind of a redesign. Honestly, I think in the long run this approach will serve you better.
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@fred.
Thanks for replying.

I will surely look to redesign it( though it is boring... wen want to add just one more feature to it. )... But then I would also like it to have graphics...
So for that how should I proceed?
Graphics is actually what I want when I re design but its ok even if i am not able to code it.
My first question is how can I code the board in swing/awt (drilling holes in board!)
Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@Fred

i think I will do the connect four redesigning after tictactoe...

For tictactoe, I will implement your strategy. But still there is a minute bug for which I am not able to debug..
I sincerely hope that I am not irritating you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Please supply full details of the bug, otherwise nobody can help.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Gurudas Bhandarkar wrote:@Fred

i think I will do the connect four redesigning after tictactoe...

For tictactoe, I will implement your strategy. But still there is a minute bug for which I am not able to debug..
I sincerely hope that I am not irritating you.


No, not at all I'm not bothered. I'm also not an expert, just someone who has an interest in the subject of recursion and gaming.

And I'm not very good at reading the code of others. There are others here who are far better than I am at that.

But I really think it will help you a lot to develop a modular system for the three main component of game tree analysis that I mentioned in my other post. That will make it easier to adapt your system. For example I know that my alphabeta method can be used more or less interchangeably with other games that I write. As a matter of fact, if you would like to try using my alphaBeta method you are certainly welcome to it.

http://fchess.pastebin.com/f153840b1

Kabir Shah
Ranch Hand

Joined: Aug 04, 2009
Posts: 125
@Fred and Ritche

As Fred pointed out, I have changed those variables and p and m.
More over, I have created copies of board in minimax function( Please check if it is right or some stupid error on my part the goal is simple- to create a empty array and copy the board in empty array, evaluate and once decision is made, erase the board.)


Fred, I really appreciate your help, hope to get good knowledge from you.
I am implementing this first as tictactoe,later i would like to extend as gomuko.(A better version of tictac toe) in which I am planning to add alpha beta pruning.
-Thank you
Hope to hear from you.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
ok I'll take another go at this. Give me a little time. In the mean time, Campbell or another mod, you may wish to move this thread to the gaming forum, as really what we are talking about is a general technique for designing turn based strategy games.

edit: never mind about moving this, I started a new thread in the gaming forum that will take a more general approach.
 
GeeCON Prague 2014
 
subject: array out of bounds