Skip to content

04. In memory board

phu54321 edited this page Feb 10, 2018 · 4 revisions

You can download this chapter's material here.

The map needs to know what each chess cell holds. To do that, we're going to store the current game status in game script. We have 64 cells on the board, so something that represents a board needs 64 variables at least. When I mean variable, I'm referring to a storage that can hold values and can be modified. Just like death counters. We're going to create 64 variables to represent a board.

You can create 64 variables in once with array. Array is a sequence of variables, and you can access each variables with their index. Each of the variables are called item. You can use EUDArray to define an array.

const array = EUDArray(64);

EUDArray(64) means that we will create an array with 64 items. Each item can be accessed by indexes. There are 64 indexes available, 0 to 63. i'th item of the array can be accessed via array[index].

array[0] = 1;  // Set 0th item of array to 1
array[3] = 3;  // Set 3rd item of array to 3
array[2] = array[0];  // Set 2nd item of the array to what 0th item of array had.

We need to store 8x8 items inside 64 items. To do that, we need a way to convert (x, y) coordinate to 0~63 index, where 0 ≤ x,y ≤ 7. To do that, we can create a conversion table like this.

| | x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | y | | : | : | : | : | : | : | : | : | | 0 | .. | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | 1 | .. | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | | 2 | .. | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 3 | .. | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | 4 | .. | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | | 5 | .. | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | | 6 | .. | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | | 7 | .. | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |

In this way, we can convert each (x, y) coordinate to corresponding 0-63 index. Note that for index, index = x + y * 8. We can use this relationship to convert coordinate from and to array index.

// Basic tile getter & setter
const array = EUDArray(64);

// Gets array value corresponding to ([x], [y])
function tile(x, y) {
    return array[8 * y + x];
}

// Set array value corresponding to ([x], [y]) to [newVal]
function setTile(x, y, newVal) {
    array[8 * y + x] = newVal;
}

You can use tile(x, y) and setTile(x, y, newVal) to get and set the array's value. Function placePiece should set the underlying in-memory board (array) as it places the piece, so we add a setTile line to the function.

function placePiece(x, y, unitType, player) {
    move1x1Loc(x, y);
    CreateUnit(1, unitType, '1x1', player);
    setTile(x, y, unitType + 1000 * player)
}

Since all unitType is less than 1000, we multiplied player by 1000 to store both player value and unitType value on a single value. We can split player and unitType with the following formula.

const pieceVal = tile(x, y)
const piecePlayer = pieceVal / 1000;
const pieceUnitType = pieceVal % 1000;

A / B means integer division. Integer division discards remainders and keeps only the quotients. A % B is the modular operator, which keeps only the remainder.

So this is the end of the chapter. We have an in-memory layout of our chess board. This will greatly help our further progress. Let's move on to moving the mighty pawns.