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:

class Board:
    """Class for Board representation"""
    def __init__(self, size):
        self.boardList = []
        self.boardSize = size
        self.totalSize = (size + 2) * (size + 1) + 1
        for i in range(0, self.totalSize):
            self.boardList.append(0)
        #BORDERS
        for i in range(0,self.boardSize+1):
            self.boardList[i] = 3
        for i in range(0,self.boardSize):
            self.boardList[(self.boardSize+1)*(i+1)]=3
        for i in range((self.boardSize+1)*(self.boardSize+1),(self.boardSize+2)*(self.boardSize+1)+1):

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

import board
class Printer:
    def printBoard(self,aBoard):
        cut=0
        line = ""
        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+"#"
            #line = line+str(aBoard.boardList[i])
            cut=cut+1
            if cut == aBoard.boardSize+1:
                print(line+"#")
                line=""
                cut = 0

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.

Leave a Reply

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