1: 2: logic 3: { 4: // Puzzles are listed in the initial puzzles array 5: // First a number between 1 and 5 to indicate difficulty and then the 9*9 grid 6: // Build the grid using 1-9 and 0 to represent an empty space 7: const int numPuzzles { 7 } 8: const array puzzles 9: { 10: 1, 11: 0,4,0,3,0,0,6,1,0, 12: 1,2,3,0,0,0,0,0,9, 13: 0,0,0,7,8,0,2,0,0, 14: 4,0,0,0,7,2,5,0,8, 15: 0,0,9,6,0,5,3,0,0, 16: 2,0,6,8,1,0,0,0,7, 17: 0,0,2,0,9,4,0,0,0, 18: 9,0,0,0,0,0,7,5,3, 19: 0,6,8,0,0,7,0,4,0, 20: 21: 2, 22: 0,0,0,0,0,6,3,0,0, 23: 0,5,0,0,0,0,0,0,8, 24: 8,6,0,2,0,1,0,9,7, 25: 1,8,0,0,0,0,2,0,0, 26: 0,0,4,1,0,7,8,0,0, 27: 0,0,2,0,0,0,0,4,9, 28: 7,3,0,8,0,5,0,6,2, 29: 2,0,0,0,0,0,0,3,0, 30: 0,0,5,9,0,0,0,0,0, 31: 32: 2, 33: 0,5,0,0,0,1,0,0,0, 34: 3,9,0,6,0,0,2,7,1, 35: 0,6,0,0,2,0,0,5,3, 36: 1,2,0,0,0,0,0,0,8, 37: 8,0,6,0,0,0,3,0,5, 38: 5,0,0,0,0,0,0,2,6, 39: 4,3,0,0,6,0,0,9,0, 40: 6,8,2,0,0,9,0,3,7, 41: 0,0,0,5,0,0,0,6,0, 42: 43: 3, 44: 5,0,0,2,0,0,4,0,1, 45: 0,0,0,0,0,8,0,0,0, 46: 4,1,0,0,0,6,0,8,0, 47: 7,0,0,0,8,0,2,0,4, 48: 0,0,4,0,0,0,3,0,0, 49: 9,0,5,0,4,0,0,0,6, 50: 0,4,0,8,0,0,0,6,9, 51: 0,0,0,9,0,0,0,0,0, 52: 8,0,3,0,0,5,0,0,7, 53: 54: 3, 55: 0,7,0,0,6,8,0,0,0, 56: 3,0,0,0,0,0,0,6,0, 57: 0,9,5,3,0,0,0,0,2, 58: 8,0,0,7,0,1,9,0,0, 59: 9,0,2,0,0,0,7,0,6, 60: 0,0,7,5,0,6,0,0,4, 61: 7,0,0,0,0,2,6,4,0, 62: 0,2,0,0,0,0,0,0,8, 63: 0,0,0,8,3,0,0,2,0, 64: 65: 4, 66: 0,3,0,7,0,0,2,9,0, 67: 2,5,8,0,0,1,7,0,0, 68: 0,0,0,0,0,5,0,0,0, 69: 0,0,9,0,0,0,8,0,0, 70: 0,0,0,4,2,3,0,0,0, 71: 0,0,2,0,0,0,3,0,0, 72: 0,0,0,8,0,0,0,0,0, 73: 0,0,5,6,0,0,9,3,7, 74: 0,9,6,0,0,4,0,8,0, 75: 76: 5, 77: 0,3,0,0,0,4,0,0,5, 78: 0,4,0,0,0,1,0,0,7, 79: 6,0,9,0,0,8,0,0,0, 80: 0,5,0,9,0,3,0,0,1, 81: 0,0,8,0,0,0,9,0,0, 82: 2,0,0,1,0,6,0,5,0, 83: 0,0,0,8,0,0,6,0,2, 84: 5,0,0,3,0,0,0,1,0, 85: 1,0,0,4,0,0,0,7,0, 86: } 87: 88: array puzzle 89: { 90: 0,0,0,0,0,0,0,0,0, 91: 0,0,0,0,0,0,0,0,0, 92: 0,0,0,0,0,0,0,0,0, 93: 0,0,0,0,0,0,0,0,0, 94: 0,0,0,0,0,0,0,0,0, 95: 0,0,0,0,0,0,0,0,0, 96: 0,0,0,0,0,0,0,0,0, 97: 0,0,0,0,0,0,0,0,0, 98: 0,0,0,0,0,0,0,0,0 99: } 100: 101: int selectedPuzzle { -1 } 102: int selectedDifficulty { 0 } 103: int selectedNumber { 0 } 104: 105: // Variables used in testing for victory 106: const array SubSquareStart { 0, 3, 6, 27, 30, 33, 54, 57, 60 } 107: local int checkOn { 0 } 108: local array numberCount { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } 109: local array puzzleComplete { 0 } 110: 111: // Allow puzzle to be chosen 112: for i (0 .. numPuzzles-1) 113: { 114: when {selectedPuzzle == -1} 115: allow 116: { 117: selectedPuzzle = i; 118: selectedDifficulty = puzzles[i*(9*9+1)]; 119: selectedNumber = 0; 120: for j (0 .. (9*9)-1) 121: { 122: puzzle[j] = puzzles[(i*(9*9+1)) + 1 + j]; 123: if (puzzle[j] == 0) {puzzle[j] = 10;} 124: } 125: } 126: label {"Picked puzzle " i} 127: } 128: 129: // Allow number to be selected 130: for i (0 .. 9) 131: { 132: when {selectedPuzzle != -1} 133: allow 134: { 135: selectedNumber = i; 136: } 137: label {"Selected " i} 138: } 139: 140: // Allow number to be placed 141: for i (0 .. (9*9)-1) 142: { 143: when {(selectedPuzzle != -1) and ((puzzle[i]%100) >= 10)} 144: allow 145: { 146: puzzle[i] = selectedNumber+10; 147: } 148: label {"Place " i} 149: } 150: 151: // The hard bit 152: when {selectedPuzzle != -1} 153: allow 154: { 155: // Check is toggleable 156: checkOn = 1 - checkOn; 157: 158: if (checkOn == 1) 159: { 160: // Do check 161: // Be optimistic... 162: puzzleComplete = 1; 163: 164: // Rows 165: for i (0 .. 8) 166: { 167: for j (0 .. 9) 168: {numberCount[j] = 0;} 169: numberCount[0] += 1; 170: 171: for j (0 .. 8) 172: {numberCount[puzzle[i*9 + j]%10] += 1;} 173: for j (0 .. 8) 174: { 175: if ((numberCount[puzzle[i*9 + j]%10] > 1) and (puzzle[i*9 + j] < 100)) 176: { 177: puzzle[i*9 + j] += 100; 178: puzzleComplete = 0; 179: } 180: } 181: } 182: // Columns 183: for i (0 .. 8) 184: { 185: for j (0 .. 9) 186: {numberCount[j] = 0;} 187: numberCount[0] += 1; 188: 189: for j (0 .. 8) 190: {numberCount[puzzle[j*9 + i]%10] += 1;} 191: for j (0 .. 8) 192: { 193: if ((numberCount[puzzle[j*9 + i]%10] > 1) and (puzzle[j*9 + i] < 100)) 194: { 195: puzzle[j*9 + i] += 100; 196: puzzleComplete = 0; 197: } 198: } 199: } 200: // Sub-squares 201: for i (0 .. 8) 202: { 203: for j (0 .. 9) 204: {numberCount[j] = 0;} 205: numberCount[0] += 1; 206: 207: for j (0 .. 8) 208: {numberCount[puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)]%10] += 1;} 209: for j (0 .. 8) 210: { 211: if ((numberCount[puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)]%10] > 1) 212: and (puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)] < 100)) 213: { 214: puzzle[SubSquareStart[i] + ((j/3)*9) + (j%3)] += 100; 215: puzzleComplete = 0; 216: } 217: } 218: } 219: } 220: if (checkOn != 1) 221: { 222: // Clear the checks 223: puzzleComplete = 0; 224: for i (0 .. 8) 225: { 226: for j (0 .. 8) 227: { 228: if (puzzle[(i*9) + j] > 100) 229: {puzzle[(i*9) + j] -= 100;} 230: } 231: } 232: } 233: } 234: label {"Check"} 235: } 236: 237: display 238: { 239: // Display 240: box {content {"Sudoku v0.2"}} 241: newline 242: 243: // Draw the puzzles that can be chosen 244: for i (0 .. numPuzzles - 1) 245: { 246: box 247: { 248: bind_action {"Picked puzzle " i} 249: 250: while_active 251: { 252: border { 1px solid black } 253: content { "Puzzle " (i+1) " - Difficulty " puzzles[i*(9*9+1)] } 254: } 255: } 256: newline 257: } 258: 259: // If they have succeeded... 260: box 261: { 262: if (puzzleComplete == 1) 263: { 264: border { 1px solid black } 265: background { #aaf } 266: content {"Congratulations!"} 267: } 268: } 269: newline 270: box {if (puzzleComplete == 1) {content{" "}}} 271: newline 272: 273: // Draw the board, assigning all the possible actions as we go 274: for y ( 0 .. 8 ) 275: { 276: for x ( 0 .. 8 ) 277: { 278: box 279: { 280: if (selectedPuzzle != -1) 281: { 282: border { 1px solid black } 283: width { 2em } 284: height { 2em } 285: background { #fff } 286: if (puzzle[(y*9)+x]%10 == 0) 287: {content {" "}} 288: if (puzzle[(y*9)+x]%10 != 0) 289: {content {puzzle[(y*9)+x]%10}} 290: 291: bind_action { "Place " ((y*9)+x) } 292: while_active 293: {background { #aaf }} 294: 295: if (puzzle[(y*9)+x] > 100) 296: {border { 1px solid red }} 297: } 298: } 299: } 300: newline 301: } 302: newline 303: 304: box {content {" "}} 305: newline 306: 307: for i ( 0 .. 9 ) 308: { 309: box 310: { 311: bind_action { "Selected " i } 312: while_active 313: { 314: border { 1px solid black } 315: width { 2em } 316: height { 2em } 317: background { #fff } 318: if (i == 0) 319: {content {" "}} 320: if (i != 0) 321: {content {i}} 322: background { #fff } 323: 324: if (i == selectedNumber) 325: {background { #aaf }} 326: } 327: } 328: } 329: newline 330: 331: box {if (selectedPuzzle != -1) {content{" "}}} 332: newline 333: 334: box 335: { 336: bind_action {"Check"} 337: while_active 338: { 339: border { 1px solid black } 340: content {"Check Puzzle"} 341: if (checkOn == 1) {content {"Clear Check"}} 342: } 343: } 344: newline 345: 346: box {if (selectedPuzzle != -1) {content{" "}}} 347: newline 348: }