#define BYTE_NUMBER_PART 15 // 00001111 #define BYTE_FLAG_PART 240 // 11110000
struct JCStruct { bool Resolved; // char FileName[255]; // char W; // char H; // char M[MAX_PUZZLE_HW][MAX_PUZZLE_ HW]; // char Vector[CHANGE_VECTOR_SIZE][3]; // [i, j, old_value] int Vector_s; // vector start pointer ( ) int Vector_e; // vector end pointer ( ) int DigitsCnt; // - ( ) };
char GetNumberFromByte(char c) // , . , 0 { return c & BYTE_NUMBER_PART; } char GetFlagFromByte (char c) // . 0, { return c & BYTE_FLAG_PART; }
#define LN_ONE 16 // 00010000 , / . 1 #define LN_VERTICAL 32 // 00100000 / ; #define LN_HORIZONTAL 48 // 00110000 / ; #define LN_LEFT_TOP 64 // 01000000 / ; #define LN_LEFT_BOTTOM 80 // 01010000 / ; #define LN_RIGHT_TOP 96 // 01100000 / ; #define LN_RIGHT_BOTTOM 112 // 01110000 / ; #define LN_RIGHT 128 // 10000000 / ; #define LN_LEFT 144 // 10010000 / ; #define LN_TOP 160 // 10100000 / ; #define LN_BOTTOM 176 // 10110000 / .
JCStruct* jc; ... if (GetFlagFromByte(jc->M[i][j]) == LN_HORIZONTAL) // { }
struct PointStruct { int x; // int y; // }; struct CurrentLineStackStruct // , { char num; // , PointStruct stack[9]; // , , char len; // }; CurrentLineStackStruct LineStack;
void ClearCurrentStack() { LineStack.len = 0; // LineStack.num = 0; // , }
/* jc - JCStruct DrawContext - -, , : jc_screen_x - x jc_screen_y - y cell_wh - jc_screen_w - jc_screen_h - */ bool Redraw(int x, int y) // x,y -- - { if (!jc->Resolved) // , if ((DrawContext.jc_screen_x <= x) && (x < DrawContext.jc_screen_x + DrawContext.jc_screen_w)) if ((DrawContext.jc_screen_y <= y) && (y < DrawContext.jc_screen_y + DrawContext.jc_screen_h)) { // int i = (x - DrawContext.jc_screen_x) / DrawContext.cell_wh; int j = (y - DrawContext.jc_screen_y) / DrawContext.cell_wh; // , , char n = GetNumberFromByte(jc->M[i][j]); if ((LineStack.len == 0) && (n == 0)) return false; // , if (GetFlagFromByte(jc->M[i][j]) > 0) return false; // , for (int s = 0; s < LineStack.len; s++) if ((LineStack.stack[s].x == i) && (LineStack.stack[s].y == j)) { // LineStack.len = s + 1; return true; } // , , ( ) if (LineStack.len == 0) { // LineStack.len++; LineStack.num = n; // , // LineStack.stack[LineStack.len - 1].x = i; LineStack.stack[LineStack.len - 1].y = j; return true; } else // , { // , // ( num) if (LineStack.len < LineStack.num) { // , , if ((abs(LineStack.stack[LineStack.len - 1].x - i) == 1 && LineStack.stack[LineStack.len - 1].y == j) || (LineStack.stack[LineStack.len - 1].x == i && abs(LineStack.stack[LineStack.len - 1].y - j) == 1) ) { // , , , // , if (n == 0 || LineStack.num == n && LineStack.len == n - 1) { LineStack.len++; LineStack.stack[LineStack.len - 1].x = i; LineStack.stack[LineStack.len - 1].y = j; return true; } return false; } // , if ((LineStack.stack[LineStack.len - 1].x != i) && (LineStack.stack[LineStack.len - 1].y == j)) { int len = abs(i - LineStack.stack[LineStack.len - 1].x); // int d = (i - LineStack.stack[LineStack.len - 1].x) / len; // ( ) for (int s = 0; s < len; s++) { if (LineStack.len < LineStack.num) { // if (GetFlagFromByte(jc->M[LineStack.stack[LineStack.len - 1].x + d][j]) > 0) { if (s > 0) return true; // - , else return false; // , } n = GetNumberFromByte(jc->M[LineStack.stack[LineStack.len - 1].x + d][j]); if (n > 0) // { if (n != LineStack.num) // , { if (s > 0) return true; // - , else return false; // , } else // , , .. n == LineStack.num { if (LineStack.num != LineStack.len + 1) // { if (s > 0) return true; // - , else return false; // , } } } LineStack.len++; LineStack.stack[LineStack.len - 1].x = LineStack.stack[LineStack.len - 2].x + d; LineStack.stack[LineStack.len - 1].y = j; } } return true; } // , if ((LineStack.stack[LineStack.len - 1].x == i) && (LineStack.stack[LineStack.len - 1].y != j)) { int len = abs(j - LineStack.stack[LineStack.len - 1].y); // int d = (j - LineStack.stack[LineStack.len - 1].y) / len; // ( ) for (int s = 0; s < len; s++) { if (LineStack.len < LineStack.num) { // if (GetFlagFromByte(jc->M[i][LineStack.stack[LineStack.len - 1].y + d]) > 0) { if (s > 0) return true; // - , else return false; // , } n = GetNumberFromByte(jc->M[i][LineStack.stack[LineStack.len - 1].y + d]); if (n > 0) // { if (n != LineStack.num) // , { if (s > 0) return true; // - , else return false; // , } else // , , .. n == LineStack.num { if (LineStack.num != LineStack.len + 1) // { if (s > 0) return true; // - , else return false; // , } } } LineStack.len++; LineStack.stack[LineStack.len - 1].x = i; LineStack.stack[LineStack.len - 1].y = LineStack.stack[LineStack.len - 2].y + d; } } return true; } return false; } else // , { return false; } } } return false; }
/* , - , */ void CheckCurrentLineStack() { // , , // , , , if ((LineStack.len > 0) && (LineStack.len == LineStack.num) && (LineStack.num == GetNumberFromByte(jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y])) ) { // SetMatrixElement(jc, LineStack.stack[0].x, LineStack.stack[0].y/*, 1*/); // // , / [0 - , 1 - , 2 - , 3 - ] char l_prev, l_next; // , for (int i = 1; i < LineStack.len - 1; i++) { // if (LineStack.stack[i - 1].x + 1 == LineStack.stack[i].x) l_prev = 0; if (LineStack.stack[i - 1].x - 1 == LineStack.stack[i].x) l_prev = 1; if (LineStack.stack[i - 1].y + 1 == LineStack.stack[i].y) l_prev = 2; if (LineStack.stack[i - 1].y - 1 == LineStack.stack[i].y) l_prev = 3; // . if (LineStack.stack[i + 1].x + 1 == LineStack.stack[i].x) l_next = 0; if (LineStack.stack[i + 1].x - 1 == LineStack.stack[i].x) l_next = 1; if (LineStack.stack[i + 1].y + 1 == LineStack.stack[i].y) l_next = 2; if (LineStack.stack[i + 1].y - 1 == LineStack.stack[i].y) l_next = 3; l_prev = MAX(l_prev, l_next) * 10 + MIN(l_prev, l_next); switch (l_prev) { case 32: jc->M[LineStack.stack[i].x][LineStack.stack[i].y] = jc->M[LineStack.stack[i].x][LineStack.stack[i].y] + LN_VERTICAL; break; case 31: jc->M[LineStack.stack[i].x][LineStack.stack[i].y] = jc->M[LineStack.stack[i].x][LineStack.stack[i].y] + LN_LEFT_TOP; break; case 30: jc->M[LineStack.stack[i].x][LineStack.stack[i].y] = jc->M[LineStack.stack[i].x][LineStack.stack[i].y] + LN_RIGHT_TOP; break; case 21: jc->M[LineStack.stack[i].x][LineStack.stack[i].y] = jc->M[LineStack.stack[i].x][LineStack.stack[i].y] + LN_LEFT_BOTTOM; break; case 20: jc->M[LineStack.stack[i].x][LineStack.stack[i].y] = jc->M[LineStack.stack[i].x][LineStack.stack[i].y] + LN_RIGHT_BOTTOM; break; case 10: jc->M[LineStack.stack[i].x][LineStack.stack[i].y] = jc->M[LineStack.stack[i].x][LineStack.stack[i].y] + LN_HORIZONTAL; break; } } // if (LineStack.stack[1].x + 1 == LineStack.stack[0].x) jc->M[LineStack.stack[0].x][LineStack.stack[0].y] = jc->M[LineStack.stack[0].x][LineStack.stack[0].y] + LN_RIGHT; if (LineStack.stack[1].x - 1 == LineStack.stack[0].x) jc->M[LineStack.stack[0].x][LineStack.stack[0].y] = jc->M[LineStack.stack[0].x][LineStack.stack[0].y] + LN_LEFT; if (LineStack.stack[1].y + 1 == LineStack.stack[0].y) jc->M[LineStack.stack[0].x][LineStack.stack[0].y] = jc->M[LineStack.stack[0].x][LineStack.stack[0].y] + LN_BOTTOM; if (LineStack.stack[1].y - 1 == LineStack.stack[0].y) jc->M[LineStack.stack[0].x][LineStack.stack[0].y] = jc->M[LineStack.stack[0].x][LineStack.stack[0].y] + LN_TOP; // if (LineStack.stack[LineStack.len - 2].x + 1 == LineStack.stack[LineStack.len - 1].x) jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] = jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] + LN_RIGHT; if (LineStack.stack[LineStack.len - 2].x - 1 == LineStack.stack[LineStack.len - 1].x) jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] = jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] + LN_LEFT; if (LineStack.stack[LineStack.len - 2].y + 1 == LineStack.stack[LineStack.len - 1].y) jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] = jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] + LN_BOTTOM; if (LineStack.stack[LineStack.len - 2].y - 1 == LineStack.stack[LineStack.len - 1].y) jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] = jc->M[LineStack.stack[LineStack.len - 1].x][LineStack.stack[LineStack.len - 1].y] + LN_TOP; DrawContext.need_save = true; } }
if (jc->Resolved == false) // { // Iw2DSetColour(ColorSchema.JCCellLineColor); for (int i = 0; i < jc->W; i++) for (int j = 0; j < jc->H; j++) { n1 = jc->M[i][j] & BYTE_FLAG_PART; switch (n1) { case LN_HORIZONTAL: Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + 0, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1 ), CIwFVec2(DrawContext.cell_wh - 0, 3) ); break; case LN_VERTICAL: Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + 0), CIwFVec2(3, DrawContext.cell_wh - 0) ); break; case LN_RIGHT: Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + 0, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(4, 3) ); break; case LN_LEFT: Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i + 1)* DrawContext.cell_wh - 4, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(4, 3) ); break; case LN_BOTTOM: Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + 0), CIwFVec2(3, 4) ); break; case LN_TOP: Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j + 1)* DrawContext.cell_wh + 0 - 4), CIwFVec2(3, 4) ); break; case LN_RIGHT_BOTTOM: // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + 0, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(DrawContext.cell_wh / 2 - 1 + 3, 3) ); // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + 0), CIwFVec2(3, DrawContext.cell_wh / 2 - 1 + 3) ); break; case LN_RIGHT_TOP: // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + 0, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(DrawContext.cell_wh / 2 - 1 + 3, 3) ); // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(3, DrawContext.cell_wh / 2 - 1 + 3) ); break; case LN_LEFT_BOTTOM: // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(DrawContext.cell_wh / 2 - 1 + 3, 3) ); // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + 0), CIwFVec2(3, DrawContext.cell_wh / 2 - 1 + 3) ); break; case LN_LEFT_TOP: // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(DrawContext.cell_wh / 2 - 1 + 3, 3) ); // Iw2DFillRect(CIwFVec2(DrawContext.jc_screen_x + (i)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1, DrawContext.jc_screen_y + (j)* DrawContext.cell_wh + DrawContext.cell_wh / 2 - 1), CIwFVec2(3, DrawContext.cell_wh / 2 - 1 + 3) ); break; } } }
/* 0, , , */ int CheckJC(JCStruct * p) { for(int i = 0; i < p->W; i++) for(int j = 0; j < p->H; j++) if(GetNumberFromByte(p->M[i][j]) > 1) // , 1 , if(GetFlagFromByte(p->M[i][j]) == 0) // return 100 * (i + 1) + (j + 1); return 0; }
Source: https://habr.com/ru/post/301184/
All Articles