board[8][8] index out of bounds exception

Mattias

Distinguished
Jul 1, 2001
4
0
18,510
Archived from groups: rec.games.chess.computer (More info?)

Hi

I would like to write my own chess-program. Reason number 1: it is
very intresting and I wanted to do it for years. 2: I would like to
see if I can write a chess program that plays better chess than me
(that should not be so hard 🙂

Anyway, I like the 8 by 8 board representation. It is simple to
understand. However homepages like
http://www.cis.uab.edu/info/faculty/hyatt/boardrep.html tells me that
8 by 8 is the sigle most stupid design decision a chess programmer
ever could make. They claim that there must be a lot of if-statmets to
check that I don't move a piece out of the board. But what if I write
my program in Java and just use the IndexOutOfBoundsException? Would
that really be such a stupid thing to do?

I'd like to here what expert Tom Kerrigan and others have to say about
this.

You are smart, you figure my anti-spam e-mail out 🙂

// Mattias
 
Archived from groups: rec.games.chess.computer (More info?)

"Mattias" <mattias_at_freefarm_se@yahoo.com> schreef in bericht
news:7795d24e.0503220758.42c5dab7@posting.google.com...
> Hi
>
> I would like to write my own chess-program. Reason number 1: it is
> very intresting and I wanted to do it for years. 2: I would like to
> see if I can write a chess program that plays better chess than me
> (that should not be so hard 🙂
>
> Anyway, I like the 8 by 8 board representation. It is simple to
> understand. However homepages like
> http://www.cis.uab.edu/info/faculty/hyatt/boardrep.html tells me that
> 8 by 8 is the sigle most stupid design decision a chess programmer
> ever could make. They claim that there must be a lot of if-statmets to
> check that I don't move a piece out of the board. But what if I write
> my program in Java and just use the IndexOutOfBoundsException? Would
> that really be such a stupid thing to do?
>
> I'd like to here what expert Tom Kerrigan and others have to say about
> this.
>
> You are smart, you figure my anti-spam e-mail out 🙂
>
> // Mattias

Depends how java checks if an array is out of bounds. That would be every
time you access an array item it will check if it is in bounds, just like
you yourself would do if you wouldn't use java but c or something. But you
might try IndexOutOfBoundsException against your own handler and compare the
results... Maybe java is super efficient

greetings Bert
 
Archived from groups: rec.games.chess.computer (More info?)

Mattias wrote:
> Hi
>
> I would like to write my own chess-program. Reason number 1: it is
> very intresting and I wanted to do it for years. 2: I would like to
> see if I can write a chess program that plays better chess than me
> (that should not be so hard 🙂
>
> Anyway, I like the 8 by 8 board representation. It is simple to
> understand. However homepages like
> http://www.cis.uab.edu/info/faculty/hyatt/boardrep.html tells me that
> 8 by 8 is the sigle most stupid design decision a chess programmer
> ever could make. They claim that there must be a lot of if-statmets to
> check that I don't move a piece out of the board. But what if I write
> my program in Java and just use the IndexOutOfBoundsException? Would
> that really be such a stupid thing to do?
>
> I'd like to here what expert Tom Kerrigan and others have to say about
> this.
>
> You are smart, you figure my anti-spam e-mail out 🙂
>
> // Mattias
I can't program in Java, but my guess is that handling of exceptions
produces an even greater overhad than a few if statements do.

--
Peter Pein
Berlin
 
Archived from groups: rec.games.chess.computer (More info?)

"Mattias" <mattias_at_freefarm_se@yahoo.com> wrote in message
news:7795d24e.0503220758.42c5dab7@posting.google.com...
> Hi
>
> I would like to write my own chess-program. Reason number 1: it is
> very intresting and I wanted to do it for years. 2: I would like to
> see if I can write a chess program that plays better chess than me
> (that should not be so hard 🙂
>
> Anyway, I like the 8 by 8 board representation. It is simple to
> understand. However homepages like
> http://www.cis.uab.edu/info/faculty/hyatt/boardrep.html tells me that
> 8 by 8 is the sigle most stupid design decision a chess programmer
> ever could make. They claim that there must be a lot of if-statmets to
> check that I don't move a piece out of the board. But what if I write
> my program in Java and just use the IndexOutOfBoundsException? Would
> that really be such a stupid thing to do?
>
> I'd like to here what expert Tom Kerrigan and others have to say about
> this.
>
> You are smart, you figure my anti-spam e-mail out 🙂
>
> // Mattias

I'm not an expert. I used a 10x10 board to make things easy for knight
jumps. It's easy to test. The other pieces work OK with 8x8 and don't
require that test, but must coexist on the same chessboard as the knights (I
suppose)!? Can you really get enough speed with java? I'd only consider
assembly for the time-critical subroutines.
--
.... ;o)
 
Archived from groups: rec.games.chess.computer (More info?)

"Mattias" <mattias_at_freefarm_se@yahoo.com> wrote in message
news:7795d24e.0503220758.42c5dab7@posting.google.com...
> Hi
>
> I would like to write my own chess-program. Reason number 1: it is
> very intresting and I wanted to do it for years. 2: I would like to
> see if I can write a chess program that plays better chess than me
> (that should not be so hard 🙂
>
> Anyway, I like the 8 by 8 board representation. It is simple to
> understand. However homepages like
> http://www.cis.uab.edu/info/faculty/hyatt/boardrep.html tells me that
> 8 by 8 is the sigle most stupid design decision a chess programmer
> ever could make. They claim that there must be a lot of if-statmets to
> check that I don't move a piece out of the board. But what if I write
> my program in Java and just use the IndexOutOfBoundsException? Would
> that really be such a stupid thing to do?

Using exceptions doesn't seem very practical.

Most of the basic programs I've seen use an array of byes [0..77] so that
any negative numbers or numbers greater than 77 or numbers ending in 8 or 9
are off the edge of the board.


Mike Leahy
"The Database Man!"
www.bookup.com
 
Archived from groups: rec.games.chess.computer (More info?)

Mattias wrote:
> They claim that there must be a lot of if-statmets to
> check that I don't move a piece out of the board. But what if I write
> my program in Java and just use the IndexOutOfBoundsException? Would
> that really be such a stupid thing to do?

In my opinion, yes.

First there is the design, or symantic issue. Exceptions are meant for
exceptional situations. If you don't expect there to be an out of
bounds exception and it happens anyway exceptions help you deal with
that. But using exceptions to check for expected things is just bad
coding style.

Second, exceptions are slower than ifs, there is all sorts of stack
allocation/deallocation that goes on when one is triggered. They are
faster when you are NOT expecting the exception to happen because
exceptions are not run unless it does. But if you are using exceptions
to catch expected things that are going to happen on a regular basis,
like pieces trying to move off the board, your code is going to be much
slower than if you used if statements.

Fact is that there are only 4 possible if cases you have to worry
about. If i < 0 or > 7, or j < 0 or > 7. You probably already have a
lot more than 4 ifs in you move generation anyway. You cut these 4 out
through different board representations, and it might be faster, but
using exceptions will just slow you down...and it's bad style to use
them like that anyway.
 
Archived from groups: rec.games.chess.computer (More info?)

"Mattias" <mattias_at_freefarm_se@yahoo.com> wrote in message
news:7795d24e.0503220758.42c5dab7@posting.google.com...
> Anyway, I like the 8 by 8 board representation. It is simple to
> vnderstand. However homepages like
> http://www.cis.vab.edv/info/facvlty/hyatt/boardrep.html tells me that
> 8 by 8 is the sigle most stvpid design decision a chess programmer
> ever covld make. They claim that there mvst be a lot of if-statmets to
> check that I don't move a piece ovt of the board.

This is trve if yov make straightforward implementation with the 8x8 board.
Bvt yov can keep envmeration of all possible moves for piece, which target
position _inside_ the board. Yov need svch arrays for king, rook, bishop,
knight, white pawn, black pawn. Qveen moves are vnion of rook moves and
bishop moves. It takes 64x8 bytes for king, 64x8x4 bytes for rook, the same
for bishop, 64x8 bytes for knight, and 64x4x2 (forward, dovble, left attack,
right attack) bytes for pawns. As a resvlt 5632 bytes.

> Bvt what if I write
> my program in Java and jvst vse the IndexOvtOfBovndsException? Wovld
> that really be svch a stvpid thing to do?
> // Mattias

Exception handling in Java is extremely slow featvre - it is considered to
be vsed for really exceptional program execvtion, when time is not important
at all. Don't vse it as regvlar programming techniqve, if yov want some kind
of performance from Java. Moreover 2 dimention arrays 8x8 are slower than
one-dimention arrays with size 64.
 
Archived from groups: rec.games.chess.computer (More info?)

Mattias <mattias_at_freefarm_se@yahoo.com> wrote:
> Anyway, I like the 8 by 8 board representation. It is simple to
> understand. However homepages like
> http://www.cis.uab.edu/info/faculty/hyatt/boardrep.html tells me that
> 8 by 8 is the sigle most stupid design decision a chess programmer
> ever could make. They claim that there must be a lot of if-statmets to
> check that I don't move a piece out of the board. But what if I write
> my program in Java and just use the IndexOutOfBoundsException? Would
> that really be such a stupid thing to do?

Exceptions will be dramatically slower because you'll be generating lots
of them. Try both routines and see which is faster.

In some cases, though, exceptions are much faster. A few years ago, I set
group of students a small project (just a week's work) to write a Java
program to calculate the prime numbers less than a million as quickly as
possible. The obvious way of doing this is to use the sieve of Eratos-
thenes and only consider odd numbers. The following code fragments will
cross off all multiples of three (modulo the usual crop of off-by-one
errors):

for (i = 4; i < 500000; i+=3)
isprime = false;

i = 4;
try {
while (true) {
isprime = false;
i += 3;
}
}
catch (/*ArrayIndexOutOfBounds*/Exception e) {}

The second is quite a bit faster. Each time round the loop, the first
code does an increment, two tests that i < 500,000 (once for the loop
bound, once for the array bound) and an assignment; the second only does
one test on i each time and catching an exception is a lot faster than
doing 150,000 tests. When I last ran the test, the Exception-based code
was about 15% faster than the code without. My code for this is at

http://www.cl.cam.ac.uk/~dmr25/superv/fthrjava/primes/PrimesOdd.java

The obvious difference between this loop and the loop to generate knight
moves is that each if statement can save you two potential exceptions
whereas each exception sieve code can save up to 150,000 if statements.


Dave.

--
David Richerby Slimy Carnivorous Gnome (TM): it's
www.chiark.greenend.org.uk/~davidr/ like a smiling garden ornament but
it's full of teeth and covered in goo!