MIT App Inventor Tutorial - Tic-Tac-Toe
MIT App Inventor Tutorial - Tic-Tac-Toe
Overview
This guide assumes that you have some background with MIT App Inventor
and that you have it set up properly on your device.
3
Things to note about the play screen include, but are not limited to:
Now that we have a field to play on, so to speak, we are ready to program
the back-end of the game.
5
Next, we will initialize the variables that will keep track of the state of each
game. There are a total of 12. They include:
In the blocks editor, the global initialization blocks will look something like
this:
The newGame procedure resets the turns passed counter, sets the current
turn to “X”, resets the winner variable, puts the nine playable buttons in a
list, and sets all their text to an empty string. An image is included below.
7
The next procedure that we should write is called isWinning in the image
below. This procedure just checks three text variables for equality, and sets
the winner to the text found in the case that they are all the same. In the
next step, we will call this procedure for every possible combination of
buttons that could win the game at the end of a turn. Giving the equality
check its own function vastly simplifies the next one.
8
To check for a winner, we will need to call the equality check hasWon
procedure on 8 total combinations of buttons. You can win tic-tac-toe
vertically, horizontally, or diagonally, and we will need to check every
possible win condition each time this procedure is called. Otherwise, we
run the risk of denying victory to the winner of each game.
This is, by far, the longest procedure we have to write because of the many
ways you can win the game.
9
The procedure itself needs to be called any time someone presses one of
the nine playable buttons. Sadly, there is no way to program a handler for
all nine buttons in a single block of code, so we have to just write nine
blocks of code- one for each button.
Known Issues/Suggestions
● The game does not announce whose turn it is at the start of the
game, even though it should always be X’s turn at the start.
● We can probably afford to set X’s text color to red and O’s text color
to blue, but whether that is necessary is a subject matter.
11
Step 7: Testing
Now in testing part we are going to test the application by following the below
steps.
You need to click on the option “Build” at the top of MIT App Inventor. Then,
you get the above options and click on the Android App (.apk) to download
the apk file.
Once you click on the Android App (.apk), you will get the above popup,
which shows the compiling percentage
Click on the “Files”, then you will get different folders and given right click on
the downloads folder.
Once you click on the “Open in Terminal”, terminal will be opened as below
Give the below command in the terminal to run the apk file
$ adb install filename.apk
13
Once the app is installed click on the “Anbox Application Manager” to start
the application installed.