1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Checks for pieces to capture and captures them
void Board::capturePieces (int newPieceX, int newPieceY)
{
// Copy the current pieces matrix
std::vector< std::vector< pieces > > tempPiecesMatrix = piecesMatrix;
// Make the large to check list
std::vector< int > largeToCheckX (0);
std::vector< int > largeToCheckY (0);
// Make the large delete list
std::vector< int > largeDeleteX;
std::vector< int > largeDeleteY;
// Add the new piece to the temporary pieces matrix
tempPiecesMatrix [newPieceX] [newPieceY] = currentTurn;
// For each branch off the placed piece, if the piece is valid and the enemy's, add it to the large to check list
for (int x = newPieceX - 1; x <= newPieceX + 1; x += 2)
{
if (inXBounds (x) && (tempPiecesMatrix [x] [newPieceY] == enemyPiece))
{
largeToCheckX.push_back (x);
largeToCheckY.push_back (newPieceY);
}
}
for (int y = newPieceY - 1; y <= newPieceY + 1; y += 2)
{
if (inYBounds (y) && (tempPiecesMatrix [newPieceX] [y] == enemyPiece))
{
largeToCheckX.push_back (newPieceX);
largeToCheckY.push_back (y);
}
}
// For each item on the large to check list (each main branch)
for (int largeToCheckCounter = 0; largeToCheckCounter != largeToCheckX.size (); ++largeToCheckCounter)
{
// Make the black found flag
bool blankFound = false;
// Make the matrix for recording if a space has been checked
std::vector< std::vector< bool > > hasBeenChecked (xDim, std::vector< bool > (yDim, false));
// Make the small to check lists
std::vector< int > smallToCheckX (0);
std::vector< int > smallToCheckY (0);
// Make the small delete list
std::vector< int > smallDeleteX (0);
std::vector< int > smallDeleteY (0);
// Set the large to check list item to the first item on the small to check list
smallToCheckX.push_back (largeToCheckX [largeToCheckCounter]);
// For each item on the small to check list
for (unsigned int smallToCheckCounter = 0; smallToCheckCounter != smallToCheckX.size (); ++smallToCheckCounter)
{
// Mark the piece as checked
hasBeenChecked [smallToCheckX [smallToCheckCounter]] [smallToCheckY [smallToCheckCounter]] = true;
// Put the piece on the small delete list
smallDeleteX.push_back (smallToCheckX [smallToCheckCounter]);
smallDeleteY.push_back (smallToCheckY [smallToCheckCounter]);
// Check around the piece on the x axis
for (int x = smallToCheckX [smallToCheckCounter] - 1; x <= smallToCheckX [smallToCheckCounter] + 1; ++x)
{
// If x is not within the x bounds, continue
if (!inXBounds (x))
{
continue;
}
// If the new piece is the enemy's and unchecked, add it to the small to check list
if ((tempPiecesMatrix [x] [smallToCheckY [smallToCheckCounter]] == enemyPiece) && (hasBeenChecked [x] [smallToCheckY [smallToCheckCounter]] == false))
{
smallToCheckX.push_back (x);
smallToCheckY.push_back (smallToCheckY [smallToCheckCounter]);
}
// If the new piece is a blank space, set blankFound to true and break
if (tempPiecesMatrix [x] [smallToCheckY [smallToCheckCounter]] == noPiece)
{
blankFound = true;
break;
}
}
// Check around the piece on the y axis
for (int y = smallToCheckY [smallToCheckCounter] - 1; y <= smallToCheckY [smallToCheckCounter] + 1; ++y)
{
// If y is not within y bounds, continue
if (!inYBounds (y))
{
continue;
}
// If the new piece is the enemy's and unchecked, add it to the small to check list
if ((tempPiecesMatrix [smallToCheckX [smallToCheckCounter]] [y] == enemyPiece) && (hasBeenChecked [smallToCheckX [smallToCheckCounter]] [y] == false))
{
smallToCheckX.push_back (smallToCheckX [smallToCheckCounter]);
smallToCheckY.push_back (y);
}
// If the new piece is a blank space, set blankFound to true and break
if (tempPiecesMatrix [smallToCheckX [smallToCheckCounter]] [y] == noPiece)
{
blankFound = true;
break;
}
}
// If blankFound is true, break to stop looking for more pieces from the current main branch
if (blankFound == true)
{
break;
}
}
// If blankFound is true, continue to start checking the next main branch
// without acting on the findings in the current branch
if (blankFound == true)
{
continue;
}
// Add the contents of the small delete list to the large delete list
for (unsigned int smallDeleteCounter = 0; smallDeleteCounter != smallDeleteX.size (); ++smallDeleteCounter)
{
largeDeleteX.push_back (smallDeleteX [smallDeleteCounter]);
largeDeleteY.push_back (smallDeleteY [smallDeleteCounter]);
}
}
// Now do the deletions from the temporary pieces matrix
for (unsigned int largeDeleteCounter = 0; largeDeleteCounter != largeDeleteX.size (); ++largeDeleteCounter)
{
int x = largeDeleteX [largeDeleteCounter];
int y = largeDeleteY [largeDeleteCounter];
tempPiecesMatrix [x] [y] = noPiece;
}
// Now check for a ko infraction, if one is found, set the koAlert and return
if (tempPiecesMatrix == twoTurnsAgo)
{
koAlert = true;
return;
}
// Now actually capture the pieces
for (unsigned int largeDeleteCounter = 0; largeDeleteCounter != largeDeleteX.size (); ++largeDeleteCounter)
{
capturePiece (largeDeleteX [largeDeleteCounter], largeDeleteY [largeDeleteCounter]);
}
// And actually place the piece
piecesMatrix [newPieceX] [newPieceY] = currentTurn;
return;
}
| |