上一周, 我们在 Microbit 上编写了第一个游戏: 吃苹果. 我两儿子很喜欢玩, 他们互相比着最高分, 大概极限是35分.
吃苹果的游戏代码和Microbit模拟器: https://makecode.microbit.org/_DV93uT7i0WuK
可能有极限吗? 即使我们做出足够迅速的反应并且没有犯错, 会出现怎么样也无法抓住苹果的情况吗?
人工智能简介-让计算机玩游戏
AI被称为人工智能, 通常被称为计算机拥有类似人类的聪明. 我们可以教Microbit如何玩这个游戏. 只需要采用非常简单的策略: 朝苹果移动(如果苹果在盘子上方, 则保持不动). 让我们定义一个名为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(); } }
然后, 我们可以把该函数放到主游戏循环函数里. 我们可以删掉用于处理按钮(A和B键)的代码. 并且提供相应的函数来向左或向右移动:
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); })
代码和Microbit模拟器: https://makecode.microbit.org/_93CihTgAFLk2
成功了! Microbit知道如何玩游戏, 它永远不会感到累. 实际上, Microbit非常擅长于玩这款游戏.
Microbit AI的改进版-游戏策略
如果让Microbit玩一会儿, 您将看不到游戏结束, 因为从理论上讲, Microbit始终能够捕捉到苹果, 即使它站立的距离最远-需要四步移动, 然后苹果掉下来需要5步才会触发游戏结束!
但是, 我们仍然可以通过选择较短的移动方向. 只需要计算向左或向右移动的成本(步长). 这种策略将会让Microbit看起来更智能一些. 这是我们的MicrobitAI的改进版本:
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 26 27 | 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) { // 随机方向都可以 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) { // 随机方向都可以 moveLeft(); } else { moveRight(); } }
可以看到, 我们先计算左移costOfMovingLeft和右移costOfMovingRight的成本, 并选择一个较短(更好)的方向作为我们的策略. 万一向左向右的代价都一样, 我们只选择一个随机的方向(用 Math.randomRange(0, 1) 生成一个0或者1的数, 各有50%的概率).
代码和 Microbit模拟器: https://makecode.microbit.org/_FpoaisRKC6ws
可以看到, 这里的人工智能是很简单的 – Microbit 会严格按照人类的指示来玩游戏. Microbit的聪明来自于决策制定 (Decision Making). 很适用于计算机. 计算会根据当前情况做出决策, 而当前情况往往可以根据现有一些条件(变量)进行计算.
电脑不会出错, 也不会疲劳-这比人类优越.
英文: Microbit Programming: Introduction to AI – Letting Computer Play the Game
loading...
上一篇: Leetcode 的在线调试器
下一篇: Microbit 游戏编程: 不会吃胖的贪食蛇 (自带人工智能)