Microsoft Small Basic

Program Listing: XMZ657-1
' Goban SVG Genarator
' Version 0.12
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-11-19
' Program ID XMZ657-1
'
GraphicsWindow.Title = "Goban SVG Generator 0.12"
LT = "<"
WQ = Text.GetCharacter(34)
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
ro = 9
If ro <= 9 Then
gw = 640
gh = 480
Else
gw = 800
gh = 600
EndIf
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
path = Program.Directory
path = "http://www.nonkit.com/svg"
imgt = ImageList.LoadImage(path + "/img/table800.jpg")
imgbb = ImageList.LoadImage(path + "/img/blackbowl134.png")
imgwb = ImageList.LoadImage(path + "/img/whitebowl134.png")
imgl = ImageList.LoadImage(path + "/img/lid110.png")
imgb = ImageList.LoadImage(path + "/img/black22.png")
imgw = ImageList.LoadImage(path + "/img/white22.png")
imgs = ImageList.LoadImage(path + "/img/star5.png")
PIXEL_COL = 22
PIXEL_ROW = 24
PX_BOWL = 134 ' diameter of bowl [pixel]
Z2_BOWL = 20 ' height of bowl / 2 [pixel]
PX_LID = 110 ' diameter of lid [pixel]
Z2_LID = 8 ' height of lid / 2 [pixel]
PX_STONE = 22 ' diameter of stone [pixel]
Z2_STONE = 2 ' height of stone / 2 [pixel]
PX_STAR = 5 ' diameter of star [pixel]
PX_CHAR = 24 ' height of character [pixel]
Z2_BOARD = 6 ' height of board / 2 [pixel]
boardSize["width"] = PIXEL_COL * (ro + 1)
boardSize["height"] = PIXEL_ROW * (ro + 1)
boardOffset["x"] = Math.Floor((gw - boardSize["width"]) / 2)
boardOffset["y"] = Math.Floor((gh - boardSize["height"]) / 2)
gridSize["width"] = PIXEL_COL * (ro - 1) + 2
gridSize["height"] = PIXEL_ROW * (ro - 1) + 2
gridOffset["x"] = Math.Floor((boardSize["width"] - gridSize["width"]) / 2)
gridOffset["y"] = Math.Floor((boardSize["height"] - gridSize["height"]) / 2)
x = boardOffset["x"]
y = boardOffset["y"]
dx = PIXEL_COL
dy = PIXEL_ROW
width = boardSize["width"]
height = boardSize["height"]
gwidth = gridSize["width"]
gheight = gridSize["height"]
gx = x + gridOffset["x"]
gy = y + gridOffset["y"]
html = LT + "!DOCTYPE html>" + CRLF
html = html + LT + "html>" + CRLF
html = html + LT + "head>" + CRLF
html = html + LT + "meta charset=" + WQ + "UTF-8" + WQ + ">" + CRLF
html = html + LT + "style type=" + WQ + "text/css" + WQ + ">" + CRLF
html = html + ".table {" + CRLF
html = html + " background-image: url(img/table800.jpg);" + CRLF
html = html + "}" + CRLF
html = html + ".shadow {" + CRLF
html = html + " fill: rgb(0, 0, 0);" + CRLF
html = html + " fill-opacity: 0.5;" + CRLF
html = html + " stroke-width: 0;" + CRLF
html = html + "}" + CRLF
html = html + LT + "/style>" + CRLF
html = html + LT + "/head>" + CRLF
html = html + LT + "body>" + CRLF
' draw table
GraphicsWindow.DrawImage(imgt, 0, 0)
svg = LT + "svg class=" + WQ + "table" + WQ + " width=" + WQ + gw + WQ + " height=" + WQ + gh + WQ + ">" + CRLF
' draw board
svg = svg + " " + LT + "defs>" + CRLF
svg = svg + " " + LT + "clipPath id=" + WQ + "clip" + WQ + ">" + CRLF
svg = svg + " " + LT + "rect x=" + WQ + x + WQ + " y=" + WQ + y + WQ
svg = svg + " width=" + WQ + width + WQ + " height=" + WQ + height + WQ + " />" + CRLF
svg = svg + " " + LT + "/clipPath>" + CRLF
svg = svg + " " + LT + "/defs>" + CRLF
s = Z2_BOARD
DrawShadow()
GraphicsWindow.BrushColor = "Burlywood"
GraphicsWindow.FillRectangle(x, y, width, height)
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/woodgrain600.png" + WQ
svg = svg + " clip-path=" + WQ + "url(#clip)" + WQ + " width=" + WQ + "600" + WQ
left = Math.Floor((600 - width) * 2 / 3)
top = 600 - height
svg = svg + " height=" + WQ + "600" + WQ + " x=" + WQ + (x - left) + WQ
svg = svg + " y=" + WQ + (y - top) + WQ + " />" + CRLF
DrawGrid()
' draw a white lid of bowl on the left of the board
offsetWLid["x"] = Math.Floor((boardOffset["x"] - PX_LID) / 2)
offsetWLid["y"] = boardOffset["y"] + Math.Floor((boardSize["height"] - PX_LID) / 2)
GraphicsWindow.BrushColor = "#80000000" ' for shadow
GraphicsWindow.FillEllipse(offsetWLid["x"] + Z2_LID, offsetWLid["y"] + Z2_LID, PX_LID, PX_LID)
cx = offsetWLid["x"] + Z2_LID + PX_LID / 2
cy = offsetWLid["y"] + Z2_LID + PX_LID / 2
svg = svg + " " + LT + "ellipse class=" + WQ + "shadow" + WQ + " cx=" + WQ + cx + WQ + " cy=" + WQ + cy + WQ
svg = svg + " rx=" + WQ + (PX_LID / 2) + WQ + " ry=" + WQ + (PX_LID / 2) + WQ + " />" + CRLF
GraphicsWindow.DrawImage(imgl, offsetWLid["x"], offsetWLid["y"])
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/lid110.png" + WQ
svg = svg + " width=" + WQ + PX_LID + WQ + " height=" + WQ + PX_LID + WQ
svg = svg + " x=" + WQ + offsetWLid["x"] + WQ
svg = svg + " y=" + WQ + offsetWLid["y"] + WQ + " />" + CRLF
' draw a white bowl on the top left corner
xb = offsetWLid["x"] - Math.Floor((PX_BOWL - PX_LID) / 2)
yb = offsetWLid["y"] - PX_BOWL - Z2_BOWL
GraphicsWindow.FillEllipse(xb + Z2_BOWL, yb + Z2_BOWL, PX_BOWL, PX_BOWL)
cx = xb + Z2_BOWL + PX_BOWL / 2
cy = yb + Z2_BOWL + PX_BOWL / 2
svg = svg + " " + LT + "ellipse class=" + WQ + "shadow" + WQ + " cx=" + WQ + cx + WQ + " cy=" + WQ + cy + WQ
svg = svg + " rx=" + WQ + (PX_BOWL / 2) + WQ + " ry=" + WQ + (PX_BOWL / 2) + WQ + " />" + CRLF
GraphicsWindow.DrawImage(imgwb, xb, yb)
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/whitebowl134.png" + WQ
svg = svg + " width=" + WQ + PX_BOWL + WQ + " height=" + WQ + PX_BOWL + WQ
svg = svg + " x=" + WQ + xb + WQ
svg = svg + " y=" + WQ + yb + WQ + " />" + CRLF
' draw a black lid of bowl on the right of the board
offsetBLid["x"] = offsetWLid["x"] + boardOffset["x"] + boardSize["width"]
offsetBLid["y"] = offsetWLid["y"]
GraphicsWindow.FillEllipse(offsetBLid["x"] + Z2_LID, offsetBLid["y"] + Z2_LID, PX_LID, PX_LID)
cx = offsetBLid["x"] + Z2_LID + PX_LID / 2
cy = offsetBLid["y"] + Z2_LID + PX_LID / 2
svg = svg + " " + LT + "ellipse class=" + WQ + "shadow" + WQ + " cx=" + WQ + cx + WQ + " cy=" + WQ + cy + WQ
svg = svg + " rx=" + WQ + (PX_LID / 2) + WQ + " ry=" + WQ + (PX_LID / 2) + WQ + " />" + CRLF
GraphicsWindow.DrawImage(imgl, offsetBLid["x"], offsetBLid["y"])
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/lid110.png" + WQ
svg = svg + " width=" + WQ + PX_LID + WQ + " height=" + WQ + PX_LID + WQ
svg = svg + " x=" + WQ + offsetBLid["x"] + WQ
svg = svg + " y=" + WQ + offsetBLid["y"] + WQ + " />" + CRLF
' draw a black bowl on the bottom right corner
xb = offsetBLid["x"] - Math.Floor((PX_BOWL - PX_LID) / 2)
yb = offsetBLid["y"] + PX_LID + Z2_BOWL
GraphicsWindow.FillEllipse(xb + Z2_BOWL, yb + Z2_BOWL, PX_BOWL, PX_BOWL)
cx = xb + Z2_BOWL + PX_BOWL / 2
cy = yb + Z2_BOWL + PX_BOWL / 2
svg = svg + " " + LT + "ellipse class=" + WQ + "shadow" + WQ + " cx=" + WQ + cx + WQ + " cy=" + WQ + cy + WQ
svg = svg + " rx=" + WQ + (PX_BOWL / 2) + WQ + " ry=" + WQ + (PX_BOWL / 2) + WQ + " />" + CRLF
GraphicsWindow.DrawImage(imgbb, xb, yb)
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/blackbowl134.png" + WQ
svg = svg + " width=" + WQ + PX_BOWL + WQ + " height=" + WQ + PX_BOWL + WQ
svg = svg + " x=" + WQ + xb + WQ
svg = svg + " y=" + WQ + yb + WQ + " />" + CRLF
' draw stones
param = "col="+ (ro - 2) + ";row=" + (ro - 2) + ";color=black;"
DrawStone()
param = "col=3;row=3;color=white;"
DrawStone()
svg = svg + LT + "/svg>" + CRLF
' show HTML
html = html + svg
html = html + LT + "/body>" + CRLF
html = html + LT + "/html>"
GraphicsWindow.BrushColor = "Black"
tbox = Controls.AddMultiLineTextBox(10, 10)
Controls.SetSize(tbox, gw - 20, gh - 20)
Shapes.SetOpacity(tbox, 50)
Controls.SetTextBoxText(tbox, html)
Sub DrawShadow
' param s
GraphicsWindow.BrushColor = "#80000000"
px = "1=" + x + ";2=" + (x + width) + ";3=" + (x + width + s) + ";"
px = px + "4=" + (x + width + s) + ";5=" + (x + s) + ";"
px = px + "6=" + x + ";"
py = "1=" + y + ";2=" + y + ";3=" + (y + s) + ";"
py = py + "4=" + (y + height + s) + ";5=" + (y + height + s) + ";"
py = py + "6=" + (y + height) + ";"
GraphicsWindow.FillTriangle(px[2], py[2], px[3], py[3], px[2], py[3])
GraphicsWindow.FillRectangle(px[5], py[3], width, height)
GraphicsWindow.FillTriangle(px[6], py[6], px[5], py[5], px[5], py[6])
svg = svg + " " + LT + "polygon class=" + WQ + "shadow" + WQ + " points=" + WQ
svg = svg + px[1] + "," + py[1] + " " + px[2] + "," + py[2] + " "
svg = svg + px[3] + "," + py[3] + " " + px[4] + "," + py[4] + " "
svg = svg + px[5] + "," + py[5] + " " + px[6] + "," + py[6]
svg = svg + WQ + " />" + CRLF
EndSub
Sub DrawGrid
GraphicsWindow.BrushColor = "Black"
' (horizontal grid)
x1 = gx
For row = 1 To ro
If (row = 1) Then
y1 = gy + (row - 1) * dy
Else
y1 = gy + 1 + (row - 1) * dy
EndIf
If (row = 1) Or (row = ro) Then
lwidth = 2
Else
lwidth = 1
EndIf
GraphicsWindow.FillRectangle(x1, y1, gwidth, lwidth)
svg = svg + " " + LT + "rect x=" + WQ + x1 + WQ + " y=" + WQ + y1 + WQ
svg = svg + " width=" + WQ + gwidth + WQ + " height=" + WQ + lwidth + WQ + " />" + CRLF
EndFor
' (vertical grid)
y1 = gy
For col = 1 To ro
If (col = 1) Then
x1 = gx + (col - 1) * dx
Else
x1 = gx + 1 + (col - 1) * dx
EndIf
If (col = 1) Or (col = ro) Then
lwidth = 2
Else
lwidth = 1
EndIf
GraphicsWindow.FillRectangle(x1, y1, lwidth, gheight)
svg = svg + " " + LT + "rect x=" + WQ + x1 + WQ + " y=" + WQ + y1 + WQ
svg = svg + " width=" + WQ + lwidth + WQ + " height=" + WQ + gheight + WQ + " />" + CRLF
EndFor
If (Math.Remainder(ro, 2) = 1) Then
' tengen
col = Math.Floor(ro / 2) + 1
row = Math.Floor(ro / 2) + 1
DrawStar()
EndIf
If (ro = 9) Then
' stars on 3rd line corner
col = 3
row = 3
DrawStar()
col = ro - 2
DrawStar()
row = ro - 2
DrawStar()
col = 3
DrawStar()
EndIf
If (ro = 13) Or (ro = 19) Then
' stars on 4th line corner
col = 4
row = 4
DrawStar()
col = ro - 3
DrawStar()
row = ro - 3
DrawStar()
col = 4
DrawStar()
EndIf
if (ro = 19) Then
' stars of 4th line edge
col = Math.Floor(ro / 2) + 1
row = 4
DrawStar()
col = 4
row = Math.floor(ro / 2) + 1
DrawStar()
col = ro - 3
row = Math.floor(ro / 2) + 1
DrawStar()
col = Math.floor(ro / 2) + 1
row = ro - 3
DrawStar()
EndIf
EndSub
Sub DrawStar
' param col, row - position to draw star
x = boardOffset["x"] + gridOffset["x"] + 1 + (col - 1) * PIXEL_COL - 2
y = boardOffset["y"] + gridOffset["y"] + 1 + (row - 1) * PIXEL_ROW - 2
GraphicsWindow.DrawImage(imgs, x, y)
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/star5.png" + WQ
svg = svg + " width=" + WQ + PX_STAR + WQ + " height=" + WQ + PX_STAR + WQ
svg = svg + " x=" + WQ + x + WQ + " y=" + WQ + y + WQ + " />" + CRLF
EndSub
Sub DrawStone
' param["col"], param["row"] - position to move stone
' param["color"] - stone color
x = boardOffset["x"] + gridOffset["x"] + 1 + (param["col"] - 1) * PIXEL_COL - PX_STONE / 2
y = boardOffset["y"] + gridOffset["y"] + 1 + (param["row"] - 1) * PIXEL_ROW - PX_STONE / 2
GraphicsWindow.BrushColor = "#80000000"
GraphicsWindow.FillEllipse(x + Z2_STONE, y + Z2_STONE, PX_STONE, PX_STONE)
cx = x + PX_STONE / 2 + Z2_STONE
cy = y + PX_STONE / 2 + Z2_STONE
svg = svg + " " + LT + "ellipse class=" + WQ + "shadow" + WQ + " cx=" + WQ + cx + WQ + " cy=" + WQ + cy + WQ
svg = svg + " rx=" + WQ + (PX_STONE / 2) + WQ + " ry=" + WQ + (PX_STONE / 2) + WQ + " />" + CRLF
If param["color"] = "black" Then
GraphicsWindow.DrawImage(imgb, x, y)
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/black22.png" + WQ
Else
GraphicsWindow.DrawImage(imgw, x, y)
svg = svg + " " + LT + "image xlink:href=" + WQ + "img/white22.png" + WQ
EndIf
svg = svg + " width=" + WQ + PX_STONE + WQ + " height=" + WQ + PX_STONE + WQ
svg = svg + " x=" + WQ + x + WQ + " y=" + WQ + y + WQ + " />" + CRLF
EndSub