STR Coding Club logo STR Coding ClubLearn to code, together
Games

How Coding Club Players Learn That No Strategy Guarantees an Easy Win

"Is there a trick to always win?" It is probably the most common question we hear when club members start coding their own games. They ask it about card games, about online shooters, about the dice game they built last week.

Illustration of a pile of sticks beside a pair of dice on a grid

"Is there a trick to always win?" It is probably the most common question we hear when club members start coding their own games. They ask it about card games, about online shooters, about the dice game they built last week. The honest answer depends entirely on the game, and finding out which kind of game you are dealing with is one of the most useful things a beginner can learn to do with code.

Gamers are surrounded by promises of an easy win. Our club's resident competitive player once listed for us the places people chase them: aim trainers sold as instant rank boosts, "unbeatable" build guides, tip sites like ankertoto, and coaching videos with titles in capital letters. She then pointed out that most of them blur two very different things, skill that improves your odds and a method that guarantees a result. In this session we pull those two apart by building two tiny games and testing them with code.

Game One: A Game With a Guaranteed Strategy

We start with a version of Nim. There is a pile of 21 sticks. Two players take turns removing 1, 2 or 3 sticks. Whoever takes the last stick loses. It sounds like a game of judgement, but it is fully solved. The player who can always leave a pile size of 1, 5, 9, 13 or 17 for their opponent will win, every single time.

Members first build the game for two humans: a loop that alternates turns, a check that the move is between 1 and 3, and a check for the last stick. That alone covers while loops, input validation and conditions.

Then they write a computer player. The rule is short: look at the pile, work out how many sticks to remove so that the pile left over has a remainder of 1 when divided by 4, and take that many. If no such move exists, take one stick and hope the human makes a mistake.

When members play against it, the reaction is always the same. The computer cannot lose if it moves second from 21 sticks. It is not lucky. It is not clever in any human sense. It simply follows a rule that the mathematics of the game guarantees.

Game Two: A Game Where Strategy Only Shifts the Odds

The second game is a press-your-luck dice game, similar to Pig. On your turn you roll a die repeatedly, adding each roll to a turn total. You can stop at any time and bank the total. If you roll a 1, you lose everything from that turn. First to 100 wins.

Members code it the same way: a loop for turns, an inner loop for rolls, a decision to keep going or stop. Then they write several computer players with simple strategies:

  • Cautious: always stop after reaching 10 points in a turn.
  • Balanced: always stop at 20.
  • Reckless: always stop at 35.
  • Random: flip a coin after each roll to decide.

Now comes the experiment. Pit every pair against each other for 10,000 games and count wins.

What the Numbers Show

Here are typical results from a club session, with the first-player advantage averaged out by swapping who starts:

MatchupWin rate of first named
Balanced vs Randomabout 88 percent
Balanced vs Cautiousabout 60 percent
Balanced vs Recklessabout 57 percent
Cautious vs Recklessabout 48 percent

The balanced strategy is clearly better than the others. It is skill, in a real and measurable sense. But it still loses roughly four games in ten against a decent opponent. No version of the code that members wrote ever got close to winning every game, and some members tried hard, adding rules about the opponent's score and the distance to 100.

That is the lesson. In Nim, strategy produces certainty. In the dice game, strategy produces an edge. Both are real, and confusing the two is how players end up believing a tip will make winning easy.

How to Tell Which Kind of Game You Are Playing

After the session, we give members a checklist to apply to any game, including online ones they play at home:

  1. Is there hidden information, such as cards you cannot see or an opponent's position off screen?
  2. Is there randomness, such as dice, shuffles, critical hit chances or random drops?
  3. Do both sides act at the same time, so neither can react to the other?

If the answer to all three is no, as in Nim, chess or tic-tac-toe, a perfect strategy exists in principle, even if it is too complex for a human to hold in their head. If any answer is yes, the best strategy can only improve your chances. Most online multiplayer games answer yes to all three.

Our earlier piece on the best games for learning to think logically leans heavily on the first type, because perfect information makes reasoning easier to practise.

Where Common Advice Goes Wrong

You will often hear "there is always an optimal play, you just have to find it." For solved games that is true. For games with chance, it is misleading. There is an optimal policy, a rule that gives the best average result, but following it perfectly still produces losing streaks. Members who simulate the balanced dice player find runs of five or six straight losses in a 10,000 game sample. That is not a bug. It is what an edge of 60 percent looks like over many games.

This matters emotionally as well as mathematically. A player who believes perfect play guarantees wins will conclude, after a losing streak, that their strategy is broken and jump to the next tip. A player who understands odds keeps the good strategy and looks at the long run.

Extending the Project

Members who finish early have plenty to try:

  • Search for the best stop threshold by testing every value from 5 to 40 and plotting win rates.
  • Make the stop threshold depend on both players' scores and see if it beats a fixed number.
  • Write the Nim computer player for any pile size and any maximum take.
  • Add a human versus computer mode to the dice game with a running win counter.

The search task is a good introduction to the idea behind how chess engines search for moves, though at a much simpler scale.

What Members Take Away

The question "is there a trick to always win?" now has a better answer in our club: it depends on whether the game is solved, and you can often find out by writing a small simulation. Skill is real. Practice and good strategy move your odds, sometimes a lot. But in any game with chance or hidden information, nothing makes winning certain, and anyone claiming otherwise has not run the numbers.

BN
Bartholomew Ng

Bartholomew taught maths for years before switching to teaching Python. He writes about the basics of the language and about teaching code without drowning learners in syntax.

More posts by Bartholomew

More in Games

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto