Hi osddeitf
Thanks your reply jc99 but i only want to count directly boolean like this:
(I saw this code in some web that use javascript language)
Square.prototype.countAboveRight = function (propertySelector) {
return propertySelector(board.map[this.row - 1][this.col + 1]) ? 1 : 0;
}
BTW: To me the name of the JS-function is also wrong since it selects something, but doesn't count anything.
And doing the selection of the AboveRight like this is no good code either, even in JS there are much better ways.
"Square.prototype.countAboveRight" like that:
function Square.countAboveRight(propertySelector): byte; //integer etc..
"Square" is a class
"board" is a component like TForm and "map" is an array of Square class
"propertySelector" like an pointer that point to the value of some boolean type in Square class
and "? 1 : 0" is a conversion between boolean and byte like I write on my code converted to Pascal
e.G: Why did you call the function count ? To me a Name like "Select..." would be more appropriate ...
i want to create a puzzle game that contains a board 20x20 and while i click on a cell i want to check very much type of data that contain by it like color : blue, red, yellow, black, ... etc...
i guess that a exists some ways to get value of data like the javascript code that i tell above like a pointer or property.
Find me a way. Thanks very much
The question of the name of your "count"-function still remains. In that function you don't actually COUNT something, you DO a selection of your bits so a name like "SelectBits" would be much more appropriate.
And even more understandable.
for the ?- operator I use a
const
Bool2byte: Array [boolean] Of Byte = (0, 1);
That way you have a fixed conversion from boolean to byte,
(sometimes a boolean is translated to a Integer(-1) that results in byte(255) )
b1: Result:= Bool2Byte(bool1);
I know that you want to count bools, but do you need single bools in TMyClass ?
You could arrange your three bools in an array
like
TMyClass
[...]
arrbool : array [TSurround] of bool;
[...]
So the count/select function could look like this:
function count(_type: TSurround; x, y: byte): byte; inline;
begin
Result := 0; // Default value
if ( x>=0 ) and ( x <= high(MyClassArray) ) and
( y>=0 ) and ( y <= high(MyClassArray[x]) ) then
result := Bool2Byte[MyClassArray[x,y].arrbool[_type]];
end;
[...]
Think also about name of your TSurround.
"For any particular thing, ask What is it in itself? What is its nature?" -- Marcus Aurelius, Meditations (c. 175); -- Hannibal Lecter, The Silence of the Lambs (1991).
An name like TGameColour or TGameTile sounds more like what it is.
Since you want to count blue, red, yellow, black, ... in a specific field and the fields next to it.
Try something like this
type TDirection=(d_itself,d_above,d_right,d_down,d_left,d_aboveright,d_rightdown,d_downleft,d_leftabove);
const DirOffset:array[TDirection] of TPoint =
(( x:0; y:0 ),( x:0; y:-1 ),( x:1; y:0 ),( x:0; y:1 ),( x:-1; y:0 ),( x:1; y:-1 ),( x:1; y:1 ),( x:-1; y:1 ),( x:-1; y:-1 ));
[...]
// Count all surrounding cells and the cell itself.
function countSurround(_type: TSurround; x, y: byte): byte;
begin
Result:=
count(_type, x + DirOffset[d_itself].x, y + DirOffset[d_itself].y)+
count(_type, x + DirOffset[d_above].x, y + DirOffset[d_above].y)+
count(_type, x + DirOffset[d_right].x, y + DirOffset[d_right].y)+
count(_type, x + DirOffset[d_down].x, y + DirOffset[d_down].y)+
count(_type, x + DirOffset[d_left].x, y + DirOffset[d_left].y);
end;
or
function countSurround(_type: TSurround; x, y: byte): byte;
var d: TDirection;
// Count only the tile and the tiles next to it.
begin
Result:=0;
for d := d_itself to d_left do
inc(result, count(_type, x + DirOffset[d].x, y + DirOffset[d].y));
end;
function countSurroundDiag(_type: TSurround; x, y: byte): byte;
var d: TDirection;
// Count the tile and all the tiles surrounding to it.
begin
Result:=0;
for d := d_itself to d_leftabove do
inc(result, count(_type, x + DirOffset[d].x, y + DirOffset[d].y));
end;
How about that ?