The chocolate bar game

Suppose there is an m \times n rectangle. I like to think of it as one of those bars of chocolate made up of squares:

Two players take turns. On a player’s turn, she must break the chocolate bar along any one of the horizontal or vertical lines, and eat the smaller piece (eating the bigger piece would be very rude). [Edited to add: if the pieces are equal, they may eat either piece.] The player who is left with a 1 \times 1 piece of chocolate, and hence cannot make another move, loses the game. (At least they get to eat the last piece of chocolate.) For example, given the above bar of chocolate, the first player has eight possible moves: she could break it along any one of the 5 vertical lines, or along any of the 3 horizontal lines. However, since she always has to eat the smaller half, some of these moves are really the same. For example, it does not make a difference whether she breaks off and eats the four leftmost squares or the four rightmost squares; in either case her opponent will be left with a 5 \times 4 bar of chocolate.

Here is an equivalent formulation of the same game: there is a pet store with m dogs and n cats. Players alternate turns. On a player’s turn, she must decide to buy either some cats or some dogs; once she makes her choice she may buy any number up to, but not exceeding, half the pet store’s inventory of that animal. For example, if the store has 3 cats and 6 dogs, then a player may buy 1 cat, or they may buy 1, 2, or 3 dogs. The loser is whoever’s turn it is when the pet store has only one cat and one dog remaining (since then they are not allowed to buy any).

First, explain why these games are the same.

I actually wrote about this a long time ago (I’m finally getting back around to following up!). In that post I explained how we can visualize the game like this:

(If some of the grid lines appear heavier than others, pay no attention; it is just a rendering artifact.) The square with position (x,y) (the bottom left is (1,1)) represents the game state with an x \times y rectangle, or a store with x cats and y dogs. A square is dark blue if it is a losing position, that is, whoever’s turn it is in this position is going to lose (assuming both players play optimally; practially speaking, of course, a player in a losing position could still win if their opponent makes a bad move). Losing positions are those from which any valid move leads to a winning position (that is, you lose if no matter what you do, your opponent will win). The light blue squares are winning positions: a position is winning if there is at least one valid move to a losing position (that is, you win if there is a move you can make which forces your opponent to lose).

The losing positions in this visualization of the chocolate bar game appear to be arranged in lines whose slope are a (positive or negative) power of two. For example, the central diagonal has a slope of 1; the next line down has a slope of 1/2; the next, 1/4; and so on.

Now, can you explain why the picture looks like this?


  1. Can you come up with a relatively simple way to characterize which positions are losing positions and which are winning positions? (Hint: think about the positions in binary.)

  2. Use your characterization to devise a strategy for winning the game. Amaze all your friends.

  3. Can you prove it? That is, prove that from any losing position (according to your characterization above) the only valid moves are to winning positions, and that from any winning position there always exists at least one valid move to a losing position.

Apparently this problem comes from the 2005 International Olympiad in Informatics. There is a full solution on that site, but it is much too complicated! Of course I will reveal my own solutions in some future posts. Until then, happy solving. Feel free to post comments, questions, partial or full solutions, etc., in the comments (so conversely, don’t look at the comments if you don’t want any spoilers!).


About Brent

Assistant Professor of Computer Science at Hendrix College. Functional programmer, mathematician, teacher, pianist, follower of Jesus.
This entry was posted in games, pattern, pictures and tagged , , , , , . Bookmark the permalink.

6 Responses to The chocolate bar game

  1. @HarMath says:

    Can a student break at either the middle vertical or horizontal move on their first move? This would leave two equal (assuming perfect breaks in our math world) pieces and thus impossible to eat the ‘smaller’ piece.

    • Brent says:

      Yes, sorry, I should have been more careful in the way I stated the rules: the player eats a piece which is not larger than the other piece. So if the pieces are exactly equal they just eat either one of them.

  2. Pingback: The chocolate bar game: losing positions in binary | The Math Less Traveled

  3. Pingback: The chocolate bar game: losing positions characterized | The Math Less Traveled

  4. Pingback: The chocolate bar game: losing positions proved | The Math Less Traveled

  5. Pingback: The chocolate bar game: variants | The Math Less Traveled

Comments are closed.