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:

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:

 

 

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:

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

Thanks for reading!

Creating a Go bot IV – The first move.

Well, now we have an empty board, we need to make our first move. My first approach is that every position has a value and the one with the highest value will be where our bot will play.

What is the easiest way to start doing that? Give a random number let’s say from 1 to 10 to every position, and -1000 to positions where it is forbidden to play (walls, places with stones, ko). Why -1000 and not -1 or something like that? Maybe in the near future from the first value, some things will rest points and -1 can be the place with highest value, so it is better to give an unreachable low value (I am writing as I am thinking).

So, where will be all those things? I am thinking in giving the bot a “brain” that will be a class or a bunch of classes that will receive the actual board, process it and return a move.

so, let’s create a class “Engine”. For starters I think on something like this:

This class has an engine that as a parameter for the constructor it gets his color.

So everytime it is his turn we call the putStone method that receives the board, it calls getValues that will give random values and -1000 to non playable places and returns the position with the highest score. In the future it will pass through a lot more methods, but now this is simple.

Maybe in the future, while adding more things, I will separate the engine in several classes, but for now it works.

So, let’s change the main.py to create the engine, show the board, let the engine play a stone and show the board again:

This is for a 9×9 board, so when I run this I get this:

Yes, I know it is not the best possible move, and it is not even a reasonable move playing 1-3 but as it is random, well, it is a move.

I wanted to run this again just to make sure it plays in other place, and it worked (will just paste the board with the move this time)

Next time I will add more parameters to make this play more “reasonable” openings.

Creating a Go Bot Part III – More about board representation

Note: In this post I will continue to transfer things from my old blog to this one with some new things.

Well, let’s put in practise the theory I learnt in the previous post. The board will be an array (I will use the class Vector) and in theory the size should be (n+2)*(n+1)-1. So, I will write some examples of this just to understand:

6×6 board = (6+2)*(6+1)-1 = 8*7+1 = 57 positions

Those will be the positions:

01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
50 51 52 53 54 55 56 57

Borders will be in positions 1, 2, 3, 4, 5, 6, 7, 8, 15, 22, 29, 36, 43, 50, 51, 52, 53, 54, 55, 56 and 57.

Another example, a 9×9 board, the minimum that is used to play frequently:

9×9 board = (9+2)*(9+1) + 1 = 11*10+1=111

 

01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 00
01 02 03 04 05 06 07 08 09 10 11

Borders will be at 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111

So, here is another way to see where boders will be in a given board:

  • 1 to n+1
  • (n+1)*x+1 (x is a for from 1 to n)
  • (n+1)*(n+1) to (n+2)*(n+1)+1

Yes, I know that positions in my array starts in 0, not in 1, but the idea was to show positions on the board without thinking in any programming language.

Now, lets start coding!

What we need is a Board class with a constructor using the size as a parameter. That is the only parameter needed, and from that all the board can be constructed:

Now, one thing we need now is a Printer class to print the board. I will add something for th future that is that depending on the value something is written. For example if 0 then print a point. If a border (3) print a hash a wall. if 1 is black and if 2 is white

And this completes the board representation part. Now that I have a board I can start coding the engine. Next chapter will be about an intuitive way to start putting stones.