Simple question

Discussion of chess software programming and technical issues.

Moderator: Ras

outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Simple question

Post by outAtime »

Is there a faster way than this which will give the same results? I hate adding all the ++ lines... ive tried just doing +=10 but it gave different results. Thx.

Code: Select all

  if ((board[p] & 1) == 1) 
      s++;	   
  if ((board[p]) == bknight) 
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;

return s;
	  
outAtime
UncombedCoconut
Posts: 319
Joined: Fri Dec 18, 2009 11:40 am
Location: Naperville, IL

Re: Simple question

Post by UncombedCoconut »

The reason it gives different results is that you did not use {curly braces} around the body of your second if statement. The compiler would read your code as follows; I doubt it is what you intended:

Code: Select all

  if ((board[p] & 1) == 1) 
      s++;	   

  if ((board[p]) == bknight) 
      s++;

  s++;
  s++;
  s++;
  s++;
  s++;
  s++;
  s++;
  s++;
  s++;

return s;
	  
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Simple question

Post by outAtime »

Ok, thanks. Im guessing this is what you meant:

Code: Select all

 if (board[p] == bknight) 
   { 
      s++;
      s++;
	  s++;
	  s++;
	  s++;
	  s++;
	  s++;
      s++;
	  s++;
	  s++;
	  s++;
	  s++;
    } 
	  
  return s;
}
id still like to get rid of all those s++'s and use something else :) s += 10 perhaps? but that would change the overall result of s right?
outAtime
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Simple question

Post by Evert »

outAtime wrote:id still like to get rid of all those s++'s and use something else :) s += 10 perhaps? but that would change the overall result of s right?
Well, yes, but only because there are 12 s++; lines in the example you posted there.
Last edited by Evert on Sun Feb 06, 2011 10:45 pm, edited 1 time in total.
Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Simple question

Post by Gerd Isenberg »

Is this serious? If you have problems with counting lines, you may use a loop ;-)

Code: Select all

if (board[p] == bknight) {
  for (int i = 0; i < 10; i++)
    s++;
}
No idea whether this is didactic appropriate, but in C/C++ you might try a kind of enriched algebra to save some branches ...

Code: Select all

s += (board[p] & 1) + 10*(board[p] == bknight);
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Simple question

Post by outAtime »

Thanks for the suggestion, that idea matches some already added code.

so for black:

Code: Select all

if (board[q] && ((board[q] & 1) == 0))
      t++;
      t += (board[q] && ((board[q] & 1) + 10*(board[q] == wknight))); 
  return t;
or no i still need the if (board[q] == wknight) { above t+= ...
is that right or can I remove (board[q] && (( section, maybe its not needed.
Thanks for the help.
outAtime
UncombedCoconut
Posts: 319
Joined: Fri Dec 18, 2009 11:40 am
Location: Naperville, IL

Re: Simple question

Post by UncombedCoconut »

outAtime wrote:Thanks for the suggestion, that idea matches some already added code.

so for black:

Code: Select all

if (board[q] && ((board[q] & 1) == 0))
      t++;
      t += (board[q] && ((board[q] & 1) + 10*(board[q] == wknight))); 
  return t;
You only need this part:

Code: Select all

  t += (board[q] && ((board[q] & 1) + 10*(board[q] == wknight))); 
  return t;
In C, the result of an "&&" operation is a number: 1 if the condition is true, or 0 if the condition is false. This is also true of ==. So, the statement's effect is: add 1 to t if board[q] is non-zero and has its LSB cleared; then add 10 to t if board[q] equals wknight.

Let me return to the point of my first post: I again see that you indented multiple statements after the "if" statement. Without curly braces, only the first one is conditional on the "if". The indentation is not significant to the compiler, but it's a valuable hint for human readers -- and misleading when it doesn't match what the code does!

I would suggest switching to a code editor that indents automatically. It will probably help you avoid bugs.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Simple question

Post by bob »

outAtime wrote:Is there a faster way than this which will give the same results? I hate adding all the ++ lines... ive tried just doing +=10 but it gave different results. Thx.

Code: Select all

  if ((board[p] & 1) == 1) 
      s++;	   
  if ((board[p]) == bknight) 
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;
      s++;

return s;
	  
+10 won't work. But you can replace the last 9 with +9 and it will.

The if only attaches the first s++ that follows it since there are no {} surrounding the whole thing.

so something like

if (c) s++;
s+=9;

should work...
Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Simple question

Post by Gerd Isenberg »

outAtime wrote:Thanks for the suggestion, that idea matches some already added code.

so for black:

Code: Select all

if (board[q] && ((board[q] & 1) == 0))
      t++;
      t += (board[q] && ((board[q] & 1) + 10*(board[q] == wknight))); 
  return t;
or no i still need the if (board[q] == wknight) { above t+= ...
is that right or can I remove (board[q] && (( section, maybe its not needed.
Thanks for the help.
Because of your source code indention and/or possible missing brackets, it is quite unclear to me what you will achive. Can you describe in words what you want to do with those statements?

However, as already mentioned by Justin, logical boolean operations (&&, ||, !), unlike the bitwise boolean operations (&, |, ~, ^) which are in fact 32 or 64 parallel operations on each bit of the operands, will only result in a {0,1}, or if interpreted as boolean, a {false, true} range. Expression like

Code: Select all

  x  && (y*10)
are in C treated implicitly as

Code: Select all

 (x != 0) && (y*10 != 0) => {0, 1} or {false, true}
That is likely not what you want. So far, forgot my "devil" optimization proposal to perform arithmetic with boolean values, and use proper and clean code with if statements and curly braces always, e.g.

Code: Select all

if ( (board[q] != 0) && ((board[q] & 1) == 0) ) {
   t++; // for whatever reason
}

if ( board[q] == wknight ) {
   t += 10; // or 9?!
}