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
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