Microbit Programming: Introduction to AI – Letting Computer Play the Game


Last week, we present a apple-catching game in Microbit: Microbit Programming: How to Make a Catching-Apple Game by Using Sprites Objects? And my sons were fond of playing such, with the top score – around 35.

The eat-apple-game is available: https://makecode.microbit.org/_DV93uT7i0WuK

Could there be a limit e.g. Would it be sometimes impossible to catch the apple even if we react fast enough and make no mistakes?

Introduction to AI – Letting Computer Play the Game

The AI is known as Artificial Intelligence which often is referred to he computer’s cleverness. We can teach the Microbit how to play the game – with the very simple strategy: moving towards the apple (and staying put if the apple is right above the plate). Let’s define a function which is named letComputerPlay

1
2
3
4
5
6
7
function letComputerPlay() {
    if (pixel.x() < apple.x()) {
        moveRight();
    } else if (pixel.x() > apple.x()) {
        moveLeft();
    }
}
function letComputerPlay() {
    if (pixel.x() < apple.x()) {
        moveRight();
    } else if (pixel.x() > apple.x()) {
        moveLeft();
    }
}

Then, we can plug into the main game loop, and thus we can remove the code for handling the buttons, and instead, providing the functions to move the plate left or right accordingly:

1
2
3
4
5
6
7
8
9
10
11
function moveLeft() {
    px--;
    if (px < 0) px = 4;
    pixel.setX(px);
})
 
function moveRight() {
    px++;
    if (px > 4) px = 0;
    pixel.setX(px);
})
function moveLeft() {
    px--;
    if (px < 0) px = 4;
    pixel.setX(px);
})

function moveRight() {
    px++;
    if (px > 4) px = 0;
    pixel.setX(px);
})

The code and Microbit simulator: https://makecode.microbit.org/_93CihTgAFLk2

And it works! the Microbit knows how to play the game, and it won’t get tired. Actually the Microbit is very good at playing this game.

Improved version of Microbit’s AI – Game playing Strategy

If you let Microbit play, for a while, you won’t see the Game over, as in theory, the Microbit will always be able to catch the apple, even it stands the farthest distance – which requires 4 moves, and the apple falls down to trigger a game over in 5 moves!

However, we can still improve the AI by choosing a shorter direction to move, by calculating the cost (steps) moving towards left or right. Here is the improved version of our Microbit‘s AI:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function letComputerPlay() {
    if (pixel.x() == apple.x()) {
        return ;
    }
    let costOfMovingLeft, costOfMovingRight;
    if (pixel.x() < apple.x()) {
        costOfMovingLeft = pixel.x() + 5 - apple.x();
    } else {
        costOfMovingLeft = pixel.x() - apple.x();
    }
    if (pixel.x() < apple.x()) {
        costOfMovingRight = apple.x() - pixel.x();
    } else {
        costOfMovingRight = 5 - pixel.x() + apple.x();
    }
    if (costOfMovingLeft < costOfMovingRight) {
        moveLeft();
    } else if (costOfMovingLeft > costOfMovingRight) {
        moveRight();
    } else if (Math.randomRange(0, 1) == 0) { // pick a random direction
        moveLeft();
    } else {
        moveRight();
    }
}
function letComputerPlay() {
    if (pixel.x() == apple.x()) {
        return ;
    }
    let costOfMovingLeft, costOfMovingRight;
    if (pixel.x() < apple.x()) {
        costOfMovingLeft = pixel.x() + 5 - apple.x();
    } else {
        costOfMovingLeft = pixel.x() - apple.x();
    }
    if (pixel.x() < apple.x()) {
        costOfMovingRight = apple.x() - pixel.x();
    } else {
        costOfMovingRight = 5 - pixel.x() + apple.x();
    }
    if (costOfMovingLeft < costOfMovingRight) {
        moveLeft();
    } else if (costOfMovingLeft > costOfMovingRight) {
        moveRight();
    } else if (Math.randomRange(0, 1) == 0) { // pick a random direction
        moveLeft();
    } else {
        moveRight();
    }
}

As you can see here, we compute the costs of moving left and moving right, and pick a shorter (better) direction as our strategy. In case there is a tie, we pick a random direction (it doesn’t matter).

The code and Microbit simulator: https://makecode.microbit.org/_FpoaisRKC6ws

There is no real AI – the Microbit would just follow exact instructions as we’ve taught it. The smartness comes from the decision making – which can be perfected interpreted by the computer – making decisions based on the current circumstances which can be computed based on the existing conditions (variables).

The computer makes no mistakes and never gets tired – which is superior to human-beings. See the improved/simplified AI (also based on Decision making, decision trees/rules) in Python: Teaching Kids Programming – Simple AI Algorithm of Decision Rules/Trees in Microbit Apple Catching Game

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
791 words
Last Post: Subtract the Product and Sum of Digits of an Integer
Next Post: C++ Coding Reference: Partial Sorting with nth_element from Algorithm header

The Permanent URL is: Microbit Programming: Introduction to AI – Letting Computer Play the Game

Leave a Reply