TesujiGoFramework/BoardRepresentation

Sub-page of TesujiGoFramework

Board representation

The most basic of all basics. In order to have a program that can play Go, we need a representation of a Go board in computer terms. Although Go is payed on a variety of board sizes, the most popular by far is 19x19. For teaching purposes or for entertainment it also is occasionally played on 13x13 or 9x9. As far as I know, smaller sizes than that are only occasionally used as testing grounds for computer programs. Also boards larger than 19x19 are so rare that I choose to ignore them completely.

Since the board is always square (with an uneven number of lines), common sense would point to using a two-dimensional array to represent the board. Unfortunately computers take much longer to access a two-dimensional array than a one-dimensional array. For this reason most serious Go playing programs will use a one-dimensional array to represent the board. The one-dimensional position z of a board-point x,y is then arrived at by the following conversion: z = x + y *19 (for a 19x19 board represented by a one-dimensional array of size 361). From now on I'll use xy instead of z as the one-dimensional equivalent of the two-dimensional coordinate x,y. Since the program will internally only use one-dimensional arrays, it will in practice not have to do such conversion often. A computer doesn't care whether the coordinate is represented in a one dimension or two.

There's a practical difficulty with representing the board in a one dimensional array however. How can the program distinguish between the borders of the board? This is still possible by looking at the coordinate. When xy%19 equals 1 or 19, or when xy/19 equals 1 or 19 we have a point on the 1st line. Next to that is the edge of the board. This is a rather cumbersome way however, and such calculation will cancel out any performance gained by using a one-dimensional representation. The common way to solve this is by usig a bigger array and use border-points around the board to indicate the board boundaries. At first you may think this would lead to a 21x21 representation, and indeed in a two-dimensional board representation this would be the case. When this gets mapped to a one-dimensional array and you'd print out the information stored, you may notice something interesting however. Whenever a border-point is reached to mark the edge of the board, you see two consecutive border-points next to each other. For this reason, the one-dimensional representation can be a little smaller. Instead of 21x21=441 points, we can make do with 20x21+1=421 points. This has one more advantage: to convert a one-dimensional coordinate to two dimensions, the division is by 20 instead of 19 or 21, which is a lot easier for humans. As said before, computers don't care about such things, but it's a lot easier when seeing one-dimensional coordinates in your debugger this way. Trust me, it's a lot easier! After this you should understand the reason behind the constant values defined at the top of the class tesuji.games.go.util.GoArray.

OK, this is initially about the Go board. The Go board has all the information there is, still it's rather limited for a computer program to do anything useful. A Go board has points that can only be in one of three states: empty, a black stone or a white stone. So that's all that the array will contain. With one addition, which will be no surprise after reading the previous section, and that is a special value for points representing the edge of the board. Since this is not a lot of information, it can easily be stored in a byte, and that's what I'll do for the framework. There are advantages and disadvantages to this. The advantage is that for the computer it's faster to access byte-arrays than others. This is because the coordinate will need to be multiplied by 2 or 4 or 8, depending on whether your basic data type is 2, 4 or 8 bytes in size. The other advantage is obviously space, the smaller the better. The disadvantage is that a computer often has a 'basic' type. Calculations using this type will usually be faster than other types, whether smaller or bigger. Most CPUs have a basic-type of 32 bits, or 4 bytes. Some nowadays have a basic type of 64 bits even. Sometimes there's a noticable difference in performance using one or the other. Still, the difference is rarely more than a few percent either way. Most of the time however I've found there's no noticable difference in performance overall at all. In that case I prefer to use as little memory as possible, so whereever information fits in a byte, I'll use bytes. Information not fitting in a byte I'll usually store in an 'int' or integer type, which is usually the CPUs basic type.

Since the playing program will need to generate a lot of different kind of data and data-structures to be able to play, we will not limit ourselves to arrays of bytes. tesuji.games.go.util.ByteArray is just one of a collection of data-types that are basically equivalent but store a different basic type.IntArray and ObjectArray are other obvious members of this collection.

Although the actual implementations of these data-stuctures store the data in a one-dimensional array, they provide interfaces to access them either as one-dimensional coordinates or two-dimensional coordinates. They also implement the abstract interface tesuji.games.general.BoardModel, which is a very abstract definition of how to access a board representation. BoardModel is the next building block.


This is a copy of the living page "TesujiGoFramework/BoardRepresentation" at Sensei's Library.
(OC) 2014 the Authors, published under the OpenContent License V1.0.
[Welcome to Sensei's Library!]
StartingPoints
ReferenceSection
About