Microbit Programming: Introduction to AI – Letting Compute
- 时间:2020-09-15 16:10:27
- 分类:网络文摘
- 阅读:145 次
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) —
推荐阅读:老少皆宜的美食豆腐还可以当作治病良药 饮酒之道:取其益而去其害 扬长避短善用之 秋冬季节这八种人不适合吃辣椒 儿童不宜吃含化学合成甜味剂的食品 十大“垃圾”食品在你家的餐桌上吗? 让你的孩子远离“三无”劣质小零食 网络传闻中的“致癌食物”是否真致癌 冬季食补:最佳的羊肉吃法,你知道吗 鸡汤虽营养丰富,但这几种病人不要喝 饮食健康与胃病食疗(一):胃病患者饮食注意事项
- 评论列表
-
- 添加评论