int checkCell(char** tik, int x, int y) { int ok; if (x == 0 && y == 0) { ok = checkLine(tik,x,y); if (ok == 0) return ok; ok = checkCol(tik,x,y); if (ok == 0) return ok; ok = checkDiag1(tik,x,y); if (ok == 0) return ok; } if (x == 0 && y != 0) { ok = checkLine(tik,x,y); if (ok == 0) return ok; } if (x != 0 && y == 0) { ok = checkCol(tik,x,y); if (ok == 0) return ok; } if ( x == 3 && y == 0) { ok = checkDiag2(tik,x,y); if (ok == 0) return ok; } return ok; }
// 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 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; }
Board* PuzzleMaker::fillBoard(int numClues) { std::vector<int> work_arr; int work_size = 0; srand(time(NULL)); for(int i = 0; i < 9; i++) { work_arr.assign(n_arr, n_arr+9); Row* curr_row = a->getRow(i); for( int j = 0; j < 9; j ++) { work_size = work_arr.size(); int rand_index = (rand() % work_size); if( checkCol(work_arr[rand_index],j,i) ) { Tile* curr_tile = a->setTile(i,j,work_arr[rand_index]); curr_row->setTile(j, curr_tile); a->getCol(j)->setTile(i,curr_tile); a->getSqr(i,j)->setTile(i,j,curr_tile); work_arr.erase(work_arr.begin()+rand_index); std::cout << "(" << i << ", " << j << ")" << std::endl; } else{ j--; std::cout << "(" << i << ", " << j << ")" << std::endl; } } } return 0; }
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; }
// 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; }
void Solver::Solve(double dt) { for (int i = 0; i < ray.size(); i++) ray[i]->setAABB(); if (applyg) applyg(body); else applyG(); BPT.Start(); bp.sort(); BPT.End(); NPT.Start(); checkCol(); NPT.End(); applyForces(dt); SLT.Start(); solveVelocities(dt); SLT.End(); MVT.Start(); solvePositions(dt); MVT.End(); clearForces(); }
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; }
/************************************************************** * 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; }
void prep(void) { hideMarker(); checkCol(); checkRow(); }