In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Artificial Intelligence

Artificial Intelligence, commonly abbreviated by AI, is the name given to a computerized mind that consists entirely of programming code.[1]

Its usage in Scratch is most common in projects in which a user can play a game against the computer.


How to make an AI

Optimal AIs

An optimal AI will need an indefinite amount of If () Then, Else blocks, loops, and/or time, so that an AI has a response to every action that the player does and/or time to examine every possible outcome. However, this is impossible to program.

Simple AIs

Most projects that use AI use special techniques, such as using variables to store different values. Those values may be previous locations, user input, and so on. They help to calculate different actions that allow the computer to make a good challenge to the player, and succeed in its task.

Perfect AIs

A practical and optimal AI will use recursion to try to adapt to the circumstances itself. Given:

  • What all moves are possible
  • An algorithm to tell if a given position is a win for the computer, loss for the computer, tie, or incomplete game

A recursive function to return the best move for a player given a board and which player can be written under the following logic:

  • If position is tied, won, or lost, return tied, won, or lost and the board
  • Otherwise:
    • Iterating through each continuation of the game for the player:
      • If the best move for the other player results in a loss for him, return "Win" and the continued position
      • If the best move for the other player results in a tie for him, return "Tie" and the continued position
      • If the best move for the other player results in a win for him, return "Loss" and the continued position

See this project for an example of strategic artificial intelligence

See the article on game trees for more on recursive functions and their use in constructing AI.

Common AIs

There is also another class of AI that depends solely upon only one of the factors. Such AI are a lot simpler and, in many cases, effective. However, they have not fulfilled the true requirements of an AI. For example, in the project Agent White, the AI moves along a given path and only tries to shoot at you. In this AI, only the user's position matters to the AI; it will rotate so that its gun turns towards the user. In the project Broomsticks, the AI only changes its position with respect to the ball.

Learning AIs

AI which can take external stimulus and decide upon the best way to use it is called a learning AI, or an AI that uses something called machine learning. Neural networks are also commonly used for learning AIs. A learning AI is able to learn off of its present and past experiences. One popular way of making a learning AI is by using a neural network. Another is by making a list of things and creating a list of things for every reply (which can be done in Scratch, although with some difficulty as 2D arrays are not easily implemented).

Other AIs

Another type of AI is used in a remix of Agent White found here. In this remix, the AI picks a random path and follows it. It uses Math and future x and y positions based on the current position of a character which you control. Then it slowly moves toward that new position until it either reaches its destination or hits a wall. In this case, instead of Artificial Intelligence, it is more of Artificial Random because it never uses intelligence other than running into walls.

Limitations of AI

One of the biggest limitations AI has been facing is speed. Scratch is a rather slow programming language; hence most AI on Scratch are slow because their scripts are too long. Complications also have been a major problem for AI as all AI programs are very large and complicated, thus the scripts may become long and too laggy to script without crashing Scratch. For example, a simple game of Tic-Tac-Toe with AI will have a script running into multiple pages due to many conditions in If blocks, and sometimes in an attempt to speed it up by making it Single Frame. The complicated script also makes remixing a problem. Because of all this, most AI projects have no improvements, causing the AI to remain glitchy.

Common Uses

  • 2 player (Computer vs Player) games
  • Path-finding
  • Having a conversation with the user (commonly referred to as a chatbot)

Example Projects

These projects have been using AI in the truest sense possible practically:

See Also


  1. Wikipedia:Artificial intelligence
  • This page was last modified on 31 March 2014, at 05:57.