Java 8, 194 167 164 163 140 135 bytes
(h,t)->{for(;t>-h;)System.out.println(t+h%2<1&&h>(h-=2)?"HH":h<1&t%4<3|t%4==h%2*2&&h<++h&t>(t-=2)?"TT":t++%4!=3-h%2*2&&h>--h?"H":"T");}
-51 bytes thanks to @ceilingcat.
Try it online.
Explanation:
I first tried all possible cases by hand:
Heads Tails Steps
1 (odd) 5 (%4==1) 1,5 → T(1,6) TT(2,4) TT(3,2) TT(4,0) HH(2,0) HH(0,0)
1 (odd) 6 (%4==2) 1,6 → see above after first step
2 (even) 6 (%4==2) 2,6 → H(1,7) H(0,8) TT(1,6) TT(2,4) TT(3,2) TT(4,0) HH(2,0) HH(0,0)
1 (odd) 7 (%4==3) 1,7 → see above after first step
3 (odd) 8 (%4==0) 3,8 → H(2,9) H(1,10) TT(2,8) TT(3,6) TT(4,4) TT(5,2) TT(6,0) HH(4,0) HH(2,0) HH(0,0)
2 (even) 9 (%4==1) 2,9 → see above after first step
2 (even) 7 (%4==3) 2,7 → T(2,8) TT(3,6) TT(4,4) TT(5,2) TT(6,0) HH(4,0) HH(2,0) HH(0,0)
2 (even) 8 (%4==0) 2,8 → see above after first step
0 (zero) 6 (%4==2) 0,6 → TT(1,4) H(0,5) TT(1,3) H(0,4) TT(1,2) TT(2,0) HH(0,0)
0 (zero) 5 (%4==1) 0,5 → see above after second step
0 (zero) 4 (%4==0) 0,4 → see above after fourth step
0 (zero) 7 (%4==3) 0,7 → T(0,8) TT(1,6) TT(2,4) TT(3,2) TT(4,0) HH(2,0) HH(0,0)
5 (odd) 0 (zero) 5,0 → H(4,1) H(3,2) TT(4,0) HH(2,0) HH(0,)
4 (even) 0 (zero) 4,0 → see above after third step
After that I knew the first moves for every possibility. Which I displayed here in table form:
| 0 %4==0 %4==1 %4==2 %4==3 < Tails
------|-------------------------------------
0 | - TT TT TT T
%2==0 | HH TT H H T
%2==1 | H H T TT H
^
Heads
As for my code:
(h,t)->{ // Method with two integer parameters and String return-type
for(;t>-h;) // Loop until both the amount of heads and tails are 0:
System.out.println( // Print with trailing newline:
t+h%2<1&& // If there are 0 tails and an even amount of heads:
h>(h-=2)? // Decrease the amount of heads by 2
"HH" // And print "HH"
:h<1&t%4<3 // Else if there are 0 heads and tails modulo 4 is 0, 1, or 2
|t%4==h%2*2&& // Or there is an even amount of heads and tails modulo 4 is 0
// Or there is an odd amount of heads and tails modulo 4 is 2:
h<++h& // Increase the amount of heads by 1
t>(t-=2)? // Decrease the amount of tails by 2
"TT" // And print "TT"
:t++%4!=3-h%2*2&& // Else-if there are an odd amount of heads and tails modulo-4 is NOT 1
// Or there are an even amount of heads and tails modulo-4 is NOT 3:
// (and increase `t` by 1 with `t++` right after this check)
h>--h? // Decrease the amount of heads by 1
"H" // And print "H"
: // Else:
"T");} // Print "T"
T
,TT
,H
,HH
or can we use other values (such as0 1 2 3
)? \$\endgroup\$