Fix some bugs, make bitwise tests pass.

master
Tait Hoyem 5 years ago
parent 6caf487f94
commit 62a0b4ce6a

@ -1,8 +1,8 @@
#ifndef BITWISE_H
#define BITWISE_H
#include <unordered_set>
#include "constants.h"
#include <unordered_set>
// Using macros for ease of use, can also use functons, but I don't see the point.
@ -67,6 +67,6 @@ Rank get_rank(int pos){
}
inline bool is_valid_position(int position){
return DEFAULT_BOARD[position] == PieceType::INV;
return DEFAULT_BOARD[position] != PieceType::INV;
}
#endif

@ -17,10 +17,12 @@ enum PieceType {
B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING
};
namespace Pieces{
const std::array<PieceType, 6> WHITE = {W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING};
const std::array<PieceType, 6> BLACK = {B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING};
}
enum Position {
NA=-1,
A8=21, B8, C8, D8, E8, F8, G8, H8,
@ -34,7 +36,7 @@ enum Position {
};
// Access with POSITION_STRING[Position]
const std::vector<std::string> POSITION_STRING = {
const std::array<std::string, 120> POSITION_STRING = {
"INV", "INV", "INV", "INV", "INV", "INV", "INV", "INV", "INV", "INV",
"INV", "INV", "INV", "INV", "INV", "INV", "INV", "INV", "INV", "INV",
"INV", "A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8", "INV",

@ -1,4 +1,5 @@
defualt: all.out
bitwise: bitwise.out
catch_main.o:
g++ -w -c -o catch_main.o catch_main.cpp
@ -6,6 +7,9 @@ catch_main.o:
custom_printing.o:
g++ -w -I../src/ -c -o custom_printing.o custom_printing.cpp
bitwise.out: catch_main.o
g++ -w -I../src -o bitwise.out catch_main.o bitwise_tests.cpp
# TODO: Allw all.out to contain bitwise tests
all.out: catch_main.o custom_printing.o
g++ -std=c++11 -ggdb -w -I../src/ -o all.out ../src/functions.cpp catch_main.o custom_printing.o main.cpp

@ -1,5 +1,5 @@
#include <bitwise_constants.h>
#include <bitwise.h>
#include <constants.h>
#include "catch.hpp"
/*
@ -12,80 +12,81 @@
* castle move flag: 1 bit
*
* (32-bit int, room to exapnd if neccessary)
* 0000 0000 0000 0000 0000 0000 0011 1111 -> From square position (& 0x3F)
* 0000 0000 0000 0000 0000 1111 1100 0000 -> To square position (>> 6 & 0xFB)
* 0000 0000 0000 0000 1111 0000 0000 0000 -> captured piece, if any (>> 12 & 0xF)
* 0000 0000 0000 1111 0000 0000 0000 0000 -> if prmoted, what to? (>> 16 & 0xF)
* 0000 0000 0001 0000 0000 0000 0000 0000 -> en passant (& 0x100000)
* 0000 0000 0010 0000 0000 0000 0000 0000 -> pawn starting move (& 0x200000)
* 0000 0000 0100 0000 0000 0000 0000 0000 -> castle move (& 0x400000)
* 0000 0000 0000 0000 0000 0000 0111 1111 -> From square position (& 0x7F)
* 0000 0000 0000 0000 0011 1111 1000 0000 -> To square position (>> 7 & 0x3F8)
* 0000 0000 0000 0011 1100 0000 0000 0000 -> captured piece, if any (>> 14 & 0xF)
* 0000 0000 0011 1100 0000 0000 0000 0000 -> if prmoted, what to? (>> 18 & 0xF)
* 0000 0000 0100 0000 0000 0000 0000 0000 -> en passant (& 0x400000)
* 0000 0000 1000 0000 0000 0000 0000 0000 -> pawn starting move (& 0x800000)
* 0000 0001 0000 0000 0000 0000 0000 0000 -> castle move (& 0x1000000)
* */
// This is testing position H1
const unsigned int MOVE_G1 = 0x3f;
// This is testing position G1
const unsigned int MOVE_H1 = 0x3e;
const unsigned int MOVE_G1 = Position::G1;
// This is testing position H1
const unsigned int MOVE_H1 = Position::H1;
// This is testing a move with no flags from G1 to H1
// // Want:
// 1111 1111 1110
// FFE
const unsigned int MOVE_G1_TO_H1 = 0xffe;
const unsigned int MOVE_G1_TO_H1 = 0b1100010'1100001;
// This is testing a move with no flags from H1 to G1
// Want:
// 1111 1011 1111
// FBF
const unsigned int MOVE_H1_TO_G1 = 0b111110111111;
const unsigned int MOVE_H1_TO_G1 = 0b1100001'1100010;
// Move from H1 to G1 and capture
const unsigned int MOVE_H1_TO_G1_get_captured_pcURE_B_KNIGHT = 0x8fbf;
const unsigned int MOVE_H1_TO_G1_CAPTURE_B_KNIGHT = 0b0010'1100001'1100010;
// Move from H1 to G1 and promote
const unsigned int MOVE_H1_TO_G1_get_promoted_to_pcOTE_TO_QUEEN = 0xb0fbf;
const unsigned int MOVE_H1_TO_G1_PROMOTE_TO_QUEEN = 0b0101'0000'1100001'1100010;
// Move from H1 to G1 and en passant
const unsigned int MOVE_H1_TO_G1_EN_PASSANT = 0x100fbf;
const unsigned int MOVE_H1_TO_G1_EN_PASSANT = 0b1'0000'0000'1100001'1100010;
// Move from H1 to G1 and its the starting move for a pawn
const unsigned int MOVE_H1_TO_G1_PAWN_START = 0x200fbf;
const unsigned int MOVE_H1_TO_G1_PAWN_START = 0b1'0'0000'0000'1100001'1100010;
// Move from H1 to G1 and castle
const unsigned int MOVE_H1_TO_G1_AND_get_castle_flagLE = 0x400fbf;
const unsigned int MOVE_H1_TO_G1_AND_CASTLE = 0b1'0'0'0000'0000'1100001'1100010;
// Want:
// From: E3 (21) [0x15] [0x15] <0x15>
// To: D2 (12) [0xB] [0xB] <0x3DF>
// (these 2 combine together) (33) [0x20] [0x3DF] <>
// Captures: B_PAWM (7) [0x7] [0x7000] <0x73DF>
// Promoted: NONE (0) [0x0] [0x0] <>
// en passant: true (1) [0x1] [0x100000] <0x1003DF>
// pawn starting move: false (0) [0x0] [0x0] <>
// castle move: false (0) [0x0] [0x0] <>
// total value: (?) [?] [?} <0x1003DF>
const unsigned int GET_ALL_INT = 0x1003DF;
// From: E4 (65)
// To: D3 (74)
// (these 2 combine together) (33)
// Captures: B_PAWM (7)
// Promoted: NONE (0)
// en passant: true (1)
// pawn starting move: false (0)
// castle move: false (0)
// total value: (?)
const unsigned int GET_ALL_INT = 0b0'0'1'0000'0001'1001010'1000001;
TEST_CASE("Test that bitwise operators return appropriate values. Move.from", "[bitwise_from_pos]"){
CHECK(get_from_sq(MOVE_G1) == Position::H1);
CHECK(get_from_sq(MOVE_H1) == Position::G1);
CHECK(get_from_sq(MOVE_G1) == Position::G1);
CHECK(get_from_sq(MOVE_H1) == Position::H1);
CHECK(get_to_sq(MOVE_G1_TO_H1) == Position::H1);
CHECK(get_from_sq(MOVE_G1_TO_H1) == Position::G1);
CHECK(get_to_sq(MOVE_H1_TO_G1) == Position::G1);
CHECK(get_from_sq(MOVE_H1_TO_G1) == Position::H1);
CHECK(get_captured_pc(MOVE_H1_TO_G1_get_captured_pcURE_B_KNIGHT) == PieceType::B_KNIGHT);
CHECK(get_promoted_to_pc(MOVE_H1_TO_G1_get_promoted_to_pcOTE_TO_QUEEN) == PieceType::B_QUEEN);
CHECK(get_captured_pc(MOVE_H1_TO_G1_CAPTURE_B_KNIGHT) == PieceType::B_KNIGHT);
CHECK(get_promoted_to_pc(MOVE_H1_TO_G1_PROMOTE_TO_QUEEN) == PieceType::B_QUEEN);
CHECK(get_en_pass_flag(MOVE_H1_TO_G1_EN_PASSANT) == 1);
CHECK(get_pawn_st_flag(MOVE_H1_TO_G1_PAWN_START) == 1);
CHECK(get_castle_flag(MOVE_H1_TO_G1_AND_get_castle_flagLE) == 1);
CHECK(get_castle_flag(MOVE_H1_TO_G1_AND_CASTLE) == 1);
CHECK(get_from_sq(GET_ALL_INT) == Position::E4);
CHECK(get_to_sq(GET_ALL_INT) == Position::D3);
CHECK(get_captured_pc(GET_ALL_INT) == PieceType::B_PAWN);
CHECK(get_promoted_to_pc(GET_ALL_INT) == PieceType::NONE);
CHECK(get_en_pass_flag(GET_ALL_INT) == 1);
CHECK(get_pawn_st_flag(GET_ALL_INT) == 0);
CHECK(get_castle_flag(GET_ALL_INT) == 0);
}
TEST_CASE("Test that is_valid_position works properly", "[is_valid_position]"){
CHECK(is_valid_position(0));
CHECK(is_valid_position(63));
CHECK_FALSE(is_valid_position(-1));
CHECK_FALSE(is_valid_position(64));
CHECK_FALSE(is_valid_position(0));
CHECK(is_valid_position(Position::A8));
CHECK(is_valid_position(Position::H1));
}

Loading…
Cancel
Save