wood burning stoves 2.0*
The moose likes Programming Diversions and the fly likes writing logic for chess game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "writing logic for chess game" Watch "writing logic for chess game" New topic
Author

writing logic for chess game

satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
If one wanted to approach writing a chess game what would be the considerations .

Most rudimentary ( and probably impossible approach )
To take a stab ::

First move by white consists of following possibilities ::
16 Pawn moves
4 Knight moves ( 2 possible moves for each Knight)

So total possibilities = 20 for the first move by white

Now for each move the possibilities grow exponentially .

Obviously this is not the way to go .
Also rather than just brute force - there is also the need to make the best possible move

Just curious - how are such programs written ?

what is the approach used ?

how is the inteligence of choosing the best move written ?

Thanks ,
~satish
[ October 09, 2008: Message edited by: satish bodas ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
This is a huge topic, but some common ideas are:

1 - You create a method that looks at any given position and makes an evaluation about how "good" or "bad" that position is. This method is often called a "static evaluation function" (SEF),because it looks only at a static position.

2 - You create a "Search tree" much as you discussed. In your example the beginning position would be the base or "root" of the tree, and you would have 20 white moves branching off of the root. For each of those 20 branches you would have 20 black branches, for each of those ~20-40 white branches, then black, then white, and so on. (Each set of branches is called a "ply".) Because computers are not infinitely fast and do not have infinite memory, you have to choose an arbitrary maximum number of plies to create and evaluate.

3 - You apply your static evaluation function to each of the positions you created and you assign each spot or "node" on the tree a value.

4 - There are various "minimum - maximum" algorithms that you can use to review the search tree's values and pick the best possible next move.

There are various ways to limit the size of the tree as you build it (called "pruning"), and of course creating a good SEF is a very difficult task. Typically your SEF would consider things like how many pieces each player has, how much each player controls the center of the board, how many total moves each player can make, how safe is each king, and so on.

This overall approach can be used for Chess, Checkers, Go, Backgammon, Othello, Connect-4, Pente, Go-Moku, and on and on.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Thanks a bunch Bert for giving an insight

I dont know how much I can achieve - but will start giving it a thought and then some shot !



Thanks ,
~satish
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: writing logic for chess game