go.py

Created by ziii

Created on September 09, 2023

4.09 KB

A basic game of go, close to an over the board experience. Uses area scoring because it’s easier to implement.

The colors of the game are fully customizable, from line 23 to 27 change the rgb values to whatever you wish.

How to play :

After starting the program enter the number of lines you want for the board.

Arrows to move the cursor.

Ok to place a stone.

Zero to remove a stone.

One to add a black stone.

Two to add a white stone.

Plus to pass turn.

Shift to remove a group of stones of the same color.


from kandinsky import fill_rect as f, draw_string as d
from ion import *
from time import monotonic
def circle(r,pos,col):
  for x in range(r):
    h=int(((r**2)-(x**2))**0.5)
    f(pos[0]-x,pos[1]-h,1,h*2,col)
    f(pos[0]+x,pos[1]-h,1,h*2,col)
def cur(vx=0,vy=0):
  global time,pos
  if monotonic()-time>0.1:
    time=monotonic()
    if grid[pos[0]][pos[1]]:
      circle(cr_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),COLORS[grid[pos[0]][pos[1]]-1])
    else:
      f(br+(pos[0]+1)*sz-cr_sz,br+(pos[1]+1)*sz-cr_sz,cr_sz*2,cr_sz*2,BOARD)
      f(br+(pos[0]+1)*sz-cr_sz,br+(pos[1]+1)*sz,cr_sz*2,1,BACKGROUND)
      f(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz-cr_sz,1,cr_sz*2,BACKGROUND)
    pos[0]+=vx
    pos[1]+=vy
    circle(cr_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),CURSOR)
BACKGROUND=(50,50,50)
BOARD=(189,148,118)
CURSOR=(255,0,0)
COLORS=(
    (0,0,0),#black
    (255,255,255),#white
)
key_pressing={KEY_OK:0,KEY_PLUS:0,KEY_ZERO:0,KEY_ONE:0,KEY_TWO:0,KEY_SHIFT:0}
key_pressed={k:0 for k in key_pressing}
l=int(input("enter grid size\n19 for official rules \n3 min 55 max: "))
if not 56>l>2:
  l=19
sz=222//(l-1) # tile size
br=(222%(l-1))//2 #border size
cr_sz=sz//4 # cursor size
st_sz=sz//2 # piece size
turn=0
pos=[0,0]
grid=[[0 for _ in range(l-2)]for _ in range(l-2)]
scores=[0,0]
TXT=("Black","White")
f(0,0,320,222,BACKGROUND)
f(0,0,222,222,BOARD)
[f(br+sz*x,br,1,222-2*br,BACKGROUND) for x in range(l)]
[f(br,br+sz*y,222-2*br,1,BACKGROUND) for y in range(l)]
d(TXT[turn],230,0,BOARD,BACKGROUND)
d("play",230,60,BOARD,BACKGROUND)
d("OK",230,78,BOard,BACKGROUND)
d("modify",230,98,BOARD,BACKGROUND)
d("0-2",230,116,BOARD,BACKGROUND)
d("pass turn",230,136,BOARD,BACKGROUND)
d("+",230,154,BOARD,BACKGROUND)
d("remove",230,174,BOARD,BACKGROUND)
d("shift",230,192,BOARD,BACKGROUND)
time=monotonic()-1
cur()
run=1
while run:
  change=1
  for k in key_pressing:
    key_pressed[k]=0
    if keydown(k):
      if not key_pressing[k]:
        key_pressed[k]=1
      key_pressing[k]=1
    else:
      key_pressing[k]=0
  if keydown(KEY_LEFT) and pos[0]>0:
    cur(vx=-1)
  elif keydown(KEY_RIGHT) and pos[0]<len(grid)-1:
    cur(vx=1)
  elif keydown(KEY_UP) and pos[1]>0:
    cur(vy=-1)
  elif keydown(KEY_DOWN) and pos[1]<len(grid[0])-1:
    cur(vy=1)
  if key_pressed[KEY_PLUS]:
    turn=not turn
    d(TXT[turn],230,0,BOard,BACKGROUND)
  elif key_pressed[KEY_ZERO] and grid[pos[0]][pos[1]]!=0:
    scores[grid[pos[0]][pos[1]]-1]-=1
    grid[pos[0]][pos[1]]=0
    circle(st_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),BOARD)
    f(br+(pos[0]+1)*sz-st_sz,br+(pos[1]+1)*sz,st_sz*2,1,BACKGROUND)
    f(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz-st_sz,1,st_sz*2,BACKGROUND)
  elif key_pressed[KEY_ONE] and grid[pos[0]][pos[1]]!=1:
    scores[0]+=1
    grid[pos[0]][pos[1]]=1
    circle(st_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),COLORS[0])
  elif key_pressed[KEY_TWO] and grid[pos[0]][pos[1]]!=2:
    scores[1]+=1
    grid[pos[0]][pos[1]]=2
    circle(st_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),COLORS[1])
  elif key_pressed[KEY_OK] and not grid[pos[0]][pos[1]]:
    grid[pos[0]][pos[1]]=turn+1
    circle(st_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),COLORS[turn])
    scores[turn]+=1
    turn=not turn
    d(TXT[turn],230,0,BOARD,BACKGROUND)
    circle(cr_sz,(br+(pos[0]+1)*sz,br+(pos[1]+1)*sz),CURSOR)
  elif key_pressed[KEY_SHIFT] and grid[pos[0]][pos[1]]!=0:
    a=grid[pos[0]][pos[1]]
    grid[pos[0]][pos[1]]+=2
    doing=1
    while doing:
      doing=0
      for x in range(l-2):
        for y in range(l-2):
          if grid[x][y]==a+2:
            grid[x][y]+=2
            doing=1
            circle(st_sz,(br+(x+1)*sz,br+(y+1)*sz),BOARD)
            f(br+(x+1)*sz-st_sz,br+(y+1)*sz,st_sz*2,1,BACKGROUND)
            f(br+(x+1)*sz,br+(y+1)*sz-st_sz,1,st_sz*2,BACKGROUND)
            for coo in[[-1,0],[1,0],[0,-1],[0,1]]:
              if x+coo[0]<l-2 and y+coo[1]<l-2 and grid[x+coo[0]][y+coo[1]]==a:
                grid[x+coo[0]][y+coo[1]]+=2
    for x in range(l-2):
      for y in range(l-2):
        if grid[x][y]>4:
          grid[x][y]=0
          scores[a-1]-=1
  else:
    change=0
  if change:
    d(str(scores[0]),230,20,COLORS[0],BOARD)
    d(str(scores[1]),230,40,COLORS[1],BOARD)