Skip to content
Browse files

Added bitboard

  • Loading branch information...
1 parent 1995051 commit 2f07eb84104b0af3686774d616d0e70aa92e8b9b @Error323 committed Apr 18, 2012
View
16 cmake/sources.cmake
@@ -3,10 +3,15 @@
# === All sources
set(SOURCES
src/bitchess.cpp
+ src/utils/types.h
+ src/utils/debugger.h
+ src/utils/verbose.cpp
+ src/utils/timer.cpp
+ src/utils/random.c
)
# === Test sources
-set(TESTS asearchtest)
+set(TESTS asearchtest bboardtest)
set(asearchtest_SOURCES
src/asearch/state.h
@@ -20,3 +25,12 @@ set(asearchtest_SOURCES
src/utils/timer.cpp
src/utils/random.c
)
+
+set(bboardtest_SOURCES
+ src/bitboard/test/main.cpp
+ src/bitboard/test/bboardtest.cpp
+ src/bitboard/bitboard.cpp
+ src/utils/types.h
+ src/utils/verbose.cpp
+ src/utils/random.c
+)
View
33 src/bitboard/bitboard.cpp
@@ -0,0 +1,33 @@
+#include "bitboard.h"
+
+#include <iostream>
+#include <sstream>
+
+namespace bboard {
+
+std::string ToString(const U64 &inBoard)
+{
+ std::stringstream ss;
+ for (int i = 7; i >= 0; i--)
+ {
+ ss << i+1;
+ Uint8 line = (inBoard >> (i*8)) & 0xff;
+ for (int j = 0; j < 8; j++)
+ ss << (((line >> j) & 1) == 1 ? " 1" : " .");
+ ss << "\n";
+ }
+ ss << " a b c d e f g h\n";
+ return ss.str();
+}
+
+void Print(const U64 &inBoard)
+{
+ std::cout << ToString(inBoard);
+}
+
+Uint8 LSBIndex(U64 inBoard)
+{
+ return magic_table[((inBoard&-inBoard)*magic) >> 58];
+}
+
+}
View
175 src/bitboard/bitboard.h
@@ -0,0 +1,175 @@
+#ifndef BITBOARD_H
+#define BITBOARD_H
+
+#include <string>
+#include "../utils/types.h"
+
+/**
+ * @namespace bitboard
+ *
+ * @brief
+ * 64 bit board and various helper functions
+ */
+namespace bboard {
+
+typedef Uint64 U64;
+#define C64(BitBoard) BitBoard##ULL
+
+
+const U64 magic = 0x022fdd63cc95386d; ///< The DeBruin magic number
+
+const Uint8 magic_table[64] =
+{
+ 0, 1, 2, 53, 3, 7, 54, 27,
+ 4, 38, 41, 8, 34, 55, 48, 28,
+ 62, 5, 39, 46, 44, 42, 22, 9,
+ 24, 35, 59, 56, 49, 18, 29, 11,
+ 63, 52, 6, 26, 37, 40, 33, 47,
+ 61, 45, 43, 21, 23, 58, 17, 10,
+ 51, 25, 36, 32, 60, 20, 57, 16,
+ 50, 31, 19, 15, 30, 14, 13, 12,
+};
+
+const U64 rook_magic[64] = {
+ 0x2080020500400f0ULL,
+ 0x28444000400010ULL,
+ 0x20000a1004100014ULL,
+ 0x20010c090202006ULL,
+ 0x8408008200810004ULL,
+ 0x1746000808002ULL,
+ 0x2200098000808201ULL,
+ 0x12c0002080200041ULL,
+ 0x104000208e480804ULL,
+ 0x8084014008281008ULL,
+ 0x4200810910500410ULL,
+ 0x100014481c20400cULL,
+ 0x4014a4040020808ULL,
+ 0x401002001010a4ULL,
+ 0x202000500010001ULL,
+ 0x8112808005810081ULL,
+ 0x40902108802020ULL,
+ 0x42002101008101ULL,
+ 0x459442200810c202ULL,
+ 0x81001103309808ULL,
+ 0x8110000080102ULL,
+ 0x8812806008080404ULL,
+ 0x104020000800101ULL,
+ 0x40a1048000028201ULL,
+ 0x4100ba0000004081ULL,
+ 0x44803a4003400109ULL,
+ 0xa010a00000030443ULL,
+ 0x91021a000100409ULL,
+ 0x4201e8040880a012ULL,
+ 0x22a000440201802ULL,
+ 0x30890a72000204ULL,
+ 0x10411402a0c482ULL,
+ 0x40004841102088ULL,
+ 0x40230000100040ULL,
+ 0x40100010000a0488ULL,
+ 0x1410100200050844ULL,
+ 0x100090808508411ULL,
+ 0x1410040024001142ULL,
+ 0x8840018001214002ULL,
+ 0x410201000098001ULL,
+ 0x8400802120088848ULL,
+ 0x2060080000021004ULL,
+ 0x82101002000d0022ULL,
+ 0x1001101001008241ULL,
+ 0x9040411808040102ULL,
+ 0x600800480009042ULL,
+ 0x1a020000040205ULL,
+ 0x4200404040505199ULL,
+ 0x2020081040080080ULL,
+ 0x40a3002000544108ULL,
+ 0x4501100800148402ULL,
+ 0x81440280100224ULL,
+ 0x88008000000804ULL,
+ 0x8084060000002812ULL,
+ 0x1840201000108312ULL,
+ 0x5080202000000141ULL,
+ 0x1042a180880281ULL,
+ 0x900802900c01040ULL,
+ 0x8205104104120ULL,
+ 0x9004220000440aULL,
+ 0x8029510200708ULL,
+ 0x8008440100404241ULL,
+ 0x2420001111000bdULL,
+ 0x4000882304000041ULL,
+};
+
+const U64 bishop_magic[64] = {
+ 0x100420000431024ULL,
+ 0x280800101073404ULL,
+ 0x42000a00840802ULL,
+ 0xca800c0410c2ULL,
+ 0x81004290941c20ULL,
+ 0x400200450020250ULL,
+ 0x444a019204022084ULL,
+ 0x88610802202109aULL,
+ 0x11210a0800086008ULL,
+ 0x400a08c08802801ULL,
+ 0x1301a0500111c808ULL,
+ 0x1280100480180404ULL,
+ 0x720009020028445ULL,
+ 0x91880a9000010a01ULL,
+ 0x31200940150802b2ULL,
+ 0x5119080c20000602ULL,
+ 0x242400a002448023ULL,
+ 0x4819006001200008ULL,
+ 0x222c10400020090ULL,
+ 0x302008420409004ULL,
+ 0x504200070009045ULL,
+ 0x210071240c02046ULL,
+ 0x1182219000022611ULL,
+ 0x400c50000005801ULL,
+ 0x4004010000113100ULL,
+ 0x2008121604819400ULL,
+ 0xc4a4010000290101ULL,
+ 0x404a000888004802ULL,
+ 0x8820c004105010ULL,
+ 0x28280100908300ULL,
+ 0x4c013189c0320a80ULL,
+ 0x42008080042080ULL,
+ 0x90803000c080840ULL,
+ 0x2180001028220ULL,
+ 0x1084002a040036ULL,
+ 0x212009200401ULL,
+ 0x128110040c84a84ULL,
+ 0x81488020022802ULL,
+ 0x8c0014100181ULL,
+ 0x2222013020082ULL,
+ 0xa00100002382c03ULL,
+ 0x1000280001005c02ULL,
+ 0x84801010000114cULL,
+ 0x480410048000084ULL,
+ 0x21204420080020aULL,
+ 0x2020010000424a10ULL,
+ 0x240041021d500141ULL,
+ 0x420844000280214ULL,
+ 0x29084a280042108ULL,
+ 0x84102a8080a20a49ULL,
+ 0x104204908010212ULL,
+ 0x40a20280081860c1ULL,
+ 0x3044000200121004ULL,
+ 0x1001008807081122ULL,
+ 0x50066c000210811ULL,
+ 0xe3001240f8a106ULL,
+ 0x940c0204030020d4ULL,
+ 0x619204000210826aULL,
+ 0x2010438002b00a2ULL,
+ 0x884042004005802ULL,
+ 0xa90240000006404ULL,
+ 0x500d082244010008ULL,
+ 0x28190d00040014e0ULL,
+ 0x825201600c082444ULL,
+};
+
+
+Uint8 LSBIndex (U64 inBoard);
+
+std::string ToString(const U64 &inBoard);
+void Print(const U64 &inBoard);
+
+} // namespace bboard
+
+#endif // BITBOARD_H
View
46 src/bitboard/test/bboardtest.cpp
@@ -0,0 +1,46 @@
+#include "bboardtest.h"
+#include "../bitboard.h"
+
+#include <iostream>
+
+using namespace bboard;
+
+CPPUNIT_TEST_SUITE_REGISTRATION(BBoardTest);
+
+BBoardTest::BBoardTest()
+ : CppUnit::TestFixture()
+{
+
+}
+
+BBoardTest::~BBoardTest()
+{
+
+}
+
+void BBoardTest::test_create()
+{
+ std::cout << std::endl;
+ U64 black = C64(0xAA55AA55AA55AA55);
+ U64 white = ~black;
+ Print(white);
+ CPPUNIT_ASSERT_EQUAL(Uint8(1), LSBIndex(white));
+
+ U64 lsb[64];
+ for (Uint8 i = 0; i < 64; i++)
+ {
+ lsb[i] = C64(0);
+ lsb[i] |= (C64(1) << i);
+ CPPUNIT_ASSERT_EQUAL(i, LSBIndex(lsb[i]));
+ }
+}
+
+void BBoardTest::setUp()
+{
+
+}
+
+void BBoardTest::tearDown()
+{
+
+}
View
24 src/bitboard/test/bboardtest.h
@@ -0,0 +1,24 @@
+#ifndef BBOARD_TEST_H
+#define BBOARD_TEST_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+class BBoardTest : public CppUnit::TestFixture
+{
+public:
+ CPPUNIT_TEST_SUITE(BBoardTest);
+ CPPUNIT_TEST(test_create);
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ BBoardTest();
+ ~BBoardTest();
+
+ void setUp();
+ void tearDown();
+
+ // Test Methods
+ void test_create();
+};
+
+#endif // BBOARD_TEST_H
View
15 src/bitboard/test/main.cpp
@@ -0,0 +1,15 @@
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+
+#include <iostream>
+
+int main(int argc, char *argv[])
+{
+ (void) argc;
+ (void) argv;
+
+ CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
+ CppUnit::TextUi::TestRunner runner;
+ runner.addTest(suite);
+ return runner.run() ? 0 : 1;
+}
View
6 src/bitchess.cpp
@@ -1,8 +1,14 @@
#include "version.h"
+#include "utils/verbose.h"
+#include "utils/debugger.h"
+
int main(int argc, char **argv)
{
(void) argc;
(void) argv;
+
+ NoticeLine(HUMAN_NAME);
+
return 0;
}

0 comments on commit 2f07eb8

Please sign in to comment.
Something went wrong with that request. Please try again.