Esempio n. 1
0
Cell* MainWindow::drCell(Cell* cell) const
{
      Cell* down = dCell(cell);
      if (!down)
	   return 0;
      return rCell(down);
}
Esempio n. 2
0
Cell* MainWindow::urCell(Cell* cell) const
{
      Cell* up = uCell(cell);
      if (!up)
	   return 0;
      return rCell(up);
}
Esempio n. 3
0
void MainWindow::addRandomDir(CellList& list)
{
  Cell* cell = list.first();
  Cell* ucell = uCell(cell);
  Cell* rcell = rCell(cell);
  Cell* dcell = dCell(cell);
  Cell* lcell = lCell(cell);

  typedef QMap<Cell::Dirs, Cell*> CellMap;
  CellMap freecells;

  if(ucell && ucell->dirs() == Cell::Free)
    freecells[Cell::U] = ucell;
  if(rcell && rcell->dirs() == Cell::Free)
    freecells[Cell::R] = rcell;
  if(dcell && dcell->dirs() == Cell::Free)
    freecells[Cell::D] = dcell;
  if(lcell && lcell->dirs() == Cell::Free)
    freecells[Cell::L] = lcell;
  if(freecells.isEmpty())
    return;

  CellMap::ConstIterator it = freecells.constBegin();
  for(int i = rand() % freecells.count(); i > 0; --i)
    ++it;

  cell->setDirs(Cell::Dirs(cell->dirs() | it.key()));
  it.value()->setDirs(contrdirs[it.key()]);
  list.append(it.value());
}
Esempio n. 4
0
bool MainWindow::updateConnections()
{
	bool newconnection[MasterBoardSize * MasterBoardSize];
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
		newconnection[i] = false;

	CellList list;
	if(!root->isRotated())
	{
		newconnection[root->index()] = true;
		list.append(root);
	}
	while(!list.isEmpty())
	{
		Cell* cell = list.first();
		Cell* ucell = uCell(cell);
		Cell* rcell = rCell(cell);
		Cell* dcell = dCell(cell);
		Cell* lcell = lCell(cell);

		if((cell->dirs() & Cell::U) && ucell && (ucell->dirs() & Cell::D) &&
				!newconnection[ucell->index()] && !ucell->isRotated())
		{
			newconnection[ucell->index()] = true;
			list.append(ucell);
		}
		if((cell->dirs() & Cell::R) && rcell && (rcell->dirs() & Cell::L) &&
				!newconnection[rcell->index()] && !rcell->isRotated())
		{
			newconnection[rcell->index()] = true;
			list.append(rcell);
		}
		if((cell->dirs() & Cell::D) && dcell && (dcell->dirs() & Cell::U) &&
				!newconnection[dcell->index()] && !dcell->isRotated())
		{
			newconnection[dcell->index()] = true;
			list.append(dcell);
		}
		if((cell->dirs() & Cell::L) && lcell && (lcell->dirs() & Cell::R) &&
				!newconnection[lcell->index()] && !lcell->isRotated())
		{
			newconnection[lcell->index()] = true;
			list.append(lcell);
		}
		list.remove(list.begin());
	}

	bool isnewconnection = false;
	for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++)
	{
		if(!board[i]->isConnected() && newconnection[i])
			isnewconnection = true;
		board[i]->setConnected(newconnection[i]);
	}
	return isnewconnection;
}
Esempio n. 5
0
void Mask::set(int nmask){
    string maskPath("levels/masks/mask-");
    maskPath += to_string(nmask) + ".txt";
    ifstream fmask(maskPath);
    m_cells.resize(m_size * m_size);
    for(int y = 0; y < m_size; ++y){
        for(int x = 0; x < m_size; ++x){
            int m;
            fmask >> m;
            rCell({x, y}) = CellMask(m);
        }
    }
    fmask.close();
}
Esempio n. 6
0
void MainWindow::cellClicked(int index)
{
  if(!start && (isGameOver() || lcd->intValue() == 0))
  {
    if(soundaction->isChecked())
      clicksound->play();
    blink(index);
  }
  else
  {
    if(soundaction->isChecked())
      turnsound->play();

    //qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
    Cell* cell = board[index];
    toggleCell(cell);
    toggleCell(lCell(cell));
    toggleCell(rCell(cell));
    toggleCell(uCell(cell));
    toggleCell(ulCell(cell));
    toggleCell(urCell(cell));
    toggleCell(dCell(cell));
    toggleCell(dlCell(cell));
    toggleCell(drCell(cell));

    //qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents);

    if(soundaction->isChecked())
      connectsound->play();

    if (!start) lcd->display(lcd->intValue() - 1);
    cellsLcd->display(cellsCount());

    if(isGameOver() && !start)
    {
      if(soundaction->isChecked())
        winsound->play();
      blink(index);    
      if(lcd->intValue() <= highscores.at(2 * (skill + 1) * NumHighscores - 1).toInt())
        addHighscore(lcd->intValue());
                   QMessageBox::information(this,tr("Game Over"),tr("Congratulations! Score %1").arg(lcd->intValue()));
    }
  }
}
Esempio n. 7
0
// adds a random direction and moves on (if possible)
void AbstractGrid::addRandomCable(QList<uint>& list)
{
    int cell = list.first();
    // find all the cells surrounding list.first()
    // (0 when cells don't exist)
    int ucell = uCell(cell); // up
    int rcell = rCell(cell); // right
    int dcell = dCell(cell); // down
    int lcell = lCell(cell); // left

    QMap<Directions, int> freeCells;

    // of those cells map the ones that are free
    if (ucell != NO_CELL && m_cells[ucell]->cables() == None) {
        freeCells[Up] = ucell;
    }
    if (rcell != NO_CELL && m_cells[rcell]->cables() == None) {
        freeCells[Right] = rcell;
    }
    if (dcell != NO_CELL && m_cells[dcell]->cables() == None) {
        freeCells[Down] = dcell;
    }
    if (lcell != NO_CELL && m_cells[lcell]->cables() == None) {
        freeCells[Left] = lcell;
    }

    if (freeCells.isEmpty()) return; // no free cells left

    QMap<Directions, int>::ConstIterator it = freeCells.constBegin();
    // move the iterator to a random direction connecting to a free cell
    for (int i = qrand() % freeCells.count(); i > 0; --i) ++it;

    // add the cable in the direction of cell
    Directions newCables = Directions(m_cells[cell]->cables() | it.key());
    m_cells[cell]->setCables(newCables);
    // add a cable in the opposite direction, on the free cell
    m_cells[it.value()]->setCables(invertDirection(it.key()));
    // append the cell that was free to the list
    list.append(it.value());
}
Esempio n. 8
0
File: cell.cpp Progetto: zhiyb/2048
QRectF Cell::boundingRect(void) const
{
	QRectF rCon(FRAME, FRAME, scene()->width() - FRAME * 2, scene()->height() - FRAME * 2);
	QRectF rCell(0, 0, (rCon.width() - FRAME * 3) / 4, (rCon.width() - FRAME * 3) / 4);
	return rCell;
}