Search

Showing posts with label translator. Show all posts
Showing posts with label translator. Show all posts

Monday, August 27, 2012

Day 19: GTP, starting the hard things



The day has come, in the weekend I was reading how GTP works and was thinking how to make it work on my program. I will try to implement a simple command to see if it works...

MAYOR UPDATES MADE IN THE main loop, this is hurting. 

Ok, I downloaded and installed GoGui, to see how it works. As the people of computer-go mailing list told me, GTP uses standard IO so just used inputStreamReader (with the bufferedReader) and System.out.Println().

Right now I implemented:  play, name, version, protocol_version, list_commands, boardsize and clearboard. For now boardsize just returns true, it is hardcoded to 19 so, every game I start will be 19x19 (I can hardcode it to whatever). boardsize calls board initialize.

This is what I get in GoGui:


Well, next have to make it play and then some refactor to make the code more clear.

Wednesday, August 15, 2012

Day 7: Coding the translator



I updated the class design with this information:


  1. I need a class Translator. For now, it will be a new actor, so in the package Actors there is a new class. It have some methods.
  2. This class have two methods: coordinateToPosition, that translates a coordinate to a position in the array. It takes 2 arguments: the coordinate and the size of the board. The other method is positionToCoordinate that translates from a position in the board array to a coordinate.
  3. There is also a private variable, coord, that have all the coordinates first part. It is needed to change it to a number. For example A=0 (0 is the position and will be the value), B=1, etc.
The code:


This should work looking at the last post. Now, let's see if this works. For now, I will add a println in the main loop so when the engine sends a position it will write the coordinate.



And...



A 22x22 board:


That's it, next time I think I will start with some loop and then I think I will try to group.


Tuesday, August 14, 2012

Day 6: Converting coordinates.

As I said yesterday, a "translator" is needed, this is the first model that I think I will implement:


  1. The engine sends the integer value to the board. This is actually working.
  2. The engine should show to the outside a coordinate, so it sends the numerical value to the Translator, it converts it to a coordinate and sends it to the outside.
  3. A coordinate comes from the outside, it is sent to the translator that convert it to an integer with the position. Then it is sent to the board.


The maths

First I need to convert from a coordinate to a number. If we look at a 6x6 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: 

  • D4 = 3 + 6 + 2 + 3*7 = 11 + 21 = 32
  • A6 = 0 + 6 + 2 + 5*7 = 8 + 35 = 43
Have to try it in 9x9:


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

And now, let's try the magic:

  • H3 = 7 + 9 + 2 + 2*10 = 18 + 20 = 38
  • B8 = 1 + 9 + 2 + 7*10 = 12 + 70 = 82
I can think now that this works.

And now... I have to convert from the postions to the equivalent coordinate:

The fastest approach to this (not in therms of algorithms but in therms of the first solution I thought) is to try all combinations against the formula. In other words, I have to try from c=1 to number of columns and from r
to number of rows.

This is why I really love to work with paper and pencil: