bool Algorithm::Search_Check() { bool ret = false; for (int i = 0; i < m_rows; ++i) for (int j = 0; j < m_columns; ++j) switch (m_MineMatrix[i][j]) { case ORIGINAL: if (m_Search_NotMine[i][j] ^ m_Search_Mine[i][j]) { if (m_Search_NotMine[i][j]) { AddOperation(Operation(Operation::OPEN, i, j)); NewWaitForIdentify(i, j); } if (m_Search_Mine[i][j]) { m_MineMatrix[i][j] = MINE; AddOperation(Operation(Operation::MARKMINE, i, j)); } ret = true; } break; } return ret; }
/* * -x {int}|clear|reset|expire:{scope} */ void ProcExpireArg(char *arg) { int when, flags; if (ParseWhen(arg, &when, &flags)) { fprintf(stderr, "Ill-formed argument: -x '%s'\n", arg); Err++; return; } flags |= ARG_EXPIRE; AddOperation(flags, when); }
/* * -f {count}{multiplier}:{type}:{scope} */ void ProcFileArg(char *arg) { long long nfiles; int flags; if (ParseCount(arg, &nfiles, &flags)) { fprintf(stderr, "Ill-formed argument: -f '%s'\n", arg); Err++; return; } flags |= ARG_FILE; AddOperation(flags, nfiles); }
/* * -b {count}{multiplier}:{type}:{scope} */ void ProcBlockArg(char *arg) { long long nblocks; int flags; if (ParseCount(arg, &nblocks, &flags)) { fprintf(stderr, "Ill-formed argument: -b '%s'\n", arg); Err++; return; } if (kflag) nblocks *= 2; /* count is 1024-byte blocks, not 512-byte */ flags |= ARG_BLOCK; AddOperation(flags, nblocks); }
/* * -t {int}:{scope} */ void ProcTimeArg(char *arg) { int when, flags; if (ParseWhen(arg, &when, &flags)) { fprintf(stderr, "Ill-formed interval: -t '%s'\n", arg); Err++; return; } if (flags & EXP_MASK) { fprintf(stderr, "Ill-formed interval: -t '%s' " "-- clear/reset/expire don't apply\n", arg); Err++; return; } flags |= ARG_GRACE; AddOperation(flags, when); }
void Algorithm::GetNotCertainOperations(void) { if (!m_WaitForCheckList_Row.IsEmpty()) { int SelectRow = -1, SelectColumn = -1; double ExplodeProbility = 1; while (!m_WaitForCheckList_Row.IsEmpty()) { int row = m_WaitForCheckList_Row.RemoveHead(); int column = m_WaitForCheckList_Column.RemoveHead(); trow.AddTail(row), tcolumn.AddTail(column); if (!m_NeverCheck[row][column]) { int minecount = 0, originalcount = 0; for (int i = 0; i < 8; ++i) { int newrow = row + RowOffset[i]; int newcolumn = column + ColumnOffset[i]; if (newrow >= 0 && newrow < m_rows && newcolumn >=0 && newcolumn < m_columns) switch (m_MineMatrix[newrow][newcolumn]) { case MINE: ++minecount; break; case ORIGINAL: ++originalcount; break; } } if ((double) (m_MineMatrix[row][column] - minecount) / originalcount < ExplodeProbility) { SelectRow = row; SelectColumn = column; ExplodeProbility = (double) (m_MineMatrix[row][column] - minecount) / originalcount; } } } while (!trow.IsEmpty()) { int row = trow.RemoveHead(); int column = tcolumn.RemoveHead(); if (!m_NeverCheck[row][column]) { m_WaitForCheckList_Row.AddTail(row); m_WaitForCheckList_Column.AddTail(column); } } if (SelectRow != -1) for (int i = 0; i < 8; ++i) { int newrow = SelectRow + RowOffset[i]; int newcolumn = SelectColumn + ColumnOffset[i]; if (newrow >= 0 && newrow < m_rows && newcolumn >=0 && newcolumn < m_columns) if (m_MineMatrix[newrow][newcolumn] == ORIGINAL) { AddOperation(Operation(Operation::OPEN, newrow, newcolumn)); NewWaitForIdentify(newrow, newcolumn); break; } } } else { bool flag = false; for (int i = 0; i < m_rows; ++i) { for (int j = 0; j < m_columns; ++j) { switch (m_MineMatrix[i][j]) { case ORIGINAL: AddOperation(Operation(Operation::OPEN, i, j)); NewWaitForIdentify(i, j); flag = true; break; } if (flag) break; } if (flag) break; } } }
void Algorithm::GetCertainOperations(void) { while(!m_WaitForCheckList_Row.IsEmpty()) { int row = m_WaitForCheckList_Row.RemoveHead(); int column = m_WaitForCheckList_Column.RemoveHead(); if (!m_NeverCheck[row][column]) { int minecount = 0, originalcount = 0; for (int i = 0; i < 8; ++i) { int newrow = row + RowOffset[i]; int newcolumn = column + ColumnOffset[i]; if (newrow >= 0 && newrow < m_rows && newcolumn >=0 && newcolumn < m_columns) switch (m_MineMatrix[newrow][newcolumn]) { case MINE: ++minecount; break; case ORIGINAL: ++originalcount; break; } } if (minecount == m_MineMatrix[row][column]) { m_NeverCheck[row][column] = true; if (originalcount) { AddOperation(Operation(Operation::QUICKOPEN, row, column)); for (int i = 0; i < 8; ++i) { int newrow = row + RowOffset[i]; int newcolumn = column + ColumnOffset[i]; if (newrow >= 0 && newrow < m_rows && newcolumn >=0 && newcolumn < m_columns) if (m_MineMatrix[newrow][newcolumn] == ORIGINAL) { m_MineMatrix[newrow][newcolumn] = ALREADYOPEN; NewWaitForIdentify(newrow, newcolumn); } } } } else if (minecount + originalcount == m_MineMatrix[row][column]) { m_NeverCheck[row][column] = true; for (int i = 0; i < 8; ++i) { int newrow = row + RowOffset[i]; int newcolumn = column + ColumnOffset[i]; if (newrow >= 0 && newrow < m_rows && newcolumn >=0 && newcolumn < m_columns) if (m_MineMatrix[newrow][newcolumn] == ORIGINAL) { m_MineMatrix[newrow][newcolumn] = MINE; AddOperation(Operation(Operation::MARKMINE, newrow, newcolumn)); } } } else { trow.AddTail(row); tcolumn.AddTail(column); } } } while (!trow.IsEmpty()) { int row = trow.RemoveHead(); int column = tcolumn.RemoveHead(); if (!m_NeverCheck[row][column]) { m_WaitForCheckList_Row.AddTail(row); m_WaitForCheckList_Column.AddTail(column); } } if (m_OperationList.IsEmpty()) { TRACE(_T("Searching...\n")); while(!m_WaitForCheckList_Row.IsEmpty()) { int row = m_WaitForCheckList_Row.RemoveHead(); int column = m_WaitForCheckList_Column.RemoveHead(); trow.AddTail(row); tcolumn.AddTail(column); if (!m_NeverCheck[row][column]) { Search_Clear(); Search(1, row, column); if (Search_Check()) break; //Search_Check(); } } while (!trow.IsEmpty()) { int row = trow.RemoveHead(); int column = tcolumn.RemoveHead(); if (!m_NeverCheck[row][column]) { m_WaitForCheckList_Row.AddTail(row); m_WaitForCheckList_Column.AddTail(column); } } } if (m_OperationList.IsEmpty()) { int originalcount2 = 0, minecount2 = 0; for (int i = 0; i < m_rows; ++i) for (int j = 0; j < m_columns; ++j) switch (m_MineMatrix[i][j]) { case ORIGINAL: ++originalcount2; break; case MINE: ++minecount2; break; } if (originalcount2 && originalcount2 + minecount2 == m_RemainderMines) for (int i = 0; i < m_rows; ++i) for (int j = 0; j < m_columns; ++j) switch (m_MineMatrix[i][j]) { case ORIGINAL: AddOperation(Operation(Operation::OPEN, i, j)); NewWaitForIdentify(i, j); break; } } }
void QuickEdit::OnAddOperation() { AddOperation(); }
int main(void) { char error = 1; char *input; char c; int32_t a = -1; struct wires *tmpWire; int str_buff = BUFFER; if (!(input = calloc(BUFFER, sizeof(char)))) { goto freeinput; } struct wires *wire; if (!(wire = calloc(1, sizeof(struct wires)))) { goto freewire; } struct operation *operations, *curOp, *tmpOp; if (!(operations = calloc(1, sizeof(struct operation)))) { fprintf(stderr, "Can't allocate operations memory\n"); goto freemem; } struct buffer *inBuff, *tmpBuff; if (!(inBuff = calloc(1, sizeof(struct buffer)))) { fprintf(stderr, "Can't allocate buffer memory\n"); goto freebuff; } // Declarations done, now down to work. tmpBuff = inBuff; // Get input and store in the buffers. while ((c = fgetc(stdin)) && !feof(stdin)) { if (c == '\n' || feof(stdin)) { tmpBuff = AddToBuff(tmpBuff, input); memset(input, 0, strlen(input)); } else { input = AddToString(input, &str_buff, c); } } // Iterate at least once over the input do { // If "a" has a value, wipe everything, give it to "b" if ((tmpWire = GetFromWire(wire, "a"))) { a = tmpWire->value; FreeWires(wire); SaveToWire(wire, "b", a); } else { a = -1; // Flag value. } curOp = operations; // For every buffer, parse it into struct operation. for (tmpBuff = inBuff->next; tmpBuff; tmpBuff = tmpBuff->next) { tmpOp = calloc(1, sizeof(struct operation)); curOp->next = tmpOp; tmpOp->prev = curOp; if (!(curOp = AddOperation(curOp->next, tmpBuff->line))) { goto freemem; } } // Then put into wires. OperationsToWires(wire, operations); } while (a == -1); PrintWires(wire); FreeWires(wire); error = 0; // If executing, no memory errors. freebuff: FreeBuff(inBuff); freemem: free(operations); freewire: free(wire); freeinput: free(input); return error; }