Skip to content

Commit

Permalink
yeah
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 20, 2017
1 parent 3f88e96 commit 9494508
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
8 changes: 7 additions & 1 deletion include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
#include <string>
#include <vector>
#include <string>
#include <memory>
#include "Move.h"

class BitBoard;

typedef std::unique_ptr<BitBoard> bup;

class BitBoard
{
private:
Expand Down Expand Up @@ -86,10 +91,11 @@ class BitBoard
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;
bup result(Move move) const;
double utility(std::string player) const;
void printState() const;
};
Expand Down
9 changes: 5 additions & 4 deletions include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

Expand All @@ -11,14 +12,14 @@ class MinimaxSearch
private:
static const int maxDepth = 7;

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;


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

};

#endif

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

typedef std::unique_ptr<BitBoard> bup;

/* Private Functions */

// These are basic 32 bit rotation functions
Expand Down Expand Up @@ -191,6 +194,14 @@ BitBoard::BitBoard(uint32_t black, uint32_t white, uint32_t 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";
Expand All @@ -207,7 +218,7 @@ std::vector<Move> BitBoard::actions() const
return moves;
}

BitBoard BitBoard::result(Move move) const
bup BitBoard::result(Move move) const
{
BitBoard currBoard(*this);
int start{-1};
Expand All @@ -223,13 +234,19 @@ BitBoard BitBoard::result(Move move) const


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 Down
40 changes: 19 additions & 21 deletions src/MinimaxSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,57 @@
#include <limits>
#include <algorithm>
#include <iostream>
#include <memory>
#include "BitBoard.h"
#include "Move.h"
#include "MinimaxSearch.h"

typedef std::unique_ptr<BitBoard> bup;
/* Private Functions */

double MinimaxSearch::minValue(const BitBoard &board, int currentDepth) const
double MinimaxSearch::minValue(bup &board, int currentDepth) const
{
if (currentDepth == maxDepth) return board.utility(board.player());
if (currentDepth == maxDepth) return board->utility(board->player());
double v = std::numeric_limits<double>::max();
for (Move move : board.actions()) {
v = std::min(v, maxValue(board.result(move), currentDepth + 1));
for (Move move : board->actions()) {
bup result = board->result(move);
v = std::min(v, maxValue(result, currentDepth + 1));
}
return v;
}

double MinimaxSearch::maxValue(const BitBoard &board, int currentDepth) const
double MinimaxSearch::maxValue(bup &board, int currentDepth) const
{
if (currentDepth == maxDepth) return board.utility(board.player());
if (currentDepth == maxDepth) return board->utility(board->player());
double v = std::numeric_limits<double>::lowest();
for (Move move : board.actions()) {
v = std::max(v, minValue(board.result(move), currentDepth + 1));
for (Move move : board->actions()) {
bup result = board->result(move);
v = std::max(v, minValue(result, currentDepth + 1));
}
return v;
}

/* Public Functions */

Move MinimaxSearch::minimaxDecision(const BitBoard &board) const
Move MinimaxSearch::minimaxDecision(bup &board) const
{
Move bestMove;
double bestMoveValue{0.0};

std::cout << board.actions().size() << std::endl;

for (Move move : board.actions()) {
for (Move move : board->actions()) {
if (bestMove.isEmpty()) {
bestMove = move;
bestMoveValue = minValue(board.result(bestMove), 0);
bup result = board->result(bestMove);
bestMoveValue = minValue(result, 0);
} else {
double nextMoveValue = minValue(board.result(move), 0);
bup result = board->result(move);
double nextMoveValue = minValue(result, 0);
if (bestMoveValue < nextMoveValue) {
bestMove = move;
bestMoveValue = nextMoveValue;
}
}
}

return bestMove;
}

/*Move MinimaxSearch::minimaxDecision(const BitBoard &board) const
{
return nullptr;
}*/
17 changes: 10 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include <iostream>
#include <cstdint>
#include <vector>
#include <memory>
#include "BitBoard.h"
#include "Move.h"
#include "MinimaxSearch.h"

int main(int argc, const char * argv[]) {
BitBoard board;
MinimaxSearch search;
for (int i = 0; i < 20; i++) {
board.printState();
Move next = search.minimaxDecision(board);
board = board.result(next);
std::cout << "==============================" << std::endl;
std::unique_ptr<BitBoard> board (new BitBoard);
MinimaxSearch searchDriver;

for (int i = 0; i < 20; ++i) {
board->printState();
Move bestMove = searchDriver.minimaxDecision(board);
board = board->result(bestMove);
std::cout << "=======================================" << std::endl;
}

return 0;
}

0 comments on commit 9494508

Please sign in to comment.