int checkForWinner(int gameBoard[BOARD_SIZE][BOARD_SIZE]){ int i, j; int priorityArray[BOARD_SIZE][BOARD_SIZE]; for(i = 0; i < BOARD_SIZE; i++){ for(j = 0; j < BOARD_SIZE; j++){ if(checkRow(gameBoard, i, 2, 1, priorityArray) == 5 || checkColumn(gameBoard, j, 2, 1, priorityArray) == 5 || checkDiagonal(gameBoard,2,1,priorityArray) == 5){ printf("Vittoria del giocatore 1! (O)\n\n"); return 1; } else if(checkRow(gameBoard, i, 2, 1, priorityArray) == 6 || checkColumn(gameBoard, j, 2, 1, priorityArray) == 6 || checkDiagonal(gameBoard,2,1,priorityArray) == 6){ printf("Vittoria del giocatore 2! (X)\n\n"); return 2; } } } if(checkDiagonal(gameBoard, 2, 1, priorityArray) == 5){ return 1; } else if(checkDiagonal(gameBoard, 2, 1, priorityArray) == 6){ return 2; } return 0; }
void checkForTileRows() { bool columnsClear[10]; bool rowsClear[10]; int z; for (z = 0; z < 10; z++) { checkColumn(z); columnsClear[z] = lastClearCheck; } for (z = 0; z < 10; z++) { checkRow(z); rowsClear[z] = lastClearCheck; } for (z = 0; z < 10; z++) { if (columnsClear[z]) { clearColumn(z); } } for (z = 0; z < 10; z++) { if (rowsClear[z]) { clearRow(z); } } }
void Solver::findPossibleSlots( const int box, std::vector<std::stack<std::pair<int,int>>>& backTrackStack, const int num) { int highestRow = (box/3)*3; //Row of upper-left box int highestCol = (box%3)*3; //Column of upper-left box std::vector<std::pair<int,int>> shuffleVec; /* Reversed the order so first item on stack is the upper-leftmost slot */ for(int rowIter = 2; rowIter >= 0; --rowIter) { for(int colIter = 2; colIter >= 0; --colIter) { bool existsRow = checkRow(highestRow + rowIter, num); bool existsCol = checkColumn(highestCol + colIter, num); if(!existsRow && !existsCol) { shuffleVec.push_back(std::make_pair(rowIter,colIter)); #ifdef DEBUG std::cout << "Adding possible slot " << rowIter << "," << colIter << " for " << num << std::endl; #endif } else if(existsRow) break; else continue; } } std::random_device seedGen; std::mt19937 randGen(seedGen()); std::shuffle(shuffleVec.begin(), shuffleVec.end(), randGen); for(auto p : shuffleVec) backTrackStack[num-1].push(p); }
// Returns a winning move if there is one, else // it returns Move(-1, -1) Move AIShell::winMoves(int**& gamestate) { for(int col = 0; col < numCols; col++) { for(int row = 0; row < numRows; row++) { if(gamestate[col][row] == NO_PIECE) { if(checkRow(col, row, AI_PIECE, gamestate)) { return Move(col, row); } else if(checkCol(col, row, AI_PIECE, gamestate)) { return Move(col, row); } else if(checkDiagonalLeftTop(col, row, AI_PIECE, gamestate)) { return Move(col, row); } else if(checkDiagonalLeftBot(col, row, AI_PIECE, gamestate)) { return Move(col, row); } if(gravityOn) {break;} } } } return Move(-1, -1); }
bool sudokuSolver::fillColumn(int row,int column){ if(row==9){ return true; } else if(column==9){ if(fillColumn(row+1,0))return true; else return false; } else if(problem[row][column]==0){ for(int i=1;i<=9;i++){ ans[row][column]=i; for(int k=row;k<9;k++){ if(k==row){ for(int l=column+1;l<9;l++) ans[k][l]=problem[k][l]; } else{ for(int l=0;l<9;l++) ans[k][l]=problem[k][l]; } } if(checkColumn(row,column) && checkRow(row,column) && checkBlock(row,column)) if(fillColumn(row,column+1))return true; } return false; } else {if(fillColumn(row,column+1))return true;else return false;} }
bool solveSudoku(vector<vector<char>>& board, unordered_set<char> s, int row, int col) { if (board[row][col] == '.') { for (int i = 1; i <= 9; i++) { board[row][col] = char(i + '0'); if (checkRow(board, s, row) && checkCol(board, s, col) && checkSubbox(board, s, row/3, col/3)) { if (row == board.size() - 1 && col == board[row].size() - 1) { return true; } else if (col == board[row].size() - 1) { if (solveSudoku(board, s, row + 1, 0)) { return true; } } else { if (solveSudoku(board, s, row, col + 1)) { return true; } } } } board[row][col] = '.'; } else { if (row == board.size() - 1 && col == board[row].size() - 1) { return true; } else if (col == board[row].size() - 1) { return solveSudoku(board, s, row + 1, 0); } else { return solveSudoku(board, s, row, col + 1); } } return false; }
// Return a copy of the column data specified by its index starting at 0 // (use the Column copy-constructor) Column Statement::getColumn(const int aIndex) { checkRow(); checkIndex(aIndex); // Share the Statement Object handle with the new Column created return Column(mStmtPtr, aIndex); }
// Return a copy of the column data specified by its column name starting at 0 // (use the Column copy-constructor) Column Statement::getColumn(const char* apName) { checkRow(); const int index = getColumnIndex(apName); // Share the Statement Object handle with the new Column created return Column(mStmtPtr, index); }
/*! * \fn void MessageProgrammesDialog::creerConnect() * \brief Créer les connections entre les différents composant du widget * */ void MessageProgrammesDialog::creerConnect() { connect(ui->addMessageButton,SIGNAL(clicked()),this,SLOT(creerMessage())); connect(ui->messageTable,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(checkRow(QModelIndex))); connect(ui->supprimerButton,SIGNAL(clicked()),this,SLOT(supprimerSelect())); connect(ui->supprimerAllButton,SIGNAL(clicked()),this,SLOT(supprimerAll())); connect(ui->modifierButton,SIGNAL(clicked()),this,SLOT(modifierSelect())); }
bool isValidSudoku(Board board) { Block block; std::vector<int> column; std::vector<int> row; bool isValidSize = true; bool isValidBlock = true; bool isValidRow = true; bool isValidColums = true; std::vector<bool> blockValidationResults; std::vector<bool> columnValidationResults; std::vector<bool> rowValidationResults; if (board.size() != 9) { return false; } for (int i = 0; i < 9; i++) { auto row = board[i]; isValidSize = isValidSize && (row.size() == 9); } if (!isValidSize) { return false; } for (unsigned int col = 0; col < BOARD_SIZE; col++) { for (unsigned int row = 0; row < BOARD_SIZE; row++) { if (!isValidValue(board[row][col])) return false; } } for (unsigned int i = 0; i < 9; i++) { block = getBlock(board, i); blockValidationResults.push_back(checkBlock(block)); } for (unsigned int i = 0; i < 9; i++) { column = getColumn(board, i); columnValidationResults.push_back(checkColumn(column)); } for (int j = 0; j < 9; j++) { row = getRow(board, j); rowValidationResults.push_back(checkRow(row)); } return checkResults(blockValidationResults, columnValidationResults, rowValidationResults); }
bool SudokuBoardGenerator::checkConstraints(int row, int col, int value, SudokuFile &sf, std::vector<std::vector<int>> &board) { if (checkRow(row, value, sf.getN(), board) && checkColumn(col, value, sf.getN(), board) && checkBox(row, col, value, sf.getP(), sf.getQ(), board)) { return true; } return false; }
int main() { int numBoards; int i, j; int check; int sudoku[9][9]; char c; int caseNum; FILE *in = fopen("sudokode.in", "r"); /* get number of boards and then process each board */ fscanf(in, "%d ", &numBoards); for (caseNum = 1; caseNum <= numBoards; caseNum++) { /* get input */ for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { /* convert values to 0-8 to make indexing easier */ fscanf(in, "%c ", &c); sudoku[i][j] = c - '1'; } } /* test all rows */ check = TRUE; for (i = 0; i < 9 && check == TRUE; i++) check = checkRow(sudoku, i); /* test all columns */ for (j = 0; j < 9 && check == TRUE; j++) check = checkCol(sudoku, j); /* test all 3x3 subsquares */ for (i = 0; i < 9 && check == TRUE; i += 3) { for (j = 0; j < 9 && check == TRUE; j += 3) { check = checkSquare(sudoku, i, j); } } /* print output */ printf("Sudoku #%d: ", caseNum); if (check) printf("Dave's the man!\n\n"); else printf("Try again, Dave!\n\n"); } fclose(in); return 0; }
bool sudokuSolver::validateProblem(){ for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ if(problem[i][j]==0)continue; else{ if(!(checkBlock(i,j)&&checkRow(i,j)&&checkColumn(i,j)))return false; } } } return true; }
void Game::insertNumber(int x, int y) { sectorX = x / 3; sectorY = y / 3; int number; for (number = 1; number < 10; number++) { if (!checkSector(number) && !checkRow(y, number) && !checkColumn(x, number)) { grid[x * 9 + y] = number; break; } } }
void gameMap::moveRows(short yBase) { int i = yBase; do { for(int j = 0; j < MAP_MAX_X; j++) mapField[i][j] = mapField[i-1][j]; }while(checkRow(i--) != ROW_EMPTY); gameMap::drawMap(); gameMech::addPoints(22); }
void PartialSolver::findPoints(const Puzzle::Board& board) { int counter(0); ///Start by finding lowest amount of a number for(int stop=2; stop<=number; stop++) { ///iterate through the numbers in decending order for(int num=number; num>0; num--) { counter = 0; ///Check each column for(int col=0; col<number; col++) { for(int row=0; row<number; row++) { if(board[row][col].isPossible(num)) { counter++; } } if(counter==stop) { variable = num; checkColumn(col, board); return; } } counter = 0; ///Check each row for(int row=0; row<number; row++) { for(int col=0; col<number; col++) { if(board[row][col].isPossible(num)) { counter++; } } if(counter==stop) { variable = num; checkRow(row, board); return; } } } } }
void Solver::solveSemiSeed(const int semiSeed) { /* Create RNG */ std::random_device seedGen; std::mt19937 randGen(seedGen()); /* Generate semiSeed first */ int highestRow = (semiSeed/3)*3; //Row of upper-left box int highestCol = (semiSeed%3)*3; //Column of upper-left box std::array<int, 9> nums = {1,2,3,4,5,6,7,8,9}; std::shuffle(nums.begin(), nums.end(), randGen); #ifdef DEBUG std::cout << "nums:" << std::endl; for(auto e : nums) std::cout << e << " "; std::cout << std::endl; #endif for(int valIter = 0; valIter < 9; valIter++) { for(int boxIter = 0; boxIter < 9; boxIter++) { int currentRow = highestRow+boxIter/3; int currentCol = highestCol+boxIter%3; bool rowExists = checkRow(currentRow,nums[valIter]); bool colExists = checkColumn(currentCol,nums[valIter]); if(!rowExists && !colExists) { if( !sudokuBoard[semiSeed].addNumber( boxIter/3, boxIter%3, nums[valIter]) ) { #ifdef DEBUG std::cout << "Did not add " << nums[valIter] << " to semiSeed at " << boxIter/3 << "," << boxIter%3 << std::endl; #endif } else break; } else if(rowExists) boxIter+=2; else continue; } } #ifdef DEBUG std::cout << "semiSeed values:" << std::endl; sudokuBoard[semiSeed].debugPrint(); #endif }
int Game::checkWinner() { int winner = 0; winner = checkRow(0, 1, 2); if (winner > 0) { return winner; } winner = checkRow(3, 4, 5); if (winner > 0) { return winner; } winner = checkRow(6, 7, 8); if (winner > 0) { return winner; } //columns //0,3,6; 1,4,7; 2,5,8; winner = checkRow(0, 3, 6); if (winner > 0) { return winner; } winner = checkRow(1, 4, 7); if (winner > 0) { return winner; } winner = checkRow(2, 5, 8); if (winner > 0) { return winner; } //diagonals //2,4,6; 0,4,8 winner = checkRow(2, 4, 6); if (winner > 0) { return winner; } winner = checkRow(0, 4, 8); if (winner > 0) { return winner; } return winner; }
// Returns the location of all threats on gamestate std::vector<std::pair<int, int> > AIShell::threats(int**& gamestate) { std::vector<std::pair<int, int> > threats; std::pair<int, int> aMove; for(int col = 0; col < numCols; col++) { for(int row = 0; row < numRows; row++) { if(gamestate[col][row] == NO_PIECE) { if(checkRow(col, row, HUMAN_PIECE, gamestate)) { aMove = std::make_pair(col, row); threats.push_back(aMove); } else if(checkCol(col, row, HUMAN_PIECE, gamestate)) { aMove = std::make_pair(col, row); threats.push_back(aMove); } else if(checkDiagonalLeftTop(col, row, HUMAN_PIECE, gamestate)) { aMove = std::make_pair(col, row); threats.push_back(aMove); } else if(checkDiagonalLeftBot(col, row, HUMAN_PIECE, gamestate)) { aMove = std::make_pair(col, row); threats.push_back(aMove); } if(gravityOn) {break;} } } } return threats; }
int dfs(int cur, int pathNum){ int r=cur/m; int c=cur-r*m; if(r-2>=0 && !checkRow(r-2)) return 0 ; if(r>=n-2 || cur==n*m){ if(checkAll()){ minx = pathNum; return 1; } if(cur==n*m) return 0; } if(dfs(cur+1, pathNum)) return 1; cover(r, c); path[pathNum] = cur+1; if(dfs(cur+1, pathNum+1)) return 1; cover(r, c); return 0; }
// Return a copy of the column data specified by its column name starting at 0 // (use the Column copy-constructor) Column Statement::getColumn(const char* apName) { checkRow(); if (mColumnNames.empty()) { for (int i = 0; i < mColumnCount; ++i) { const char* pName = sqlite3_column_name(mStmtPtr, i); mColumnNames[pName] = i; } } const TColumnNames::const_iterator iIndex = mColumnNames.find(apName); if (iIndex == mColumnNames.end()) { throw SQLite::Exception("Unknown column name."); } // Share the Statement Object handle with the new Column created return Column(mStmtPtr, (*iIndex).second); }
int main(void){ // 顯示陣列 int bingoTable[SIZE][SIZE] = {0}; int playerTable[SIZE][SIZE] = {0}; int *tmpRand; int score = 0; int guessTime = 0; tmpRand = createRandNum(); setValueInTable(tmpRand, bingoTable); // 以下為 while or do while while(score < 3){ printf("|隱藏的賓果盤|\n"); displayTable(bingoTable); printf("|玩家戳號盤|\n"); displayTable(playerTable); printf("Score:%d\n", score); int number; printf("Plz input a number:"); scanf(" %d", &number); selectNumber(bingoTable, playerTable, number); guessTime++; score = score + checkRow(playerTable) + checkColumn(playerTable) + checkCross(playerTable); system("CLS"); } printf("|隱藏的賓果盤|\n"); displayTable(bingoTable); printf("|玩家戳號盤|\n"); displayTable(playerTable); printf("恭喜結束遊戲\n"); printf("Score:%d\n", score); printf("猜測次數:%2d\n", guessTime); system("pause"); return 0; }
bool SudokuProblem::solveRecursion(int row, int col) { recursionCounter++; // Wenn das Soduko bereits gelöst ist, gebe true zurück if (isSolved()) return true; // Gehe zum ersten leeren Feld: // Solange wir nicht alle Zeilen durchlaufen haben // und wir nicht auf einem leeren Feld sind while (row < 9 && sudoku[row][col] != 0) { // Gehe ein Feld weiter col++; // Wenn wir am Ende einer Zeile angekommen sind if (col == 9) { // Gehe zur nächsten Zeile row++; col = 0; } } // Probiere die Werte 1-9 im leeren Feld aus: // Durchlaufe die Werte 1-9 for (int i = 1; i <= 9; i++) { // Setze den aktuellen Wert in das aktuelle Feld sudoku[row][col] = i; // Prüfe ob sich daraus ein gültiges Sudoku ergibt if (checkRow(row) && checkCol(col) && checkBlock(row, col) && solveRecursion(row, col)) { return true; } } // Wenn bis hier kein true zurück gegeben wurde: // Wert zurücksetzen und erneut aufrufen lassen. sudoku[row][col] = 0; return false; }
void eightQueen() { for(int i=0;i<8;i++) for(int j=0;j<8;j++) chessBoard[i][j] = 0; printf("Enter x,y position to place the first queen:\n"); int x,y,k=0; scanf("%d",&x); scanf("%d",&y); chessBoard[x-1][y-1] = 1; //Inititilize the first queen position to 1 for(int i=x;k<8;i=(i+1)%8,k++) { for(int j=0;j<8;j++) { if(checkColum(j)==0 && checkRow(i)==0 && checkDiagonal(i,j)==0) { //If no Conflict push the queen position to the stack push(i,j); chessBoard[i][j] = 1; break; } // When a queen cannot be placed at any position if(j==7) { struct stack *temp; temp = pop(); i = temp->x1; j = temp->y1; free(temp); chessBoard[i][j] = 0; k--; } } } display(); }
int checkAll(){ if(n>1 && checkRow(n-1) && checkRow(n-2) || n==1 && checkRow(0)) return 1; return 0; }
/************************************************************** * This method will use the methods above combined to check all * three statuses at the same time * * @param solve the board for all three situation * @param row will use the current row * @param col will use the current col * @param num will find the number contained in section * @return bool type found for true not there for false **************************************************************/ bool canPlace(int grid[MAX][MAX], int row, int col, int num) { return checkRow(grid, row, num) == false && checkCol(grid, col, num) == false && checkSection(grid, row - row%3 , col - col%3, num)== false; }
/** * @overload sudukutree.h */ int getNodeChildren(void* suduku, void*** optionalSolutions) { Suduku* tempSuduku = (Suduku*)suduku; SlotIndex emptySlotIndex = {DEFAULT_ROW_INDEX, DEFAULT_COL_INDEX}; firstEmptySlot(tempSuduku, &emptySlotIndex); //reach to the end of the table if(emptySlotIndex.col == DEFAULT_COL_INDEX || emptySlotIndex.row == DEFAULT_ROW_INDEX) { return 0; } int* possibleValues = (int*)malloc(tempSuduku->tableSize * sizeof(int)); if(possibleValues == NULL) { //allocation faild return 0; } int index; //initalize possibleValues array for(index = 0; index < tempSuduku->tableSize; index++) { *(possibleValues + index) = POSSIBLE_VALUE_INITIALIZE; } //check for valid possible Values checkCol(suduku, &emptySlotIndex, possibleValues); checkRow(suduku, &emptySlotIndex, possibleValues); checksubSqure(suduku, &emptySlotIndex, possibleValues); //check how mach possible Values the are int childrenCounter = 0; for(index = 0; index < tempSuduku->tableSize; index++) { if(*(possibleValues + index) == POSSIBLE_VALUE_INITIALIZE) { childrenCounter++; } } //create an array of children - each children is the suduku table with different possible value Suduku** childrenArray; childrenArray = (Suduku**)malloc(childrenCounter * sizeof(Suduku*)); //need to check for faild allocation if(childrenArray == NULL) { //allocation faild return 0; } *optionalSolutions = (void**)childrenArray; Suduku* child = NULL; for(index = 0; index < tempSuduku->tableSize; index++) { //if current value is optional if(*(possibleValues + index) == POSSIBLE_VALUE_INITIALIZE) { child = (Suduku*)copyNode(tempSuduku); child->table[TABLE(tempSuduku->tableSize, emptySlotIndex.row, emptySlotIndex.col)] = \ index + 1; child->fullSlots++; *childrenArray = child; childrenArray++; } } free(possibleValues); possibleValues = NULL; return childrenCounter; }
bool isValidSudoku(vector<vector<char>>& board) { return checkColumn(board)&&checkRow(board)&&checkBlock(board); }
void Grid::Loop( ) { CubeReader cr; std::vector< std::string > vOutput = stack.getCube(); cubeUnedited = vOutput; cube = cr( vOutput ); Fetcher f( cube ); for ( auto& x : cube ) { size_t c = x.find( ";" ); x.erase( c, std::string::npos ); std::cout << x << std::endl; } //gp.setCube( stack.getCube() ); gp.setCube( cube ); std::vector< std::string > vst; for ( int i = 0; i < ppl.vTexts.size(); i++ ) alldecks.push_back( vst ); while (rWin.isOpen()) { sf::Event event; while (rWin.pollEvent(event)) { if (event.type == sf::Event::Closed) { rWin.close();isRunning=0; exit(0); } } if (!End) { gp.getimgs( ); checkRow(); for (int i = 0; i < gp.vRects.size(); i++) { if ( gp.vRects[i].contains ( sf::Mouse::getPosition( rWin ) ) && !sf::Keyboard::isKeyPressed( sf::Keyboard::LAlt ) && stack.whosTurn == stack.ID ) { std::vector<int> Ints; //d::cout << i ; Ints = Friends(i,0); for (int j = 0; j < Ints.size(); j++) { gp.vShapes[ Ints[ j ] ].setFillColor(sf::Color::Red); } if ( sf::Keyboard::isKeyPressed( sf::Keyboard::LControl ) ) { std::cout << std::endl << gp.cube[ i ] << std::endl; } if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && shootclock.getElapsedTime().asSeconds() > 0.5 ) { preShoot(Ints);break; } if (i == 8 || i == 7 || i == 6) break; i = Ints.back() + 1; } gp.vShapes[i].setFillColor(sf::Color(255,255,255,255)); } for (int i = 0; i < gp.vRects.size(); i++) { if ( gp.vRects[i].contains( sf::Mouse::getPosition( rWin ) ) && sf::Keyboard::isKeyPressed( sf::Keyboard::LAlt ) && stack.whosTurn == stack.ID ) { std::vector<int> Ints; Ints = Friends(i,1); for (int j = 0; j < Ints.size(); j++) { gp.vShapes[ Ints[ j ] ].setFillColor(sf::Color::Red); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { preShoot(Ints);break; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) { for (int ii = 0; ii < Ints.size(); ii++) { ///std::cout << rCards[Ints.at(ii)].Name << std::endl; } } i = Ints.back() + 1; } //gp.vShapes[i].setFillColor(sf::Color(255,255,255,255)); } if (sf::Mouse::isButtonPressed(sf::Mouse::Middle) || sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) { for (int i = 0; i < gp.vRects.size(); i++) { if ( gp.vRects[ i ].contains( sf::Mouse::getPosition( rWin ) ) ) { zoom.setTexture( gp.vShapes[ i ].getTexture(), true ); zoom.setSize(sf::Vector2f(500,700)); zoomclock.restart(); } } } } rWin.clear( ); //if (zoomclock.getElapsedTime().asSeconds() < 0.02)rWin.draw(zoom); if ( !End ) { for ( auto x : gp.vShapes ) rWin.draw( x ); for ( auto x : gp.vTexts ) rWin.draw( x ); //rWin.draw( gp.) ppl.draw( rWin ); if (zoomclock.getElapsedTime().asSeconds() < 0.02)rWin.draw(zoom); } if ( End ) rWin.draw( txtEnd ); rWin.display(); } }
void prep(void) { hideMarker(); checkCol(); checkRow(); }