• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array out of bounds

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please supply full details of the bug, otherwise nobody can help.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic