Microbit Programming: Introduction to AI – Letting Compute
- 时间:2020-09-15 16:10:27
- 分类:网络文摘
- 阅读:135 次
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) —
推荐阅读:游泗北新城记作文500字 SEO收录异常诊断:负载均衡架构导致的SEO问题及解决方案 宝塔面板出现漏洞,站长如何做才能让网站更加安全? 掌握这7大SEO优化技巧,提高网站自然排名 网络推广SEO优化是怎么做的? 工信部ICP备案备案系统全新改版,速度更快,用户体验更好 影响网站排名的反向链接细节因素盘点 seo优化六步走网站优化基础策略分享 SEO赚不到钱是病,得治! 影响网站排名的反向链接细节因素盘点
- 评论列表
-
- 添加评论