Suppose that two players, $A$ and $B$, have $a$ dollars and $b$ dollars respectively.
We can set up the following events:
- $E_i$: player $B$ wins the total fortune $a+b$ starting with $i$ dollars
- $F$ : player $B$ wins the first game
Let $p_i = \Bbb{P}(E_i)$. Therefore, $p_i$ is the probability that player $B$ wins the total fortune $a+b$ starting with $i$ dollars. (We will let $i = b$ eventually.) Note that $\{F,\bar{F}\}$ is a partition of the sample space. Let $p=\Bbb{P}(F)$ and $q=\Bbb{P}(\bar{F})=1-p$. Therefore, by the Law of Total Probability
$$p_i = \Bbb{P}(E_i|F)\Bbb{P}(F) + \Bbb{P}(E_i|\bar{F})\Bbb{P}(\bar{F})=pp_{i+1}+(1-p)p_{i-1}$$
or $$p_i = pp_{i+1} + qp_{i−1}.\tag 1$$
The $p_{i+1}$ appears in the formula since if player $B$ wins the first game, then he
or she has won a dollar from player $A$. Similarly, $p_{i−1}$ appears since if player
$B$ loses the first game, then he or she pays player $A$ one dollar, and therefore
has one less dollar. Clearly, $p_0 = 0$ since if player $B$ has no money, then he or
she is already ruined. Also, $p_{a+b} = 1$ since player $B$ cannot be ruined if he or
she has all the money.
Since $p+q=1$, equation (1) can be rewritten as $pp_i +qp_i= pp_{i+1} + qp_{i−1}$,
yielding
$$p_{i+1} − p_i =\gamma (p_i − p_{i−1})$$
where we put $\gamma=\frac{q}{p}$.
In particular,
$p_2-p_1=\gamma(p_1-p_0)=\gamma p_1$ (since $p_0 = 0$), so that $
p_3 -p_2 = \gamma(p_2 - p_1) = \gamma^2p_1$; and more generally
$$
p_{i+1} − p_i =\gamma^i p_1\qquad \text{for}\; 0<i<a+b
$$
Thus
$$
p_{i+1} − p_1 =\sum_{k=1}^i (p_{k+1} − p_k)=\sum_{k=1}^i\gamma^k p_1
$$
yielding
$$
p_{i+1} =p_1+p_1\sum_{k=1}^i\gamma^k =p_1 \sum_{k=0}^i\gamma^k=
\begin{cases}
p_1\frac{1-\gamma^{i+1}}{1-\gamma} & \text{if }p\ne q\\
p_1(i+1) & \text{if }p= q
\end{cases} \tag 2
$$
(Here we are using the geometric sum $\sum_{k=0}^n a^i=\frac{1-a^{n+1}}{1-a}$ for any number $a$ and any integer $n\ge 1$.)
Choosing $i = a+b - 1$ and using the fact that $p_{a+b} = 1$ yields
$$
1=p_{a+b}=\begin{cases}
p_1\frac{1-\gamma^{a+b}} {1-\gamma}& \text{if }p\ne q\\
p_1(a+b) & \text{if }p= q
\end{cases}
$$
from which we conclude that
$$
p_{1} =
\begin{cases}
\frac{1-\gamma}{1-\gamma^{a+b}} & \text{if }p\ne q\\
\frac{1}{a+b} & \text{if }p= q
\end{cases} \tag 3
$$
Combining equations (2) and (3) gives
$$
p_{i} =
\begin{cases}
\frac{1-\gamma^i}{1-\gamma^{a+b}} & \text{if }p\ne q\\
\frac{i}{a+b} & \text{if }p= q
\end{cases}
$$
or
$$
p_{i} =
\begin{cases}
\frac{1-(q/p)^i}{1-(q/p)^{a+b}} & \text{if }p\ne q\\
\frac{i}{a+b} & \text{if }p= q
\end{cases}
$$
So taking $i=b$, and for $a=2$ and $b=3$, we have
$$
\Bbb{P}(E_b)=p_b=
\begin{cases}
\frac{1-2^b}{1-2^{a+b}}=\frac{1-2^3}{1-2^{5}}=\frac{7}{31} & \text{if }p=\frac{1}{3},\, q=\frac{2}{3};\gamma=\frac{q}{p}=2\\
\frac{b}{a+b}=\frac{3}{5} & \text{if }p= q=\frac{1}{2}
\end{cases}
$$
observing that $\Bbb{P}(Heads)=\Bbb P(F)$.