Jump to content

dice roller


Recommended Posts

This I made for a friend who needed something like this for an forum rpg but that's beside the point.

What it does: input 1 is the kind of dice (d4/d6/d8 whatever any integer will do) and input 2 is the amount of dices, outcome is the combined number of the dice rolls.

I'd like to see if someone has comments on things I can do better since I'm just starting with autoit, thx in advance.

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Dice roller", 235, 88, 303, 219)
$dice = GUICtrlCreateInput("1", 16, 40, 33, 21)
$amount = GUICtrlCreateInput("1", 64, 40, 33, 21)
$roll = GUICtrlCreateButton("roll", 112, 40, 33, 25, 0)
$Dc = GUICtrlCreateLabel("Type", 16, 8, 28, 17)
$number = GUICtrlCreateLabel("Amount", 64, 8, 40, 17)
$outcome = GUICtrlCreateLabel("Result:", 160, 48, 37, 17)
$updown = GUICtrlCreateUpdown($dice)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

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

        Case $Form2
        Case $Form2
        Case $Form2
        Case $Form2
        Case $dice
        Case $updown
        Case $amount
        Case $roll
            $d = GUICtrlRead($dice)
            $a = GUICtrlRead($amount)
            $1 = 1
            $res=0
            While $1 <= $a
                $dc = Random(1, $d, 1)
                $res = $res+$dc
                $1 = $1 +1
                WEnd
            $outcome = GUICtrlCreateInput($res, 200, 40, 30, 21)
        Case $Dc
        Case $number
        Case $outcome
    EndSwitch
WEnd
Link to comment
Share on other sites

Not much wrong with it. You don't need to assign variables to labels that have static text.

#include <GUIConstants.au3>

$Form2 = GUICreate("Dice roller", 235, 88, 303, 219)
GUISetState(@SW_SHOW)

$dice = GUICtrlCreateInput("1", 16, 40, 33, 21)
$amount = GUICtrlCreateInput("1", 64, 40, 33, 21)
$roll = GUICtrlCreateButton("roll", 112, 40, 33, 25, 0)

;Static labels
GUICtrlCreateLabel("Type", 16, 8, 28, 17)
GUICtrlCreateLabel("Amount", 64, 8, 40, 17)
GUICtrlCreateLabel("Result:", 160, 48, 37, 17)
$result = GUICtrlCreateLabel("00", 200, 48, 37, 17)
$updown = GUICtrlCreateUpdown($dice)

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

        Case $roll
            $d = GUICtrlRead($dice)
            $a = GUICtrlRead($amount)
            $1 = 1
            $res=0
            For $X = 1 to $a
                $dc = Random(1, $d, 1)
                $res += $dc
            Next
            GuiCtrlSetData($result,$res)
    EndSwitch
WEnd
Link to comment
Share on other sites

Now they want to make more roll options. I already made a version just making more inputs/roll buttons naming it $dice1/2/3/4/5 and same with cases. Now I'd like to make a option where you can fill in the amount of options.

So first they have to give the $options to decide the amount of lines (line consists of the dice/amount/outcome).

Then the window should be big enough to show it (each line adds 40) so that would be:

$height = 48 + $options*40

$Form2 = GUICreate("Dice roller", 235, $height, 303, 219)

if I'm right that is.

Then the creating buttons the same idea with height.

Pff.. getting a bit confused right now, too much new info at the same time to work with.

Basicly it should add one var that then decides how much nex buttons come and where they come.

Link to comment
Share on other sites

This will put of the roll results into a listview.

#include <GUIConstants.au3>

GUICreate("listview items",220,250, 100,200)
GUISetState()

$listview = GUICtrlCreateListView (" # | Value",10,10,200,150);,$LVS_SORTDESCENDING)

GuiCtrlCreateLabel("# Rolls", 10, 173)
$numRolls = GuiCtrlCreateInput("1", 60, 170, 30, 20)

GuiCtrlCreateLabel("# Sides", 10, 203)
$numSides = GuiCtrlCreateInput("6", 60, 200, 30, 20)

$buttonRoll = GUICtrlCreateButton ("Roll",120,180,70,30)

Do
  $msg = GUIGetMsg ()
     
   Switch $msg
    Case $buttonRoll
        For $X = 1 to GUICtrlRead($numRolls)
            GUICtrlCreateListViewItem($X & "|" & Random(1,GuiCtrlRead($numSides),1),$listview)
        Next
   EndSwitch
Until $msg = $GUI_EVENT_CLOSE
Link to comment
Share on other sites

Ok I've used the list stuff now but changed it so each line shows:

Rolls | Dice | Result

But I couldn't figure out how to clear the list, looked in the helpfile but couldn't find an option so help theyr would be appreciated :)

here what I have now:

#include <GUIConstants.au3>

GUICreate("listview items",220,250, 100,200)
GUISetState()

$listview = GUICtrlCreateListView (" Rolls | Dice | Result",10,10,200,150);,$LVS_SORTDESCENDING)

GuiCtrlCreateLabel("# Rolls", 10, 173)
$numRolls = GuiCtrlCreateInput("1", 60, 170, 30, 20)

GuiCtrlCreateLabel("# Sides", 10, 203)
$numSides = GuiCtrlCreateInput("6", 60, 200, 30, 20)

$buttonRoll = GUICtrlCreateButton ("Roll",120,180,70,30)

Do
  $msg = GUIGetMsg ()
     
   Switch $msg
   Case $buttonRoll
            $d = GUICtrlRead($numSides)
            $a = GUICtrlRead($numRolls)
            $1 = 1
            $res=0
            For $X = 1 to $a
                $dc = Random(1, $d, 1)
                $res += $dc
            Next
            GUICtrlCreateListViewItem($a & "|" & "d"&$d  & "|" & $res,$listview)
   EndSwitch
Until $msg = $GUI_EVENT_CLOSE

ohw and how can I put my text in autoit code here? :)

Edited by Wicked667
Link to comment
Share on other sites

Edit, new row for total.

Current version looks like this:

Posted Image

#include <GUIConstants.au3>
#Include <GuiListView.au3>

GUICreate("Diceroller",278,260, 100,210)
GUISetState()

$listview = GUICtrlCreateListView (" Dice | Type | Bonus | Result | Total",10,10,258,150);,$LVS_SORTDESCENDING)
$List_h = GUICtrlGetHandle($Listview)
GuiCtrlCreateLabel(" Amount", 10, 173)
$numRolls = GuiCtrlCreateInput("1", 60, 170, 40, 20)

GuiCtrlCreateLabel(" Type", 10, 203)
$numSides = GuiCtrlCreateInput("6", 60, 200, 40, 20)

GuiCtrlCreateLabel ("Bonus", 10, 233)
$numBonus = GUICtrlCreateInput("0", 60, 230, 40, 20)

$buttonRoll = GUICtrlCreateButton ("Roll",120,173,70,30)
$buttonClear = GUICtrlCreateButton ("Clear",120,218,70,30)

$updown = GUICtrlCreateUpdown($numRolls)
$updown2 = GUICtrlCreateUpdown($numSides)
$updown3 = GUICtrlCreateUpdown($numBonus)

$total = 0
Do
  $msg = GUIGetMsg ()
     
   Switch $msg
   Case $buttonRoll
            $d = GUICtrlRead($numSides)
            $a = GUICtrlRead($numRolls)
            $1 = 1
            $res=0
            For $X = 1 to $a
                $dc = Random(1, $d, 1)
                $res += $dc
            Next
            $res = $res+GuiCtrlRead($numBonus)
            $total = $total + $res
            GUICtrlCreateListViewItem($a & "|" & "d"&$d  & "|" & GuiCtrlRead($numBonus) & "|" & $res & "|" & $total,$listview)
        Case $buttonClear
            $total = 0
            _GUICtrlListView_DeleteAllItems($list_h)
    EndSwitch
Until $msg = $GUI_EVENT_CLOSE
Edited by Wicked667
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...