Coordinates Game in MakeCode for Minecraft

begin game
Early in the game, most of the spaces on the grid are white. The green blocks indicate that the player got the coordinates of that position correct while the red blocks were incorrect. The center cyan block is the origin (center) of the grid, which is coordinate { 0  0 }.
finish green
At the end of a game, all blocks are green except those that were missed by the player. The percentage score is shown above the grid.

Coordinates Game is a companion to Coordinates Tutor, which is described in Coordinates Tutor in MakeCode for Minecraft. Coordinates Game enables a player to demonstrate their knowledge of coordinates.

How to Play

To play Coordinates Game, type “run” in the command window. The program will display one of the windows below. It will oscillate between the two windows reminding the player that X and Z correspond to east and south, respectively.

The game will then display { X Z } coordinates, such as {  1  -1 } on the left below. The player’s challenge is to break the white block in the grid corresponding to the displayed coordinates. If the player gets it right, a green block will replace the broken white block at the coordinate {  1  -1 }. If the player gets it wrong, a red block will replace the broken one. The program then displays the next set of coordinates for the player until all positions  in the grid have been used. At this time, the score is displayed as a percentage of correct answers (see above).

The game loop, simply put is:

  • the program displays a position as a set of coordinates,
  • the player breaks the white glass block at that position in the grid,
  • the program replaces the broken white block with either green or red blocks corresponding to right or wrong, respectively, and
  • the program displays the next position coordinates, and so on until all positions have been used.

Code Design

The big-picture design of the program is depicted below. The program first initializes, which includes building the grid and compass as well as creating a list of all positions on the grid. At first, all these positions are available to be displayed to a player. As each position is used, it is removed from the list. When there are no positions remaining, the game is over.


In operation,  after the program posts position coordinates,  nothing happens until the player breaks a block, which generates an on-broken event. The position of the broken block is compared against the displayed position using a sneaky method that I will describe later. If correct, the broken block and the block under it are replaced by green ones. If wrong, a red block is placed at the correct position with a red block under it, which indicates that the player got that one wrong. The broken block is repaired by replacing the white glass.

The Code

The first version of Coordinates Game code was completed in July. I didn’t like it because on rare occasions weird things occurred that I didn’t understand. For example,

  1. Sometimes. towards the end of the game the on-white-glass event stopped firing and would not restart. No matter how much glass I broke, no event. I put many say-blocks in the program so that I was absolutely sure there was no explainable reason for this to occur. I still don’t know why this happened and I could not reproduce the problem outside my program.
  2. My original program did repairs asynchronously in a forever loop. On rare occasions, this interfered with game play. I eventually understood how this rare thing happened. To fix it,  I had to give up asynchronous repairs. This turned out wonderfully, as the resulting code is totally reliable and much faster. How great is that?

I changed and changed and changed the design as well as then code to make it more understandable and reliable. I did thought experiments, had nightmares and awakened with new ideas that sometimes worked and sometimes didn’t. I trashed large sections of the code and started over again and again. My 6-weeks on-and-off effort was rewarded. Now the program has no anomalies, at least none that I have found after playing the game many times.

The figure below shows all the code for the Coordinates Game. Starting on the left is the initialization code. Towards the middle is the code that posts coordinates. Moving right is the on-broken events that triggers processing of a player’s answer. To the right is the code that makes repairs.

all code with labels

run on-chat command

This on-chat command is how the player starts the program. It does the following:

  1. Moves the player to a new, pristine location and sets the origin variable.
  2. Calculates the opposing corners of the grid as { -5 -5 } to { +5 +5 } that will be used for building the grid.
  3. Sets the print position where the coordinates and score sill be displayed.
  4. Launches the on-chat command brackets to print the brackets within which coordinates will be displayed. Because it launches an on-chat command instead of using a function, the program continues while the brackets are being drawn.
  5. Launches the on-chat command compass to draw a simple compass while the program continues to run.
  6. Calls the function canvas to build a grid.
  7. Initializes the counts of right and wrong answer to zero.
  8. Calls a function to flash between { X Z } and { E S } to remind the player that X is East and Z is South.
  9. Calls a function to initialize the available-positions-list.
  10. Launches an on-chat command to post the first position coordinates. Because of using the on-chat command instead of a function, after launching the on-chat, the program continues and exits.
  11. At this point nothing happens until the player breaks a block, hopefully, corresponding to the correct answer.
run code

Display  New Position Coordinates

This on-chat command run asynchronously and performs the following functions:

  1. Calls a function to get a random grid position from those that have not been used; that is, is available.
  2. If all position have been used, the on-chat command end is launched to post the score and end the game.
  3. If an unused position was found, the answer position is calculated as origin, which is  the position at the center of the grid, plus the available position, which is a position of the form { X 0 Z }, where X and Z are each a  value between -5 and +5. This is more fully explained in the section following this one. The variables answer_X and answer_Z are the X and Z coordinates or the answer position, respectively.
  4. Prepares the coordinates for printing in the correct location by putting a space in front positive coordinates. The space occupies the space that is occupied by a minus sign in negative coordinates.
  5. Increments nPrints, which is used to select the color of block used to print the next coordinates. The color oscillates so that there is no confusion during the time when one coordinate is being replaced with a new one.
  6. Launches the two on-chat commands that prints the two coordinates at the same time. If this were done with a single print, it would be irritatingly slow.

Available-Positions List

The list of available positions is key for keeping track of which grid positions have not yet been used. There were other more convoluted ways of doing this, but using a position list — an undocumented feature in MakeCode for Minecraft —  is neat. Usually, a coder should not use undocumented features of a language, but at 76 years of age, I can do anything I want. Since in JavaScript, lists of positions is allowed, I trust the makers of MakeCode will not eliminate the feature.

The function has an outer loop corresponding to the X coordinate of a position and an inner loop corresponding to Z. Each position { X 0 Z } is added to the list. { X 0 Z } is a world position; that is, each coordinate is just a number that can be added to origin to get a world position of a block on the grid. Each position added to the list is of the form { X 0 Z }, where X and Z are each a  value between -5 and +5.

init array code

The get available position function does the following:

  1. Checks the length of the available positions list. If the length is 0, meaning there are no positions available, it sets the Boolean variable availableFound to false and exists; otherwise, it continues at the next step.
  2. Picks a random position from the list as the variable availablePosition and removes it from the list.
  3. Sets availableFound to true and exits.
get available code

Process on-broken events

When a player breaks a white or cyan block on the grid, events are triggered that call the breakCommon function, which does the following:

  1. If the variable answerPosition has not yet been set, the function repairs the broken block and exits.
  2. If the block at the answer position is air, the answer is correct; otherwise not. I think this is sneaky or perhaps I mean clever, but I wouldn’t want to brag.
  3. For correct answers, the block at the answer position as well as the one below it are replaced with green blocks.
  4. For incorrect answers, the block at the answer position as well as the one under it, are replaced with red blocks. But before that, the block above the answer position flashes green a few times to indicate where the correct answer is. The wrong white block broken by the user, is left as air until repairs are done in the last step of this function.
  5. Before the on-chat command is launched to post coordinates of a new position on the grid, the function pauses one second to give the player a chance to view the current position coordinates at the same time as the corresponding block turns green or red.
  6. Finally, a function is called to do repairs, which replaces the air block, which was  left by a wrong answer, with a white block.
break code


The do_repairs function runs every time a white or cyan block is broken; that is, an answer is given to the displayed position question. Repairs are needed when the player breaks a block that is not the correct answer. The first replace block  in the function fixes that. Also, on rare occasions, the flashing green block above the correct answer is left as a green block, not air as it should have been. The second replace assures that all blocks above the grid are air. The last step is to set the origin block, which might have been changed by mistake or broken as a correct answer to grid position { 0 0 }.

do repairs

Besides wrong answers, players make other mistakes when breaking things; for example, a player might break a grass block next to the grid. Or, a player might just be plain ornery, breaking things to see what happens. The remaining repair routines run in response to events when something is broken by a player that should not have been either by mistake or orneriness. The following should not be broken:

  • green blocks
  • red blocks
  • axis market blocks (white carpet)
  • grass blocks

When these blocks break, the associated on-broken event makes the needed repairs. How cool is that?

fix red and green
axes markers
repair grass

Code Requires a Flat World

This code assumes a flat world such as that described in MakeCode for Minecraft Sandbox World: Make It Flat and Simple.

Get the Code

Coordinates Game code is shared on Code Connection at this URL

To get and use the code, follow these steps.

import button
Import choices

Click the Import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.

Import URL

Click the Import URL button , which is on the right, to open the window shown below.

Import Copy link
import url with url

Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”

go ahead button

Click the Go ahead! button .

The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.

We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.