How do I get a count of number of pieces of a specific type on the board in GAME code?
The need for this recently came up while I was programming the code for calculating the original position in Fischer Random Chess. But since I was identifying pieces with binary numbers instead of piece labels, it will have to be adapted. This has worked in tests:
In this function, #p is the parameter you supply to the function, while #0 is the parameter of the lambda function, which corresponds to an element of the spaces array. This function goes through the spaces array, adding each space with the designated piece to the array that the aggregate operator returns. The count operator then returns how many elements are in that array. You may use it like so:
print fn piece-count R;
This shorter function will also work. Note that it will create an array of true values, whereas the one at the top will create an array of coordinates. If you don't need to know where the pieces are, this function will be a bit quicker:
If you do need to know where the pieces are, then it would be better to break the function at top into two steps. Written as a single function for counting, these produce the same results.
The need for this recently came up while I was programming the code for calculating the original position in Fischer Random Chess. But since I was identifying pieces with binary numbers instead of piece labels, it will have to be adapted. This has worked in tests:
In this function, #p is the parameter you supply to the function, while #0 is the parameter of the lambda function, which corresponds to an element of the spaces array. This function goes through the spaces array, adding each space with the designated piece to the array that the aggregate operator returns. The count operator then returns how many elements are in that array. You may use it like so:
This shorter function will also work. Note that it will create an array of true values, whereas the one at the top will create an array of coordinates. If you don't need to know where the pieces are, this function will be a bit quicker:
If you do need to know where the pieces are, then it would be better to break the function at top into two steps. Written as a single function for counting, these produce the same results.