Difference between revisions of "CPW-Engine attacks"

From Chessprogramming wiki
Jump to: navigation, search
(Created page with "'''Home * Engines * CPW-Engine * Attacks''' This page holds the functions responsible for detecting if a given square is attacked by a given player. =i...")
 
 
Line 3: Line 3:
 
This page holds the functions responsible for detecting if a given square is attacked by a given player.
 
This page holds the functions responsible for detecting if a given square is attacked by a given player.
  
=isAttacked=  
+
= isAttacked =
 
<pre>
 
<pre>
 
#include "stdafx.h"
 
#include "stdafx.h"
 
#include "0x88_math.h"
 
#include "0x88_math.h"
  
int isAttacked( char byColor, S8 sq ) {
+
int isAttacked(char byColor, S8 sq) {
/* pawns */
+
  /* pawns */
if ( byColor == WHITE ) {
+
  if (byColor == WHITE) {
  if ( IS_SQ( sq + SE ) &&
+
    if (IS_SQ(sq + SE) &&
        isPiece( WHITE, PAWN, sq + SE )
+
      isPiece(WHITE, PAWN, sq + SE)
 
       )
 
       )
 
       return 1;
 
       return 1;
  if ( IS_SQ( sq + SW ) &&
+
    if (IS_SQ(sq + SW) &&
        isPiece( WHITE, PAWN, sq + SW )
+
      isPiece(WHITE, PAWN, sq + SW)
 
       )
 
       )
 
       return 1;
 
       return 1;
}
+
  }
else {
+
  else {
  if ( IS_SQ( sq + NE ) &&
+
    if (IS_SQ(sq + NE) &&
        isPiece( BLACK, PAWN, sq + NE )
+
      isPiece(BLACK, PAWN, sq + NE)
 
       )
 
       )
 
       return 1;
 
       return 1;
  if ( IS_SQ( sq + NW ) &&
+
    if (IS_SQ(sq + NW) &&
        isPiece( BLACK, PAWN, sq + NW )
+
      isPiece(BLACK, PAWN, sq + NW)
 
       )
 
       )
 
       return 1;
 
       return 1;
}
+
  }
+
 
/* knights */
+
  /* knights */
if ( leaperAttack( byColor, sq, KNIGHT ) )
+
  if (leaperAttack(byColor, sq, KNIGHT))
  return 1;
+
    return 1;
+
 
/* kings */
+
  /* kings */
if ( leaperAttack( byColor, sq, KING ) )
+
  if (leaperAttack(byColor, sq, KING))
  return 1;
+
    return 1;
+
 
/* straight line sliders */
+
  /* straight line sliders */
    if ( straightAttack( byColor, sq, NORTH ) ||
+
  if (straightAttack(byColor, sq, NORTH) ||
        straightAttack( byColor, sq, SOUTH ) ||
+
    straightAttack(byColor, sq, SOUTH) ||
        straightAttack( byColor, sq, EAST ) ||
+
    straightAttack(byColor, sq, EAST) ||
        straightAttack( byColor, sq, WEST )
+
    straightAttack(byColor, sq, WEST)
      )
+
    )
      return 1;
+
    return 1;
+
 
/* diagonal sliders */
+
  /* diagonal sliders */
    if ( diagAttack( byColor, sq, NE ) ||
+
  if (diagAttack(byColor, sq, NE) ||
        diagAttack( byColor, sq, SE ) ||
+
    diagAttack(byColor, sq, SE) ||
        diagAttack( byColor, sq, NW ) ||
+
    diagAttack(byColor, sq, NW) ||
        diagAttack( byColor, sq, SW )
+
    diagAttack(byColor, sq, SW)
      )
+
    )
      return 1;
+
    return 1;
+
 
    return 0;
+
  return 0;
 
}
 
}
 
</pre>
 
</pre>
=leaperAttack=  
+
= leaperAttack =
 
<pre>
 
<pre>
int leaperAttack( char byColor, S8 sq, char byPiece ) {
+
int leaperAttack(char byColor, S8 sq, char byPiece) {
    S8 nextSq;
+
  S8 nextSq;
for ( int dir = 0; dir < 8; dir++ ) {
+
  for (int dir = 0; dir < 8; dir++) {
        nextSq = sq + vector[byPiece][dir];
+
    nextSq = sq + vector[byPiece][dir];
        if ( IS_SQ(nextSq) &&
+
    if (IS_SQ(nextSq) &&
            isPiece( byColor, byPiece, nextSq )
+
      isPiece(byColor, byPiece, nextSq)
          )
+
      )
          return 1;
+
      return 1;
    }
+
  }
    return 0;
+
  return 0;
 
}
 
}
 
</pre>
 
</pre>
=straightAttack=  
+
= straightAttack =
 
<pre>
 
<pre>
 
int straightAttack(char byColor, S8 sq, int vect) {
 
int straightAttack(char byColor, S8 sq, int vect) {
    int nextSq = sq + vect;
+
  int nextSq = sq + vect;
+
 
    while ( IS_SQ(nextSq) ) {
+
  while (IS_SQ(nextSq)) {
      if (b.color[nextSq] != COLOR_EMPTY ) {
+
    if (b.color[nextSq] != COLOR_EMPTY) {
          if ( ( b.color[nextSq] == byColor ) &&
+
      if ((b.color[nextSq] == byColor) &&
                ( b.pieces[nextSq] == ROOK || b.pieces[nextSq] == QUEEN )
+
        (b.pieces[nextSq] == ROOK || b.pieces[nextSq] == QUEEN)
              )
+
        )
              return 1;
+
        return 1;
          return 0;
+
      return 0;
          }
 
      nextSq = nextSq + vect;
 
 
     }
 
     }
    return 0;
+
    nextSq = nextSq + vect;
 +
  }
 +
  return 0;
 
}
 
}
 
</pre>
 
</pre>
=diagAttack=  
+
= diagAttack =
 
<pre>
 
<pre>
 
int diagAttack(int byColor, S8 sq, int vect) {
 
int diagAttack(int byColor, S8 sq, int vect) {
    int nextSq = sq + vect;
+
  int nextSq = sq + vect;
+
 
    while ( IS_SQ( nextSq ) ) {
+
  while (IS_SQ(nextSq)) {
      if (b.color[ nextSq ] != COLOR_EMPTY ) {
+
    if (b.color[nextSq] != COLOR_EMPTY) {
          if ( ( b.color[nextSq] == byColor ) &&
+
      if ((b.color[nextSq] == byColor) &&
                ( b.pieces[nextSq] == BISHOP || b.pieces[nextSq] == QUEEN )
+
        (b.pieces[nextSq] == BISHOP || b.pieces[nextSq] == QUEEN)
              )
+
        )
              return 1;
+
        return 1;
          return 0;
+
      return 0;
          }
 
      nextSq = nextSq + vect;
 
 
     }
 
     }
     return 0;
+
     nextSq = nextSq + vect;
 +
  }
 +
  return 0;
 
}
 
}
 
</pre>
 
</pre>
 
'''[[CPW-Engine|Up one Level]]'''
 
'''[[CPW-Engine|Up one Level]]'''

Latest revision as of 15:40, 18 December 2018

Home * Engines * CPW-Engine * Attacks

This page holds the functions responsible for detecting if a given square is attacked by a given player.

isAttacked

#include "stdafx.h"
#include "0x88_math.h"

int isAttacked(char byColor, S8 sq) {
  /* pawns */
  if (byColor == WHITE) {
    if (IS_SQ(sq + SE) &&
      isPiece(WHITE, PAWN, sq + SE)
      )
      return 1;
    if (IS_SQ(sq + SW) &&
      isPiece(WHITE, PAWN, sq + SW)
      )
      return 1;
  }
  else {
    if (IS_SQ(sq + NE) &&
      isPiece(BLACK, PAWN, sq + NE)
      )
      return 1;
    if (IS_SQ(sq + NW) &&
      isPiece(BLACK, PAWN, sq + NW)
      )
      return 1;
  }

  /* knights */
  if (leaperAttack(byColor, sq, KNIGHT))
    return 1;

  /* kings */
  if (leaperAttack(byColor, sq, KING))
    return 1;

  /* straight line sliders */
  if (straightAttack(byColor, sq, NORTH) ||
    straightAttack(byColor, sq, SOUTH) ||
    straightAttack(byColor, sq, EAST) ||
    straightAttack(byColor, sq, WEST)
    )
    return 1;

  /* diagonal sliders */
  if (diagAttack(byColor, sq, NE) ||
    diagAttack(byColor, sq, SE) ||
    diagAttack(byColor, sq, NW) ||
    diagAttack(byColor, sq, SW)
    )
    return 1;

  return 0;
}

leaperAttack

int leaperAttack(char byColor, S8 sq, char byPiece) {
  S8 nextSq;
  for (int dir = 0; dir < 8; dir++) {
    nextSq = sq + vector[byPiece][dir];
    if (IS_SQ(nextSq) &&
      isPiece(byColor, byPiece, nextSq)
      )
      return 1;
  }
  return 0;
}

straightAttack

int straightAttack(char byColor, S8 sq, int vect) {
  int nextSq = sq + vect;

  while (IS_SQ(nextSq)) {
    if (b.color[nextSq] != COLOR_EMPTY) {
      if ((b.color[nextSq] == byColor) &&
        (b.pieces[nextSq] == ROOK || b.pieces[nextSq] == QUEEN)
        )
        return 1;
      return 0;
    }
    nextSq = nextSq + vect;
  }
  return 0;
}

diagAttack

int diagAttack(int byColor, S8 sq, int vect) {
  int nextSq = sq + vect;

  while (IS_SQ(nextSq)) {
    if (b.color[nextSq] != COLOR_EMPTY) {
      if ((b.color[nextSq] == byColor) &&
        (b.pieces[nextSq] == BISHOP || b.pieces[nextSq] == QUEEN)
        )
        return 1;
      return 0;
    }
    nextSq = nextSq + vect;
  }
  return 0;
}

Up one Level