Recent

Author Topic: How to count directly boolean type in surround?  (Read 3356 times)

osddeitf

  • New Member
  • *
  • Posts: 22
How to count directly boolean type in surround?
« on: June 17, 2015, 09:04:16 am »
I have a program have an array of class like this:
Code: [Select]
...
TMyClass = class(TObject)
public
  Bool1 : boolean;
  Bool2 : boolean;
  Bool3 : boolean;
end;
...
var MyClassArray: array of array of TMyClass;
...
setlength(MyClassArray, 20, 20);
...
Then the bool1, bool2, bool3 were processed to get their result.
And i get random one of MyClassArray to check out the surround count of bool1, bool2, bool3 near by it.

I do that:
Code: [Select]
...
type TSurround = (b1, b2, b3);
...
function count(_type: TSurround; x, y: byte): byte;
begin
  case _type of
    b1: Result:= byte(MyClassArray[x,y].bool1);
    b2: Result:= byte(MyClassArray[x,y].bool2);
    b3: Result:= byte(MyClassArray[x,y].bool3);
  end;
end;

function countBelow(_type: TSurround; x, y: byte): byte;
begin
  Result:= count(_type, x, y);
end;
... //other count function

function countSurround(_type: TSurround; x, y: byte): byte;
begin
  if (x = 0) and (y = 0) then
    begin
      Result:= countBelow(_type, x, y)+ countBelowRight(_type, x, y)+ countRight(_type, x, y);
      exit;
    end;
  ... //other else conner, up, left, middle...
end;

function countBool1(x, y: byte): byte;
begin
  Result:= countSurround(b1, x, y);
end;
...// other countBool

i guess that exists some other method to count it directly to make the code easier to read and correct when i need to change. Thanks for any help. :)

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to count directly boolean type in surround?
« Reply #1 on: June 21, 2015, 04:52:19 am »
Code: [Select]
...
type TSurround = (b1, b2, b3);
...
function count(_type: TSurround; x, y: byte): byte;
begin
  case _type of
    b1: Result:= byte(MyClassArray[x,y].bool1);
    b2: Result:= byte(MyClassArray[x,y].bool2);
    b3: Result:= byte(MyClassArray[x,y].bool3);
  end;
end;
[...]
function countSurround(_type: TSurround; x, y: byte): byte;
begin
  if (x = 0) and (y = 0) then
    begin
      Result:= countBelow(_type, x, y)+ countBelowRight(_type, x, y)+ countRight(_type, x, y);
      exit;
    end;
  ... //other else conner, up, left, middle...
end;
[...]
First you could improve your count-function like:
Code: [Select]
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
    with MyClassArray[x,y] do
       case _type of
         b1: Result:= byte(bool1);
         b2: Result:= byte(bool2);
         b3: Result:= byte(bool3);
       end;
end;
[...]
e.G: Why did you call the function count ? To me a Name like "Select..." would be more appropriate ...
you could omit the (x/y >= 0 ) since byte are over 0 by default, but if want bigger arrays and use integer you have to check.

This makes your countSurround much easier:
Code: [Select]
// Count all surrounding cells and the cell itself.
function countSurround(_type: TSurround; x, y: byte): byte;
begin
   Result:=
      count(_type, x, y)+
      count(_type, x +1, y)+
      count(_type, x, y+1)+
      count(_type, x -1, y)+
      count(_type, x, y-1);
end;
this works unless your array is smaller then 255 for each dimesion, but then you should go for x,y:integer and then it's working again.

It's hard to suggest anything, without changing too much. It would help if you tell more about your application,
e.G: is then TMyclass cut in stone, or could it contain an array of bool ? Or is there a reason why you use byte instead of integer witch is much more general ?
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

osddeitf

  • New Member
  • *
  • Posts: 22
Re: How to count directly boolean type in surround?
« Reply #2 on: July 02, 2015, 02:54:00 pm »
First you could improve your count-function like:
Code: [Select]
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
    with MyClassArray[x,y] do
       case _type of
         b1: Result:= byte(bool1);
         b2: Result:= byte(bool2);
         b3: Result:= byte(bool3);
       end;
end;
[...]
e.G: Why did you call the function count ? To me a Name like "Select..." would be more appropriate ...
you could omit the (x/y >= 0 ) since byte are over 0 by default, but if want bigger arrays and use integer you have to check.

This makes your countSurround much easier:
Code: [Select]
// Count all surrounding cells and the cell itself.
function countSurround(_type: TSurround; x, y: byte): byte;
begin
   Result:=
      count(_type, x, y)+
      count(_type, x +1, y)+
      count(_type, x, y+1)+
      count(_type, x -1, y)+
      count(_type, x, y-1);
end;
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;
}

"Square.prototype.countAboveRight" like that:
Code: [Select]
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 :D

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to count directly boolean type in surround?
« Reply #3 on: July 03, 2015, 12:30:07 am »
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:
Code: [Select]
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 :D
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 
Code: [Select]
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) )
Code: [Select]
  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
Code: [Select]
  TMyClass
  [...]
  arrbool : array [TSurround] of bool;
  [...]

So the count/select function could look like this:
Code: [Select]
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.
Quote
"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
Code: [Select]
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
Code: [Select]
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 ?
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

osddeitf

  • New Member
  • *
  • Posts: 22
Re: How to count directly boolean type in surround?
« Reply #4 on: July 03, 2015, 08:35:04 am »
Ok, i fixed my code. Thanks for helps.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to count directly boolean type in surround?
« Reply #5 on: July 03, 2015, 04:55:29 pm »
So what does your code look like now.
The main point about code is that it's working and YOU understand it.
So if you don't understand MY code it's not Godunov (Er.. good enough ;) ) for YOU.
Another good advise would be: Make lot of comments. (But even i don't do that all the time )
So share your latest artwork with us, so we'll work it out somehow.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

 

TinyPortal © 2005-2018