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:
import board import random class Engine: def __init__(self,color): self.color=color #Here is the process to put a stone. def putStone(self,aBoard): #Here goes all the magic. valuesList = self.getValues(aBoard) return self.returnChosenOne((valuesList)) def getValues(self,aBoard): boardListValues =  for i in range(0,aBoard.getBoardTotalPositions()): if aBoard.boardList[i]!=0: #Edges, occupied spaces, ko boardListValues.append(-1000) else: boardListValues.append(random.randint(0,10)) return boardListValues def returnChosenOne(self,valuesList): return valuesList.index(max(valuesList))
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:
from printer import Printer from board import Board from Engine import Engine evilTesuji = Engine(1) mainBoard = Board(9) mainPrinter = Printer() mainPrinter.printBoard(mainBoard) move = evilTesuji.putStone(mainBoard) mainBoard.boardList[move]=evilTesuji.color mainPrinter.printBoard(mainBoard)
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.