I'm developing a Poker app. It is almost done, and I'm looking for improvement. One of the things I wonder is whether I should change how I perform iterations. I currently iterate by using tail-recursive calls. However, a friend of mine suggested that I use a while
loop instead because loops don't require stack space.
private async Task Turns()
{
_turns = ReturnTurns();
GC.KeepAlive(Updates);
if (!Player.FoldTurn && Player.Chips > 0)
{
if (Player.Turn)
{
SetPlayerStuff(true);
Call -= Player.PreviousCall;
_up = int.MaxValue;
_turnCount++;
Bot1.Turn = true;
_restart = true;
}
}
if (!Player.Turn)
{
await Flip(0);
}
if (Player.FoldTurn || !Player.Turn || Player.Chips <= 0)
{
Call = TempCall;
if (StatusLabels[Player.EnumCasted].Contains(RepetitiveVariables.Fold))
{
Bot1.Turn = true;
}
SetPlayerStuff(false);
Bot1 = (Bot)await RotateTurns(Bot1, Bot1.EnumCasted);
Bot2 = (Bot)await RotateTurns(Bot2, Bot2.EnumCasted);
Bot3 = (Bot)await RotateTurns(Bot3, Bot3.EnumCasted);
Bot4 = (Bot)await RotateTurns(Bot4, Bot4.EnumCasted);
Bot5 = (Bot)await RotateTurns(Bot5, Bot5.EnumCasted);
_restart = false;
}
if (!_restart)
{
await Turns();
}
}
That's how I think it should look like with a while
loop:
private async Task Turns()
{
while (true)
{
_turns = ReturnTurns();
GC.KeepAlive(Updates);
if (!Player.FoldTurn && Player.Chips > 0)
{
if (Player.Turn)
{
SetPlayerStuff(true);
Call -= Player.PreviousCall;
_up = int.MaxValue;
_turnCount++;
Bot1.Turn = true;
_restart = true;
}
}
if (!Player.Turn)
{
await Flip(0);
}
if (Player.FoldTurn || !Player.Turn || Player.Chips <= 0)
{
Call = TempCall;
if (StatusLabels[Player.EnumCasted].Contains(RepetitiveVariables.Fold))
{
Bot1.Turn = true;
}
SetPlayerStuff(false);
Bot1 = (Bot) await RotateTurns(Bot1, Bot1.EnumCasted);
Bot2 = (Bot) await RotateTurns(Bot2, Bot2.EnumCasted);
Bot3 = (Bot) await RotateTurns(Bot3, Bot3.EnumCasted);
Bot4 = (Bot) await RotateTurns(Bot4, Bot4.EnumCasted);
Bot5 = (Bot) await RotateTurns(Bot5, Bot5.EnumCasted);
_restart = false;
}
if (!_restart)
{
continue;
}
break;
}
}
async
it's all rewritten to use a state machine at compile time. \$\endgroup\$