Microbit Programming: Introduction to AI – Letting Compute

  • 时间:2020-09-15 16:10:27
  • 分类:网络文摘
  • 阅读:104 次

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.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
《食品安全法》修订四大焦点引发关注  秋冬季节风寒感冒 葱白有治疗感冒功效  秋冬时节抗病毒防感冒的食疗养生方  吃香蕉太多也可能伤害到身体的健康  糖类在食物烹饪中能起到什么作用呢?  健康饮食:五种不适宜在深夜吃的食物  养阴益肺润燥止咳之梨的六款食疗方  枸杞子的食用方法以及滋补养生功效  身体健康无虚证的人不宜食用枸杞子  食疗养生枸杞子泡水喝有4大保健功效 
评论列表
添加评论