Creating a Go Bot part VI – GTP

The next step to create the go bot is to make it compatible with GTP (Go Text Protocol) which is a standard for go bots and online go. This protocol have an “oficial website” that is well documented.

The idea is that the gtp engine is between the main loop and the engine, something like this:

MAIN <——GTP—–>Evil-Tesuji

I don’t remember if I said this earlier, but the engine name is “Evil Tesuji”.

There is a lot of refactor, but let’s put some code. It is not the best one, is just the first version of this and heavy refactoring will be needed in the near future.

From gtp protocol I implemented three commands:

  • Version: Gives the version of the engine.
  • Play: puts a stone in the board
  • Genmove: Waits a play for some color.

As long as I understand, gtp commands comes from the standard input, so, first I need to input waiting for command.

So let me explain some things of the code:

First I wait for an input, and then I send it to my gtp engine. My Gtp engine will do lots of things and return a color and a position. Then that color will be put in the board array. There are some exceptions like version, pass or resign moves.

If I send version command, color code is 50 so it goes to print the version. Same will happen in the future with machine name and all descriptions, they will have different color codes.

On the other hand resign will have position -10000 and pass -5000. For that I changed coordinate_to_position method (I am quitting all camel case):


For gtp, code is kind of self explanatory, you can choose between the available commands and each one will go to a method to resolve and return a color and a position. On genmove command it calls the engine and makes it play something with the chosen color.


Here is a screenshot of this working:


Electronic vote

Three facts before I start writing:

  1. I love technology advances, I love programming and I like how the world evolves thanks to technology.
  2. In Argentina there is a debate about electronic vote (Wikipedia has part of an article speacking about our case here).
  3. This blog is not about politics, and I do not sympathize with any political party. Of course I have political ideas, but I don’t trust in politicians.

The first impression is that I would love to have an electronic way to vote. It is fast, less paper consumed, less ways to do political patronage. But… I am against voting that way.

Let me tell you why:

  • Fraud: Some people says that voting this way avoids fraud because counting votes and filling telegrams is not anymore in (corrupted) human hands. Well, the program is made by (maybe corrupted) human hands. At that point this is just changing who put the hands moving from visible cheating to a slightly more difficult cheating, but the cheating factor is not deleted, it is only more difficult to find it.
  • Porposal: Computers count faster than humans. It is ok. But also computers count what programmers want to count, but we will go to that point later. The system that wants to be implemented in Argentina uses computers but also papers that are printed from the computers, so, votes in paper should be counted, unless we trust what the computer says. If we have to count paper either way, why use computers?
  • Source code: Here is for me as a developer, the main point: Who codes the voting program, and more important: who audits it? If it is the state or justice, you have trust a player that is also a referee. If it is a private corporation, it is contracted by the state, so, nothing changes. One solution is that the code is open source, so anyone with programming skills can read and audit.
  • Source code part II: Let’s say that the code is open source and the company that developed the voting systems gives you the code, who audits that the code is the same that the election computers have?
  • Source code part III: In the middle of voting, a machine stops working. It is changed, who audits that? (ok, the last three points could be resumed as one).
  • Security: ANY software is vulnerable. No matter how secure the company makes it (and lets be nice and say that the company really wants to make it secure), any software is vulnerable. About the BUE, there are some articles (In spanish) about vulnerabilities in the system. Here, Here, Here.
  • Examples: Venezuela is a good example of electronic voting manipulation. Most people in the country rejected to vote in the last election. Maduro’s government said that 8 million people voted and even the company that made the software, Smartmatic said that there was a big fraud. In other elections, Maduro himself claimed that he can know who voted against him.
  • Examples II: Lots of countries that had electronic voting systems are starting to ban it. Netherlands and Germany are some examples.

So, is electronic vote the problem? At some point I am starting to think that fraud is going to happen no matter what kind of voting system we use, but using computers the difference is that fraud will happen without most of the people notice it.

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 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.

Creating a Go bot Part II -The board-


Note: Lots of this material was taken from my old go blog with some new text that I am adding now because some years passed since that, I am not the same, my coding skills are not the same and my vision and ideas are not the same.

Let’s begin with the development of the bot. First, it needs to understand that there is a go board (goban) and it has positions, but what is the best way to do this?

Long, long time ago there was a document at sensei’s library that I don’t see so, I will copy from my old blog:

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 played on a variety of board sizes, the most popular by far is 19×19. For teaching purposes or for entertainment it also is occasionally played on 13×13 or 9×9. As far as I know, smaller sizes than that are only occasionally used as testing grounds for computer programs. Also boards larger than 19×19 are so rare that I choose to ignore them completely.

For now I will only assume that the board is square so rows == columns, nothing else, the game should be able to be played in any NxN board where N > 0 (well, let’s say N>1, because 1×1 needs to enable suicide move).

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 19×19 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.

Ok, so as I assumed this board will always be square it follows at some point what I was thinking I should do. For computing pourposes I think it is the same if lines are even or uneven.

Two-dimensional array is a big mistake here, I want to save all the memory I can so I think it is fair to change to a simple array.

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.

I remember when I first red this, I thought that was the end even before starting, but let’s say that after a while I get the idea of how to do this. It seems to be a lot of trouble just to replace a two dimensional array by a simple array, but it is not. Let’s check what’s next:

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 21×21 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 21×21=441 points, we can make do with 20×21+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

So, you can still imagine a 2 dimensional array using a one-dimensional array, it is just to quit one border x row, because from a row to another you only need one space that will be the border. Something like this:

#*********   <—The next one will be a border.

This is just a 9×9 board example, but this would work on any size.

Next point is what I will put in any position of the array, so for our convenience we can use:

  • 0 = empty
  • 1 = black
  • 2 = white
  • 3 = wall
  • 4 = ko

With this I think I am covering all posibilities, if not, I can add more values later.

In my next post I will add more information about this just to not make this post too long. I know this post was a lot about theory, but believe me, next one will show the Python solution for this.

See you then!

Creating a Go bot Part I

One of my hobbies is programming (even if I work as a developer I love to program free of pressure of doing that as a job). Another hobbie I have is Go. I started to play with lots of interruptions lots of years ago. First time I watched Hikaru No Go (an excellent anime, don’t miss it).  Right now I am around 5 kyu in KGS and you can find me with the nickname “draculinio” (feel free to ask me for a game or a little chat if you see me online).

Some years ago, when nobody thoght something like Alphago would exist, I wanted to create a bot that can play go and connect it to KGS or any other server that admits go bots.  My objective was not something very big or powerfull, just something that can play coherent moves. I think that now I can begin again, I will rescue some old posts from my old go programming blog so, some part of the text will be copied from that blog. I know my programming skills are better now than then, and with some parts of those old posts and new information I will be creating the go bot and posting about updates in this blog.

Some things about the project:

  • Language: Python 3
  • Open Source, I will see in the near future what’s the best licese for this.
  • Repo in:
  • Feel free to branch or clone.
  • Let’s see where can we go now.

Let’s put some short term first tasks

  1. Create the board representation
  2. Add a random stone
  3. Implement a mechanism to receive stones played by opponent.

Some rules for the project:

  1. Document all
  2. Push frequently
  3. Don’t hardcode (maybe some temporal hardcoding will be needed, but well documented and changed ASAP).


So, those three are the first things to do. Wish me luck!




A Counting Problem

I really love sites like Hackerrank and their programming challenges. Lately, one of them took my attention given that the simplest and standard solution didn´t work. The challenge is named “Find a String” and the idea is really simple: Count substring occurrences inside a string.

My first impression was the obvious one: “Hey, Python have this implemented”, so I tried the count method. This counts the number of times a sub string appears in a string, it is used like this:


But, the program failed. Look at this (there was another restriction, strings should have less or equal 200 characters):

If you run this, the result is 1 but the expected result is obviously 2.

My second thought was using regex library, something like this:


But again, the result was 1.

So, times like this is when you have to stop thinking in default language methods and libraries and start thinking in algorithmic solutions.

Here is something that happens to me when something standard is not doing what I want: I do things like a craftsman, so this is a quick approach of the function:

This might overkill the problem and there are many improvements in the code that can be done to make it faster, but for now, this would work.

So, the lesson is: always test your code, maybe some things that you think are OK because you are using the language standards can fail.

Testing AR XIII Conference

Once again I was invited to speak in Testing Ar meetup. This time the meetup was in Belatrix that is really a great place to do this kind of events.

This time I talked about the “Non event” of 2016, Selenium 3. Why small changes made a great change towards the future of testing automation.

Testing Ar is a great QA community from Argentina, very well organized and with superb people. Every month they do meetups in different companies.  If you want to see Testing Ar events you can see their meetup site here. If you want to see past events you can see their youtube channel.


Here is the conference (In spanish):

Creating a Twitter Bot II – Random Twits


Hello again, welcome back to this “tutorial” about creating a Twitter bot, if you missed first part, please read it here.

So, now that we can Twit, maybe we can do some interesting things with that.

Creating random Tweets

As I wrote in the last article, one problem of tweeting “Hello World” is that you can’t twit twice the same text. In the other hand, we want the program to run all the time, tweeting different things, so we will make our program twit every x seconds a random text.

First thing is to create a class that will have some common functionalities and in there create a method that will create random texts of x characters.


Even if the code is easy, here is a little explanation of how this works:

I have an array of characters that can be used for the text, and then a for from 0 to the number of desired characters it concatenates a string with new random character. In the end that string is returned.

Now that we have the functionality to send some random text, we can post tweets with a fixed delay. Here is one way to do that. Remember main file we have? Let’s work on that file and do something like this:

We need time library to make it post every x time. time.time() returns the time as a floating point number expressed in seconds since the epoch, in UTC. For more information about Time library look here. Next step is to make objects of class Common and Twitter and connect to our Twitter account.

Now that we are connected we enter in an infinite loop and say sleep x seconds (in the example I putted 30, but you can put what you want) and then post a random tweet of 140 characters calling the randomText method.

If you want the code, fork it, review it, do whatever you want just clone it from here

Next time we will do some changes to our profile, see who follows us, follow people, etc. so stay tuned!


1 2