C#递归错误
本文关键字:错误 递归 | 更新日期: 2023-09-27 18:30:10
我有一个非常奇怪的问题。我使用的是递归方法,该方法应该在6个玩家(5个机器人和1个玩家)中轮换,但它并没有做到这一点,一旦它到达机器人5(最后一个),它就会跳过玩家的回合,转到机器人1、机器人2、机器人3、机器人4、机器人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 :)
}
}
}
}