/* * Returns true if a move is legal for the given side; false otherwise. */ bool Board::checkMove(Move *m, Side side) { // Passing is only legal if you have no moves. if (m == NULL) return !hasMoves(side); int X = m->getX(); int Y = m->getY(); // Make sure the square hasn't already been taken. if (occupied(X, Y)) return false; Side other = (side == BLACK) ? WHITE : BLACK; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dy == 0 && dx == 0) continue; // Is there a capture in that direction? int x = X + dx; int y = Y + dy; if (onBoard(x, y) && get(other, x, y)) { do { x += dx; y += dy; } while (onBoard(x, y) && get(other, x, y)); if (onBoard(x, y) && get(side, x, y)) return true; } } } return false; }
// RECURSION bool MazeBoard::checkPath(Coords start_pos, Coords end_pos, std::vector<Coords> &open_queue) { if (start_pos == end_pos) return true; open_queue.push_back(start_pos); // expand to right if (board[INDEX_C(start_pos)].canGo(RIGHT) && onBoard(start_pos.right()) && board[INDEX_C(start_pos.right())].canGo(LEFT) && notInQueue(start_pos.right(), open_queue) && checkPath(start_pos.right(), end_pos, open_queue)) return true; else if (board[INDEX_C(start_pos)].canGo(DOWN) && onBoard(start_pos.down()) && board[INDEX_C(start_pos.down())].canGo(UP) && notInQueue(start_pos.down(), open_queue) && checkPath(start_pos.down(), end_pos, open_queue)) return true; else if (board[INDEX_C(start_pos)].canGo(LEFT) && onBoard(start_pos.left()) && board[INDEX_C(start_pos.left())].canGo(RIGHT) && notInQueue(start_pos.left(), open_queue) && checkPath(start_pos.left(), end_pos, open_queue)) return true; else if (board[INDEX_C(start_pos)].canGo(UP) && onBoard(start_pos.up()) && board[INDEX_C(start_pos.up())].canGo(DOWN) && notInQueue(start_pos.up(), open_queue) && checkPath(start_pos.up(), end_pos, open_queue)) return true; return false; }
void MineField::contentsMousePressEvent( QMouseEvent* e ) { int c = e->pos().x() / cellSize; int r = e->pos().y() / cellSize; if ( onBoard( r, c ) ) cellPressed( r, c ); else currCol = currRow = -1; }
void MineField::contentsMouseReleaseEvent( QMouseEvent* e ) { int c = e->pos().x() / cellSize; int r = e->pos().y() / cellSize; if ( onBoard( r, c ) && c == currCol && r == currRow ) cellClicked( r, c ); if ( flagAction == FlagNext ) { flagAction = NoAction; } }
/* * Modifies the board to reflect the specified move. */ void Board::doMove(Move *m, Side side) { // A NULL move means pass. if (m == NULL) return; /* // Ignore if move is invalid. if (!checkMove(m, side)) return; */ int X = m->getX(); int Y = m->getY(); Side other = (side == BLACK) ? WHITE : BLACK; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dy == 0 && dx == 0) continue; int x = X; int y = Y; do { x += dx; y += dy; } while (onBoard(x, y) && get(other, x, y)); if (onBoard(x, y) && get(side, x, y)) { x = X; y = Y; x += dx; y += dy; while (onBoard(x, y) && get(other, x, y)) { set(side, x, y); x += dx; y += dy; } } } } set(side, X, Y); }
CellData *cell( int row, int col ) { return onBoard(row, col) ? _cells[col + _cols * row] : 0; }