Let [tex]Y_{n}[/tex] be the gambler's winnings after n games. Clearly, [tex]Y_{n}[/tex] is a martingale. We introduce a new stochastic process [tex]Z_{n}[/tex], where [tex]Z_{n}={Y_{n}}^2-n[/tex]. It can be shown that [tex]Z_{n}[/tex] is a martingale with respect to [tex]Y_{n}[/tex]. (Can you try to show this?)
Let N be the random variable for the step where the gambler's winnings first reach A or -B. Then, we have [tex]E(Z_{N})=E({Y_{N}}^2)-E(N)[/tex]. By applying the Martingale Stopping Theorem (first check the necessary conditions are satisfied), we can show [tex]E(Z_{N})=0[/tex].
This leaves us with [tex]E(N)=E({Y_{N}}^2)[/tex]. To determine [tex]E({Y_{N}}^2)[/tex], use the definition of expectation, and observe that [tex]Y_{N}[/tex] can only take the values A or -B. To calculate the relevant probabilities, apply some formulae related to stopping times of Markov Chains (with stationary transition probabilities). We are now able to compute [tex]E({Y_{N}}^2)[/tex], which will be equal to [tex]E(N)[/tex].