Dicebot Programmer mode tutorial 03 - Martingale

in dicebot •  8 years ago 

Now that we understand the process of how the bot handles the programmer mode and the bets, let's take a look at our first script.

Note
This specific tutorial and the next 2 to come is mostly aimed at users with next to no experience in programming. Skip to the completed script if you have a history in programming.


In this script, we'll be betting 1 satoshi in a martingale method, meaning double up on every loss and resetting to base on a win.


So first of, defining the dobet function and closing it:

function dobet()  --dobet definition, no parameters

end --every function needs to be closed with an end clause


All of the martingale logic needs to be done inside of this function. First, how do we determine whether the bet was a win or a loss. There's a "win" variable that's a boolean. This variable tells us whether the most recent bet was a win or a loss. A summary of the available variables can be found here.


If functions are used to make decisions in programs. Ifs basically work as follows:

if <something>
  do this thing
else
  do something else

whatevers in <something> needs to be a true or false value. For example (6/3)=3 is false, but (9/3)=3 is true. Except for one thing. In programming, we don't use = to check whether values are equal to each other, we use ==. A single equals sign is used to assign values, a double is to compare values.


So x=3 sets the value of x to 3. x==3 checks whether x is equal to 3


We need to check whether win is true or false, so the if would be something like


if win == true

But win is a boolean, and win == true also gives back a boolean, so you can actually just do

if win


In LUA, if statements use the format

if <something> then
do something
end

Or

if <something> then
do something
else
do something else
end

Like functions, every if or if-else statement needs to be closed off with an end.


We want to know if the bet is a win, so we can reset the bet, and if it isn't, we can multiply it by 2.

if win then
 reset to base
end


We can use the nextbet function to set the value for the next bet the bot places. Since we want to bet at 1 satoshi base, we can use


if win then
 nextbet = 0.00000001 -- set the base bet
end


Note
In programming, one can use comments to explain what things do or why certain things are done.
Comments are not part of the program and doesn't influence the execution in any way.

In lua, comments are with -- in front of the comment.
everything after -- is a comment

This is a comment:
--set the base bet


Now we have the script resetting the bet after each win, we just need it to multiply after every loss. We know that a bet either wins, or loses. We're already checking whether it wins, so we can use the if - else format to multiply it by 2 on a loss.


if win then   
 nextbet = 0.00000001 -- set the base bet
else
 -- set the new bet that's been multiplied by 2
end 


But how do we know what to multiply by 2? We can find out what the previous bet was that the bot placed using the previousbet variable. and we can multiply it by 2 using the multiply operator, *. To multiply a variable by two, you use: variablename*2, and you can assign this to another variable using the = sign. So, newvariable = oldvariable*2.


if win then      
nextbet = 0.00000001 -- set the base bet  
else   
nextbet = previousbet*2
end  


Now we just need to add that inside the dobet function and we have our completed martingale script:

function dobet()
if win then
 nextbet=0.00000001
else
 nextbet=previousbet*2
end
end


To use this script, paste it into your code box, go to the console tab and enter start() to start the bot, and stop() to stop the bot. Alternatively, you can use runsim(1,1000) to run a simulation of 1000 bets with a starting balance of 1 coin.


Congratulations, you just wrote a DiceBot script!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!