| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package main |
| 14 |
|
| 15 |
import ( |
| 16 |
"image" |
| 17 |
"image/color" |
| 18 |
_ "image/jpeg" |
| 19 |
_ "image/png" |
| 20 |
"log" |
| 21 |
"math/rand" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"strings" |
| 25 |
|
| 26 |
"golang.org/x/image/draw" |
| 27 |
) |
| 28 |
|
| 29 |
const maxBoard = 21 |
| 30 |
|
| 31 |
var ZP = image.ZP |
| 32 |
|
| 33 |
type stoneColor uint8 |
| 34 |
|
| 35 |
const ( |
| 36 |
blank stoneColor = iota |
| 37 |
black |
| 38 |
white |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
type Piece struct { |
| 44 |
stone *Stone |
| 45 |
ij IJ |
| 46 |
delta image.Point |
| 47 |
color stoneColor |
| 48 |
} |
| 49 |
|
| 50 |
type Board struct { |
| 51 |
Dims |
| 52 |
pieces []*Piece |
| 53 |
image *image.RGBA |
| 54 |
stone []Stone |
| 55 |
numBlackStones int |
| 56 |
numWhiteStones int |
| 57 |
} |
| 58 |
|
| 59 |
type Stone struct { |
| 60 |
originalImage *image.RGBA |
| 61 |
originalMask *image.Alpha |
| 62 |
image *image.RGBA |
| 63 |
mask *image.Alpha |
| 64 |
} |
| 65 |
|
| 66 |
func NewBoard(dim, percent int) *Board { |
| 67 |
switch dim { |
| 68 |
case 9, 13, 19, 21: |
| 69 |
default: |
| 70 |
return nil |
| 71 |
} |
| 72 |
boardTexture := get("goboard.jpg", 0) |
| 73 |
b := new(Board) |
| 74 |
b.Dims.Init(dim, 100) |
| 75 |
b.pieces = make([]*Piece, maxBoard*maxBoard) |
| 76 |
b.image = image.NewRGBA(boardTexture.Bounds()) |
| 77 |
draw.Draw(b.image, b.image.Bounds(), boardTexture, ZP, draw.Src) |
| 78 |
dir, err := os.Open("asset") |
| 79 |
if err != nil { |
| 80 |
log.Fatal(err) |
| 81 |
} |
| 82 |
defer dir.Close() |
| 83 |
names, err := dir.Readdirnames(0) |
| 84 |
if err != nil { |
| 85 |
log.Fatal(err) |
| 86 |
} |
| 87 |
circleMask := makeCircle() |
| 88 |
|
| 89 |
for _, name := range names { |
| 90 |
if strings.HasPrefix(name, "blackstone") { |
| 91 |
s, m := makeStone(name, circleMask) |
| 92 |
b.stone = append(b.stone, Stone{s, m, nil, nil}) |
| 93 |
b.numBlackStones++ |
| 94 |
} |
| 95 |
} |
| 96 |
for _, name := range names { |
| 97 |
if strings.HasPrefix(name, "whitestone") { |
| 98 |
s, m := makeStone(name, circleMask) |
| 99 |
b.stone = append(b.stone, Stone{s, m, nil, nil}) |
| 100 |
b.numWhiteStones++ |
| 101 |
} |
| 102 |
} |
| 103 |
b.Resize(percent) |
| 104 |
return b |
| 105 |
} |
| 106 |
|
| 107 |
func (b *Board) Resize(percent int) { |
| 108 |
b.Dims.Resize(percent) |
| 109 |
for i := range b.stone { |
| 110 |
stone := &b.stone[i] |
| 111 |
stone.image = resizeRGBA(stone.originalImage, b.stoneDiam) |
| 112 |
stone.mask = resizeAlpha(stone.originalMask, b.stoneDiam) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func resizeRGBA(src *image.RGBA, size int) *image.RGBA { |
| 117 |
dst := image.NewRGBA(image.Rect(0, 0, size, size)) |
| 118 |
draw.ApproxBiLinear.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Src, nil) |
| 119 |
return dst |
| 120 |
} |
| 121 |
|
| 122 |
func resizeAlpha(src *image.Alpha, size int) *image.Alpha { |
| 123 |
dst := image.NewAlpha(image.Rect(0, 0, size, size)) |
| 124 |
draw.ApproxBiLinear.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Src, nil) |
| 125 |
return dst |
| 126 |
} |
| 127 |
|
| 128 |
func (b *Board) piece(ij IJ) *Piece { |
| 129 |
return b.pieces[(ij.j-1)*b.Dims.dim+ij.i-1] |
| 130 |
} |
| 131 |
|
| 132 |
func jitter() int { |
| 133 |
max := 25 * *scale / 100 |
| 134 |
if max&1 == 0 { |
| 135 |
max++ |
| 136 |
} |
| 137 |
return rand.Intn(max) - max/2 |
| 138 |
} |
| 139 |
|
| 140 |
func (b *Board) putPiece(ij IJ, piece *Piece) { |
| 141 |
b.pieces[(ij.j-1)*b.Dims.dim+ij.i-1] = piece |
| 142 |
if piece != nil { |
| 143 |
piece.ij = ij |
| 144 |
piece.delta = image.Point{jitter(), jitter()} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func (b *Board) selectBlackPiece() *Piece { |
| 149 |
return &Piece{ |
| 150 |
stone: &b.stone[rand.Intn(b.numBlackStones)], |
| 151 |
color: black, |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func (b *Board) selectWhitePiece() *Piece { |
| 156 |
return &Piece{ |
| 157 |
stone: &b.stone[b.numBlackStones+rand.Intn(b.numWhiteStones)], |
| 158 |
color: white, |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func makeStone(name string, circleMask *image.Alpha) (*image.RGBA, *image.Alpha) { |
| 163 |
stone := get(name, stoneSize0) |
| 164 |
dst := image.NewRGBA(stone.Bounds()) |
| 165 |
|
| 166 |
draw.Draw(dst, dst.Bounds(), image.Black, ZP, draw.Src) |
| 167 |
|
| 168 |
draw.DrawMask(dst, dst.Bounds(), stone, ZP, circleMask, ZP, draw.Over) |
| 169 |
return dst, makeShadowMask(stone) |
| 170 |
} |
| 171 |
|
| 172 |
func get(name string, size int) image.Image { |
| 173 |
f, err := os.Open(filepath.Join("asset", name)) |
| 174 |
if err != nil { |
| 175 |
log.Fatal(err) |
| 176 |
} |
| 177 |
i, _, err := image.Decode(f) |
| 178 |
if err != nil { |
| 179 |
log.Fatal(err) |
| 180 |
} |
| 181 |
f.Close() |
| 182 |
if size != 0 { |
| 183 |
r := i.Bounds() |
| 184 |
if r.Dx() != size || r.Dy() != size { |
| 185 |
log.Fatalf("bad stone size %s for %s; must be %d[2]×%d[2]", r, name, size) |
| 186 |
} |
| 187 |
} |
| 188 |
return i |
| 189 |
} |
| 190 |
|
| 191 |
func makeCircle() *image.Alpha { |
| 192 |
mask := image.NewAlpha(image.Rect(0, 0, stoneSize0, stoneSize0)) |
| 193 |
|
| 194 |
|
| 195 |
for y := 0; y < stoneSize0; y++ { |
| 196 |
y2 := stoneSize0/2 - y |
| 197 |
y2 *= y2 |
| 198 |
for x := 0; x < stoneSize0; x++ { |
| 199 |
x2 := stoneSize0/2 - x |
| 200 |
x2 *= x2 |
| 201 |
if x2+y2 <= stoneRad2 { |
| 202 |
mask.SetAlpha(x, y, color.Alpha{255}) |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
return mask |
| 207 |
} |
| 208 |
|
| 209 |
func makeShadowMask(stone image.Image) *image.Alpha { |
| 210 |
mask := image.NewAlpha(stone.Bounds()) |
| 211 |
|
| 212 |
|
| 213 |
const size = 256 |
| 214 |
const diam = 225 |
| 215 |
for y := 0; y < size; y++ { |
| 216 |
y2 := size/2 - y |
| 217 |
y2 *= y2 |
| 218 |
for x := 0; x < size; x++ { |
| 219 |
x2 := size/2 - x |
| 220 |
x2 *= x2 |
| 221 |
if x2+y2 > stoneRad2 { |
| 222 |
red, _, _, _ := stone.At(x, y).RGBA() |
| 223 |
mask.SetAlpha(x, y, color.Alpha{255 - uint8(red>>8)}) |
| 224 |
} else { |
| 225 |
mask.SetAlpha(x, y, color.Alpha{255}) |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
return mask |
| 230 |
} |
| 231 |
|
| 232 |
func (b *Board) Draw(m *image.RGBA) { |
| 233 |
r := b.image.Bounds() |
| 234 |
draw.Draw(m, r, b.image, ZP, draw.Src) |
| 235 |
|
| 236 |
x := b.xInset + b.squareWidth/2 |
| 237 |
y := b.yInset + b.squareHeight/2 |
| 238 |
wid := b.lineWidth |
| 239 |
for i := 0; i < b.dim; i++ { |
| 240 |
r := image.Rect(x, y, x+wid, y+(b.dim-1)*b.squareHeight) |
| 241 |
draw.Draw(m, r, image.Black, ZP, draw.Src) |
| 242 |
x += b.squareWidth |
| 243 |
} |
| 244 |
|
| 245 |
x = b.xInset + b.squareWidth/2 |
| 246 |
for i := 0; i < b.dim; i++ { |
| 247 |
r := image.Rect(x, y, x+(b.dim-1)*b.squareWidth+wid, y+wid) |
| 248 |
draw.Draw(m, r, image.Black, ZP, draw.Src) |
| 249 |
y += b.squareHeight |
| 250 |
} |
| 251 |
|
| 252 |
spot := 4 |
| 253 |
if b.dim < 13 { |
| 254 |
spot = 3 |
| 255 |
} |
| 256 |
points := []IJ{ |
| 257 |
{spot, spot}, |
| 258 |
{spot, (b.dim + 1) / 2}, |
| 259 |
{spot, b.dim + 1 - spot}, |
| 260 |
{(b.dim + 1) / 2, spot}, |
| 261 |
{(b.dim + 1) / 2, (b.dim + 1) / 2}, |
| 262 |
{(b.dim + 1) / 2, b.dim + 1 - spot}, |
| 263 |
{b.dim + 1 - spot, spot}, |
| 264 |
{b.dim + 1 - spot, (b.dim + 1) / 2}, |
| 265 |
{b.dim + 1 - spot, b.dim + 1 - spot}, |
| 266 |
} |
| 267 |
for _, ij := range points { |
| 268 |
b.drawPoint(m, ij) |
| 269 |
} |
| 270 |
|
| 271 |
for i := 1; i <= b.dim; i++ { |
| 272 |
for j := 1; j <= b.dim; j++ { |
| 273 |
ij := IJ{i, j} |
| 274 |
if p := b.piece(ij); p != nil { |
| 275 |
b.drawPiece(m, ij, p) |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func (b *Board) drawPoint(m *image.RGBA, ij IJ) { |
| 282 |
pt := ij.XYCenter(&b.Dims) |
| 283 |
wid := b.lineWidth |
| 284 |
sz := wid * 3 / 2 |
| 285 |
r := image.Rect(pt.x-sz, pt.y-sz, pt.x+wid+sz, pt.y+wid+sz) |
| 286 |
draw.Draw(m, r, image.Black, ZP, draw.Src) |
| 287 |
} |
| 288 |
|
| 289 |
func (b *Board) drawPiece(m *image.RGBA, ij IJ, piece *Piece) { |
| 290 |
xy := ij.XYStone(&b.Dims) |
| 291 |
xy = xy.Add(piece.delta) |
| 292 |
draw.DrawMask(m, xy, piece.stone.image, ZP, piece.stone.mask, ZP, draw.Over) |
| 293 |
} |
| 294 |
|
| 295 |
func (b *Board) click(m *image.RGBA, x, y, button int) bool { |
| 296 |
ij, ok := XY{x, y}.IJ(&b.Dims) |
| 297 |
if !ok { |
| 298 |
return false |
| 299 |
} |
| 300 |
switch button { |
| 301 |
default: |
| 302 |
return false |
| 303 |
case 1: |
| 304 |
b.putPiece(ij, b.selectBlackPiece()) |
| 305 |
case 2: |
| 306 |
b.putPiece(ij, b.selectWhitePiece()) |
| 307 |
case 3: |
| 308 |
b.putPiece(ij, nil) |
| 309 |
} |
| 310 |
render(m, b) |
| 311 |
return true |
| 312 |
} |