Cell* MainWindow::dlCell(Cell* cell) const { Cell* down = dCell(cell); if (!down) return 0; return lCell(down); }
Cell* MainWindow::ulCell(Cell* cell) const { Cell* up = uCell(cell); if (!up) return 0; return lCell(up); }
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()); }
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; }
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())); } } }
// 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()); }