[英]C# Recursion error?
我有很奇怪的问题。 我正在使用递归方法,该方法应该在 6 个玩家(5 个机器人和 1 个玩家)之间轮换,但是一旦它到达 bot 5(这是最后一个),它就不会完全执行此操作,它会跳过玩家的回合并离开到 bot 1、bot 2、bot 3、bot 4、bot 5,然后轮到玩家,换句话说,它跳过了一个玩家回合。 我将在这里原始粘贴代码,如果有不清楚的地方请在评论中解释。
private async Task Turns()
{
turns = ReturnTurns();
GC.KeepAlive(Updates);
if (!PFturn && Chips > 0)
{
if (Pturn)
{
call -= PreviousCalls.PreviousPlayerCall;
pbTimer.Visible = true;
pbTimer.Value = 1000;
t = 60;
up = int.MaxValue;
Timer.Start();
bRaise.Enabled = true;
bCall.Enabled = true;
bRaise.Enabled = true;
bRaise.Enabled = true;
bFold.Enabled = true;
turnCount++;
B1turn = true;
}
}
if (PFturn || !Pturn || Chips <= 0)
{
if (pStatus.Text.Contains("Fold"))
{
B1turn = true;
}
if (!Pturn)
{
await Flip(0);
}
pbTimer.Visible = false;
bRaise.Enabled = false;
bCall.Enabled = false;
bRaise.Enabled = false;
bRaise.Enabled = false;
bFold.Enabled = false;
Timer.Stop();
if (!B1Fturn && B1turn)
{
int previous = PreviousCalls.PreviousBot1Call;
call -= PreviousCalls.PreviousBot1Call;
if (Properties.Settings.Default.ThinkCheck)
{
AutoCloseMsb.Show("Bot 1 Turn", "Turns", ThinkTime);
}
Combinations(2, 3, ref b1Type, ref b1Power, b1Status);
Ai(2, 3, ref bot1Chips, ref B1turn, ref B1Fturn, b1Status, b1Power, b1Type, ref previous);
await CheckTextBoxes();
B1turn = false;
turnCount++;
B2turn = true;
PreviousCalls.PreviousBot1Call = previous;
}
if (B1Fturn)
{
turns = ReturnTurns();
B2turn = true;
}
await Flip(1);
if (!B2Fturn && B2turn)
{
int previous = PreviousCalls.PreviousBot2Call;
call -= PreviousCalls.PreviousBot2Call;
Combinations(4, 5, ref b2Type, ref b2Power, b2Status);
if (Properties.Settings.Default.ThinkCheck)
{
AutoCloseMsb.Show("Bot 2 Turn", "Turns", ThinkTime);
}
Ai(4, 5, ref bot2Chips, ref B2turn, ref B2Fturn, b2Status, b2Power, b2Type, ref previous);
await CheckTextBoxes();
B2turn = false;
turnCount++;
B3turn = true;
PreviousCalls.PreviousBot2Call = previous;
}
B3turn = true;
if (B2Fturn)
{
turns = ReturnTurns();
B3turn = true;
}
await Flip(2);
if (!B3Fturn && B3turn)
{
int previous = PreviousCalls.PreviousBot3Call;
call -= PreviousCalls.PreviousBot3Call;
Combinations(6, 7, ref b3Type, ref b3Power, b3Status);
if (Properties.Settings.Default.ThinkCheck)
{
AutoCloseMsb.Show("Bot 3 Turn", "Turns", ThinkTime);
}
Ai(6, 7, ref bot3Chips, ref B3turn, ref B3Fturn, b3Status, b3Power, b3Type, ref previous);
await CheckTextBoxes();
B3turn = false;
turnCount++;
B4turn = true;
PreviousCalls.PreviousBot3Call = previous;
}
if (B3Fturn)
{
turns = ReturnTurns();
B4turn = true;
}
await Flip(3);
if (!B4Fturn && B4turn)
{
int previous = PreviousCalls.PreviousBot4Call;
call -= PreviousCalls.PreviousBot4Call;
Combinations(8, 9, ref b4Type, ref b4Power, b4Status);
if (Properties.Settings.Default.ThinkCheck)
{
AutoCloseMsb.Show("Bot 4 Turn", "Turns", ThinkTime);
}
Ai(8, 9, ref bot4Chips, ref B4turn, ref B4Fturn, b4Status, b4Power, b4Type, ref previous);
await CheckTextBoxes();
B4turn = false;
turnCount++;
B5turn = true;
PreviousCalls.PreviousBot4Call = previous;
}
if (B4Fturn)
{
turns = ReturnTurns();
B5turn = true;
}
await Flip(4);
if (!B5Fturn && B5turn)
{
int previous = PreviousCalls.PreviousBot5Call;
call -= PreviousCalls.PreviousBot5Call;
Combinations(10, 11, ref b5Type, ref b5Power, b5Status);
if (Properties.Settings.Default.ThinkCheck)
{
AutoCloseMsb.Show("Bot 5 Turn", "Turns", ThinkTime);
}
Ai(10, 11, ref bot5Chips, ref B5turn, ref B5Fturn, b5Status, b5Power, b5Type, ref previous);
await CheckTextBoxes();
B5turn = false;
turnCount++;
Pturn = true;
PreviousCalls.PreviousBot5Call = previous;
}
if (B5Fturn)
{
turns = ReturnTurns();
Pturn = true;
}
await Flip(5);
if (!restart)
{
await Turns();
}
}
}
听起来你想以更有状态的方式来解决这个问题。 一个简化的例子可能是:
// Player interface - the game engine will pass game state to the TakeTurn method
// The target of the TakeTurn call can mutate the game state
public interface IPlayer {
void TakeTurn(GameState state);
// Some info that other bots/players might need to look at
int Chips { get; }
// also add other useful bits of info that other game components may need
int SomeOtherUsefulInfo { get; }
}
public class GameState {
// Is the game over?
bool GameOver;
// State information the bots/player will use to make decisions
int ChipsInPot;
// Reference to previous player which could help with AI decisions etc
IPlayer previousPlayer;
}
// Both Bot and Human class implement IPlayer which means they can both
// be handled by the same game pipeline step but have radically different
// implementations - such as AI or manual interaction
public class Bot : IPlayer {
// AI here making sure to implement the IPlayer interface
}
public class Human : IPlayer {
// Human player handling code but with same interface
}
// Game engine simply runs a loop to keep passing turns
public class GameEngine {
GameState _state;
List<IPlayer> _players;
public GameEngine() {
_players = new List<IPlayer>();
_players.Add(new Bot("Fred"));
_players.Add(new Bot("James"));
_players.Add(new Bot("Arnold"));
_players.Add(new Human("Jake"));
}
public void GameLoop() {
while(!_state.GameOver) {
foreach(var player in _players) {
player.TakeTurn(_state); // etc - keep mutating state and looping until the game ends
// Add more complexity here :)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.