board.cpp Example File
accelgame/src/board.cpp
#include <bb/cascades/Color>
#include <bb/cascades/ImageView>
#include "board.hpp"
using namespace bb::cascades;
const static int s_boardDimension = 9;
const static int s_tileSize = 50;
Board::Board(bb::cascades::Container *boardContainer, QObject *parent)
: QObject(parent)
, m_board(boardContainer)
{
qsrand(QDateTime::currentDateTime().toMSecsSinceEpoch());
for (int x = 0; x < s_boardDimension; x++) {
m_blockMap << QVector<bool>();
for (int y = 0; y < s_boardDimension; y++)
m_blockMap[x] << false;
}
}
bb::cascades::Container * Board::board() const
{
return m_board;
}
bool Board::canMoveTo(int x, int y) const
{
if (x < 0 || x >= s_boardDimension || y < 0 || y >= s_boardDimension)
return false;
return m_blockMap[x][y] == false;
}
void Board::reset()
{
Q_FOREACH (Control *block, m_blocks) {
m_board->remove(block);
}
qDeleteAll(m_blocks);
m_blocks.clear();
for (int x = 0; x < s_boardDimension; x++)
for (int y = 0; y < s_boardDimension; y++)
m_blockMap[x][y] = false;
QVector<QPoint> blockCoordinates;
for (int i = 0; i < 20; i++) {
const QPoint newPoint(qrand() % s_boardDimension, qrand() % s_boardDimension);
if (blockCoordinates.contains(newPoint))
continue;
if (newPoint == QPoint(0, 0))
continue;
blockCoordinates << newPoint;
}
Q_FOREACH (const QPoint position, blockCoordinates) {
m_blockMap[position.x()][position.y()] = true;
ImageView *block = new ImageView();
block->setPreferredWidth(50);
block->setPreferredHeight(50);
block->setImage(Image(QUrl("asset:///images/block.png")));
block->setTranslationX(position.x() * s_tileSize);
block->setTranslationY(position.y() * s_tileSize);
m_board->add(block);
m_blocks << block;
}
}