PDA

View Full Version : smart chessboard: data structure and implementation (long)


vurx
03-19-2005, 07:16 PM
This post is not asking how to write the code per se but rather how to organize the data. I am writing a script that reads a chess position from an external text file. The external file contains a number of FEN (http://loiodice.com/chess/cc-fen.sht) strings. These strings are parsed by the script and stored in a two-dimensional array. A function reads the position information, creates a board, and sets up the pieces.

Current structure:
_root.board - is a blank MC that holds the squares and pieces
_root.board.sq## - 64 squares named sq[rank][file]
_root.board.pc## - pieces placed 'on top of' the squares pc[rank][file]

That was the easy part. The 'smart board' part is next.

What does it do?
The board loads a position and presents it to the user. The position is a simple mate-in-one or mate-in-two. The user moves a piece. The board evaluates the position. The board checks to see if the current position is checkmate or not. This requires several steps and is getting messy and complicated, more on this later...

Basic desired functionality:
• Only the color to move is allowed to move pieces.
• The pieces can only make legal moves.
• Essentially, the board must 'know' the state of the game*.
• If checkmate is delivered, the next position is loaded; else, the position is reset.

* i.e. check, check mate, stalemate, en passant squares, castling availability, 50 move rule, 3-fold repetition of position, etc.

So what's the problem?
Move validation is the problem. If you know how to play chess you know there are many rules that must be evaluated before a particular move can be regarded as 'legal.' My dilemma is where and in what form to store this information.

Current method:
I have an array called boardArray. I use it to check to see which squares are occupied when generating the moves for each piece (I am generating moves for all the pieces at once).
I store the square names that the piece can move to into an array and attach the array to the square that the piece is currently on.

One example:
I started thinking about how to detect if a piece is pinned to its king. I can think of one way to do it. Have a function that searches all squares that a queen could move to from the kings square. The function finds enemy long range pieces (bishops,rooks, and queens) and counts/stores the pieces that are between them. If there is only one piece between the king and an enemy long range piece, that piece is 'pinned'. Where/How do I store this information?

All that said, I know 'how' to write the code, that's not the issue. I want to do it correctly and cleanly. Do i need to write it in AS2.0? Classes? What would experienced coders do? I can hack it together, make it work but it will be ugly and inefficient.

Any feedback will be greatly appreciated,
-- Aaron

Xeef
03-19-2005, 09:17 PM
hi

i was thinking on it mayby to make it as a game for my footer (later much later)
the link seams to by what i need (no idea a bouth the math behind it)
not realy looked to the link to not pulute my brain whit it in the moment

here something what i did fast for you hope it gives you some idea

i woud make classes in the end (mayby a main class for the pice and a class for each individual pice which extend the main pice class)

I am generating moves for all the pieces at once
this i not understand 1 turn 1 move !!!

//place a combobox in the library
//create a BOX size (50X50) linkage "Field"
//
//first click on a fild will select it (red) second deselect it
//select the pice in the combobox
//if you can move to the field you rollover it will by green if not blue
//
//no check for pices in the way of movement
//no check for king move in chess // or rochade
//peon special moves not implemented // also not the atack of the peon
//also not Black/White peon
//
import mx.controls.ComboBox;
A = [{label:"peon"}, {label:"bishops"}, {label:"rock"}, {label:"horse"}, {label:"king"}, {label:"quien"}];
createClassObject(ComboBox, "CB", 1, {_y:-200}).dataProvider = A;
x = 0;
Color0 = 0;
Color1 = 0x555555;
for (a=0; a<8; a++) {
x = (x) ? 0 : 1;
for (b=0; b<8; b++) {
O = _root.attachMovie("Field", "F_"+a+"_"+b, _root.getNextHighestDepth(), {_x:50*a, _y:50*b});
var my_color:Color = new Color(O);
O.Row = a;
O.Col = b;
O.Typ = x;
if (x) {
x = 0;
my_color.setRGB(Color1);
} else {
x = 1;
my_color.setRGB(Color2);
}
O.onPress = function() {
if (Selected == this) {
Selected = undefined;
var my_color:Color = new Color(this);
my_color.setRGB(_root["Color"+this.Typ]);
} else {
if (!Selected) {
Selected = this;
var my_color:Color = new Color(this);
my_color.setRGB(0xFF0000);
}
}
};
O.onRollOver = function() {
Check = 0;
if (this<>Selected && Selected) {
switch (_root.CB.selectedItem.label) {
case "peon" :
if (Selected.Col == this.Col+1 && Selected.Row == this.Row) {
Check = 1;
}
break;
case "bishops" :
if (Math.abs(Selected.Col-this.Col) == Math.abs(Selected.Row-this.Row)) {
Check = 1;
}
break;
case "rock" :
if (!(Selected.Row-this.Row) || !(Selected.Col-this.Col)) {
Check = 1;
}
break;
case "horse" :
if (Math.abs(Selected.Row-this.Row) == 2 && Math.abs(Selected.Col-this.Col) == 1) {
Check = 1;
}
if (Math.abs(Selected.Row-this.Row) == 1 && Math.abs(Selected.Col-this.Col) == 2) {
Check = 1;
}
break;
case "king" :
if (Math.abs(Selected.Col-this.Col)<2 && Math.abs(Selected.Row-this.Row)<2) {
Check = 1;
}
break;
case "quien" :
if (!(Selected.Row-this.Row) || !(Selected.Col-this.Col)) {
Check = 1;
}
if (Math.abs(Selected.Col-this.Col) == Math.abs(Selected.Row-this.Row)) {
Check = 1;
}
break;
}
var my_color:Color = new Color(this);
if (Check) {
my_color.setRGB(0x00FF00);
} else {
my_color.setRGB(0x0000FF);
}
}
};
O.onRollOut = function() {
if (this<>Selected) {
var my_color:Color = new Color(this);
my_color.setRGB(_root["Color"+this.Typ]);
}
};
}
}



Hmmm just came in my mind it's easy to create ocupied Fields (key+MousePress) will mayby do it later :D
not wana think abouth the actual logic for computer moves (this woud by to much in the moment)

vurx
03-21-2005, 02:43 AM
Xeef: i dont understand most of what you just said.

im thinking i should make a board class. i'll play around with that. not a whole lot of feedback here...

Xeef
03-21-2005, 10:44 AM
Hmmm

1. to make a chees game is one of the most addvanced things you can programm
-->you woun't get much fead back on this :p
2. you asking how to organize the program struktur this is also one Hmm not even one this is the question of the questions
if some body not already do it (for the particular problem) it take more time to do it then to actualy program the code
i coud give you tips do this do that (where by you always can program one thing in a million diferent ways) and one person woud find this way easyer and an other that
and just god know the best way
you also have to think the hole story from the begin to the end else way it easily can happens that you come to a deadend or make unessary work

to have the structure for a program is 60%(somethime even 95%) of the work
you will find nowhere anccer on this unless somebody is just on to do the same thing
and is friendly enought to help you

but cheess is done a couple of times so there shoud by descriptions or tutorials on this (likely not in flash) google around