#!/usr/bin/scriba IMPORT visio.bas GLOBAL CONST nl = "\n" GLOBAL CONST FAKE = 12345 GLOBAL CONST EMPTY = 0 GLOBAL CONST WHITE = -1 GLOBAL CONST BLACK = 1 GLOBAL CONST GROUPIDSTART = 10 REM **************************************************************Main program REM ss_debug(1) REM v_debug(v_true) CALL Initialize_Global CALL Create_Gopanel REPEAT v_wait(V_FALSE) IF v_window_resized(gopanel) THEN CALL Draw_Board IF v_button_clicked(newbutton) THEN CALL New_Board IF v_canvas_clicked(canvas) THEN CALL Put_Stone IF v_button_clicked(buttoncapt) THEN CALL Show_Prisoners UNTIL v_button_clicked(quitbutton) v_exit END REM **************************************************************Initialize global parameters SUB Initialize_Global v_init() REM This array keeps the current go-board, wipe Go playing board FOR j = 1 to 9 FOR i = 1 to 9 board[i,j] = EMPTY NEXT i NEXT j REM These keep the coordinates of the last move played lastmovex = 0 lastmovey = 0 REM These keep the total amount of captured stones for each color capturedbywhite = 0 capturedbyblack = 0 END SUB REM **************************************************************Create Go panel SUB Create_Gopanel gopanel = v_window("The incredible Go panel", 300, 350, V_POS_NONE) gogrid = v_grid(gopanel, 50, 55) frame = v_frame(gogrid, 1, 1, 48, 42, V_SHADOW_ETCHED_IN) canvas = v_canvas(gogrid, 2, 2, 46, 40) radioblack = v_radio(gogrid,1,20,44,10,2,"Black") radiowhite = v_radio(gogrid,1,20,48,10,2,"White") buttoncapt = v_button(gogrid,10,44,8,6,"Show\nprison") newbutton = v_button(gogrid, 1, 44, 8, 6, "New") quitbutton = v_button(gogrid, 41, 44, 8, 6, "Quit") gostatus = v_status_bar(gogrid, 0,52,50,3) v_status_set_text(gostatus, "New game started!") v_show(gopanel) END SUB REM ************************************************************Draw the Go board SUB Draw_Board LOCAL xfactor, yfactor REM Calculate square size xfactor = INT(v_canvas_width(canvas)/9) yfactor = INT(v_canvas_height(canvas)/9) REM Draw vertical axes FOR i = xfactor/2 TO v_canvas_width(canvas) - xfactor/2 STEP xfactor v_canvas_line(canvas, INT(i), INT(yfactor/2), INT(i), INT(yfactor/2)+yfactor*8) NEXT i REM Draw horizontal axes FOR i = yfactor/2 TO v_canvas_height(canvas) - yfactor/2 STEP yfactor v_canvas_line(canvas, INT(xfactor/2), INT(i), INT(xfactor/2)+xfactor*8, INT(i)) NEXT i REM Draw the positions of the stones FOR i = 1 TO 9 FOR j = 1 TO 9 IF board[i,j] = BLACK THEN v_canvas_fg_color(0,0,0) v_canvas_circle(canvas,(i-1)*xfactor+xfactor/2,(j-1)*yfactor+yfactor/2,xfactor/2-1,yfactor/2-1,1) ELSE IF board[i,j] = WHITE THEN v_canvas_fg_color(65535,65535,65535) v_canvas_circle(canvas,(i-1)*xfactor+xfactor/2,(j-1)*yfactor+yfactor/2,xfactor/2-1,yfactor/2-1,1) v_canvas_fg_color(0,0,0) v_canvas_circle(canvas,(i-1)*xfactor+xfactor/2,(j-1)*yfactor+yfactor/2,xfactor/2-1,yfactor/2-1,0) END IF NEXT j NEXT i END SUB REM ************************************************************Reset the board & draw SUB New_Board FOR j = 1 to 9 FOR i = 1 to 9 board[j,i] = EMPTY NEXT i NEXT j v_canvas_clear(canvas) CALL Draw_Board lastmovex = 0 lastmovey = 0 capturedbywhite = 0 capturedbyblack = 0 v_status_set_text(gostatus, "New game started!") END SUB REM ************************************************************Show captured stones SUB Show_Prisoners v_status_set_text(gostatus, "Captured by white: "&capturedbywhite&" - Captured by black: "&capturedbyblack) END SUB REM ************************************************************Put a stone & draw SUB Put_Stone LOCAL mousex, mousey, xfactor, yfactor mousex = v_canvas_mousex(canvas) mousey = v_canvas_mousey(canvas) xfactor = INT(v_canvas_width(canvas)/9) yfactor = INT(v_canvas_height(canvas)/9) FOR j = 1 to 9 FOR i = 1 to 9 IF (mousex>(j-1)*xfactor) AND (mousex<(j-1)*xfactor+xfactor) THEN IF (mousey>(i-1)*yfactor) AND (mousey<(i-1)*yfactor+yfactor) THEN IF v_radio_get_status(radioblack) THEN IF board[j,i] = EMPTY THEN board[j,i] = BLACK v_status_set_text(gostatus,"Black played.") lastmovex = j lastmovey = i ELSE v_status_set_text(gostatus,"Cannot play here!") END IF ELSE IF board[j,i] = EMPTY THEN board[j,i] = WHITE v_status_set_text(gostatus,"White played.") lastmovex = j lastmovey = i ELSE v_status_set_text(gostatus,"Cannot play here!") END IF END IF END IF END IF NEXT i NEXT j CALL Draw_Board CALL Captured_Stones CALL Determine_Suicide END SUB REM ************************************************************Was last move suicide? SUB Determine_Suicide LOCAL i, j, group, freedoms, counter, current REM Wipe groups board with fake value FOR j = 0 TO 10 FOR i = 0 TO 10 groups[j,i] = FAKE NEXT i NEXT j REM Copy current playing board to groups board FOR j = 1 TO 9 FOR i = 1 TO 9 groups[j,i] = board[j,i] NEXT i NEXT j REM Clear temp array's keeping stone positions FOR i = 1 to 81 posx[i] = 0 posy[i] = 0 NEXT i REM Initialize local variables freedoms = 0 counter = 1 current = 1 posx[current] = lastmovex posy[current] = lastmovey group = groups[lastmovex,lastmovey] groups[lastmovex,lastmovey] = -1*group DO IF groups[posx[current]-1,posy[current]] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current]-1,posy[current]] = group THEN groups[posx[current]-1,posy[current]] = -1*group counter += 1 posx[counter] = posx[current]-1 posy[counter] = posy[current] END IF IF groups[posx[current]+1,posy[current]] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current]+1,posy[current]] = group THEN groups[posx[current]+1,posy[current]] = -1*group counter += 1 posx[counter] = posx[current]+1 posy[counter] = posy[current] END IF IF groups[posx[current],posy[current]-1] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current],posy[current]-1] = group THEN groups[posx[current],posy[current]-1] = -1*group counter += 1 posx[counter] = posx[current] posy[counter] = posy[current]-1 END IF IF groups[posx[current],posy[current]+1] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current],posy[current]+1] = group THEN groups[posx[current],posy[current]+1] = -1*group counter += 1 posx[counter] = posx[current] posy[counter] = posy[current]+1 END IF current += 1 LOOP UNTIL current > counter IF freedoms = 0 THEN board[lastmovex, lastmovey] = EMPTY v_canvas_clear(canvas) CALL Draw_Board v_status_set_text(gostatus, "This was an illegal move! Play again...") END IF END SUB REM ************************************************************Find captured stones SUB Captured_Stones LOCAL i, j, group, freedoms, counter, current, total REM Wipe groups board FOR j = 0 TO 10 FOR i = 0 TO 10 groups[j,i] = FAKE NEXT i NEXT j REM Copy current playing board to groups board FOR j = 1 TO 9 FOR i = 1 TO 9 groups[j,i] = board[j,i] NEXT i NEXT j REM Clear temp array's keeping stone positions FOR i = 1 to 81 posx[i] = 0 posy[i] = 0 NEXT i REM Initialize used variables total = 0 FOR j = 1 TO 9 FOR i = 1 TO 9 REM Check: place is empty and is not the last color played IF (ABS(groups[j,i]) <> EMPTY) AND (groups[j,i]<>board[lastmovex,lastmovey]) THEN freedoms = 0 counter = 1 current = 1 posx[current] = j posy[current] = i group = groups[j,i] groups[j,i] = -5*group DO IF groups[posx[current]-1,posy[current]] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current]-1,posy[current]] = group THEN groups[posx[current]-1,posy[current]] = -5*group counter += 1 posx[counter] = posx[current]-1 posy[counter] = posy[current] END IF IF groups[posx[current]+1,posy[current]] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current]+1,posy[current]] = group THEN groups[posx[current]+1,posy[current]] = -5*group counter += 1 posx[counter] = posx[current]+1 posy[counter] = posy[current] END IF IF groups[posx[current],posy[current]-1] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current],posy[current]-1] = group THEN groups[posx[current],posy[current]-1] = -5*group counter += 1 posx[counter] = posx[current] posy[counter] = posy[current]-1 END IF IF groups[posx[current],posy[current]+1] = EMPTY THEN freedoms += 1 ELSE IF groups[posx[current],posy[current]+1] = group THEN groups[posx[current],posy[current]+1] = -5*group counter += 1 posx[counter] = posx[current] posy[counter] = posy[current]+1 END IF current += 1 LOOP UNTIL current > counter IF freedoms = 0 THEN counter = 1 current = 1 posx[current] = j posy[current] = i group = groups[j,i] groups[j,i] = EMPTY board[j,i] = EMPTY DO IF groups[posx[current]-1,posy[current]] = group THEN groups[posx[current]-1,posy[current]] = EMPTY board[posx[current]-1,posy[current]] = EMPTY counter += 1 posx[counter] = posx[current]-1 posy[counter] = posy[current] END IF IF groups[posx[current]+1,posy[current]] = group THEN groups[posx[current]+1,posy[current]] = EMPTY board[posx[current]+1,posy[current]] = EMPTY counter += 1 posx[counter] = posx[current]+1 posy[counter] = posy[current] END IF IF groups[posx[current],posy[current]-1] = group THEN groups[posx[current],posy[current]-1] = EMPTY board[posx[current],posy[current]-1] = EMPTY counter += 1 posx[counter] = posx[current] posy[counter] = posy[current]-1 END IF IF groups[posx[current],posy[current]+1] = group THEN groups[posx[current],posy[current]+1] = EMPTY board[posx[current],posy[current]+1] = EMPTY counter += 1 posx[counter] = posx[current] posy[counter] = posy[current]+1 END IF current += 1 LOOP UNTIL current > counter v_canvas_clear(canvas) CALL Draw_Board total += counter END IF END IF NEXT i NEXT j IF total > 0 THEN v_status_set_text(gostatus, total &" stones captured!") IF board[lastmovex, lastmovey] = WHITE THEN capturedbywhite += total IF board[lastmovex, lastmovey] = BLACK THEN capturedbyblack += total END SUB