Sign in to follow this  
Followers 0
Paulie

Make A Gui Grid

14 posts in this topic

#1 ·  Posted (edited)

how to you make a 9x9 grid in gui that you can input numbers on each grid? i don't know what function to use

also i need to be able to individually read text of each gridspace as well

Edited by Paulie

Share this post


Link to post
Share on other sites



in scripts and scraps you can use the table.au3 and then put input boxes in where the cells are

Share this post


Link to post
Share on other sites

how to you make a 9x9 grid in gui that you can input numbers on each grid? i don't know what function to use

also i need to be able to individually read text of each gridspace as well

Hmmm... 9 by 9 you say? How about something like:

CODE
;------------------------------------------------------

; Sodoku_GUI.au3

;

; v0.2 - 07May06 - by PsaltyDS at www.autoitscript.com forum

;

; An experiment in AutoIT scripting for a Sodoku fan.

;------------------------------------------------------

; Version History:

; ----------------

; 0.1 06May06 Basic GUI display done, no functionality

; 0.2 07May06 Basic button actions and clicking on a cell working

;------------------------------------------------------

; Include Files

#include<guiconstants.au3>

; Default game file

$SodokuIni = @ScriptDir & "\Sodoku001.ini"

; Change to OnEvent mode

Opt("GUIOnEventMode", 1)

; Edit these variables to tweak the GUI dimensions:

$Gap = 10 ; Gap between elements

$Title_H = 40 ; Title height

$Button_Cnt = 4 ; Number of control buttons

$Button_H = 30 ; Button height

$Cell_Sz = 50 ; Size of each Sodoku row/col cell

; GUI dimensions calculated from above values:

$Square_Sz = $Cell_Sz * 3 ; Size of major squares

$Gui_W = ($Square_Sz * 3) + ($Gap * 4) ; GUI width

$Title_W = $Gui_W - ($Gap * 2) ; Title label width

$Button_Left = $Gap ; Left edge of first button

$Button_Top = ($Gap * 2) + $Title_H ; Top of buttons

$Button_W = Round(($Gui_W - ($Gap * ($Button_Cnt + 1))) / $Button_Cnt) ; Button width

$Graphic_Left = $Gap ; Left edge of graphic area

$Graphic_Top = $Button_Top + $Button_H + $Gap ; Top of graphic area

$Graphic_W = ($Square_Sz * 3) + ($Gap * 2) ; Width of graphic area

$Graphic_H = $Graphic_W ; Height of graphic area

$Gui_H = $Title_H + $Button_H + $Graphic_H + ($Gap * 4) ; GUI height

; GUI colors

$GraphicAreaColor = 0x707070

$CellLineColor = 0x000000

$ActiveCellColor = 0xffffff

$InactiveCellColor = 0xa0a0a0

; 3D 9x9x3 array of Sodoku cells

; $Cells[r][c][p] where: r = row, c = col, p = parameter

; $Cells[0][0][p] = top, left

; $Cells[8][8][p] = bottom, right

; Parameters:

; [r][c][0] = Count of remaining numbers

; [r][c][1] = 1,2,3,4,5,6,7,8,9 (remaining numbers not eliminated)

; [r][c][2] = Control ID of cell

; [r][c][3] = Control ID of text label

; When the count at [r][c][0] = 1 the square is solved

Dim $Cells[9][9][4]

; 2D array of row results

; r = row 0 thru 8, row 0 is top, 8 is bottom

; [r][0] = Count of remaining numbers, when count=0 row is solved

; [r][1] = 1,2,3,4,5,6,7,8,9 (remaining numbers to locate)

Dim $Rows[9][2]

; 2D array of column results

; c = column 0 thru 8, column 0 is left, 8 is right

; [c][0] = Count of remaining numbers, when count=0 column is solved

; [c][1] = 1,2,3,4,5,6,7,8,9 (remaining numbers to locate)

Dim $Columns[9][2]

; 2D array of square results

; s = square 0 thru 8, square 0 is top/left, square 8 is bottom right

; [0] = Count of remaining numbers, when count=0 square is solved

; [1] = 1,2,3,4,5,6,7,8,9 (remaining numbers to locate)

Dim $Squares[9][2]

; Initialize the puzzle arrays

_InitPuzzle()

;------------------------------------------------------

; Create the GUI

;------------------------------------------------------

$GuiHandle = GUICreate("Sodoku GUI", $Gui_W, $Gui_H)

GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiClosed", $GuiHandle)

$Title_1 = GUICtrlCreateLabel("SODOKU GUI", $Gap, $Gap, $Title_W, $Title_H, $SS_CENTER)

GUICtrlSetFont($Title_1, 20, 800)

$Button_1 = GUICtrlCreateButton("LOAD", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_1, "_Button_1")

$Button_2 = GUICtrlCreateButton("SAVE", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_2, "_Button_2")

$Button_3 = GUICtrlCreateButton("CLEAR", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_3, "_Button_3")

$Button_4 = GUICtrlCreateButton("SOLVE", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_4, "_Button_4")

$GraphicArea = GUICtrlCreateGraphic ($Graphic_Left, $Graphic_Top, $Graphic_W, $Graphic_H)

GUICtrlSetGraphic ($GraphicArea, $GUI_GR_COLOR, $GraphicAreaColor, $GraphicAreaColor)

GUICtrlSetGraphic ($GraphicArea, $GUI_GR_RECT, 0, 0, $Graphic_W, $Graphic_H)

GUICtrlSetState($GraphicArea, $GUI_DISABLE) ; Disabled so the background won't be clickable

_DrawSquares()

_UpdateGUI()

GUISetState(@SW_SHOW, $GuiHandle)

While 1

Sleep(2000)

WEnd

;End of main sequence

Exit

; =======================================================

; Local Functions

; =======================================================

; -------------------------------------------

; _DrawSquares()

; Draw the Sodoku squares on the GUI

; -------------------------------------------

Func _DrawSquares()

Local $Cell_Left = $Graphic_Left

Local $Cell_Top = $Graphic_Top

For $r = 0 To 8

$Cell_Left = $Graphic_Left

For $c = 0 To 8

$Cells[$r][$c][2] = GUICtrlCreateGraphic ($Cell_Left, $Cell_Top, $Cell_Sz, $Cell_Sz)

GUICtrlSetGraphic ($Cells[$r][$c][2], $GUI_GR_COLOR, $CellLineColor, $ActiveCellColor)

GUICtrlSetGraphic ($Cells[$r][$c][2], $GUI_GR_RECT, 0, 0, $Cell_Sz, $Cell_Sz)

GuiCtrlSetOnEvent($Cells[$r][$c][2], "_ClickOnCell")

; Create text label on cell

$Cells[$r][$c][3] = GUICtrlCreateLabel("", $Cell_Left + 4, $Cell_Top + 4, $Cell_Sz - 8, $Cell_Sz - 8, $SS_CENTER)

GUICtrlSetBkColor($Cells[$r][$c][3], $ActiveCellColor)

$Cell_Left = $Cell_Left + $Cell_Sz

If $c = 2 Or $c = 5 Then $Cell_Left = $Cell_Left + $Gap

Next

$Cell_Top = $Cell_Top + $Cell_Sz

If $r = 2 Or $r = 5 Then $Cell_Top = $Cell_Top + $Gap

Next

EndFunc ;==>_DrawSquares

; -------------------------------------------

; _UpdateGUI()

; Update the dynamic data on the Sodoku squares

; -------------------------------------------

Func _UpdateGUI()

Local $r, $c, $t, $sText

Local $avSplit

For $r = 0 To 8

For $c = 0 To 8

If $Cells[$r][$c][0] = 1 Then

; Cell is solved show result in large font

GUICtrlSetFont($Cells[$r][$c][3], 32, 800)

GUICtrlSetData($Cells[$r][$c][3], $Cells[$r][$c][1])

Else

; Cell not solved yet, get remaining numbers

$sText = ""

$avSplit = StringSplit($Cells[$r][$c][1], ",")

For $t = 1 To $avSplit[0]

$sText = $sText & $avSplit[$t]

If $t = 3 Or $t = 6 Then $sText = $sText & @CRLF

Next

GUICtrlSetFont($Cells[$r][$c][3], 9, 400)

GUICtrlSetData($Cells[$r][$c][3], $sText)

EndIf

Next

Next

EndFunc ;==>_UpdateGUI

; -------------------------------------------

; _Button_1()

; Button_1 pushed: Load

; -------------------------------------------

Func _Button_1()

Local $r, $c, $sSaveData

Local $avRowData[9]

Local $avColData[10]

Local $avCellData[9][9]

; Get Sodoku ini file

$SodokuIni = FileOpenDialog("Sodoku GUI", @ScriptDir, "Sodoku INI file (Sodoku*.ini)", 8, $SodokuIni)

If @error Then

MsgBox(16, "Sodoku GUI", "Error, game not loaded!")

Return 0

EndIf

; Read the row data into an array

For $r = 0 To 8

$sSaveData = IniRead($SodokuIni, "SodokuGUI", $r, 0)

If Not $sSaveData Then

MsgBox(16, "Sodoku GUI", "File does not contain valid Sodoku game data!" & @CRLF & _

"Game not loaded!")

Return 0

Else

$avRowData[$r] = $sSaveData

EndIf

Next

; Split each row into columns and save to cell array

For $r = 0 To 8

$avColData = StringSplit($avRowData[$r], ",")

If $avColData[0] <> 9 Then

MsgBox(16, "Sodoku GUI", "File does not contain valid Sodoku game data!" & @CRLF & _

"Game not loaded!")

Return 0

EndIf

For $c = 0 To 8

$avCellData[$r][$c] = $avColData[$c + 1]

Next

Next

; Only after seeing valid data, offer to load the game

If MsgBox(32 + 1, "Sodoku GUI", "Are you sure you want to load over the current game?") <> 1 Then Return 0

; Initialize puzzle arrays

_InitPuzzle()

; Write game data to $Cells array

For $r = 0 To 8

For $c = 0 To 8

If $avCellData[$r][$c] = 0 Then

$Cells[$r][$c][0] = 9

$Cells[$r][$c][1] = "1,2,3,4,5,6,7,8,9"

Else

$Cells[$r][$c][0] = 1

$Cells[$r][$c][1] = $avCellData[$r][$c]

EndIf

Next

Next

; Update the GUI

_UpdateGUI()

EndFunc ;==>_Button_1

; -------------------------------------------

; _Button_2()

; Button_2 pushed: Save

; -------------------------------------------

Func _Button_2()

Local $r, $c, $sSaveData

$SodokuIni = FileOpenDialog("Sodoku GUI", @ScriptDir, "Sodoku INI file (Sodoku*.ini)", 8, $SodokuIni)

If @error Then

MsgBox(16, "Sodoku GUI", "Error, game not saved!")

Return 0

Else

If MsgBox(32, "Sodoku GUI", "Save game to " & $SodokuIni & "?") = 1 Then

For $r = 0 To 8

$sSaveData = ""

For $c = 0 To 8

If $sSaveData <> "" Then $sSaveData = $sSaveData & ","

If $Cells[$r][$c][0] = 1 Then

$sSaveData = $sSaveData & $Cells[$r][$c][1]

Else

$sSaveData = $sSaveData & 0

EndIf

Next

If Not IniWrite($SodokuIni, "SodokuGUI", $r, $sSaveData) Then

MsgBox(16, "Sodoku GUI", "Error writing to file: " & $SodokuIni & @CRLF & _

"Game not saved!")

Return 0

EndIf

Next

EndIf

EndIf

EndFunc ;==>_Button_2

; -------------------------------------------

; _Button_3()

; Button_3 pushed: Clear

; -------------------------------------------

Func _Button_3()

If MsgBox(32 + 1, "Sodoku GUI", "Are you sure you want to clear the current puzzle?" & @CRLF & _

"Any unsaved changes will be lost!" & @CRLF & _

" Click OK to clear the puzzle, or CANCEL keep current data...") = 1 Then

; Initialize the arrays

_InitPuzzle()

; Update puzzle GUI

_UpdateGUI()

EndIf

EndFunc ;==>_Button_3

; -------------------------------------------

; _Button_4()

; Button_4 pushed: Solve

; -------------------------------------------

Func _Button_4()

MsgBox(64, "Sodoku GUI", "'SOLVE' button pushed.", 3)

EndFunc ;==>_Button_4

; -------------------------------------------

; _GuiClosed()

; GUI closed, exit

; -------------------------------------------

Func _GuiClosed()

Exit

EndFunc ;==>_GuiClosed

; -------------------------------------------

; _ClickOnCell()

; Clicked on one of the cells

; Control ID of cell is in @GUI_CTRLID

; Control IDs can be looked up at $Cells[r][c][2]

; -------------------------------------------

Func _ClickOnCell()

Local $r, $c, $sMsg, $sNewValue

Local $CellCtrlID = @GUI_CtrlId

; Identify which cell was clicked

For $r = 0 To 8

For $c = 0 To 8

If $Cells[$r][$c][2] = $CellCtrlID Then ExitLoop 2

Next

Next

; Show current status of the cell and ask about changing it

If $Cells[$r][$c][0] = 1 Then

$sMsg = "Sodoku cell [" & $r & "][" & $c & "] is solved." & @CRLF & _

"Answer was: " & $Cells[$r][$c][1]

Else

$sMsg = "Sodoku cell [" & $r & "][" & $c & "] is not yet solved." & @CRLF & _

"Remaining possibilities are: " & $Cells[$r][$c][1]

EndIf

$sMsg = $sMsg & @CRLF & @CRLF & "Do you want to manualy set or clear the cell?" & @CRLF & _

@CRLF & "NOTE: Manualy setting a cell will clear all other unsolved cells." & @CRLF & _

@CRLF & "Click OK to manualy set the cell, or CANCEL to leave the current value..."

If MsgBox(32 + 1, "Sodoku GUI", $sMsg) = 1 Then

$sNewValue = InputBox("Sodoku GUI", "Enter a nuber from 1 thru 9 to set, or 0 to clear the cell: ", "", "", 400, 100)

Select

Case $sNewValue = "" Or Not StringIsInt($sNewValue) Or $sNewValue < 0 Or $sNewValue > 9

MsgBox(64, "Debug", "Bad entry: " & $sNewValue, 3)

Return 0

Case $sNewValue = 0

MsgBox(64, "Debug", "Clear the cell...", 3)

Case $sNewValue >= 1

MsgBox(64, "Debug", "Set the cell to: " & $sNewValue, 3)

EndSelect

EndIf

EndFunc ;==>_ClickOnCell

; -------------------------------------------

; _InitPuzzle()

; Initializes all cells, rows, columns, and squares of the puzzle.

; -------------------------------------------

Func _InitPuzzle()

Local $r, $c, $s

; Initialize $Cells array

For $r = 0 To 8

For $c = 0 To 8

$Cells[$r][$c][0] = 9

$Cells[$r][$c][1] = "1,2,3,4,5,6,7,8,9"

Next

Next

; Initialize $Rows array

For $r = 0 To 8

$Rows[$r][0] = 9

$Rows[$r][0] = "1,2,3,4,5,6,7,8,9"

Next

; Initialize $Columns array

For $c = 0 To 8

$Columns[$c][0] = 9

$Columns[$c][0] = "1,2,3,4,5,6,7,8,9"

Next

; Initialize $Squares array

For $s = 0 To 8

$Squares[$s][0] = 9

$Squares[$s][0] = "1,2,3,4,5,6,7,8,9"

Next

EndFunc ;==>_InitPuzzle

Just a game experiment I'm working on... :)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Hmmm... 9 by 9 you say? How about something like:

CODE
;------------------------------------------------------

; Sodoku_GUI.au3

;

; v0.2 - 07May06 - by PsaltyDS at www.autoitscript.com forum

;

; An experiment in AutoIT scripting for a Sodoku fan.

;------------------------------------------------------

; Version History:

; ----------------

; 0.1 06May06 Basic GUI display done, no functionality

; 0.2 07May06 Basic button actions and clicking on a cell working

;------------------------------------------------------

; Include Files

#include<guiconstants.au3>

; Default game file

$SodokuIni = @ScriptDir & "\Sodoku001.ini"

; Change to OnEvent mode

Opt("GUIOnEventMode", 1)

; Edit these variables to tweak the GUI dimensions:

$Gap = 10 ; Gap between elements

$Title_H = 40 ; Title height

$Button_Cnt = 4 ; Number of control buttons

$Button_H = 30 ; Button height

$Cell_Sz = 50 ; Size of each Sodoku row/col cell

; GUI dimensions calculated from above values:

$Square_Sz = $Cell_Sz * 3 ; Size of major squares

$Gui_W = ($Square_Sz * 3) + ($Gap * 4) ; GUI width

$Title_W = $Gui_W - ($Gap * 2) ; Title label width

$Button_Left = $Gap ; Left edge of first button

$Button_Top = ($Gap * 2) + $Title_H ; Top of buttons

$Button_W = Round(($Gui_W - ($Gap * ($Button_Cnt + 1))) / $Button_Cnt) ; Button width

$Graphic_Left = $Gap ; Left edge of graphic area

$Graphic_Top = $Button_Top + $Button_H + $Gap ; Top of graphic area

$Graphic_W = ($Square_Sz * 3) + ($Gap * 2) ; Width of graphic area

$Graphic_H = $Graphic_W ; Height of graphic area

$Gui_H = $Title_H + $Button_H + $Graphic_H + ($Gap * 4) ; GUI height

; GUI colors

$GraphicAreaColor = 0x707070

$CellLineColor = 0x000000

$ActiveCellColor = 0xffffff

$InactiveCellColor = 0xa0a0a0

; 3D 9x9x3 array of Sodoku cells

; $Cells[r][c][p] where: r = row, c = col, p = parameter

; $Cells[0][0][p] = top, left

; $Cells[8][8][p] = bottom, right

; Parameters:

; [r][c][0] = Count of remaining numbers

; [r][c][1] = 1,2,3,4,5,6,7,8,9 (remaining numbers not eliminated)

; [r][c][2] = Control ID of cell

; [r][c][3] = Control ID of text label

; When the count at [r][c][0] = 1 the square is solved

Dim $Cells[9][9][4]

; 2D array of row results

; r = row 0 thru 8, row 0 is top, 8 is bottom

; [r][0] = Count of remaining numbers, when count=0 row is solved

; [r][1] = 1,2,3,4,5,6,7,8,9 (remaining numbers to locate)

Dim $Rows[9][2]

; 2D array of column results

; c = column 0 thru 8, column 0 is left, 8 is right

; [c][0] = Count of remaining numbers, when count=0 column is solved

; [c][1] = 1,2,3,4,5,6,7,8,9 (remaining numbers to locate)

Dim $Columns[9][2]

; 2D array of square results

; s = square 0 thru 8, square 0 is top/left, square 8 is bottom right

; [0] = Count of remaining numbers, when count=0 square is solved

; [1] = 1,2,3,4,5,6,7,8,9 (remaining numbers to locate)

Dim $Squares[9][2]

; Initialize the puzzle arrays

_InitPuzzle()

;------------------------------------------------------

; Create the GUI

;------------------------------------------------------

$GuiHandle = GUICreate("Sodoku GUI", $Gui_W, $Gui_H)

GUISetOnEvent($GUI_EVENT_CLOSE, "_GuiClosed", $GuiHandle)

$Title_1 = GUICtrlCreateLabel("SODOKU GUI", $Gap, $Gap, $Title_W, $Title_H, $SS_CENTER)

GUICtrlSetFont($Title_1, 20, 800)

$Button_1 = GUICtrlCreateButton("LOAD", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_1, "_Button_1")

$Button_2 = GUICtrlCreateButton("SAVE", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_2, "_Button_2")

$Button_3 = GUICtrlCreateButton("CLEAR", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_3, "_Button_3")

$Button_4 = GUICtrlCreateButton("SOLVE", $Button_Left, $Button_Top, $Button_W, $Button_H)

$Button_Left = $Button_Left + $Button_W + $Gap

GuiCtrlSetOnEvent($Button_4, "_Button_4")

$GraphicArea = GUICtrlCreateGraphic ($Graphic_Left, $Graphic_Top, $Graphic_W, $Graphic_H)

GUICtrlSetGraphic ($GraphicArea, $GUI_GR_COLOR, $GraphicAreaColor, $GraphicAreaColor)

GUICtrlSetGraphic ($GraphicArea, $GUI_GR_RECT, 0, 0, $Graphic_W, $Graphic_H)

GUICtrlSetState($GraphicArea, $GUI_DISABLE) ; Disabled so the background won't be clickable

_DrawSquares()

_UpdateGUI()

GUISetState(@SW_SHOW, $GuiHandle)

While 1

Sleep(2000)

WEnd

;End of main sequence

Exit

; =======================================================

; Local Functions

; =======================================================

; -------------------------------------------

; _DrawSquares()

; Draw the Sodoku squares on the GUI

; -------------------------------------------

Func _DrawSquares()

Local $Cell_Left = $Graphic_Left

Local $Cell_Top = $Graphic_Top

For $r = 0 To 8

$Cell_Left = $Graphic_Left

For $c = 0 To 8

$Cells[$r][$c][2] = GUICtrlCreateGraphic ($Cell_Left, $Cell_Top, $Cell_Sz, $Cell_Sz)

GUICtrlSetGraphic ($Cells[$r][$c][2], $GUI_GR_COLOR, $CellLineColor, $ActiveCellColor)

GUICtrlSetGraphic ($Cells[$r][$c][2], $GUI_GR_RECT, 0, 0, $Cell_Sz, $Cell_Sz)

GuiCtrlSetOnEvent($Cells[$r][$c][2], "_ClickOnCell")

; Create text label on cell

$Cells[$r][$c][3] = GUICtrlCreateLabel("", $Cell_Left + 4, $Cell_Top + 4, $Cell_Sz - 8, $Cell_Sz - 8, $SS_CENTER)

GUICtrlSetBkColor($Cells[$r][$c][3], $ActiveCellColor)

$Cell_Left = $Cell_Left + $Cell_Sz

If $c = 2 Or $c = 5 Then $Cell_Left = $Cell_Left + $Gap

Next

$Cell_Top = $Cell_Top + $Cell_Sz

If $r = 2 Or $r = 5 Then $Cell_Top = $Cell_Top + $Gap

Next

EndFunc ;==>_DrawSquares

; -------------------------------------------

; _UpdateGUI()

; Update the dynamic data on the Sodoku squares

; -------------------------------------------

Func _UpdateGUI()

Local $r, $c, $t, $sText

Local $avSplit

For $r = 0 To 8

For $c = 0 To 8

If $Cells[$r][$c][0] = 1 Then

; Cell is solved show result in large font

GUICtrlSetFont($Cells[$r][$c][3], 32, 800)

GUICtrlSetData($Cells[$r][$c][3], $Cells[$r][$c][1])

Else

; Cell not solved yet, get remaining numbers

$sText = ""

$avSplit = StringSplit($Cells[$r][$c][1], ",")

For $t = 1 To $avSplit[0]

$sText = $sText & $avSplit[$t]

If $t = 3 Or $t = 6 Then $sText = $sText & @CRLF

Next

GUICtrlSetFont($Cells[$r][$c][3], 9, 400)

GUICtrlSetData($Cells[$r][$c][3], $sText)

EndIf

Next

Next

EndFunc ;==>_UpdateGUI

; -------------------------------------------

; _Button_1()

; Button_1 pushed: Load

; -------------------------------------------

Func _Button_1()

Local $r, $c, $sSaveData

Local $avRowData[9]

Local $avColData[10]

Local $avCellData[9][9]

; Get Sodoku ini file

$SodokuIni = FileOpenDialog("Sodoku GUI", @ScriptDir, "Sodoku INI file (Sodoku*.ini)", 8, $SodokuIni)

If @error Then

MsgBox(16, "Sodoku GUI", "Error, game not loaded!")

Return 0

EndIf

; Read the row data into an array

For $r = 0 To 8

$sSaveData = IniRead($SodokuIni, "SodokuGUI", $r, 0)

If Not $sSaveData Then

MsgBox(16, "Sodoku GUI", "File does not contain valid Sodoku game data!" & @CRLF & _

"Game not loaded!")

Return 0

Else

$avRowData[$r] = $sSaveData

EndIf

Next

; Split each row into columns and save to cell array

For $r = 0 To 8

$avColData = StringSplit($avRowData[$r], ",")

If $avColData[0] <> 9 Then

MsgBox(16, "Sodoku GUI", "File does not contain valid Sodoku game data!" & @CRLF & _

"Game not loaded!")

Return 0

EndIf

For $c = 0 To 8

$avCellData[$r][$c] = $avColData[$c + 1]

Next

Next

; Only after seeing valid data, offer to load the game

If MsgBox(32 + 1, "Sodoku GUI", "Are you sure you want to load over the current game?") <> 1 Then Return 0

; Initialize puzzle arrays

_InitPuzzle()

; Write game data to $Cells array

For $r = 0 To 8

For $c = 0 To 8

If $avCellData[$r][$c] = 0 Then

$Cells[$r][$c][0] = 9

$Cells[$r][$c][1] = "1,2,3,4,5,6,7,8,9"

Else

$Cells[$r][$c][0] = 1

$Cells[$r][$c][1] = $avCellData[$r][$c]

EndIf

Next

Next

; Update the GUI

_UpdateGUI()

EndFunc ;==>_Button_1

; -------------------------------------------

; _Button_2()

; Button_2 pushed: Save

; -------------------------------------------

Func _Button_2()

Local $r, $c, $sSaveData

$SodokuIni = FileOpenDialog("Sodoku GUI", @ScriptDir, "Sodoku INI file (Sodoku*.ini)", 8, $SodokuIni)

If @error Then

MsgBox(16, "Sodoku GUI", "Error, game not saved!")

Return 0

Else

If MsgBox(32, "Sodoku GUI", "Save game to " & $SodokuIni & "?") = 1 Then

For $r = 0 To 8

$sSaveData = ""

For $c = 0 To 8

If $sSaveData <> "" Then $sSaveData = $sSaveData & ","

If $Cells[$r][$c][0] = 1 Then

$sSaveData = $sSaveData & $Cells[$r][$c][1]

Else

$sSaveData = $sSaveData & 0

EndIf

Next

If Not IniWrite($SodokuIni, "SodokuGUI", $r, $sSaveData) Then

MsgBox(16, "Sodoku GUI", "Error writing to file: " & $SodokuIni & @CRLF & _

"Game not saved!")

Return 0

EndIf

Next

EndIf

EndIf

EndFunc ;==>_Button_2

; -------------------------------------------

; _Button_3()

; Button_3 pushed: Clear

; -------------------------------------------

Func _Button_3()

If MsgBox(32 + 1, "Sodoku GUI", "Are you sure you want to clear the current puzzle?" & @CRLF & _

"Any unsaved changes will be lost!" & @CRLF & _

" Click OK to clear the puzzle, or CANCEL keep current data...") = 1 Then

; Initialize the arrays

_InitPuzzle()

; Update puzzle GUI

_UpdateGUI()

EndIf

EndFunc ;==>_Button_3

; -------------------------------------------

; _Button_4()

; Button_4 pushed: Solve

; -------------------------------------------

Func _Button_4()

MsgBox(64, "Sodoku GUI", "'SOLVE' button pushed.", 3)

EndFunc ;==>_Button_4

; -------------------------------------------

; _GuiClosed()

; GUI closed, exit

; -------------------------------------------

Func _GuiClosed()

Exit

EndFunc ;==>_GuiClosed

; -------------------------------------------

; _ClickOnCell()

; Clicked on one of the cells

; Control ID of cell is in @GUI_CTRLID

; Control IDs can be looked up at $Cells[r][c][2]

; -------------------------------------------

Func _ClickOnCell()

Local $r, $c, $sMsg, $sNewValue

Local $CellCtrlID = @GUI_CtrlId

; Identify which cell was clicked

For $r = 0 To 8

For $c = 0 To 8

If $Cells[$r][$c][2] = $CellCtrlID Then ExitLoop 2

Next

Next

; Show current status of the cell and ask about changing it

If $Cells[$r][$c][0] = 1 Then

$sMsg = "Sodoku cell [" & $r & "][" & $c & "] is solved." & @CRLF & _

"Answer was: " & $Cells[$r][$c][1]

Else

$sMsg = "Sodoku cell [" & $r & "][" & $c & "] is not yet solved." & @CRLF & _

"Remaining possibilities are: " & $Cells[$r][$c][1]

EndIf

$sMsg = $sMsg & @CRLF & @CRLF & "Do you want to manualy set or clear the cell?" & @CRLF & _

@CRLF & "NOTE: Manualy setting a cell will clear all other unsolved cells." & @CRLF & _

@CRLF & "Click OK to manualy set the cell, or CANCEL to leave the current value..."

If MsgBox(32 + 1, "Sodoku GUI", $sMsg) = 1 Then

$sNewValue = InputBox("Sodoku GUI", "Enter a nuber from 1 thru 9 to set, or 0 to clear the cell: ", "", "", 400, 100)

Select

Case $sNewValue = "" Or Not StringIsInt($sNewValue) Or $sNewValue < 0 Or $sNewValue > 9

MsgBox(64, "Debug", "Bad entry: " & $sNewValue, 3)

Return 0

Case $sNewValue = 0

MsgBox(64, "Debug", "Clear the cell...", 3)

Case $sNewValue >= 1

MsgBox(64, "Debug", "Set the cell to: " & $sNewValue, 3)

EndSelect

EndIf

EndFunc ;==>_ClickOnCell

; -------------------------------------------

; _InitPuzzle()

; Initializes all cells, rows, columns, and squares of the puzzle.

; -------------------------------------------

Func _InitPuzzle()

Local $r, $c, $s

; Initialize $Cells array

For $r = 0 To 8

For $c = 0 To 8

$Cells[$r][$c][0] = 9

$Cells[$r][$c][1] = "1,2,3,4,5,6,7,8,9"

Next

Next

; Initialize $Rows array

For $r = 0 To 8

$Rows[$r][0] = 9

$Rows[$r][0] = "1,2,3,4,5,6,7,8,9"

Next

; Initialize $Columns array

For $c = 0 To 8

$Columns[$c][0] = 9

$Columns[$c][0] = "1,2,3,4,5,6,7,8,9"

Next

; Initialize $Squares array

For $s = 0 To 8

$Squares[$s][0] = 9

$Squares[$s][0] = "1,2,3,4,5,6,7,8,9"

Next

EndFunc ;==>_InitPuzzle

Just a game experiment I'm working on... :)

lol Yes I was doing a AutoIt Sudoku

I think it would be fun to try

Now how did you know? :(:D

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

lol Yes I was doing a AutoIt Sudoku

I think it would be fun to try

Now how did you know? :(:D

I'm a Sodoku fan myself, but I rub holes in the paper if I try to do it pencil, and the puzzle looks like a fountain pen exploded on it if I do it in ink. I thought it would be cool to do an AutoIT script for it. The SOLVE button on my GUI is the least important part (and doesn't work yet). What I really wanted was a way to enter the puzzle from the paper, or wherever, and then save it. So the SAVE/LOAD buttons and the .ini file format for saved games were what I really wanted.

It also works out some kinks in my technique for self-adjusting GUIs. You'll note in the code that I can tweak the size of certain elements in the top of the listing and the rest of the GUI spacing is calculated from those.

The next step, for v0.3, will be more usefull options when you click on a cell, and a print button. I'm also thinking of putting pull down menus at the top instead of buttons - since I haven't done that in AutoIT yet and need to learn.

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I'm a Sodoku fan myself, but I rub holes in the paper if I try to do it pencil, and the puzzle looks like a fountain pen exploded on it if I do it in ink. I thought it would be cool to do an AutoIT script for it. The SOLVE button on my GUI is the least important part (and doesn't work yet). What I really wanted was a way to enter the puzzle from the paper, or wherever, and then save it. So the SAVE/LOAD buttons and the .ini file format for saved games were what I really wanted.

It also works out some kinks in my technique for self-adjusting GUIs. You'll note in the code that I can tweak the size of certain elements in the top of the listing and the rest of the GUI spacing is calculated from those.

The next step, for v0.3, will be more usefull options when you click on a cell, and a print button. I'm also thinking of putting pull down menus at the top instead of buttons - since I haven't done that in AutoIT yet and need to learn.

:)

What I REALLY want is a script that generates plausable games randomly so they are never the same

and have the ability to increase the board size to like 16x16 or something for difficulty i'm not into a solver

Share this post


Link to post
Share on other sites

What I REALLY want is a script that generates plausable games randomly so they are never the same

and have the ability to increase the board size to like 16x16 or something for difficulty i'm not into a solver

Yeah the solver wasn't the point (the solve button will probably become PRINT in the next version). As for generating puzzles... that's more math and game theory than I know yet...

For instance, what's the smallest number of solved cells you can provide and still have only one possible answer? I don't know. How do you select solved cells for the begining based on dificulty? I don't know.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Yeah the solver wasn't the point (the solve button will probably become PRINT in the next version). As for generating puzzles... that's more math and game theory than I know yet...

For instance, what's the smallest number of solved cells you can provide and still have only one possible answer? I don't know. How do you select solved cells for the begining based on dificulty? I don't know.

:)

I guess it wouldn't be too dificult, just a ALOT of conditional statements

Just pick like 15 of the 81 cells and say

$cell[4][6] = random(1,9)
If $cell[4][6] = $cell[1][6], $cell[2][6], $cell[3][6], $cell[5][6], $cell[6][6], $cell[7][6], $cell[8][6], $cell[9][6], $cell[4][1], $cell[4][2], $cell[4][3], $cell[4][4], $cell[4][5], $cell[4][7], $cell[4][8], $cell[4][9] then
Do 
$cell[4][6] = random(1,9)
Until $cell[4][6] <> $cell[1][6], $cell[2][6], $cell[3][6], $cell[5][6], $cell[6][6], $cell[7][6], $cell[8][6], $cell[9][6], $cell[4][1], $cell[4][2], $cell[4][3], $cell[4][4], $cell[4][5], $cell[4][7], $cell[4][8], $cell[4][9] 
Endif

Then repeat that for all 15 cells Note:this isn't including the box part of it, it doesn't check the 3x3's only the rows and colums.

I'm sure theres a formula you could use to write these easier, but that would be what it would involve

math, and sunday night is still the weekend :(

Share this post


Link to post
Share on other sites

I have started this script, but i was wondering if there was a way to format these code lines easier like with variables so that i don't have to hand make these conditional statements, I'm sure theres a way, but its just too much logic for my one brain to handle,

Backround:

I have split the sudoku board into 9 3x3 peices instead of 1 9x9 (to better handle 3x3 checking) --- these are referenced as $t1-9 in decenting order

so for each 3x3 check, i need it to take all cells that contain info and compare it to that cell, however i must exclude that cell to avoid redundancies

code for $t1[1][1]

Notice how all other cells in $t1 are referenced to other than [1][1]

;1,1
If $t1[1][1] not "" then
If $t1[1][1] = $t1[1][2],$t1[1][3],$t1[2][1],$t1[2][2],$t1[2][3],$t1[3][1]$t1[3][2]$t1[3][3]then
GUICtrlSetBkColor($t1[1][1],0x0000ff)
Endif
Endif

how do i do that with all cells without re-writing that 80 times

Share this post


Link to post
Share on other sites

I have started this script, but i was wondering if there was a way to format these code lines easier like with variables so that i don't have to hand make these conditional statements, I'm sure theres a way, but its just too much logic for my one brain to handle,

Backround:

I have split the sudoku board into 9 3x3 peices instead of 1 9x9 (to better handle 3x3 checking) --- these are referenced as $t1-9 in decenting order

so for each 3x3 check, i need it to take all cells that contain info and compare it to that cell, however i must exclude that cell to avoid redundancies

code for $t1[1][1]

Notice how all other cells in $t1 are referenced to other than [1][1]

;1,1
If $t1[1][1] not "" then
If $t1[1][1] = $t1[1][2],$t1[1][3],$t1[2][1],$t1[2][2],$t1[2][3],$t1[3][1]$t1[3][2]$t1[3][3]then
GUICtrlSetBkColor($t1[1][1],0x0000ff)
Endif
Endif

how do i do that with all cells without re-writing that 80 times

My intention was to derive the coordinates in a function. I can't do anything with the code at the moment 'cause I'm not on a Windows box. But the idea was like:

$av_Coords =_ReadSquare($i_Sqr)

Where it would work out from the square number (0-8) the 2D coordinates of the squares and return them as an array. Then no matter which square it was you would still access via For loops of $av_Coords[0] thru [8].

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

IDK if this will work lol just changed daves labels to inputs, you should be able to type everything u need in there

HAHAHA was so bored just thought that adding a limit to the input boxes would be nice for this since you can limit it to 1 might add Only numbers or letters IDK whatever im bored

#include <GUIConstants.au3>
GUICreate("Sudoku",500,500)
   $1x1 = _GUICtrlCreateTable(25, 25, 3, 3, 40, 40,1)
   $1x2 = _GUICtrlCreateTable(25, 170, 3, 3, 40, 40,1)
   $1x3 = _GUICtrlCreateTable(25, 315, 3, 3, 40, 40,1)
   $2x1 = _GUICtrlCreateTable(170, 25, 3, 3, 40, 40,1)
   $2x2 = _GUICtrlCreateTable(170, 170, 3, 3, 40, 40,1)
   $2x3 = _GUICtrlCreateTable(170, 315, 3, 3, 40, 40,1)
   $3x1 = _GUICtrlCreateTable(315, 25, 3, 3, 40, 40,1)
   $3x2 = _GUICtrlCreateTable(315, 170, 3, 3, 40, 40,1)
   $3x3 = _GUICtrlCreateTable(315, 315, 3, 3, 40, 40,1)
GUISetState ()

do
$msg = guigetmsg()
until $msg = $GUI_EVENT_CLOSE

;=============================================================================
;
; Function Name:   _GUICtrlCreateTable()
;
; Description:   Creates a table, resembling the html-style
;
; Syntax:         _GUICtrlCreateTable($left, $top, $rows, $cols, $width, $height, [$border])
;
; Parameter(s);     $left = left side of the table
;                   $top = top of the table
;                   $rows = number of rows to be created
;                   $cols = number of columns to be created
;                   $width = width of ONE cell
;                   $height = height of ONE cell
;                   $border = [optional] thickness of the border, default: 1
;
; Return Value(s): array[rows][cols], used to set values with GUICtrlSetData
;
; Note:         do NOT overwrite the returned array[0][0], it contains data for the _GUICtrlTableSpan() function
;
; Author:           rakudave <rakudave@gmx.net>
;=============================================================================


Func _GUICtrlCreateTable($_left, $_top, $_rows, $_cols, $_width, $_height,$_limit, $_border = 1)
Local $_table[$_rows +1][$_cols +1]
$_table[0][0] = $_left & "|" & $_top & "|" & $_rows & "|" & $_cols & "|" & $_width & "|" & $_height & "|" & $_border
GUICtrlCreateLabel("",$_left,$_top, $_width * $_rows + $_rows * $_border + $_border +2, $_height * $_cols + $_cols * $_border + $_border +2)
If $_border > 0 then GUICtrlSetStyle(-1,$SS_BLACKFRAME)
for $x = 1 to $_rows
    for $y = 1 to $_cols
        GUICtrlCreateLabel("",($x -1) * $_width + $_left + $x * $_border +1,($y -1) * $_height + $_top + $y * $_border +1, $_width, $_height)
        If $_border > 0 then GUICtrlSetStyle(-1,$SS_BLACKFRAME)
        $_table[$x][$y] = GUICtrlCreateInput("",($x -1) * $_width + $_left + $x * $_border +4,($y -1) * $_height + $_top + $y * $_border +4, $_width -6, $_height -6)
        GUICtrlSetLimit(-1,$_limit)
    next
next
return $_table
Endfunc

;=============================================================================
;
; Function Name:   _GUICtrlTableSpan()
;
; Description:   this function is used to unite some cells created by _GUICtrlCreateTable()
;
; Syntax:         _GUICtrlTableSpan($id, $from_row, $from_col, $to_row, $to_col)
;
; Parameter(s);     $id = the parameter returned by _GUICtrlCreateTable()
;                   $from_row = left index of the first cell to unite
;                   $from_col = top index of the first cell
;                   $to_row = left index of the last cell
;                   $to_col = top index of the last cell
;
; Return Value(s): the new parameter of the combined cell
;
; Author:           rakudave <rakudave@gmx.net>
;=============================================================================

Func _GUICtrlTableSpan($_id, $from_row, $from_col, $to_row, $to_col)
$_tabledata = Stringsplit($_id[0][0],"|");$_left, $_top, $_rows, $_cols, $_width, $_height, $_border
for $x = $from_row to $to_row
    for $y = $from_col to $to_col
        GUICtrlDelete($_id[$x][$y])
    next
next
GUICtrlCreateInput("",$_tabledata[1] + ($from_row - 1) * $_tabledata[5] + $from_row * $_tabledata[7] +1, $_tabledata[2] + ($from_col - 1) * $_tabledata[6] + $from_col * $_tabledata[7] +1,($to_row - $from_row + 1) * $_tabledata[5] + ($to_row -$from_row) * $_tabledata[7],($to_col - $from_col + 1) * $_tabledata[6] + ($to_col - $from_col) * $_tabledata[7])
If $_tabledata[7] > 0 then GUICtrlSetStyle(-1,$SS_BLACKFRAME)
GUICtrlCreateInput("",$_tabledata[1] + ($from_row - 1) * $_tabledata[5] + $from_row * $_tabledata[7] +2, $_tabledata[2] + ($from_col - 1) * $_tabledata[6] + $from_col * $_tabledata[7] +2,($to_row - $from_row + 1) * $_tabledata[5] + ($to_row -$from_row) * $_tabledata[7] -2,($to_col - $from_col + 1) * $_tabledata[6] + ($to_col - $from_col -1) * $_tabledata[7] + $_tabledata[7] -2)
$_id[$from_row][$from_col] = GUICtrlCreateInput("",$_tabledata[1] + ($from_row - 1) * $_tabledata[5] + $from_row * $_tabledata[7]  +4, $_tabledata[2] + ($from_col - 1) * $_tabledata[6] + $from_col * $_tabledata[7] + $_tabledata[7] +2,($to_row - $from_row + 1) * $_tabledata[5] + ($to_row -$from_row -1) * $_tabledata[7] -4,($to_col - $from_col + 1) * $_tabledata[6] + ($to_col - $from_col -1) * $_tabledata[7] -4)
return $_id[$from_row][$from_col]
Endfunc
Edited by thatsgreat2345

Share this post


Link to post
Share on other sites

Thanks for the help, but i'm not sure you guys understood what i was asking(or didn't know how to respond)

@psaltyds-I don't quite understand, but with a little elaboration this could be promising

@thatsgreat2345- well this wasn't quite what i was looking for but it was an improvement to the script :)

To explain in more detail:

Since every 3x3 zone on a sudoku game board can only contain each number once, i need a conditional statement saying:

If cell1,1 is = to cell1,2 or cell1,3 or cell2,1 ect. then turn it red to indicate illegal value

however, that is a LOT LOT LOT of conditional statements with very many changes to handwrite :( , so i would like a couple of varibles that increase each time to folow the format of that statement but change the values so that it doesn't turn it red if "cell1,1 = cell1,1" and so my script isn't 900 lines long

Share this post


Link to post
Share on other sites

I figure i might as well give what I have so far for this script, its not much, but still something

These condition statements are all that is slowing me down from having this in the Scripts and scraps, cause once i figure this out, the rest is smooth

CODE
#include <GUIConstants.au3>

#include <_GUICtrlCreateTable.au3>

;----------------------------Gui Table------------------------------

GUICreate("Sudoku",700,600)

Global $t1 = _GUICtrlCreateTable(55, 5, 3, 3, 60, 60)

GUICtrlSetData($t1[1][1],"2")

GUICtrlSetData($t1[1][2],"2")

Global $t2 = _GUICtrlCreateTable(250, 5, 3, 3, 60, 60)

Global $t3 = _GUICtrlCreateTable(445, 5, 3, 3, 60, 60)

Global $t4 = _GUICtrlCreateTable(55, 200, 3, 3, 60, 60)

Global $t5 = _GUICtrlCreateTable(250, 200, 3, 3, 60, 60)

Global $t6 = _GUICtrlCreateTable(445, 200, 3, 3, 60, 60)

Global $t7 = _GUICtrlCreateTable(55, 395, 3, 3, 60, 60)

Global $t8 = _GUICtrlCreateTable(250, 395, 3, 3, 60, 60)

Global $t9 = _GUICtrlCreateTable(445, 395, 3, 3, 60, 60)

GUISetState ()

do

$msg = guigetmsg()

until $msg = $GUI_EVENT_CLOSE

;-----------------Initial Conditional Statements-----------------

#cs

Mod Func:GUICtrlSetData($var[0][0],"Number")

Initial filled cells:

$t1[3][3]

$t1[1][2]

$t4[2][1]

$t4[3][1]

$t7[1][1]

$t7[3][3]

$t2[1][3]

$t2[2][2]

$t5[2][2]

$t8[1][2]

$t8[2][1]

$t3[3][1]

$t6[2][1]

$t6[3][2]

$t9[3][3]

$t9[2][3]

$t9[3][2]

#ce

While 1

;===============Cell Check for 3x3's======================

Share this post


Link to post
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
Sign in to follow this  
Followers 0