#!/bin/bash
# MartingaleSimulator v2
startBetAmount=1
startBankAmount=10000
#if [[ ${#} -eq 2 ]]; then
# startBetAmount=${1}
# startBankAmount=${2}
#else
# echo "Enter starting bet (As positive non-zero integer)"
# read -r startBetAmount
# echo "Enter bank amount (As positive non-zero integer)"
# read -r startBankAmount
#fi
numBetsCast=0
currentBetAmount=1
currentBankAmount="${startBankAmount}"
greatestWin=0
greatestWinBetNumber=0
biggestLoss=0
greatestLossBetNumber=0
#echo numBetsCast=$numBetsCast
echo currentBetAmount=$currentBetAmount
echo currentBankAmount=$currentBankAmount
echo "Running simlation"
while (( "${currentBankAmount}" >= "${currentBetAmount}" )); do
numBetsCast=$((${numBetsCast} + 1))
coinToss=$((${RANDOM} % 2))
if (( ${coinToss} == 0 )); then
#echo "We won!!!!"
currentBankAmount=$(("${currentBankAmount}" + "${currentBetAmount}"))
if (( ${currentBankAmount} > ${greatestWin} )); then
greatestWin=${currentBankAmount}
greatestWinBetNumber=${numBetsCast}
#printf "Bet ${greatestWinBetNumber} put our bank at \$${greatestWin}\n"
fi
currentBetAmount=1
else
#echo "We lost!!! Double the bet and roll again!"
currentBankAmount=$(("${currentBankAmount}" - "${currentBetAmount}"))
newBetAmount=$(("${currentBetAmount}" * 2))
if (( ${currentBetAmount} > ${biggestLoss} )); then
biggestLoss=${currentBetAmount}
greatestLossBetNumber=${numBetsCast}
printf "Bet %-10b is our biggest loss yet!! Doubling bet to %-10b. Current balance is %-10b\n" "${greatestLossBetNumber}" "\$${newBetAmount}" "\$${currentBankAmount}"
fi
currentBetAmount="${newBetAmount}"
fi
done
echo "Cannot afford to double bet to \$${currentBetAmount}. You are forced to abandon the martingale strategy since you only have \$${currentBankAmount} left."
echo "You have cast a total of ${numBetsCast} bets."
echo "Should have quit at bet ${greatestWinBetNumber} when you were ahead by \$$((${greatestWin} - ${startBankAmount}))"