Skip to content

Seg fault fixing #2

Merged
merged 3 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,38 @@
#include <string>
#include <vector>
#include <string>
#include <memory>
#include "Move.h"

class BitBoard;

typedef std::unique_ptr<BitBoard> bup;
typedef std::unique_ptr<Move> mup;
typedef std::vector<std::unique_ptr<Move>> vmup;

class BitBoard
{
private:
/* Static bit masks */
/* Static bit masks */

// Used to determine whether a move from that direction is valid
// Used to determine whether a move from that direction is valid
static const uint32_t upRight = 0xFBFBEBBA;
static const uint32_t upLeft = 0xFDF9EDBC;
static const uint32_t downRight = 0x79FBF3DB;
static const uint32_t downLeft = 0x7DFDF5DD;

// Edge locations, all 4 sides
static const uint32_t edgeLoc = 0x86061E67;

static const uint32_t rowMask(int index){
static const uint32_t r [] = { 0x00041041,
0x00082082,
0x04104100,
0x08208200,
0x10410004,
0x20820008,
0x41104010,
0x82100820};
0x00082082,
0x04104100,
0x08208200,
0x10410004,
0x20820008,
0x41104010,
0x82100820};
return r[index];
}

Expand All @@ -43,19 +50,19 @@ class BitBoard
uint32_t m_blackPieces = 0x41C71C3;
uint32_t m_whitePieces = 0xE3820C38;
uint32_t m_kings = 0;

bool m_isBlacksTurn = true;

// static constexpr uint32_t rowMask[8];
/* Class member functions */

// These are basic 32 bit rotation functions
// Details here: http://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c
uint32_t rotl32 (uint32_t n, unsigned int c) const;
uint32_t rotr32 (uint32_t n, unsigned int c) const;

const uint32_t playerPieces() const;

const int count(uint32_t i) const;

// Heuristic functions
Expand All @@ -75,21 +82,23 @@ class BitBoard

int getIndex(uint32_t piece) const;

void addJumps(std::vector<Move> &moves) const;
void addNormalMoves(std::vector<Move> &moves) const;
void generateAllJumps(BitBoard board, Move move, uint32_t piece, std::vector<Move> &moves) const;
void addJumps(vmup &moves) const;
void addNormalMoves(vmup &moves) const;
void generateAllJumps(BitBoard board, uint32_t piece, vmup &moves) const;
void generateAllJumps(BitBoard board, mup &move, uint32_t piece, vmup &moves) const;
BitBoard boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo) const;
std::vector<uint32_t> generateImmediateJumps(BitBoard &board, uint32_t piece) const;
void addNewMove(uint32_t start, uint32_t end, std::vector<Move> &moves) const;
void addNewMove(uint32_t start, uint32_t end, vmup &moves) const;
std::string pieceToString(int piece) const;

public:
BitBoard();
BitBoard(uint32_t black, uint32_t white, uint32_t kings);
BitBoard(const BitBoard &board);

std::string player() const;
std::vector<Move> actions() const;
BitBoard result(Move move) const;
vmup actions() const;
bup result(mup &move) const;
double utility(std::string player) const;
void printState() const;
};
Expand Down
14 changes: 9 additions & 5 deletions include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@

#include <queue>
#include <string>
#include <memory>
#include "Move.h"
#include "BitBoard.h"

typedef std::unique_ptr<Move> mup;

class MinimaxSearch
{
private:
static const int maxDepth = 7;
static const int maxDepth = 9;

double minValue(const BitBoard &board, int currentDepth) const;
double maxValue(const BitBoard &board, int currentDepth) const;
double minValue(std::unique_ptr<BitBoard> &board, int currentDepth) const;
double maxValue(std::unique_ptr<BitBoard> &board, int currentDepth) const;
double alphaBeta(std::unique_ptr<BitBoard> &board, double alpha, double beta, int depth) const;


public:
Move minimaxDecision(const BitBoard &board) const;
Move minimaxDecisionStack(const BitBoard &board) const;
mup minimaxDecision(std::unique_ptr<BitBoard> &board) const;

};

#endif

8 changes: 8 additions & 0 deletions include/Move.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <vector>
#include <cstdint>
#include <iostream>

class Move
{
Expand All @@ -13,11 +14,18 @@ class Move

Move();
Move(int start);
Move(const Move &move);
void addMove(int move);
void removeLast();
bool isEmpty();
int length();
std::vector<int> getMoves() { return moves; }

void printMoves() {
for (int m : moves) {
std::cout << m << "->" << std::endl;
}
}
};

#endif
96 changes: 61 additions & 35 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
#include <vector>
#include <cmath>
#include <iostream>
#include <memory>
#include "Move.h"
#include "BitBoard.h"

typedef std::unique_ptr<BitBoard> bup;
typedef std::unique_ptr<Move> mup;
typedef std::vector<std::unique_ptr<Move>> vmup;

/* Private Functions */

// These are basic 32 bit rotation functions
Expand All @@ -15,16 +20,16 @@ uint32_t BitBoard::rotl32 (uint32_t n, unsigned int c) const
{
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);

c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n<<c) | (n>>( (-c)&mask ));
c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n<<c) | (n>>( (-c)&mask ));
}

uint32_t BitBoard::rotr32 (uint32_t n, unsigned int c) const
{
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);

c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n>>c) | (n<<( (-c)&mask ));
c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n>>c) | (n<<( (-c)&mask ));
}

// Given a piece represented by a binary value return it's index
Expand All @@ -33,19 +38,19 @@ int BitBoard::getIndex(uint32_t piece) const
return (int)std::log2(piece);
}

void BitBoard::addJumps(std::vector<Move> &moves) const
void BitBoard::addJumps(vmup &moves) const
{
uint32_t notOcc = ~(m_whitePieces|m_blackPieces);
for (int i = 0; i < 32; ++i) {
// grab the specific piece
uint32_t piece = (m_isBlacksTurn) ? m_blackPieces & 1<<i : m_whitePieces & 1<<i;
if (piece) {
generateAllJumps(*this, Move(), piece, moves);
generateAllJumps(*this, piece, moves);
}
}
}

void BitBoard::addNormalMoves(std::vector<Move> &moves) const
void BitBoard::addNormalMoves(vmup &moves) const
{
uint32_t notOcc = ~(m_whitePieces|m_blackPieces);
for (int i = 0; i < 32; ++i) {
Expand Down Expand Up @@ -78,17 +83,27 @@ void BitBoard::addNormalMoves(std::vector<Move> &moves) const
}
}

void BitBoard::generateAllJumps(BitBoard board, Move move, uint32_t piece, std::vector<Move> &moves) const
void BitBoard::generateAllJumps(BitBoard board, uint32_t piece, vmup &moves) const
{
if (move.isEmpty()) move.addMove(getIndex(piece)); // Add start position
mup move (new Move);
generateAllJumps(board, move, piece, moves);
}

void BitBoard::generateAllJumps(BitBoard board, mup &move, uint32_t piece, vmup &moves) const
{
if (move->isEmpty()) move->addMove(getIndex(piece)); // Add start position
std::vector<uint32_t> immediateJumps = generateImmediateJumps(board, piece);

if (immediateJumps.size() == 0) {
if (move.length() > 1) moves.push_back(move); // If no more jumps add move to list of moves
if (move->length() > 1) {
mup newMove(new Move(*move));
moves.push_back(std::move(newMove)); // If no more jumps add move to list of moves
}
} else {
for (uint32_t x : immediateJumps) {
move.addMove(getIndex(x));
move->addMove(getIndex(x));
generateAllJumps(boardMove(board, piece, x), move, x, moves);
move.removeLast();
move->removeLast();
}
}
}
Expand All @@ -99,7 +114,7 @@ BitBoard BitBoard::boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo) c
uint32_t white = board.m_whitePieces;
uint32_t black = board.m_blackPieces;
uint32_t kings = board.m_kings;

// First figure out if a piece has been take
int pieceLoc = getIndex(piece);
int moveLoc = getIndex(moveTo);
Expand All @@ -108,9 +123,7 @@ BitBoard BitBoard::boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo) c
int diff = std::abs((double)pieceLoc - moveLoc);
int avg = ((pieceLoc + moveLoc) / 2);
if (board.m_isBlacksTurn) {
if (diff == 2 || diff == 14) {
white &= ~(1<<avg);
}
if (diff == 2 || diff == 14) white &= ~(1<<avg);
black = (black | moveTo) & ~piece;
kings = kings | (black & blackKingSpots);
} else {
Expand All @@ -130,7 +143,7 @@ std::vector<uint32_t> BitBoard::generateImmediateJumps(BitBoard &board, uint32_t
uint32_t notOcc = ~(board.m_whitePieces|board.m_blackPieces);
uint32_t temp;
bool isKing = piece & board.m_kings;
if (m_isBlacksTurn) {
if (board.m_isBlacksTurn) {
temp = (rotl32(piece, 7) & upLeft) & board.m_whitePieces;
if ((rotl32(temp, 7) & upLeft) & notOcc)
moves.push_back(rotl32(temp, 7));
Expand Down Expand Up @@ -161,14 +174,15 @@ std::vector<uint32_t> BitBoard::generateImmediateJumps(BitBoard &board, uint32_t
moves.push_back(rotl32(temp, 1));
}
}

return moves;
}

void BitBoard::addNewMove(uint32_t start, uint32_t end, std::vector<Move> &moves) const
void BitBoard::addNewMove(uint32_t start, uint32_t end, vmup &moves) const
{
Move newMove(getIndex(start));
newMove.addMove(getIndex(end));
moves.push_back(newMove);
mup newMove( new Move(getIndex(start)));
newMove->addMove(getIndex(end));
moves.push_back(std::move(newMove));
}

std::string BitBoard::pieceToString(int piece) const
Expand All @@ -186,19 +200,27 @@ std::string BitBoard::pieceToString(int piece) const
BitBoard::BitBoard() {};

BitBoard::BitBoard(uint32_t black, uint32_t white, uint32_t kings) :
m_blackPieces(black), m_whitePieces(white), m_kings(kings)
m_blackPieces(black), m_whitePieces(white), m_kings(kings)
{
m_isBlacksTurn = true;
}

BitBoard::BitBoard(const BitBoard &board)
{
m_whitePieces = board.m_whitePieces;
m_blackPieces = board.m_blackPieces;
m_kings = board.m_kings;
m_isBlacksTurn = board.m_isBlacksTurn;
}

std::string BitBoard::player() const
{
return m_isBlacksTurn ? "black" : "white";
}

std::vector<Move> BitBoard::actions() const
vmup BitBoard::actions() const
{
std::vector<Move> moves;
vmup moves;
addJumps(moves);
if (moves.empty()) {
addNormalMoves(moves);
Expand All @@ -207,29 +229,34 @@ std::vector<Move> BitBoard::actions() const
return moves;
}

BitBoard BitBoard::result(Move move) const
bup BitBoard::result(mup &move) const
{
BitBoard currBoard(*this);
int start{-1};

for (int x : move.getMoves()) {
for (int x : move->getMoves()) {
if (start == -1) {
start = x;
start = x;
} else {
currBoard = boardMove(currBoard, 1<<start, 1<<x);
start = x;
currBoard = boardMove(currBoard, 1<<start, 1<<x);
start = x;
}
}


currBoard.m_isBlacksTurn = !m_isBlacksTurn;
return currBoard;
bup result (new BitBoard(currBoard));
return result;
}

double BitBoard::utility(std::string player) const
{
// TODO: Do something useful here
return 0.0;
if (m_isBlacksTurn) {
std::bitset<32> bits (m_blackPieces);
return 12 - bits.count();
}

std::bitset<32> bits (m_whitePieces);
return bits.count() - 12;
}

void BitBoard::printState() const
Expand All @@ -243,4 +270,3 @@ void BitBoard::printState() const
std::cout<<"-"<<pieceToString(19)<<"-"<<pieceToString(13)<<"-"<<pieceToString(7)<<"-"<<pieceToString(1)<<std::endl;
std::cout<<pieceToString(18)<<"-"<<pieceToString(12)<<"-"<<pieceToString(6)<<"-"<<pieceToString(0)<<"-"<<std::endl;
}

Loading