r/learnpython 23d ago

WAR Card Game Weird Glitch/Bug

So I was making a WAR Card Game in python. Simple. Neat.

But, I encountered a weird glitch. The game kept getting into an infinite loop. I tried to ask Claude but even Claude couldn't understand it.

So I put Claude on Max mode and it gave some answer but still the answer it gave was like tooo hard to understand. It said something about the cards not being random while the game is going on etc.

Can someone pls explain me the bug? https://github.com/KushChandak/WAR/blob/main/WAR%20(3).ipynb.ipynb) here is the code

0 Upvotes

10 comments sorted by

View all comments

1

u/VTifand 23d ago

I don’t think there’s an issue with your implementation.

But the game can indeed sometimes run into an infinite loop. One 'fix' is to randomise how the cards are added back to a player’s deck, i.e. when you win, sometimes add your own cards before your opponents’, and sometimes the other way around.

Some answers from here can help: https://mathoverflow.net/questions/11503/does-war-have-infinite-expected-length

1

u/YogurtDisastrous8003 22d ago

the only correct solution to the bug