style: implement clang-format styling parameters

This commit is contained in:
A.M. Rowsell 2026-09-22 02:06:49 -04:00
commit f2ce83335c
Signed by: amr
GPG key ID: E0879EDBDB0CA7B1
10 changed files with 462 additions and 547 deletions

11
.clang-format Normal file
View file

@ -0,0 +1,11 @@
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
UseTab: Never
PointerAlignment: Right
ReferenceAlignment: Right
SpaceBeforeParens: Never
BreakBeforeBraces: Attach
AccessModifierOffset: -2
AllowShortFunctionsOnASingleLine: Inline
ColumnLimit: 120

303
Board.cpp
View file

@ -36,8 +36,7 @@ Board::Board() {
boardGrid[i].resize(8); boardGrid[i].resize(8);
} }
Board::~Board() { Board::~Board() {}
}
void Board::setupInitialPosition() { void Board::setupInitialPosition() {
// set up the board grid with smart pointers // set up the board grid with smart pointers
@ -45,46 +44,46 @@ void Board::setupInitialPosition() {
playerTurn = PL_WHITE; playerTurn = PL_WHITE;
for(int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++) {
switch(i) { switch(i) {
case 0: case 0:
// white back rank // white back rank
boardGrid[i][0] = std::make_unique<Rook>(PIECE_WHITE); boardGrid[i][0] = std::make_unique<Rook>(PIECE_WHITE);
boardGrid[i][1] = std::make_unique<Knight>(PIECE_WHITE); boardGrid[i][1] = std::make_unique<Knight>(PIECE_WHITE);
boardGrid[i][2] = std::make_unique<Bishop>(PIECE_WHITE); boardGrid[i][2] = std::make_unique<Bishop>(PIECE_WHITE);
boardGrid[i][3] = std::make_unique<Queen>(PIECE_WHITE); boardGrid[i][3] = std::make_unique<Queen>(PIECE_WHITE);
boardGrid[i][4] = std::make_unique<King>(PIECE_WHITE); boardGrid[i][4] = std::make_unique<King>(PIECE_WHITE);
boardGrid[i][5] = std::make_unique<Bishop>(PIECE_WHITE); boardGrid[i][5] = std::make_unique<Bishop>(PIECE_WHITE);
boardGrid[i][6] = std::make_unique<Knight>(PIECE_WHITE); boardGrid[i][6] = std::make_unique<Knight>(PIECE_WHITE);
boardGrid[i][7] = std::make_unique<Rook>(PIECE_WHITE); boardGrid[i][7] = std::make_unique<Rook>(PIECE_WHITE);
break; break;
case 1: case 1:
// white pawn rank // white pawn rank
for(int j = 0; j <= 8; j++) for(int j = 0; j <= 8; j++)
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_WHITE); boardGrid[i][j] = std::make_unique<Pawn>(PIECE_WHITE);
break; break;
case 2: case 2:
case 3: case 3:
case 4: case 4:
case 5: case 5:
for(int j = 0; j <= 8; j++) for(int j = 0; j <= 8; j++)
boardGrid[i][j] = nullptr; boardGrid[i][j] = nullptr;
break; break;
case 6: case 6:
// black pawn rank // black pawn rank
for(int j = 0; j <= 8; j++) for(int j = 0; j <= 8; j++)
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_BLACK); boardGrid[i][j] = std::make_unique<Pawn>(PIECE_BLACK);
break; break;
case 7: case 7:
// black back rank // black back rank
boardGrid[i][0] = std::make_unique<Rook>(PIECE_BLACK); boardGrid[i][0] = std::make_unique<Rook>(PIECE_BLACK);
boardGrid[i][1] = std::make_unique<Knight>(PIECE_BLACK); boardGrid[i][1] = std::make_unique<Knight>(PIECE_BLACK);
boardGrid[i][2] = std::make_unique<Bishop>(PIECE_BLACK); boardGrid[i][2] = std::make_unique<Bishop>(PIECE_BLACK);
boardGrid[i][3] = std::make_unique<Queen>(PIECE_BLACK); boardGrid[i][3] = std::make_unique<Queen>(PIECE_BLACK);
boardGrid[i][4] = std::make_unique<King>(PIECE_BLACK); boardGrid[i][4] = std::make_unique<King>(PIECE_BLACK);
boardGrid[i][5] = std::make_unique<Bishop>(PIECE_BLACK); boardGrid[i][5] = std::make_unique<Bishop>(PIECE_BLACK);
boardGrid[i][6] = std::make_unique<Knight>(PIECE_BLACK); boardGrid[i][6] = std::make_unique<Knight>(PIECE_BLACK);
boardGrid[i][7] = std::make_unique<Rook>(PIECE_BLACK); boardGrid[i][7] = std::make_unique<Rook>(PIECE_BLACK);
default: default:
break; break;
} }
} }
return; return;
@ -99,7 +98,6 @@ void Board::clearBoard() {
} }
} }
int Board::setupFromFEN(std::string strFEN) { int Board::setupFromFEN(std::string strFEN) {
std::vector<std::string> splitFEN = split(strFEN, ' '); std::vector<std::string> splitFEN = split(strFEN, ' ');
if(splitFEN.size() != 6) // a valid FEN *must* contain 6 fields if(splitFEN.size() != 6) // a valid FEN *must* contain 6 fields
@ -113,104 +111,104 @@ int Board::setupFromFEN(std::string strFEN) {
for(auto &ranks : splitField1) { for(auto &ranks : splitField1) {
for(auto &sym : ranks) { for(auto &sym : ranks) {
switch(sym) { switch(sym) {
case 'R': case 'R':
// white rook // white rook
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_WHITE); boardGrid[rank][file] = std::make_unique<Rook>(PIECE_WHITE);
file++; file++;
break; break;
case 'r': case 'r':
// black rook // black rook
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_BLACK); boardGrid[rank][file] = std::make_unique<Rook>(PIECE_BLACK);
file++; file++;
break; break;
case 'N': case 'N':
// white knight // white knight
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_WHITE); boardGrid[rank][file] = std::make_unique<Knight>(PIECE_WHITE);
file++; file++;
break; break;
case 'n': case 'n':
// black knight // black knight
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_BLACK); boardGrid[rank][file] = std::make_unique<Knight>(PIECE_BLACK);
file++; file++;
break; break;
case 'B': case 'B':
// white bishop // white bishop
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_WHITE); boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_WHITE);
file++; file++;
break; break;
case 'b': case 'b':
// black bishop // black bishop
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_BLACK); boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_BLACK);
file++; file++;
break; break;
case 'Q': case 'Q':
// white queen // white queen
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_WHITE); boardGrid[rank][file] = std::make_unique<Queen>(PIECE_WHITE);
file++; file++;
break; break;
case 'q': case 'q':
// black queen; // black queen;
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_BLACK); boardGrid[rank][file] = std::make_unique<Queen>(PIECE_BLACK);
file++; file++;
break; break;
case 'K': case 'K':
// white king // white king
if(wKingPlaced) { if(wKingPlaced) {
// invalid FEN, more than 1 white king // invalid FEN, more than 1 white king
return -1;
} else {
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
wKingPlaced = true;
// store king position for later
whiteKing.file = static_cast<File>(file);
whiteKing.rank = static_cast<Rank>(rank);
file++;
}
break;
case 'k':
// black king
if(bKingPlaced) {
// invalid FEN, more than 1 black king
return -1;
} else {
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
bKingPlaced = true;
// store king position for later
blackKing.file = static_cast<File>(file);
blackKing.rank = static_cast<Rank>(rank);
file++;
}
break;
case 'P':
// white pawn
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
file++;
break;
case 'p':
// black pawn
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
file++;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8': {
std::string skipStr(1, sym);
skip = atoi(skipStr.c_str());
//skip--; // fix off by 1 error
while(skip--) {
boardGrid[rank][file++] = nullptr; // remove reference
}
break;
}
default:
// invalid character?
return -1; return -1;
break; } else {
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
wKingPlaced = true;
// store king position for later
whiteKing.file = static_cast<File>(file);
whiteKing.rank = static_cast<Rank>(rank);
file++;
}
break;
case 'k':
// black king
if(bKingPlaced) {
// invalid FEN, more than 1 black king
return -1;
} else {
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
bKingPlaced = true;
// store king position for later
blackKing.file = static_cast<File>(file);
blackKing.rank = static_cast<Rank>(rank);
file++;
}
break;
case 'P':
// white pawn
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
file++;
break;
case 'p':
// black pawn
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
file++;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8': {
std::string skipStr(1, sym);
skip = atoi(skipStr.c_str());
// skip--; // fix off by 1 error
while(skip--) {
boardGrid[rank][file++] = nullptr; // remove reference
}
break;
}
default:
// invalid character?
return -1;
break;
} }
if(file > 7) { if(file > 7) {
file = 0; file = 0;
@ -267,17 +265,17 @@ int Board::setupFromFEN(std::string strFEN) {
// ====== END OF FIELD 3 ====== // ====== END OF FIELD 3 ======
// ====== START OF FIELD 4 ====== // ====== START OF FIELD 4 ======
// if a pawn has just moved and can be attacked enPassant, it will be listed here // if a pawn has just moved and can be attacked enPassant, it will be listed here
//#ifdef DEBUG // #ifdef DEBUG
// for(auto &ranks : boardGrid) { // for(auto &ranks : boardGrid) {
// for(auto &files : ranks) { // for(auto &files : ranks) {
// if(files) // if(files)
// std::cout << files->getPieceName() << " "; // std::cout << files->getPieceName() << " ";
// else // else
// std::cout << "E "; // std::cout << "E ";
// } // }
// std::cout << "\n"; // std::cout << "\n";
// } // }
//#endif // #endif
return 0; return 0;
} }
@ -289,7 +287,8 @@ void Board::nextTurn() {
void Board::movePiece(Square from, Square to) { void Board::movePiece(Square from, Square to) {
Piece *movingPiece = getPieceAt(from); Piece *movingPiece = getPieceAt(from);
if(!movingPiece) return; if(!movingPiece)
return;
Square newEnPassantTarget = {INVALID_RANK, INVALID_FILE}; Square newEnPassantTarget = {INVALID_RANK, INVALID_FILE};

141
Piece.cpp
View file

@ -49,14 +49,14 @@ bool Piece::finalMoveChecks(std::vector<Move> *moveList, Board &board) {
// *INDENT-ON* // *INDENT-ON*
bool King::checkForCheck(Board &board) const { bool King::checkForCheck(Board &board) const {
std::vector<std::vector<int>> kingVulnerable = { std::vector<std::vector<int>> kingVulnerable = {
{-1, 0}, // Up {-1, 0}, // Up
{-1, -1}, // up-left {-1, -1}, // up-left
{-1, 1}, // up-right {-1, 1}, // up-right
{1, 0}, // Down {1, 0}, // Down
{1, -1}, // down-left {1, -1}, // down-left
{1, 1}, // down-right {1, 1}, // down-right
{0, -1}, // Left {0, -1}, // Left
{0, 1}, // Right {0, 1}, // Right
{-1, -2}, // 1 {-1, -2}, // 1
{-1, 2}, // 2 {-1, 2}, // 2
{-2, -1}, // 3 {-2, -1}, // 3
@ -73,14 +73,14 @@ bool King::checkForCheck(Board &board) const {
std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {
{-1, 0}, // Up {-1, 0}, // Up
{-1, -1}, // up-left {-1, -1}, // up-left
{-1, 1}, // up-right {-1, 1}, // up-right
{1, 0}, // Down {1, 0}, // Down
{1, -1}, // down-left {1, -1}, // down-left
{1, 1}, // down-right {1, 1}, // down-right
{0, -1}, // Left {0, -1}, // Left
{0, 1} // Right {0, 1} // Right
}; };
for(auto &dir : directions) { for(auto &dir : directions) {
// establish r/f for square to check // establish r/f for square to check
@ -89,10 +89,11 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
if(targetSquare.isValid()) { if(targetSquare.isValid()) {
const Piece *target = board.getPieceAt(targetSquare); // examine the target square const Piece *target = board.getPieceAt(targetSquare); // examine the target square
if(!target) { // if square is empty (nullptr) if(!target) { // if square is empty (nullptr)
moveList.push_back({from, targetSquare}); // then it's potentially a legal move moveList.push_back({from, targetSquare}); // then it's potentially a legal move
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour } else if(target &&
moveList.push_back({from, targetSquare}); // then again it's potentially legal target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
moveList.push_back({from, targetSquare}); // then again it's potentially legal
break; break;
} else { } else {
// otherwise it's one of our pieces // otherwise it's one of our pieces
@ -128,11 +129,11 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
directions.insert(directions.end(), knightAttacks.begin(), knightAttacks.end()); // concatenate the knight attacks directions.insert(directions.end(), knightAttacks.begin(), knightAttacks.end()); // concatenate the knight attacks
for(auto &moves : moveList) { for(auto &moves : moveList) {
Square startSquare = moves.to; // get the potential moveto square Square startSquare = moves.to; // get the potential moveto square
for(auto &dir : directions) { // now go through all directions for(auto &dir : directions) { // now go through all directions
int r = startSquare.rank + dir[0]; int r = startSquare.rank + dir[0];
int f = startSquare.file + dir[1]; int f = startSquare.file + dir[1];
bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal
bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack
while(r >= 0 && r < 8 && f >= 0 && f < 8) { while(r >= 0 && r < 8 && f >= 0 && f < 8) {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
const Piece *target = board.getPieceAt(targetSquare); // access the square we're examining const Piece *target = board.getPieceAt(targetSquare); // access the square we're examining
@ -149,12 +150,14 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
// we are being attacked by a knight, so remove it // we are being attacked by a knight, so remove it
moves.to.rank = INVALID_RANK; moves.to.rank = INVALID_RANK;
moves.to.file = INVALID_FILE; moves.to.file = INVALID_FILE;
} else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) && (!diagonal && !knight)) { } else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) &&
(!diagonal && !knight)) {
// again, being attacked, remove it // again, being attacked, remove it
moves.to.rank = INVALID_RANK; moves.to.rank = INVALID_RANK;
moves.to.file = INVALID_FILE; moves.to.file = INVALID_FILE;
} }
if(target->getPieceType() == PAWN && (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) { if(target->getPieceType() == PAWN &&
(abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) {
moves.to.rank = INVALID_RANK; moves.to.rank = INVALID_RANK;
moves.to.file = INVALID_FILE; moves.to.file = INVALID_FILE;
} }
@ -173,11 +176,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
} }
} }
// will this actually work? // will this actually work?
moveList.erase( moveList.erase(std::remove_if(moveList.begin(), moveList.end(), [&](const auto &x) { return !x.to.isValid(); }));
std::remove_if(moveList.begin(), moveList.end(),
[&](const auto & x) {
return !x.to.isValid();
}));
return moveList; return moveList;
} }
@ -197,9 +196,9 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[4][2] = { const int directions[4][2] = {
{-1, 0}, // Up {-1, 0}, // Up
{1, 0}, // Down {1, 0}, // Down
{0, -1}, // Left {0, -1}, // Left
{0, 1} // Right {0, 1} // Right
}; };
for(auto &dir : directions) { for(auto &dir : directions) {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
@ -234,14 +233,14 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {
{-1, 0}, // Up {-1, 0}, // Up
{-1, -1}, // up-left {-1, -1}, // up-left
{-1, 1}, // up-right {-1, 1}, // up-right
{1, 0}, // Down {1, 0}, // Down
{1, -1}, // down-left {1, -1}, // down-left
{1, 1}, // down-right {1, 1}, // down-right
{0, -1}, // Left {0, -1}, // Left
{0, 1} // Right {0, 1} // Right
}; };
for(auto &dir : directions) { for(auto &dir : directions) {
// establish r/f for square to check // establish r/f for square to check
@ -249,11 +248,12 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
int f = from.file + dir[1]; int f = from.file + dir[1];
while(r >= 0 && r < 8 && f >= 0 && f < 8) { while(r >= 0 && r < 8 && f >= 0 && f < 8) {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
const Piece *target = board.getPieceAt(targetSquare);// examine the target square const Piece *target = board.getPieceAt(targetSquare); // examine the target square
if(!target) { // if square is empty (NULL) if(!target) { // if square is empty (NULL)
moveList.push_back({from, targetSquare}); // then it's potentially a legal move moveList.push_back({from, targetSquare}); // then it's potentially a legal move
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour } else if(target &&
moveList.push_back({from, targetSquare}); // then again it's potentially legal target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
moveList.push_back({from, targetSquare}); // then again it's potentially legal
break; break;
} else { } else {
// otherwise it's one of our pieces // otherwise it's one of our pieces
@ -280,16 +280,7 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {{-1, -2}, {-1, 2}, {-2, -1}, {-2, 1}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
{-1, -2},
{-1, 2},
{-2, -1},
{-2, 1},
{1, -2},
{1, 2},
{2, -1},
{2, 1}
};
for(auto &dir : directions) { for(auto &dir : directions) {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
@ -320,9 +311,9 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {
{-1, -1}, // up-left {-1, -1}, // up-left
{-1, 1}, // up-right {-1, 1}, // up-right
{1, -1}, // down-left {1, -1}, // down-left
{1, 1}, // down-right {1, 1}, // down-right
}; };
for(auto &dir : directions) { for(auto &dir : directions) {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
@ -358,33 +349,34 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const
std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[2][4][2] = { const int directions[2][4][2] = {{
{ // black
// black {-1, 0}, // Up
{-1, 0}, // Up {-2, 0}, // 2up first move
{-2, 0}, // 2up first move {-1, 1}, // attack to right
{-1, 1}, // attack to right {-1, -1} // attack to left
{-1, -1} // attack to left },
}, {
{ // white
// white {1, 0}, // down
{1, 0}, // down {2, 0}, // 2down first move
{2, 0}, // 2down first move {1, 1}, // attack to right
{1, 1}, // attack to right {1, -1} // attack to left
{1, -1} // attack to left }};
}
};
if(this->getColour() == PIECE_BLACK) { if(this->getColour() == PIECE_BLACK) {
// go through black options // go through black options
for(auto &dir : directions[0]) { for(auto &dir : directions[0]) {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions if(r >= 0 && r < 8 && f >= 0 &&
f < 8) { // no need for a while loop as we only have finite moves in limited directions
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
const Piece *target = board.getPieceAt(targetSquare); const Piece *target = board.getPieceAt(targetSquare);
if(dir[0] == -2 && !this->checkIfMoved()) { if(dir[0] == -2 && !this->checkIfMoved()) {
// then 2 is potentially legal // then 2 is potentially legal
if(!target && board.isSquareEmpty(Square{static_cast<Rank>(r + 1), static_cast<File>(f)})) // check both squares for pieces of any colour if(!target &&
board.isSquareEmpty(Square{static_cast<Rank>(r + 1),
static_cast<File>(f)})) // check both squares for pieces of any colour
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;
@ -410,7 +402,8 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
// go through white options // go through white options
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions if(r >= 0 && r < 8 && f >= 0 &&
f < 8) { // no need for a while loop as we only have finite moves in limited directions
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
const Piece *target = board.getPieceAt(targetSquare); const Piece *target = board.getPieceAt(targetSquare);
if(dir[0] == 2 && !this->checkIfMoved()) { if(dir[0] == 2 && !this->checkIfMoved()) {
@ -419,7 +412,7 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;
} else if(abs(dir[1]) == 1) { // attempted capture (diagonal) } else if(abs(dir[1]) == 1) { // attempted capture (diagonal)
if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture
// legal capture // legal capture
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});

View file

@ -1,90 +1,63 @@
#include <cstdint> #include <cstdint>
#include <vector>
#include <memory> #include <memory>
#include <vector>
/* Chess_Queen_White */ /* Chess_Queen_White */
#define Chess_Queen_White_width 45 #define Chess_Queen_White_width 45
#define Chess_Queen_White_height 45 #define Chess_Queen_White_height 45
const uint8_t Chess_Queen_White_bits[] = { const uint8_t Chess_Queen_White_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x60, 0xf0, 0xc1, 0x00, 0x00, 0x00, 0xf0, 0x90, 0xe1, 0x01, 0x00, 0x00, 0x98, 0x91, 0x31, 0x03, 0x00,
0x00, 0x60, 0xf0, 0xc1, 0x00, 0x00, 0x00, 0xf0, 0x90, 0xe1, 0x01, 0x00, 0x60, 0x98, 0xf1, 0x31, 0xc3, 0x00, 0xf0, 0xf0, 0xe0, 0xe0, 0xe1, 0x01, 0x98, 0x61, 0x60, 0xe0, 0x30, 0x03,
0x00, 0x98, 0x91, 0x31, 0x03, 0x00, 0x60, 0x98, 0xf1, 0x31, 0xc3, 0x00, 0x98, 0xe1, 0x60, 0x60, 0x30, 0x03, 0xf0, 0xe0, 0xe0, 0x60, 0xe0, 0x01, 0xe0, 0xe0, 0xe0, 0x70, 0xe0, 0x00,
0xf0, 0xf0, 0xe0, 0xe0, 0xe1, 0x01, 0x98, 0x61, 0x60, 0xe0, 0x30, 0x03, 0xc0, 0xe0, 0xb1, 0x70, 0x70, 0x00, 0xc0, 0xe1, 0xb1, 0x50, 0x38, 0x00, 0xc0, 0x63, 0xb1, 0x59, 0x38, 0x00,
0x98, 0xe1, 0x60, 0x60, 0x30, 0x03, 0xf0, 0xe0, 0xe0, 0x60, 0xe0, 0x01, 0x80, 0x62, 0x93, 0xc9, 0x3c, 0x00, 0x80, 0x66, 0x93, 0xcd, 0x36, 0x00, 0x80, 0x6c, 0x1e, 0xcd, 0x36, 0x00,
0xe0, 0xe0, 0xe0, 0x70, 0xe0, 0x00, 0xc0, 0xe0, 0xb1, 0x70, 0x70, 0x00, 0x80, 0x6d, 0x1e, 0xc7, 0x13, 0x00, 0x80, 0x79, 0x0c, 0xc7, 0x11, 0x00, 0x00, 0x71, 0x0c, 0xc7, 0x19, 0x00,
0xc0, 0xe1, 0xb1, 0x50, 0x38, 0x00, 0xc0, 0x63, 0xb1, 0x59, 0x38, 0x00, 0x00, 0xf1, 0xff, 0xff, 0x19, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x1f, 0x00,
0x80, 0x62, 0x93, 0xc9, 0x3c, 0x00, 0x80, 0x66, 0x93, 0xcd, 0x36, 0x00, 0x00, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0xff, 0x1f, 0x0e, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00,
0x80, 0x6c, 0x1e, 0xcd, 0x36, 0x00, 0x80, 0x6d, 0x1e, 0xc7, 0x13, 0x00, 0x00, 0x18, 0x00, 0x80, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x01, 0x00,
0x80, 0x79, 0x0c, 0xc7, 0x11, 0x00, 0x00, 0x71, 0x0c, 0xc7, 0x19, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00,
0x00, 0xf1, 0xff, 0xff, 0x19, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x04, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00,
0x00, 0x0f, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x06, 0xff, 0x1f, 0x0e, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
0x00, 0x18, 0x00, 0x80, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0x00,
0x00, 0xf8, 0xff, 0xff, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00,
0x00, 0x0c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00,
0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x04, 0x00,
0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Chess King White */ /* Chess King White */
#define Chess_King_White_width 45 #define Chess_King_White_width 45
#define Chess_King_White_height 45 #define Chess_King_White_height 45
const uint8_t Chess_King_White_bits[] = { const uint8_t Chess_King_White_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x78, 0x08, 0xc2, 0x03, 0x00, 0x00, 0xff, 0x0f, 0xfe, 0x1f, 0x00, 0x80, 0x03, 0x0f, 0x1e, 0x38, 0x00,
0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00, 0xc0, 0x00, 0x1c, 0x07, 0x60, 0x00, 0xc0, 0x00, 0x18, 0x03, 0x60, 0x00, 0x60, 0x00, 0xb0, 0x01, 0xc0, 0x00,
0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x78, 0x08, 0xc2, 0x03, 0x00, 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x40, 0x00, 0xc0, 0x00,
0x00, 0xff, 0x0f, 0xfe, 0x1f, 0x00, 0x80, 0x03, 0x0f, 0x1e, 0x38, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0x80, 0x01, 0x40, 0x00, 0x30, 0x00,
0xc0, 0x00, 0x1c, 0x07, 0x60, 0x00, 0xc0, 0x00, 0x18, 0x03, 0x60, 0x00, 0x00, 0x03, 0xfe, 0x0f, 0x18, 0x00, 0x00, 0xce, 0xff, 0x7f, 0x0e, 0x00, 0x00, 0xfc, 0x00, 0xe0, 0x07, 0x00,
0x60, 0x00, 0xb0, 0x01, 0xc0, 0x00, 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x00, 0x30, 0xf0, 0x81, 0x01, 0x00, 0x00, 0x90, 0xff, 0x3f, 0x01, 0x00, 0x00, 0xf0, 0x03, 0xf8, 0x01, 0x00,
0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x40, 0x00, 0xc0, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x10, 0xfe, 0x0f, 0x01, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00,
0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0x00, 0xf0, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x07, 0x7c, 0x00, 0x00,
0x80, 0x01, 0x40, 0x00, 0x30, 0x00, 0x00, 0x03, 0xfe, 0x0f, 0x18, 0x00, 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xce, 0xff, 0x7f, 0x0e, 0x00, 0x00, 0xfc, 0x00, 0xe0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
0x00, 0x30, 0xf0, 0x81, 0x01, 0x00, 0x00, 0x90, 0xff, 0x3f, 0x01, 0x00,
0x00, 0xf0, 0x03, 0xf8, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00,
0x00, 0x10, 0xfe, 0x0f, 0x01, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00,
0x00, 0xf0, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00,
0x00, 0xc0, 0x07, 0x7c, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* Chess Rook White */ /* Chess Rook White */
#define Chess_Rook_White_width 45 #define Chess_Rook_White_width 45
#define Chess_Rook_White_height 45 #define Chess_Rook_White_height 45
const uint8_t Chess_Rook_White_bits[] = { const uint8_t Chess_Rook_White_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xf8, 0xe3, 0x07, 0x00, 0x00, 0xcc, 0x18, 0x63, 0x06, 0x00, 0x00, 0xcc, 0x1f, 0x7f, 0x06, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xf8, 0xe3, 0x07, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00,
0x00, 0xcc, 0x18, 0x63, 0x06, 0x00, 0x00, 0xcc, 0x1f, 0x7f, 0x06, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00,
0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00,
0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00,
0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00,
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00,
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00,
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00,
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00,
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00,
0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00,
0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00,
0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00,
0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

View file

@ -17,11 +17,11 @@
#ifndef BOARD_HPP #ifndef BOARD_HPP
#define BOARD_HPP #define BOARD_HPP
#include "Piece.hpp"
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Piece.hpp"
class Piece; class Piece;
/** @enum Players /** @enum Players
@ -44,22 +44,19 @@ struct Square;
*/ */
class Board { class Board {
private: private:
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid; /**< This holds the game state. std::vector<std::vector<std::unique_ptr<Piece>>>
* It is a 2D vector of Piece types, or nullptr for empty squares boardGrid; /**< This holds the game state.
* @image html boardGrid.svg "Logical diagram of boardGrid vector" */ * It is a 2D vector of Piece types, or nullptr for empty squares
* @image html boardGrid.svg "Logical diagram of boardGrid vector" */
Players playerTurn; Players playerTurn;
Square enPassantTargetSquare = {INVALID_RANK, INVALID_FILE}; Square enPassantTargetSquare = {INVALID_RANK, INVALID_FILE};
// let's get super object-oriented, baby // let's get super object-oriented, baby
// these help the getters and setters access the boardGrid // these help the getters and setters access the boardGrid
// and also make them shorter and less duplicative // and also make them shorter and less duplicative
std::unique_ptr<Piece> &at(int r, int f) { std::unique_ptr<Piece> &at(int r, int f) { return boardGrid[r][f]; }
return boardGrid[r][f];
}
const std::unique_ptr<Piece> &at(int r, int f) const { const std::unique_ptr<Piece> &at(int r, int f) const { return boardGrid[r][f]; }
return boardGrid[r][f];
}
std::unique_ptr<Piece> &at(const Square &sq) { std::unique_ptr<Piece> &at(const Square &sq) {
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)]; return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
@ -68,6 +65,7 @@ class Board {
const std::unique_ptr<Piece> &at(const Square &sq) const { const std::unique_ptr<Piece> &at(const Square &sq) const {
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)]; return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
} }
public: public:
Board(); Board();
virtual ~Board(); virtual ~Board();
@ -75,52 +73,26 @@ class Board {
// These are to allow Piece to access Board in a controlled way // These are to allow Piece to access Board in a controlled way
// instead of adding a friend declaration for every subclass // instead of adding a friend declaration for every subclass
// ----- Getters ----- // ----- Getters -----
Piece *getPieceAt(int r, int f) { Piece *getPieceAt(int r, int f) { return at(r, f).get(); }
return at(r, f).get(); const Piece *getPieceAt(int r, int f) const { return at(r, f).get(); }
}
const Piece *getPieceAt(int r, int f) const {
return at(r, f).get();
}
Piece *getPieceAt(const Square &sq) { Piece *getPieceAt(const Square &sq) { return at(sq).get(); }
return at(sq).get(); const Piece *getPieceAt(const Square &sq) const { return at(sq).get(); }
} Square getEnPassantTargetSquare() const { return enPassantTargetSquare; }
const Piece *getPieceAt(const Square &sq) const {
return at(sq).get();
}
Square getEnPassantTargetSquare() const {
return enPassantTargetSquare;
}
// ----- Setters ----- // ----- Setters -----
void setPieceAt(int r, int f, std::unique_ptr<Piece> piece) { void setPieceAt(int r, int f, std::unique_ptr<Piece> piece) { at(r, f) = std::move(piece); }
at(r, f) = std::move(piece); void setPieceAt(const Square &sq, std::unique_ptr<Piece> piece) { at(sq) = std::move(piece); }
}
void setPieceAt(const Square &sq, std::unique_ptr<Piece> piece) {
at(sq) = std::move(piece);
}
void clearSquare(int r, int f) { void clearSquare(int r, int f) { at(r, f).reset(); }
at(r, f).reset(); void clearSquare(const Square &sq) { at(sq).reset(); }
}
void clearSquare(const Square &sq) {
at(sq).reset();
}
void setEnPassantTargetSquare(Square sq) { void setEnPassantTargetSquare(Square sq) { enPassantTargetSquare = sq; }
enPassantTargetSquare = sq; void clearEnPassantTargetSquare() { enPassantTargetSquare = {INVALID_RANK, INVALID_FILE}; }
}
void clearEnPassantTargetSquare() {
enPassantTargetSquare = {INVALID_RANK, INVALID_FILE};
}
// ----- Utility ----- // ----- Utility -----
bool isSquareEmpty(int r, int f) const { bool isSquareEmpty(int r, int f) const { return at(r, f) == nullptr; }
return at(r, f) == nullptr; bool isSquareEmpty(const Square &sq) const { return at(sq) == nullptr; }
}
bool isSquareEmpty(const Square &sq) const {
return at(sq) == nullptr;
}
/** A function to setup the initial board /** A function to setup the initial board
* *

View file

@ -13,9 +13,7 @@
class NeoPixel { class NeoPixel {
public: public:
NeoPixel(uint8_t stringLength) : length(stringLength) { NeoPixel(uint8_t stringLength) : length(stringLength) { gpioPin = LATBbits.LATB9; };
gpioPin = LATBbits.LATB9;
};
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue); uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void allPixelsOff(void); void allPixelsOff(void);
@ -27,4 +25,3 @@ class NeoPixel {
}; };
#endif // NEOPIXEL_HPP #endif // NEOPIXEL_HPP

View file

@ -22,34 +22,24 @@
* it helps make it obvious what certain bits of code are doing. * it helps make it obvious what certain bits of code are doing.
*/ */
#include <cstdint>
#include <algorithm> #include <algorithm>
#include <utility> #include <cstdint>
#include <string>
#include <memory> #include <memory>
#include <string>
#include <utility>
#include <vector> #include <vector>
class Board; class Board;
enum PieceType { enum PieceType { PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY };
PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY
};
enum PieceColour { enum PieceColour { PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY };
PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY
};
enum Rank { enum Rank { R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7, INVALID_RANK = 255 };
R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7, INVALID_RANK = 255
};
enum File { enum File { A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, INVALID_FILE = 255 };
A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, INVALID_FILE = 255
};
enum CastleSide { enum CastleSide { KINGSIDE = 1, QUEENSIDE = 2 };
KINGSIDE = 1, QUEENSIDE = 2
};
/** @struct Square /** @struct Square
* A struct that represents a square on the chess board. * A struct that represents a square on the chess board.
@ -62,9 +52,7 @@ struct Square {
* A function to test if the square is a valid square on the chessboard * A function to test if the square is a valid square on the chessboard
* @return True if the square is within the chessboard, False otherwise * @return True if the square is within the chessboard, False otherwise
*/ */
bool isValid() const { bool isValid() const { return rank >= 0 && rank < 8 && file >= 0 && file < 8; }
return rank >= 0 && rank < 8 && file >= 0 && file < 8;
}
}; };
/** @struct Move /** @struct Move
* A struct representing the start and end squares of a Move. * A struct representing the start and end squares of a Move.
@ -84,40 +72,28 @@ struct Move {
class Piece { class Piece {
private: private:
friend class Board; friend class Board;
protected: protected:
PieceColour colour; PieceColour colour;
PieceType pieceType; PieceType pieceType;
std::string pieceName; /**< std::string pieceName is a string showing the full name of the piece, i.e. "PAWN" */ std::string pieceName; /**< std::string pieceName is a string showing the full name of the piece, i.e. "PAWN" */
std::string std::string pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess
pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess notation, ie N for Knight */ notation, ie N for Knight */
bool hasMoved = false; bool hasMoved = false;
public: public:
Piece(PieceColour pColour) : colour(pColour) {}
Piece(PieceColour pColour) : colour(pColour) {
}
virtual ~Piece(); virtual ~Piece();
PieceColour getColour() const { return colour; }
PieceColour getColour() const { std::string getPieceName() const { return pieceName; }
return colour;
}
std::string getPieceName() const { std::string getPieceSymbol() const { return pieceSymbol; }
return pieceName;
}
std::string getPieceSymbol() const { PieceType getPieceType() const { return pieceType; }
return pieceSymbol;
}
PieceType getPieceType() const { bool checkIfMoved() const { return hasMoved; }
return pieceType;
}
bool checkIfMoved() const {
return hasMoved;
}
/** /**
* A virtual const function that should get all legal moves for a piece, but it may * A virtual const function that should get all legal moves for a piece, but it may
* include some moves that need to be pruned, like those exposing the king to check. * include some moves that need to be pruned, like those exposing the king to check.
@ -130,14 +106,13 @@ class Piece {
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const;
bool finalMoveChecks(std::vector<Move> *moveList, Board &board); bool finalMoveChecks(std::vector<Move> *moveList, Board &board);
}; };
class King : public Piece { class King : public Piece {
private: private:
friend class Board; friend class Board;
public:
public:
King(PieceColour colour) : Piece(colour) { King(PieceColour colour) : Piece(colour) {
pieceName = "King"; pieceName = "King";
pieceSymbol = "K"; pieceSymbol = "K";
@ -156,12 +131,9 @@ class King : public Piece {
return false; return false;
} }
void setCastleQS(bool canCastle) { void setCastleQS(bool canCastle) { canCastleQS = canCastle; }
canCastleQS = canCastle; void setCastleKS(bool canCastle) { canCastleKS = canCastle; }
}
void setCastleKS(bool canCastle) {
canCastleKS = canCastle;
}
protected: protected:
bool canCastleQS = true; bool canCastleQS = true;
bool canCastleKS = true; bool canCastleKS = true;
@ -170,8 +142,8 @@ class King : public Piece {
class Rook : public Piece { class Rook : public Piece {
friend class Board; friend class Board;
public:
public:
Rook(PieceColour colour) : Piece(colour) { Rook(PieceColour colour) : Piece(colour) {
pieceName = "Rook"; pieceName = "Rook";
pieceSymbol = "R"; pieceSymbol = "R";
@ -182,8 +154,8 @@ class Rook : public Piece {
class Queen : public Piece { class Queen : public Piece {
friend class Board; friend class Board;
public:
public:
Queen(PieceColour colour) : Piece(colour) { Queen(PieceColour colour) : Piece(colour) {
pieceName = "Queen"; pieceName = "Queen";
pieceSymbol = "Q"; pieceSymbol = "Q";
@ -194,8 +166,8 @@ class Queen : public Piece {
class Knight : public Piece { class Knight : public Piece {
friend class Board; friend class Board;
public:
public:
Knight(PieceColour colour) : Piece(colour) { Knight(PieceColour colour) : Piece(colour) {
pieceName = "Knight"; pieceName = "Knight";
pieceSymbol = "N"; pieceSymbol = "N";
@ -206,8 +178,8 @@ class Knight : public Piece {
class Bishop : public Piece { class Bishop : public Piece {
friend class Board; friend class Board;
public:
public:
Bishop(PieceColour colour) : Piece(colour) { Bishop(PieceColour colour) : Piece(colour) {
pieceName = "Bishop"; pieceName = "Bishop";
pieceSymbol = "B"; pieceSymbol = "B";
@ -218,8 +190,8 @@ class Bishop : public Piece {
class Pawn : public Piece { class Pawn : public Piece {
friend class Board; friend class Board;
public:
public:
Pawn(PieceColour colour) : Piece(colour) { Pawn(PieceColour colour) : Piece(colour) {
pieceName = "Pawn"; pieceName = "Pawn";
pieceSymbol = "P"; pieceSymbol = "P";
@ -227,8 +199,8 @@ class Pawn : public Piece {
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
void promote(const Square &promotionSquare, Board &board, PieceType promoteTo); void promote(const Square &promotionSquare, Board &board, PieceType promoteTo);
protected: protected:
bool firstMove = true; bool firstMove = true;
}; };
#endif // PIECE_HPP #endif // PIECE_HPP

View file

@ -6,18 +6,15 @@
*/ */
#ifndef STRFUNCS_HPP #ifndef STRFUNCS_HPP
#define STRFUNCS_HPP #define STRFUNCS_HPP
#include <string>
#include <sstream>
#include <vector>
#include <iterator> #include <iterator>
#include <sstream>
#include <string>
#include <vector>
template <typename Out> template <typename Out> void split(const std::string &s, char delim, Out result);
void split(const std::string &s, char delim, Out result);
std::vector<std::string> split(const std::string &s, char delim); std::vector<std::string> split(const std::string &s, char delim);
#endif /* STRFUNCS_HPP */
#endif /* STRFUNCS_HPP */

142
main.cpp
View file

@ -10,51 +10,51 @@
// PIC32MX270F256B Configuration Bit Settings // PIC32MX270F256B Configuration Bit Settings
// 'C' source line config statements // 'C' source line config statements
// DEVCFG3 // DEVCFG3
#pragma config USERID = 0xBEEF // Enter Hexadecimal value (Enter Hexadecimal value) #pragma config USERID = 0xBEEF // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations) #pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations) #pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module) #pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module) #pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module)
// DEVCFG2 // DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider) #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier) 96MHz #pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier) 96MHz
#pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (2x Divider) 48MHz #pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (2x Divider) 48MHz
#pragma config UPLLEN = ON // USB PLL Enable (Enabled) #pragma config UPLLEN = ON // USB PLL Enable (Enabled)
#pragma config FPLLODIV = DIV_8 // System PLL Output Clock Divider (PLL Divide by 8) 12MHz #pragma config FPLLODIV = DIV_8 // System PLL Output Clock Divider (PLL Divide by 8) 12MHz
// DEVCFG1 // DEVCFG1
#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL) #pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled) #pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled) #pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled) #pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled) #pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2) #pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576) #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode) #pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls)) #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%) #pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0 // DEVCFG0
#pragma config JTAGEN = ON // JTAG Enable (JTAG Port Enabled) #pragma config JTAGEN = ON // JTAG Enable (JTAG Port Enabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable) #pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled) #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled) #pragma config CP = OFF // Code Protect (Protection Disabled)
// #pragma config statements should precede project file includes. // #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF. // Use project enums instead of #define for ON and OFF.
#include <xc.h> #include <sys/attribs.h> // for ISR macros
#include <sys/attribs.h> // for ISR macros
#include <sys/kmem.h> #include <sys/kmem.h>
#include <xc.h>
#include "inc/Board.hpp" #include "inc/Board.hpp"
#include "inc/Piece.hpp"
#include "inc/NeoPixel.hpp" #include "inc/NeoPixel.hpp"
#include "inc/Piece.hpp"
#define F_CPU 12000000UL // 12MHz #define F_CPU 12000000UL // 12MHz
#define F_PER 6000000UL // 6MHz #define F_PER 6000000UL // 6MHz
#define SPI_BAUD 1000000 // 1MHz hopefully #define SPI_BAUD 1000000 // 1MHz hopefully
volatile uint8_t spi_rx_buffer[8]; volatile uint8_t spi_rx_buffer[8];
@ -69,7 +69,8 @@ extern "C" int open(const char *buf, int flags, int mode) {
void sendChar(uint8_t c) { void sendChar(uint8_t c) {
U1TXREG = c; U1TXREG = c;
while(!U1STAbits.TRMT); // wait for transmission while(!U1STAbits.TRMT)
; // wait for transmission
return; return;
} }
@ -103,7 +104,7 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
* *
*/ */
#define SL_TRIS TRISBbits.TRISB9 #define SL_TRIS TRISBbits.TRISB9
#define SL_LAT LATBbits.LATB9 #define SL_LAT LATBbits.LATB9
uint8_t initSystem(void) { uint8_t initSystem(void) {
/* set up GPIO */ /* set up GPIO */
@ -111,45 +112,45 @@ uint8_t initSystem(void) {
SYSKEY = 0xAA996655; SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA; SYSKEY = 0x556699AA;
CFGCON &= ~(1 << 13); // unlock PPS CFGCON &= ~(1 << 13); // unlock PPS
SDI1R = 0b0100; // RB8 SDI1R = 0b0100; // RB8
RPA0R = 0b0001; // U1TX RPA0R = 0b0001; // U1TX
SYSKEY = 0x12345678; // lock SYSKEY SYSKEY = 0x12345678; // lock SYSKEY
ANSELBCLR = 0xFFFF; // port B all digital ANSELBCLR = 0xFFFF; // port B all digital
SL_TRIS = 0; // RA0 as output SL_TRIS = 0; // RA0 as output
SL_LAT = 1; // set high SL_LAT = 1; // set high
/* Set up SPI1 */ /* Set up SPI1 */
SPI1CON = 0; // reset SPI config SPI1CON = 0; // reset SPI config
SPI1BRG = (F_PER / (2 * SPI_BAUD)) - 1; // calculate for 1MHz SPI1BRG = (F_PER / (2 * SPI_BAUD)) - 1; // calculate for 1MHz
SPI1STATCLR = _SPI1STAT_SPIROV_MASK; // Clear overflow SPI1STATCLR = _SPI1STAT_SPIROV_MASK; // Clear overflow
SPI1CONbits.MSTEN = 1; // Enable Master mode SPI1CONbits.MSTEN = 1; // Enable Master mode
SPI1CONbits.CKP = 0; // Clock idle low SPI1CONbits.CKP = 0; // Clock idle low
SPI1CONbits.CKE = 1; // Transmit on falling edge SPI1CONbits.CKE = 1; // Transmit on falling edge
SPI1CONbits.SMP = 1; // Input sampled at end SPI1CONbits.SMP = 1; // Input sampled at end
SPI1CONbits.ON = 1; // Enable SPI SPI1CONbits.ON = 1; // Enable SPI
/* set up UART */ /* set up UART */
U1BRG = 9; // 9600 baud (was 38 @ 24MHz) U1BRG = 9; // 9600 baud (was 38 @ 24MHz)
U1STAbits.UTXEN = 1; // enable transmitter U1STAbits.UTXEN = 1; // enable transmitter
U1MODEbits.ON = 1; // enable UART U1MODEbits.ON = 1; // enable UART
/* set up DMA */ /* set up DMA */
// clear all 4 DMA channel flags & IE // clear all 4 DMA channel flags & IE
IEC1CLR = 0xF0000000; IEC1CLR = 0xF0000000;
IFS1CLR = 0xF0000000; IFS1CLR = 0xF0000000;
DMACONbits.ON = 1; //enable DMA controller DMACONbits.ON = 1; // enable DMA controller
// === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) === // === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) ===
DCH0CON = 0; // Reset channel config DCH0CON = 0; // Reset channel config
DCH0ECON = 0; DCH0ECON = 0;
DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF
DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer
DCH0SSIZ = 1; // Source size = 1 byte DCH0SSIZ = 1; // Source size = 1 byte
DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes
DCH0CSIZ = 1; // Cell transfer size = 1 byte DCH0CSIZ = 1; // Cell transfer size = 1 byte
DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive
DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger
DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags
DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt
DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher) DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher)
DCH0CONbits.CHAEN = 1; // Auto-enable on trigger DCH0CONbits.CHAEN = 1; // Auto-enable on trigger
DCH0CONbits.CHEN = 1; // Enable channel DCH0CONbits.CHEN = 1; // Enable channel
return 0; return 0;
} }
@ -157,8 +158,8 @@ void initInterrupts(void) {
INTCONbits.MVEC = 1; // Enable multi-vector interrupts INTCONbits.MVEC = 1; // Enable multi-vector interrupts
__builtin_enable_interrupts(); __builtin_enable_interrupts();
IPC10bits.DMA0IP = 3; // Priority level 3 IPC10bits.DMA0IP = 3; // Priority level 3
// IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag // IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag
// IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt // IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt
} }
void startSPI_DMA_Transfer(void) { void startSPI_DMA_Transfer(void) {
@ -168,7 +169,8 @@ void startSPI_DMA_Transfer(void) {
SL_LAT = 1; SL_LAT = 1;
DCH0CONbits.CHEN = 1; DCH0CONbits.CHEN = 1;
for(int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++) {
while(SPI1STATbits.SPITBF); // Wait if TX buffer full while(SPI1STATbits.SPITBF)
; // Wait if TX buffer full
SPI1BUF = 0x00; // Send dummy byte to clock in data SPI1BUF = 0x00; // Send dummy byte to clock in data
} }
return; return;
@ -192,15 +194,15 @@ extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) {
DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag
// DMA RX completed — spi_rx_buffer[] now contains the data // DMA RX completed — spi_rx_buffer[] now contains the data
} }
// IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag // IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag
} }
//extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) { // extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) {
// if (USBE2CSR1bits.RXPKTRDY) { // if (USBE2CSR1bits.RXPKTRDY) {
// int count = USB_receive_EP2(); // int count = USB_receive_EP2();
// // Echo back // // Echo back
// USB_send_EP2(EP[2].rx_buffer, count); // USB_send_EP2(EP[2].rx_buffer, count);
// USBCSR1bits.EP2RXIF = 0; // USBCSR1bits.EP2RXIF = 0;
// }
// IFS1bits.USBIF = 0; // clear USB interrupt flag
// } // }
// IFS1bits.USBIF = 0; // clear USB interrupt flag
//}

View file

@ -1,7 +1,6 @@
#include "inc/strFuncs.hpp" #include "inc/strFuncs.hpp"
template <typename Out> template <typename Out> void split(const std::string &s, char delim, Out result) {
void split(const std::string &s, char delim, Out result) {
std::istringstream iss(s); std::istringstream iss(s);
std::string item; std::string item;
while(std::getline(iss, item, delim)) while(std::getline(iss, item, delim))