Creating a Go Bot Part V -Coordinates system-

Note: Part of this article is copied from my old blog.

Note 2: The repo is here

Now that the engine knows how to play a random valid move, there is something more that we have to do previously to make the engine play “less” randomly.

Using GTP (in next posts we will talk a little more about Go Text Protocol that will be implemented in the project) the engine sends and receives coordinates, so we need something to translate from positions to coordinates and from coordinates to positions.

Right now, this convertion will be responsability of the board class, maybe in the future it can change, but I think it is board responsability to do that translation, what do you think about that?

My first idea is this one:

def posToCoordinate(self,position):
        column=['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
        finalCoord=""
        fPositionFound = False
        for row in range(1,self.boardSize):
            for col in range(0,self.boardSize):
                if col+self.boardSize+2+(row-1)*(self.boardSize+1) == position:
                    fPositionFound = True
                    finalCoord = column[col]+str(row)
                    break

        return finalCoord

Sure it works for boards until 25×25, to make bigger boards I would need to add more letters to the list, but for now let it be like this.

Now, the program will receive a coordinate, so I need to put it as a position to update my board array.

First I need to convert from a coordinate to a number. If we look at a 6×6 board with coordinates we see this:

    A    B   C    D    E   F
00 01 02 03 04 05 06
1 07 08 09 10 11 12 13
2 14 15 16 17 18 19 20
3 21 22 23 24 25 26 27
4 28 29 30 31 32 33 34
5 35 36 37 38 39 40 41
6 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
Now, let’s think that letters have a numerical value A=0, B=1, C=2, D=3, E=4…
Using as reference: Row=r, Column=c, n=number of rows or columns
I can use:  c + n + 2 + (r-1)*(n+1) 
 
(I guess it is easy for people with good knowledge of mathematics, but for me, to get here was like hell).
For example (in 6×6 board):
  • D4 = 3 + 6 + 2 + (4-1)*(6+1) = 3 + 6 + 2 + 3*7 = 11 + 21 = 32
  • A6 = 0 + 6 + 2 + 5*7 = 8 + 35 = 43

But this position is starting in 1, but Python arrays stars in 0, so I have to rest 1 to the total:

def coordinateToPosition(self,coordinate):
        column = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U','V', 'W', 'X', 'Y', 'Z']
        return (column.index(coordinate[0])+self.boardSize+2+(int(coordinate[1])-1)*(self.boardSize+1))-1

 

 

Another thing I need to work with this is to make a better print of the board , to look like this:

Just to not loose time on this and to continue with the important part, I will make it with numbers starting from top, it is the same as it is rotated and mirrored.

This is a quick version:

class Printer:
    def printBoard(self,aBoard):
        column = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U','V', 'W', 'X', 'Y', 'Z']
        rowCounter=0
        cut=0
        line = ""
        colCounter = " "
        for i in range(0,aBoard.boardSize):
            colCounter=colCounter+column[i]
        print(colCounter)
        for i in range(0, aBoard.totalSize-1):
            if aBoard.boardList[i]==0:
                line = line+"."
            if aBoard.boardList[i]==1:
                line = line+"B"
            if aBoard.boardList[i]==2:
                line = line+"W"
            if aBoard.boardList[i]==3:
                line = line+"#"
            cut=cut+1
            if cut == aBoard.boardSize+1:
                if rowCounter>=1 and rowCounter<=aBoard.boardSize:
                    print(line+"#"+str(rowCounter))
                else:
                    print(line + "#")
                rowCounter=rowCounter+1
                line=""
                cut = 0

I changed the main script to print both convertions and this appeared:

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *