diff --git a/Threat.h b/Threat.h new file mode 100644 index 0000000..f5c39fc --- /dev/null +++ b/Threat.h @@ -0,0 +1,42 @@ +#ifndef __THREAT_H_INC +#define __THREAT_H_INC + +namespace Threat { + byte can_capture(byte square) { + // TODO make this more efficient + byte t = 0; + Board::swap_side(); + Movegen gen = Movegen(); + Move m; + while((m = gen.next_move()).sq_to != 255) { + if(m.sq_to == square) { + t++; + } + } + Board::swap_side(); + return t; + } + + byte threats(byte square) { + byte t = 0; + Board::swap_side(); + t = can_capture(square); + Board::swap_side(); + return t; + } + + bool is_check() { + byte king_ptr = Board::field[PTR_W_KING | Board::black_moving()]; + return (threats(king_ptr)) > 0; + } + + bool illegal() { + // if the enemy king can be captured, move they did is illegal. + byte king_ptr = Board::field[PTR_W_KING | !Board::black_moving()]; + return (can_capture(king_ptr)) > 0; + + } + +} + +#endif \ No newline at end of file