Search

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:


No comments:

Post a Comment