Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22855

Real Python: Minimax in Python: Learn How to Lose the Game of Nim

$
0
0

Games are fun! Their well-defined rules let you explore different strategies in search of a surefire way to win. The minimax algorithm is used to choose the optimal move at any point in a game. You’ll learn how to implement a minimax player in Python that can play the game of Nim perfectly.

In this tutorial, you’ll focus on minimax. However, to visualize how the algorithm works, it’s nice to work with a concrete game. You’ll learn the rules of Nim, which is a game with simple rules and limited choices. Using Nim in the examples allows you to focus on the principles of minimax without getting lost in game rules.

As you follow this tutorial, you’ll:

  • Learn the principles of the minimax algorithm
  • Play several variants of the game of Nim
  • Implement the minimax algorithm
  • Lose Nim against a minimax player
  • Use alpha-beta pruning to optimize the minimax algorithm

You can download the source code used in this tutorial, as well as a game simulator where you can play different variants of Nim against a minimax opponent, by clicking the link below:

Source Code:Click here to download the free source code that you’ll use to lose the game of Nim against your minimax player.

Play a Simplified Game of Nim

The roots of Nim go back a long time. While variants of the game have been played throughout history, Nim got its name in 1901 when Charles L. Bouton published Nim, a Game With a Complete Mathematical Theory.

Nim is a game for two players, which always ends with one player winning. The game consists of several counters lying on the game table and the players taking turns removing one or more counters. In the first half of this tutorial, you’ll play a simplified version of Nim with the following rules:

  • There are several counters in a shared pile.
  • Two players take alternating turns.
  • On their turn, a player removes one, two, or three counters from the pile.
  • The player that takes the last counter loses the game.

You’ll refer to this game as Simple-Nim. Later, you’ll learn the rules of regular Nim. They’re not much more complicated, but Simple-Nim is easier to reason about.

Note: Now it’s time to play! You’re going to start with the analog version, so clear a space on your desk. Play a few rounds of Simple-Nim to get a feel for the rules. Along the way, keep an eye out for any winning strategy that you come across.

You can use any objects that you happen to have available as counters, or you can use a notepad to keep track of the current number of counters in play. Somewhere between ten and twenty counters is a good starting point for a game.

If you don’t have an opponent nearby, you can play against yourself for now. By the end of the tutorial, you’ll have programmed an opponent that you can play against.

To demonstrate the rules, two players—Mindy and Maximillian—will play a game of Simple-Nim, starting with twelve counters. Mindy goes first:

  • Mindy takes two counters, leaving ten in the pile.
  • Maximillian takes one counter, leaving nine in the pile.
  • Mindy takes three counters, leaving six in the pile.
  • Maximillian takes two counters, leaving four in the pile.
  • Mindy takes three counters, leaving one in the pile.
  • Maximillian takes the last counter and loses the game.

In this game, Maximillian takes the last counter, so Mindy is the winner.

Nim, including Simple-Nim, is an intriguing game because the rules are simple enough that the game can be completely analyzed. You’ll use Nim to explore the minimax algorithm, which is able to play the game perfectly. This means that a minimax player will always win the game if at all possible.

Get to Know the Minimax Algorithm

Games have been a fertile ground for inventing and testing artificial intelligence algorithms. Games are well suited to this kind of research because of their clearly defined rules. One of the most famous algorithms is minimax. The name describes how one player tries to maximize their score while the other player tries to minimize it.

Minimax can be applied to many different games and more general decisions. In this tutorial, you’ll learn how to teach minimax to play Nim. However, you can use the same principles in other games, like tic-tac-toe and chess, as well.

Explore Game Trees

Recall the game that Mindy and Maximillian played in the previous section. The following table shows all the moves of the game:

MindyPileMaximillian
🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙
🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙
🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙
🪙🪙🪙🪙🪙🪙🪙🪙🪙
🪙🪙🪙🪙🪙🪙
🪙🪙🪙🪙
🪙

This representation of the game explicitly shows how many counters each player removed on their turn. However, there’s some redundant information in the table. You can represent the same game by only keeping track of the number of counters in the pile and whose turn it is:

Player to movePile
Mindy🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙 (12)
Maximillian🪙🪙🪙🪙🪙🪙🪙🪙🪙🪙 (10)
Mindy🪙🪙🪙🪙🪙🪙🪙🪙🪙 (9)
Maximillian🪙🪙🪙🪙🪙🪙 (6)
Mindy🪙🪙🪙🪙 (4)
Maximillian🪙 (1)

While the number of counters each player removed on their turn isn’t explicitly spelled out, you can work it out by comparing the number of counters in the pile before and after a turn.

Read the full article at https://realpython.com/python-minimax-nim/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 22855

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>