Counting liberties on a Go (Baduk, Weiqi) board

14 views (last 30 days)
Hello,
I am trying to code a Go board in Matlab by creating a 19x19 matrix, in wich empty spaces are reprsented by a 0, black stones are represented by a 1, and white stones are represented by a -1.
I am having trouble creating a function that counts liberties, and that tells the board that a stone (or group of stones) has zero liberties and was killed, and he has to replace the 1 or -1 with a zero.
My initial idea was to start from the last move that was played, and check the liberties of the enemy stones around him; then check the liberties of the stone just played (and the eventual group he was in). However, I have trouble creating the iterative process. Is there a simple way to check the values in adjacent elements of a matrix?
  1 Comment
Walter Roberson
Walter Roberson on 26 Feb 2022
nlfilter might be easier to code, checking the entire board at the same time
It could probably also be coded as a conv2 call and some comparisons. But it has been years since I last looked at the rules so I do not recall the exact conditions at the moment (in particular I do not recall the edge conditions)

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 26 Feb 2022
Edited: John D'Errico on 26 Feb 2022
This link defines the rules for a liberty:
I read them only quickly, but I think I get the concept. Can you use conv2? Yes. it should work. Consider this randomly generated 10x10 board:
GB = round(rand(10)*2) - 1
GB = 10×10
0 -1 0 -1 0 1 1 0 -1 -1 1 -1 1 1 1 0 0 1 -1 0 0 1 -1 0 0 0 -1 0 1 0 0 1 0 1 0 0 0 1 0 0 0 1 0 -1 -1 -1 -1 1 0 1 0 -1 0 -1 0 -1 1 1 -1 0 1 1 0 0 0 -1 -1 1 1 1 1 0 1 1 1 -1 -1 1 0 1 1 0 -1 1 -1 -1 1 0 -1 0 -1 0 1 0 0 -1 0 -1 0 -1
Yes, I know that as an actual game of go, it is meaningless, even pretty silly looking. But I am a bridge player, not go. Sorry about that. Regardless, what does a liberty mean? Any cell has as many liberties as it has empty horizontally and vertically immediate neighboring cells. And we can count that using a simple call to conv2.
kernel = [0 1 0;1 0 1;0 1 0];
liberties = conv2(1 - abs(GB),kernel,'same')
liberties = 10×10
0 2 0 2 0 2 2 0 1 1 2 0 1 1 3 2 1 3 1 1 1 1 2 1 3 3 4 0 3 2 2 2 1 3 2 3 1 3 2 2 2 2 2 1 2 1 1 1 1 3 1 2 2 3 1 1 0 0 2 0 1 2 2 2 2 1 0 0 1 1 1 1 2 1 1 0 0 2 0 2 1 2 1 1 1 0 2 0 4 0 1 1 2 1 1 2 0 3 0 2
So if I understand what a liberty means, the element GB(1,1) has no liberties, because its neighbors are all filled. But the cell GB(1,2) is a black stone, and it has exactly 1 liberty, to the immediate left.
Similarly, the black stone at position GB(9,1) is on the edge. It has 3 liberties. There are cells in the interior that had 4 liberties.
Can you find all black stones, that have any liberties? Simple.
blackstoneswithsomeliberty = liberties.*(GB == 1)
blackstoneswithsomeliberty = 10×10
0 0 0 0 0 2 2 0 0 0 2 0 1 1 3 0 0 3 0 0 0 1 0 0 0 0 0 0 3 0 0 2 0 3 0 0 0 3 0 0 0 2 0 0 0 0 0 1 0 3 0 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 1 1 1 0 2 1 1 0 0 2 0 2 1 0 0 1 0 0 2 0 0 0 0 0 2 0 0 0 0 0 0 0
So wherever that result is non-zero, we have a black stone that has at least one liberty. The number in there indicates the number of liberties it has. And while I have not read the rules for go at all deeply, you should be able to use tricks like that to make your work easy enough. Writing an intelligent game of go in MATLAB is a deeper problem that I'd not touch of course. I'd need a Deeper Mind to handle that.
  1 Comment
Lollo Senti
Lollo Senti on 26 Feb 2022
Thank you very much! This work very well for single stones, and with a bit of tweaking i should be able to make it work for groups of stones too!

Sign in to comment.

More Answers (0)

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!