Subtraction: The Game – solution.

As I promised it is time to answer the question, how to live… I mean to play. If you happen to win the game about subtracting balls, I congratulate you. If you win every time, I compliment you even more but why didn’t you try to push the green button? Yes, when the computer plays first, you cannot win this game. The computer doesn’t make mistakes and his first move determines the result. So, you have to start the game with good first move and consequently make all proper next moves.
What does it mean proper, should you ask?

The answer refers to interesting issue, which always was subconsciously  interesting to me. I mean the process of discovering the solution to some mystery, exercise or problem and the extent to which we really understand found solution. I will only touch the surface of this subject but I’m sure it will be coming back in the very next posts.

I was that lucky, that I showed Subtraction Game to a few people and could look at how they cope with the problem the game introduces. Some of them I didn’t even know earlier so I really didn’t have any presumptions of their approach. First way of doing it was of course trial and error method. After some observations about what moves lead to what consequences and after testing some edge cases, the person applying this method came to conclusion that for example one should always take two balls. Which sometimes lead to the success and sometimes didn’t. Only a few trials were enough to satisfy the player with the win. This method was, of course, harder to be successful for players more eager to take the option where computer played first. Unless…

There was one player, who noticed that if computer always wins if it starts the game, it is easier in this mode to find what computer chooses to play and mimick it in own game when he initiates the play. And after couple of trials it certainly appeared that computer probably always begins with subtracting two balls and then if the player takes one ball, it chooses two balls and if the player chooses one ball, it chooses two balls. Always the opposite or saying it differently, that except first move, player and computer take 3 balls together in one turn. And this is a quite right solution!

But why is it so and do we really understand it?
We can check it out, for instance, by increasing the number of balls by 2. The game is near the same then, but if we take away two balls at the beginning we shift the game as if the computer starts with twelve balls. Additionally we can make it harder by raising the number of balls we can remove in one turn. For example, what if you eliminate one, two or three balls at once. I encourage you to experiment below in specially prepared version of the game where you can initialize both parameters as you like, if it is the starting balls number or balls-to-take-in-a-move number alike. In this way you can create your own variant of the game from whole class of games about subtracting the balls.

OK. We’ve experimented – was it helpul to understand the problem or made it even more confusing? Let’s note how important it is who starts the game. First move determines who takes the twelfth ball after at least five turns more. How, on the other hand, does the endgame look like? If you want to win you have to leave the last ball for your opponent. If there are two balls – you take one, if there are three balls – you take two. So what if there are four balls left, which means you cannot leave only one ball after your move, what should you do? Unfortunately in this situation you have no option, whatever you do, the opponent is the one who has a chance to leave you with one ball at the end because we give him more than one but less than four balls.

At this very moment we should get enlightened that we not only win by leaving the last ball for opponent but also, in a way, when we leave the forth ball to him. We win a stage which enables us to win the next one.

Therefore we can look at the problem from different angle. If we must make an opponent to take the forth ball from the end to win the last one, we could, in our imagination, remove 3 last balls and to this moment play as if the forth from the end would be the last ball. And here comes simple idea to do such things with the next groups of 3 balls until the first three or less balls remain. From these first balls we leave just one for opponent ensuring he takes it and leaves us more than one from the next group.

We execute this strategy until the happy end. See how I divided the balls into groups on the picture below. The same should be done during the game, leaving the last ball of every group for the opponent.

Pic. 1 – Division into groups

Now try again to play the above version of the game and check if you can generalize this strategy for any numbers of balls. For easing the counting and dividing into groups I added informations during the game about how many balls are left and how many of them are chosen by the player or the computer.

I think that, at this moment, we are a little bit tired of the subject of balls and this game, so I will let myself stop it for the time being, despite my plan of describing the decision tree and elegant mathematical formula of the game’s algorithm. I’ll do it  when other opportunity occurs, in the meantime I should go forward!

As usual all comments and discussions are kindly welcome 🙂

News and plans #1

Hi!

Currently I am occupied with tasks without immediate realization in blog posts. Next entry is in creation process which will take some more time cause it is quite complex. That’s why I’ve come to an idea of short summary about what I’m up to now. We are still under influence of New Year’s enthusiasm so I will make some plans for near future too.

NEWS

  1. I am at the ending of this course on Coursera: Algorithms: Design and Analysis, part 1, all weeks assignments passed with positive grade and there is only final test left. That is why I am doing a repetition of all algorithms from the course and until 01/22 I should pass them all.
  2. I train table tennis really hard, now I have 3-times-a-week training cycle. My mini-goal is to regularly return with topspin stroke after a backspin service which is quite hard for me right now but possible in general.
  3. The Subtraction Game version 2.0 is finished. You can now set starting parameters of the game: balls in total and number of balls to be taken in one move. There is version 2.1 under development which will display game status so you don’t have to count balls on screen manually.
  4. I published Subtraction Game on GitHub here.
  5. I am currently reading:

    Eloquent JavaScript
  6. I repeat WHOLE mathematics – if anyone has an interesting exercise on the elementary or high school level – please send me one 🙂
    In this quest I am backed by Khan Academy!

PLANS

  1. Until January 21st I want to pass Algorithms final exam and shout it out on LinkedIn 🙂
  2. In January I want to publish a post with new, 2.1 version of balls game and a short analysis of it.
  3. And February is a month of starting a development of a whole new game! Keep it confidential and I tell you that this will be an economy game. A little bit more complicated than the first one, but still carefully scoped because it must be finished too. The deadline is the May 25th (not accidental but I’ll tell you about this another time). It is quite far but I will keep you updated and make some more detailed schedule to stay on track.
  4. Before the release of the new game I want to feed you with two easier but still valuable posts. The first one about some elementary high school problem which may sometimes cause trouble but there’s a formula for it to not be stressful. The second one uses fresh knowledge from Algorithms course and is about calculating running median by using two heaps. I will try to answer questions about how to do it and when it is worth to do it, if ever.
  5. I wish to publish a post every month so keep your fingers crossed!
  6. Simultaneously to new game development I want to undertake the endeavour to continue my Full Stack Web Development specialization.
    Its current 3rd course Front-End JavaScript Frameworks: AngularJS could strongly support me during programming of the second game which also will be in form of web application.
  7. I move on with math repetition: in this semester Algebra and Geometry 🙂

And that’s all for now! I hope this public declaration will help me in achieving my goals. Keep fingers crossed, please!

In the beginning was the game…

Hi!

Without much ado – what this blog is for, why, how often – I cut to the chase…
And to the gift for my first Readers.

Since when I’m interested in computers, that is since childhood in 1980’s, the first and the most important applications of computers for me were games.

A lot has changed since then, computer became my main tool in everyday work, and its huge popularity made me a little
forgetting about how it fascinated me once when, in spite of its 64KB of memory and 2MHz processor under the hood, transducer me into another magnificent worlds, challenged me intellectually and manually.

Game developers appeared to me as gorgeous magicians, poets, splendid musicians and and brilliant engineers in one person. The dreams of becoming one of such genius seemed inappropriate. After all my game won’t be on the same level as theirs, I can’t program so well, I don’t no anything about graphics, I don’t have musical talent, etc…

Sometimes we set the constraints for ourselves, that doesn’t allow us to just feel the joy of what we really enjoy doing.
And making games is even bigger joy than playing them.
And making games which are played with joy is a top fulfillment.

Is it possible to make your first games being almost forty?
Yes it is, like starting to paint, sing, jog or to do DIY.
One just needs to stop dreaming…

So I encourage you to play my first game below, which is inspired by well known play.
The rules are:

  • You have 12 balls in a row and the last one of them is special – it is a bomb!
  • Choose who plays first (you or a computer) and then players make moves in turns – every turn you take away one or two balls.
  • Who takes the last ball away, loses!

Play against computer and see if and how you can win against him.


I hope the game gave you a moment of joy as it made me a lot of joy to make it. This simple game is made in HTML and JavaScript which I currently study.

Before I started programming this game I made a little design in the form of document, which is known in game development world as GDD – Game Design Document. This is very useful stage of game creation.
When you have the ideas behind the game well-thought and written down it is easier to focus on implementation.
If you know the look of the game, images, how computer and player moves, what is the algorithm to win the game, you know where you are heading and take care only of technical problems. Of course there were some small issues on the way but most of the game was considered in advance.

I concluded a few bonus features in GDD which are not in the game because they could have extended the implementation. First of all I wanted the game to be finished!
The game doesn’t have a big replayibility, with you knowing how to win it the fun is over.
So you are encouraged to comment if it was surprising to you, if you want me to implement bonus features from GDD or make your own ideas how to enrich the gameplay.
And perhaps you would like to read how to win this game and why, how computer plays and how to make the computer learn this game. The second topic for sure will be covered in the blog, so you can choose which one first.

So comment, choose, suggest!

P.S. For interested in JavaScript: it turned out that move is relative, onMouseOut event is generated not only by cursor moving out of the object but also by object under the cursor removed from DOM structure.

Icons made by Madebyoliver from www.flaticon.com is licensed by CC 3.0 BY
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
Icons made by Roundicons from www.flaticon.com is licensed by CC 3.0 BY