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0 0 0 0 8 7 1 0 0 0 0 0 0 4 0 9 0 0 +0 0 4 0 1 0 0 2 0 0 6 0 0 0 9 0 1 5 0 2 0 0 3 0 7 0 0 8 0 0 4 0 6 0 0 0 0 9 0 0 0 7 0 5 3 0 0 1 0 0 0 8 0 0 5 0 7 0 0 0 0 8 9 0 0 3 0 9 4 6 0 0 0 1 0 8 0 2 0 0 0 +4 0 3 0 0 1 0 0 0 0 2 0 0 0 6 0 0 7 0 0 8 5 0 0 0 1 9 1 0 0 0 9 0 2 5 0 0 0 0 4 7 0 0 0 0 9 0 6 0 0 8 0 0 0 0 0 0 8 0 2 0 7 3 2 0 5 3 0 0 0 6 0 0 7 4 0 0 0 0 8 0 +0 7 9 8 0 0 3 0 0 0 0 1 6 0 0 2 0 0 5 0 0 0 0 0 8 7 6 0 1 5 0 7 0 0 6 0 0 0 0 0 3 9 0 0 0 0 0 0 0 0 4 5 2 8 8 4 0 0 0 2 0 0 0 0 3 0 0 5 0 1 0 0 0 0 0 7 0 0 6 9 0 \ No newline at end of file diff --git a/code.txt b/code.txt new file mode 100644 index 0000000..68ecf14 --- /dev/null +++ b/code.txt @@ -0,0 +1,806 @@ +INCLUDE Irvine32.inc +;SUDOKU SOLVER AND GAME + +Authentication0 PROTO, valt:DWORD +Authentication1 PROTO, valt:DWORD +checkGrid PROTO, rowNo:DWORD, colNo:DWORD +.data + ;GRID-INFO: + N BYTE 9 + score DWORD 0 + grid BYTE 81 DUP (?) + gridcopy BYTE 81 DUP (?) + gridgame_tmp BYTE 1630 DUP(0) ;81 ELEMENTS, 80 SPACES (EACH LINE CONTAINS + ONE SUDOKU-TOTAL 10 SUDOKUS) + + ;BOOLEANS: + bool1 BYTE ? + bool2 BYTE ? + mbool3 BYTE 0 + bool4 BYTE ? + bool5 BYTE 0 + bool6 BYTE 0 + + ;MESSAGES: + end_msg BYTE "\\----- GAME IS NOW EXITING -----//", 0 + mesg BYTE "The solved grid is: ", 0 + msg BYTE "No solution exists for the given grid!", 0 + msg2 BYTE "Unsolved Grid:", 0 + msg3 BYTE "Solved Grid:", 0 + msg4 BYTE "Enter the row no: ", 0 + msg5 BYTE "Enter the column no: ", 0 + msg6 BYTE "Enter the value: ", 0 + msg7 BYTE "You Won!!!", 0 + space BYTE ' ', 0 + multiplespace BYTE " ", 0 + vertical BYTE "| ", 0 + horizontal BYTE "---", 0 + rowmsg BYTE "Enter the elements for ROW-", 0 + msgg BYTE "This is an INVALID VALUE! ", 0 + st_msg1 BYTE "Hey! I'm Shaggy!", 0 + st_msg2 BYTE "Do You Wanna PLAY SUDOKU with me or LET me SOLVE one with you??", 0 + st_msg3 BYTE "ENTER 0 TO START THE GAME", 0 + st_msg4 BYTE "ENTER 1 TO GET YOUR SUDOKU SOLVED", 0 + st_msg5 BYTE "Your Option: ", 0 + st_msg6 BYTE "ENTER ANY KEY TO GO BACK TO THE MAIN MENU......", 0 + sc_msg BYTE "Woah! Your score is ", 0 + ;FILE-HANDLING: + filename BYTE "Sudoku.txt", 0 + filehandle DWORD ? + + +.code + Authentication1 PROC USES edx, valt:DWORD ;checks whether the data is from 1 to 9 + MOV bool6, 0 + MOV edx, valt + CMP edx, 9 + JNC J1 + CMP edx, 1 + JNC J2 + + J1: + JZ J2 + RET + + J2: + MOV bool6, 1 ;TRUE + RET + Authentication1 ENDP + + Authentication0 PROC USES edx eax, valt:DWORD ;checks whether the data is from 0 to 9 + MOV bool5, 0 + MOV edx, valt + CMP edx, 9 + JNC J1 + CMP edx, 0 + JNC J2 + + J1: + JZ J2 + RET + + J2: + MOV bool5, 1 ;TRUE + RET + Authentication0 ENDP + + checkGrid PROC USES eax ebx esi, rowNo:DWORD, colNo:DWORD + MOV bool6, 0 + MOV ebx, OFFSET gridcopy + MOV eax, rowNo + MUL N + ADD ebx, eax + MOV esi, colNo + + cmp BYTE PTR [ebx+esi], 0 + JNZ J1 + MOV bool6, 1 + J1: + RET + checkGrid endp + + inputValues PROC USES eax esi edx ebx + LOCAL rowNo:DWORD, colNo:DWORD, Value:DWORD + + L1: ; While Loop + MOV rowNo, 0 + MOV colNo, 0 + LEA eax, rowNo + LEA ebx, colNo + + PUSH eax + PUSH ebx + CALL findEmptyPlace + ADD esp, 8 + + CMP bool2, 1 ;empty place found + JNZ J3 + + MOV ebx, OFFSET grid + MOV esi, 0 + MOV edx, OFFSET msg4 + CALL WriteString + CALL readDec + MOV rowNo, eax + INVOKE Authentication1, rowNo + CMP bool6, 1 + JNZ J1 + DEC rowNo + + MOV bool5, 0 + MOV bool6, 0 + MOV edx, OFFSET msg5 + CALL WriteString + CALL readDec + MOV colNo, eax + INVOKE Authentication1, colNo + CMP bool6, 1 + JNZ J1 + DEC colNo + + PUSH rowNo + PUSH colNo + INVOKE checkGrid, rowNo, colNo + CMP bool6, 1 + JNZ J1 + + MOV bool6, 0 + MOV bool5, 0 + MOV edx, OFFSET msg6 + CALL WriteString + CALL readDec + MOV Value, eax + + MOV bool4, 0 + PUSH rowNo + PUSH colNo + PUSH Value + CALL isvalidplace + ADD esp, 12 + CMP bool4, 1 + JNZ J1 + + MOV eax, rowNo + MUL N + ADD ebx, eax + MOV esi, colNo + MOV eax, Value + INVOKE Authentication1, eax + CMP bool6, 1 + JNZ J1 ;The case of invalid + + ADD score, 5 + MOV BYTE PTR [ebx+esi], al + CALL printGrid + JMP J2 + + J1: + CALL crlf + MOV edx, OFFSET msgg + CALL WriteString + CALL crlf + CALL crlf + CALL PrintGrid + + J2: + JMP L1 + + J3: + MOV bool2, 0 + MOV edx, OFFSET msg7 + CALL crlf + CALL writeString + MOV edx, OFFSET sc_msg + CALL crlf + CALL crlf + CALL crlf + CALL WriteString + MOV eax, score + CALL WriteDec + + CALL crlf + CALL crlf + MOV edx, OFFSET st_msg6 + CALL WriteString + CALL crlf + CALL crlf + + CALL readChar + + RET + inputValues ENDP + + inputgrid PROC USES eax ebx ecx edx + LOCAL var_counter: DWORD + MOV var_counter, 0 + MOVZX ecx, N + MOV ebx, OFFSET grid + MOV eax, 0 + L1: + PUSH ecx + MOV esi, 0 + CALL PrintGrid + MOV edx, OFFSET rowmsg + CALL Writestring + MOV eax, var_counter + INC eax + CALL WriteDec + MOVZX ecx, N + CALL crlf + L2: + CALL ReadInt + INVOKE Authentication0, eax + CMP bool5, 1 + JNZ JM2 + + CMP eax, 0 + JZ J1 + PUSH var_counter + PUSH esi + PUSH eax + CALL isvalidplace + ADD esp, 12 + CMP bool4, 1 + JNZ JM2 + MOV bool4, 0 + J1: + MOV BYTE PTR [ebx+esi], al + MOV eax, 0 + INC esi ;COL-INC + LOOP L2 + JMP JM3 + JM2: + MOV edx, OFFSET msgg + CALL WriteString + CALL crlf + JMP L2 + JM3: + INC var_counter + CALL clrscr + MOVZX edi, N + ADD ebx, edi ;ROW-JUMP + POP ecx + DEC ecx + CMP ecx, 0 + JNZ L1 + RET + inputgrid ENDP + + printHorizontalLine PROC USES ecx + PUSH ebp + MOV ebp, esp + MOVZX ecx, N + DEC ecx + CALL crlf + MOV edx, OFFSET multiplespace + CALL WriteString + L1: + MOV edx, OFFSET horizontal + CALL WriteString + LOOP L1 + + POP ebp + RET + printHorizontalLine ENDP + + PrintGrid PROC USES eax edx ebx esi edi ecx + PUSH ebp + MOV ebp, esp + + CALL printHorizontalLine + CALL crlf + + MOV ebx, OFFSET grid + MOVZX ecx, N + + l1: + MOV edi, ecx + MOV esi, 0 + MOVZX ecx, N + MOV edx, OFFSET multiplespace + CALL WriteString + l2: + CMP ecx, 6 + JC J2 + JNZ J1 + MOV edx, OFFSET vertical + CALL WriteString + J2: + CMP ecx, 3 + JNZ J1 + MOV edx, OFFSET vertical + CALL WriteString + J1: + CMP ecx, 9 + JNZ J5 + MOV edx, OFFSET vertical + CALL WriteString + J5: + + MOVZX eax, BYTE PTR [ebx+esi] + CALL WriteDec + MOV edx, OFFSET space + CALL WriteString + INC esi + + CMP ecx, 1 + JNZ J6 + MOV edx, OFFSET vertical + CALL WriteString + J6: + LOOP l2 + MOVZX eax, N + ADD ebx, eax + + CMP edi, 7 + JC J4 + JNZ J3 + CALL printHorizontalLine + + J4: + CMP edi, 4 + JNZ J3 + CALL printHorizontalLine + J3: + + CMP edi, 1 + JNZ J7 + CALL printHorizontalLine + J7: + + CALL crlf + MOV ecx, edi + DEC ecx + CMP ecx, 0 + JNZ l1 + POP ebp + CALL crlf + RET + PrintGrid ENDP + + isPresentIncol PROC USES ecx esi ebx eax edx + PUSH ebp + MOV ebp, esp + MOVZX ecx, N + MOV ebx, OFFSET grid + MOV esi, [ebp+32] ;COL-VALUE + MOVZX edx, BYTE PTR [ebp+28] ;NUM-VALUE + L1: + PUSH ecx + MOVZX ecx, N + CMP BYTE PTR [ebx+esi], dl + JZ J2 + ADD ebx, ecx ;row-jump + POP ecx + LOOP L1 + + POP ebp + RET + + J2: + POP ecx + INC mbool3 + POP ebp + RET + isPresentIncol ENDP + + isPresentInrow PROC USES ecx esi ebx eax edx + PUSH ebp + MOV ebp, esp + MOVZX ecx, N + MOV ebx, OFFSET grid + MOV eax, [ebp+32] ;row + MUL N + ADD ebx, eax + MOV esi, 0 ; COL-VALUE + MOVZX edx, BYTE PTR [ebp+28] ;NUM-VALUE + L1: + PUSH ecx + MOVZX ecx, N + CMP BYTE PTR [ebx+esi], dl + JZ J2 + INC esi ;COL-INC + POP ecx + LOOP L1 + + POP ebp + RET + + J2: + POP ecx + INC mbool3 + POP ebp + RET + isPresentInrow ENDP + + isPresentInBox PROC USES ecx esi ebx eax edx edi + PUSH ebp + MOV ebp, esp + MOV ebx, OFFSET grid + MOV esi, 0 + + MOVZX eax, BYTE PTR [ebp+40] ;row-row%3 + MOVZX edi, BYTE PTR [ebp+32] ;EDI = NUM + MOV ecx, 3 + MUL N ;eax + MOV edx, [ebp+36] ;col-col%3 + ADD ebx, eax + + L1: + PUSH ecx + MOV ecx, 3 + MOV esi, edx + L2: + PUSH ecx + MOVZX ecx, BYTE PTR [ebx+esi] + CMP edi, ecx + JZ J1 + POP ecx + INC esi + loop L2 + PUSH edx + MOVZX edx, N + ADD ebx, edx ;OFFSET+9 (For next row) + POP edx + POP ecx + loop L1 + + POP ebp + RET + + J1: + INC mbool3 + POP ecx + POP ecx + POP ebp + RET + isPresentInBox ENDP + + isValidPlace PROC + PUSH eax + PUSH edx + PUSH esi + PUSH ebp + MOV ebp, esp + mov mbool3, 0 + + PUSH DWORD PTR [ebp+28] ;ROW + PUSH DWORD PTR [ebp+20] ;NUM + call isPresentInRow + add esp, 8 + + PUSH [ebp+24] ;COL + PUSH [ebp+20] ;NUM + call isPresentInCol + add esp, 8 + + MOV esi, 3 + MOV edx, 0 + MOV eax, [ebp+28] + DIV esi + MOV eax, [ebp+28] + SUB eax, edx + PUSH eax ;row- (row%3) + + mov edx, 0 + mov eax, [ebp+24] + DIV esi + MOV eax, [ebp+24] + SUB eax, edx + + PUSH eax ;col- (col%3) + PUSH [ebp+20] ;NUM + CALL isPresentInBox + ADD esp, 12 + + CMP mbool3, 0 + JNZ J1 + MOV bool4, 1 + J1: + POP ebp + POP esi + POP edx + POP eax + RET + isValidPlace ENDP + + findEmptyPlace PROC USES edx esi edi ebx eax ecx ;return address at ebp+28 + PUSH ebp + MOV ebp, esp + MOV ebx, OFFSET grid + + MOV eax, [ebp+36] ;row-address + MOV edx, [ebp+32] ;col-address + MOV DWORD PTR [eax], 0 ;row + MOV DWORD PTR [edx], 0 ;col + + MOVZX ecx, N + L1: + PUSH ecx + MOV esi, 0 + + MOV BYTE PTR [edx], 0 + + MOVZX ecx, N + L2: + CMP BYTE PTR [ebx+esi], 0 + JZ ST1 + INC esi + INC BYTE PTR [edx] + LOOP L2 + MOVZX edi, N + ADD ebx, edi + INC BYTE PTR [eax] + POP ecx + LOOP L1 + POP ebp + MOV bool2, 0 ;FALSE + RET + + ST1: + POP ecx + POP ebp + MOV bool2, 1 ;TRUE + RET + findEmptyPlace ENDP + + SolveSudoku PROC USES eax ebx esi edi edx + PUSH ebp + mov ebp, esp + sub esp, 12 + lea eax, [ebp-4] ; row + lea ebx, [ebp-8] ; col + + PUSH eax + PUSH ebx + CALL findEmptyPlace + ADD esp, 8 + + CMP bool2, 1 ;empty place found + JZ J1 + MOV bool1, 1 ;TRUE + MOV esp, ebp + POP ebp + RET + + J1: + MOV bool2, 0 + MOV DWORD PTR [ebp-12], 1 ;try each number + MOVZX ecx, N + L1: + PUSH [eax] ;row + PUSH [ebx] ;col + PUSH DWORD PTR [ebp-12] + CALL isValidPlace + ADD esp, 12 + + CMP bool4, 1 + JNZ ST1 + MOV bool4, 0 + PUSH eax + MOV edi, OFFSET grid + MOV eax, [eax] ;row + MOV esi, [ebx] ;col + MUL N + ADD edi, eax + MOV edx, DWORD PTR [ebp-12] + MOV BYTE PTR [edi+esi], dl + POP eax + + MOV bool1, 0 + PUSH ecx + CALL SolveSudoku + POP ecx + CMP bool1, 1 + JZ ST2 + + PUSH eax + MOV edi, OFFSET grid + MOV eax, [eax] ;row + MOV esi, [ebx] ;col + MUL N + ADD edi, eax + MOV BYTE PTR [edi+esi], 0 + POP eax + + ST1: + INC DWORD PTR [ebp-12] + mov edi, DWORD PTR [ebp-12] + LOOP L1 + + mov esp, ebp + POP ebp + MOV bool1, 0 ;FALSE + RET + + ST2: + mov esp, ebp + POP ebp + RET + SolveSudoku ENDP + + ReadTheGrid PROC USES ecx edx eax esi edi ebx + PUSH ebp + MOV ebp, esp + + MOV score, 0 + MOV edx, OFFSET filename + CALL OpenInputFile + MOV filehandle, eax + MOV ecx, SIZEOF gridgame_tmp + MOV edx, OFFSET gridgame_tmp + CALL ReadFromFile + + MOV ebx, OFFSET gridgame_tmp + MOVZX eax, N ;UPPER BOUND FOR PSEUDO-RANDOM NUMBER + CALL Randomize + CALL RandomRange ;RANDOM NUMBER IN eax + MOVZX ecx, N + MOV edx, 0 + RANDGRID: + CMP edx, eax + JE ASS1 + ADD ebx, 163 + INC edx + LOOP RANDGRID + + ;transfering the values of the randomly selected grid without spaces + ASS1: + MOV esi, ebx + MOV ecx, LENGTHOF grid + MOV edi, OFFSET grid + CLD + L2: + MOVSB + INC esi + LOOP L2 + + MOV esi, OFFSET grid + + MOV ecx, LENGTHOF gridcopy + MOV edi, OFFSET gridcopy + CLD + L3: + MOVSB + LOOP L3 + + MOV ecx, LENGTHOF grid + MOV esi, OFFSET grid + MOV edi, OFFSET gridcopy + L1: + SUB BYTE PTR [esi], 48 + INC esi + SUB BYTE PTR [edi], 48 + INC edi + LOOP L1 + + POP ebp + RET + ReadTheGrid ENDP + + PlayGame PROC + ENTER 1, 0 + CALL ReadTheGrid + CALL PrintGrid + CALL inputValues + + LEAVE + RET + PlayGame ENDP + + initialisegrid PROC USES ecx edi eax + PUSH ebp + MOV eax, 0 + MOV ecx, SIZE grid + MOV edi, OFFSET grid + CLD + REP STOSB + POP ebp + RET + initialisegrid ENDP + + SudokuSolver PROC USES edx + PUSH ebp + MOV ebp, esp + + CALL inputgrid + CALL SolveSudoku + CMP bool1, 1 + JNZ ST2 + MOV edx, OFFSET mesg + CALL WriteString + CALL crlf + CALL crlf + CALL PrintGrid + JMP ST3 + + ST2: + MOV edx, OFFSET msg + CALL WriteString + CALL crlf + CALL crlf + + ST3: + CALL crlf + CALL crlf + MOV edx, OFFSET st_msg6 + CALL WriteString + CALL crlf + CALL crlf + + CALL readChar + + POP ebp + RET + SudokuSolver ENDP + + WelcomeInterface PROC USES ebx edx + PUSH ebp + MOV ebp, esp + MOV eax, 16 + MOV bl, 15 + MUL bl + CALL SetTextColor + CALL clrscr + + MOV edx, OFFSET st_msg1 + CALL WriteString + CALL crlf + + MOV edx, OFFSET st_msg2 + CALL WriteString + CALL crlf + + MOV edx, OFFSET st_msg3 + CALL WriteString + CALL crlf + + MOV edx, OFFSET st_msg4 + CALL WriteString + CALL crlf + + MOV edx, OFFSET st_msg5 + CALL WriteString + MOV eax, 0 + CALL ReadInt + CALL clrscr + + POP ebp + RET + WelcomeInterface ENDP + + + ;ENTERING-POINT: + main PROC USES eax ebx edx ecx + ENTER 0, 0 + L1: + CALL WelcomeInterface + ;USER'S CHOICE IS RETURNING IN EAX + CMP eax, 0 + JNZ J1 + + ;MODULE FOR THE SUDOKU-PLAYER (SUDOKU-GAME) + CALL PlayGame + JMP J2 + + J1: + CMP eax, 1 + JNZ J2 + + ;MODULE FOR USER-GIVEN SUDOKU (SUDOKU-SOLVER) + CALL initialisegrid + CALL SudokuSolver + + Loop L1 + J2: + CALL crlf + CALL crlf + MOV edx, OFFSET end_msg + CALL crlf + CALL WriteString + CALL crlf + LEAVE + EXIT + RET + main ENDP +END MAIN \ No newline at end of file