the basics of creating a go bot
Posted: Wed Feb 11, 2015 7:42 am
i'm not sure if these algorithms are correct but they look like they are. i programmed them myself in python so hopefully they are right. please comment.
Code: Select all
board = ["+"]*361
black = 'B'
white = 'W'
empty = '+'
turn = "B"
ko = -1
def liberties(n, t, boardc, turn):
#check the current position n. if empty, mark it and increase counter by 1
if n == '+':
boardc[n] = '*'
return t+1
#if stone, mark it and recursively check left right top and bottom.
elif boardc[n] == turn:
if turn == "B"
boardc[n] = 'b'
else:
boardc[n] = 'w'
if n%19 < 18:
t = t +liberties(n+1, t, boardc)
if n%19 > 0:
t = t +liberties(n-1, t, boardc)
if n/19 > 0:
t = t +liberties(n-19, t, boardc)
if n/19 < 18:
t = t +liberties(n+19, t, boardc)
else:
return t
def group(n,t,boardc,turn):
same as liberty except count stones instead of empty.
if boardc[n] == turn:
if turn == "B"
boardc[n] = 'b'
else:
boardc[n] = 'w'
t += 1
if n%19 < 18:
group(n+1, t, boardc,turn)
if n%19 > 0:
group(n-1, t, boardc,turn)
if n/19 > 0:
group(n-19, t, boardc,turn)
if n/19 < 18:
group(n+19, t, boardc,turn)
else:
return t
def next(turn):
if turn == 'B':
turn = 'W'
else:
turn = 'B'
return turn
def is_legal(n,board,ko,turn):
if n =="pass":
return True
if n == "resign"
return True
if n == ko:
return False
#if you capture, its legal, if not, check suicide
boardc = board[:]
turn = next(turn)
if n%19 <18 and liberties(n+1,0,boardc,turn)==0:
return True
boardc = board[:]
if n%19 >0 and liberties(n-1,0,boardc,turn)==0:
return True
boardc = board[:]
if n/19 >0 and liberties(n-19,0,boardc,turn) ==0:
return True
boardc = board[:]
if n/19 <18 and liberties(n+19,0,boardc,turn) ==0:
return True
boardc = board[:]
turn = next(turn)
if liberties(n,0,boardc,turn) ==0:
return False
else:
return True
def capture(board):
#clear captured stones denoted b or w, return total captured
total = 0
for i in range(0,len(board)):
if board[i] != '+':
if board[i] != "W" or board[i] != "B":
board[i] = '+'
total += 1
return total
def play(n,board,ko,turn):
capture stones, detect ko (single capture with a group of size 1).
new_ko = -1
caps = 0
if is_legal(n,board,ko,turn):
boardc = board[:]
turn = next(turn)
if n%19 <18 and liberties(n+1,0,boardc,turn)==0:
liberties(n+1,0,board,turn)
caps += capture(board,turn)
if caps == 1:
new_ko = n+1
boardc = board[:]
if n%19 >0 and liberties(n-1,0,boardc,turn)==0:
liberties(n+1,0,board,turn)
caps += capture(board,turn)
if caps == 1:
new_ko = n-1
boardc = board[:]
if n/19 >0 and liberties(n-19,0,boardc,turn) ==0:
liberties(n-19,0,board,turn)
caps += capture(board,turn)
if caps == 1:
new_ko = n-19
boardc = board[:]
if n/19 <18 and liberties(n+19,0,boardc,turn) ==0:
liberties(n+19,0,board,turn)
caps += capture(board,turn)
if caps == 1:
new_ko = n+19
boardc = board[:]
if caps == 1:
turn = next(turn)
if group(n,0,boardc,turn) == 1:
ko = new_ko
turn = next(turn)
turn = next(turn)
board[n] = turn
turn = next(turn)
def scoresection(board, que, color, n)
#mark each empty space until you get to a stone. if it's different color, no territory.
if board[n] == "+":
board[n] = "*"
que += [n]
if n%19 < 18:
k,c = scoresection(board,que,color,n+1)
if c == "":
return k,c
if n%19 > 0:
k,c = scoresection(board,que,color,n-1)
if c == "":
return k,c
if n/19 < 18:
k,c = scoresection(board,que,color,n+19)
if c == "":
return k,c
if n/19 > 0:
k,c = scoresection(board,que,color,n-19)
if c == "":
return k,c
elif board[n] == "B" and color = "":
color = "B"
elif board[n] == "W" and color = "":
color = "W"
elif color == board[n]:
return que,color
elif n not in que:
return [],""
def evaluatescore(board,komi,handicap):
#score the section of all 361 points. return total.
que = []
color =""
totalBlack = 0
totalWhite = 0
boardC = board[:]
for i in range(0,361):
que,color = scoresection(boardC,que,color,n)
if color != "":
for i in range(0,len(que)):
board[que[i]] = color
for i in range(0,361):
if boardC[i] == "B":
totalBlack += 1
if boardC[i] == "W":
totalWhite += 1
return totalBlack -totalWhite -komi -handicap