Jump to content

Help simplifying a code


Ruben
 Share

Recommended Posts

Hello again, i need your help with a little program that im making.

Im trying to create a simple sudoku generator and later on a solver but im having trouble with finding the right commands for this

The idea is to use use random with all numbers from 1 to 9 and then put it in the right spot, (you will understand what i want to do if you play sudoku)

My only problem is the "if" + "and" commands, i would have to make looong line if i want to make it this way... i woul'd like to know if there's a better way to do this also because the code lags allot too.

Here's what i made so far

#include
#include
#include
#include

HotKeySet('{ESC}','_exit')
HotKeySet('{PAUSE}','pause')
HotKeySet('{HOME}','play')

Local $aArray[11][11]
$r = 1
$c = 0
pause()

Func play()

While $r < 10
$c = $c+1

If $c = 10 then
$c = 1
$r = $r +1
EndIf


While $aArray[$r][$c] = 0
$somevalue = Random(1,9,1)
if $aArray[$r][0] <> $somevalue And $aArray[$r][1] <> $somevalue And $aArray[$r][2] <> $somevalue And $aArray[$r][3] <> $somevalue And $aArray[$r][4] <> $somevalue And $aArray[$r][5] <> $somevalue And $aArray[$r][6] <> $somevalue And $aArray[$r][7] <> $somevalue And $aArray[$r][8] <> $somevalue And $aArray[$r][9] <> $somevalue And $aArray[0][$c] <> $somevalue And $aArray[1][$c] <> $somevalue And $aArray[2][$c] <> $somevalue And $aArray[3][$c] <> $somevalue And $aArray[4][$c] <> $somevalue And $aArray[5][$c] <> $somevalue And $aArray[6][$c] <> $somevalue And $aArray[7][$c] <> $somevalue And $aArray[8][$c] <> $somevalue Then
$aArray[$r][$c] = $somevalue
EndIf
WEnd

Sleep(20)
WEnd

_ArrayDisplay($aArray, "dsffsdf")

$Form1 = GUICreate("Form1", 238, 251, 399, 365)

$x=1
$y=0
$Input11 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 20, 20, 20)
$Input12 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 20, 20, 20)
$Input13 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 20, 20, 20)
$Input14 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 20, 20, 20)
$Input15 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 20, 20, 20)
$Input16 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 20, 20, 20)
$Input17 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 20, 20, 20)
$Input18 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 20, 20, 20)
$Input19 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 20, 20, 20)
$x=2
$Input21 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 40, 20, 20)
$Input22 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 40, 20, 20)
$Input23 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 40, 20, 20)
$Input24 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 40, 20, 20)
$Input25 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 40, 20, 20)
$Input26 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 40, 20, 20)
$Input27 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 40, 20, 20)
$Input28 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 40, 20, 20)
$Input29 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 40, 20, 20)
$x=3
$Input31 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 60, 20, 20)
$Input32 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 60, 20, 20)
$Input33 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 60, 20, 20)
$Input34 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 60, 20, 20)
$Input35 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 60, 20, 20)
$Input36 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 60, 20, 20)
$Input37 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 60, 20, 20)
$Input38 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 60, 20, 20)
$Input39 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 60, 20, 20)
$x=4
$Input41 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 90, 20, 20)
$Input42 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 90, 20, 20)
$Input43 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 90, 20, 20)
$Input44 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 90, 20, 20)
$Input45 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 90, 20, 20)
$Input46 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 90, 20, 20)
$Input47 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 90, 20, 20)
$Input48 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 90, 20, 20)
$Input49 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 90, 20, 20)
$x=5
$Input51 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 110, 20, 20)
$Input52 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 110, 20, 20)
$Input53 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 110, 20, 20)
$Input54 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 110, 20, 20)
$Input55 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 110, 20, 20)
$Input56 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 110, 20, 20)
$Input57 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 110, 20, 20)
$Input58 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 110, 20, 20)
$Input59 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 110, 20, 20)
$x=6
$Input61 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 130, 20, 20)
$Input62 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 130, 20, 20)
$Input63 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 130, 20, 20)
$Input64 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 130, 20, 20)
$Input65 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 130, 20, 20)
$Input66 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 130, 20, 20)
$Input67 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 130, 20, 20)
$Input68 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 130, 20, 20)
$Input69 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 130, 20, 20)
$x=7
$Input71 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 160, 20, 20)
$Input72 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 160, 20, 20)
$Input73 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 160, 20, 20)
$Input74 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 160, 20, 20)
$Input75 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 160, 20, 20)
$Input76 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 160, 20, 20)
$Input77 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 160, 20, 20)
$Input78 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 160, 20, 20)
$Input79 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 160, 20, 20)
$x=8
$Input81 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 180, 20, 20)
$Input82 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 180, 20, 20)
$Input83 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 180, 20, 20)
$Input84 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 180, 20, 20)
$Input85 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 180, 20, 20)
$Input86 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 180, 20, 20)
$Input87 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 180, 20, 20)
$Input88 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 180, 20, 20)
$Input89 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 180, 20, 20)
$x=9
$Input91 = GUICtrlCreateInput($aArray[$x][$y+1], 20, 200, 20, 20)
$Input92 = GUICtrlCreateInput($aArray[$x][$y+2], 40, 200, 20, 20)
$Input93 = GUICtrlCreateInput($aArray[$x][$y+3], 60, 200, 20, 20)
$Input94 = GUICtrlCreateInput($aArray[$x][$y+4], 90, 200, 20, 20)
$Input95 = GUICtrlCreateInput($aArray[$x][$y+5], 110, 200, 20, 20)
$Input96 = GUICtrlCreateInput($aArray[$x][$y+6], 130, 200, 20, 20)
$Input97 = GUICtrlCreateInput($aArray[$x][$y+7], 160, 200, 20, 20)
$Input98 = GUICtrlCreateInput($aArray[$x][$y+8], 180, 200, 20, 20)
$Input99 = GUICtrlCreateInput($aArray[$x][$y+9], 200, 200, 20, 20)

GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

EndSwitch
WEnd

EndFunc

Func pause()
While 1
sleep (1000)
WEnd
EndFunc

Func _exit()
Exit
EndFunc

Thanks

Edited by Ruben
Link to comment
Share on other sites

Use a Loop for your comparisons

$j=0
For $i = 0 To UBound ( $aArray ) -1
$aArray[$r][$i] <> $somevalue Then $j +=1
Next
If $j = UBound ( $aArray ) Then $aArray[$r][$c] = $somevalue

Thanks but the problem isn't make a loop that add values to the array but that checks the whole row "$r" and column "$c" in the 2d array if there's a value equal to the random number before apply it

Later on i would also have to make it check 3x3 areas as-well because Sudoku works that way

Edit*

After looking at the loop better, i saw that it may work with some changes

Edited by Ruben
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...