baduk

package module
v0.0.0-...-661f1b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2017 License: MIT Imports: 7 Imported by: 1

README

Baduk Readme

Yo dawg, I heard you liked Go, so I put Go in your Go so you could Go on the Go.

What?

I like Go (the game, also known as Baduk) and Go (the programming language) and decided to build this support library so I could build a Go-webapp to play Go (hence the Yo-dawging).

But mainly, I built this with the express (and eventual) purpose to play Go with my former (and future?) roommate, and he's much more amenable to my ideas when they involve wordplay, puns, recursive thinking, and self-references. (hi Steve!)

Further down the line, once Go reaches 1.5, I'm even going to venture making a Android app. So you can Go on the Go on the Go. :D

Usage

Import the package like any remote repo in Go:

import "github.com/acityinohio/baduk"

Then initialize a baduk.Board

var b baduk.Board
err := b.Init(13) //Size can be anywhere from 4 to 19, inclusive
//don't forget your errors!
if err != nil {
	//deal with err
}

You can set pieces using (x,y) coordinates, anywhere from (0,0) to (size-1, size-1). The origin (0,0) is considered the top left of the board. Setting pieces will automatically capture other chains (first checking the opponent's chains, then your own).

var err error
err = b.SetB(0,0)   //Sets black piece at 0,0
err = b.SetW(12,12) //Sets white piece at 12,12
if err != nil {
	//dealWithIt.gif
}
//You can even print it prettily on the terminal!
fmt.Printf(b.PrettyString()) 
//And get a score of the board if you'd like
fmt.Printf(b.ScorePretty())

My favorite part (and what will help eventually with the whole web app shtick) is that every state of the board can be Encoded/Decoded into a compressed, URL-friendly base64-encoded string. Check it:

enc, err := b.Encode() //Encodes URL-safe string into enc
if err != nil {
	//dealWithIt.gif
}
fmt.Println(enc) //Now do whatever you want with the string
var c baduk.Board
err = c.Decode(enc) //...like create a new board with the same state

This will be super useful for ephemeral, webapp-based games with canonical URLs.

For more details about the package, I've sprinkled comments throughout it like a good idomatic Gopher, which means nice GoDocs. You can check them out here.

Testing

You can test the package with the handy "go test" command---the tests are included in the baduk_tests.go package.

Why not call this package Go?

Yes, we all get off on Xzibit memes, but that goes too far, even for me. Think of the name collisions/children.

Documentation

Overview

Package baduk implements a library for playing games of Baduk/Go. It's optimized for code simplicity, and doesn't include any AI support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

type Board struct {
	Size int
	Grid [][]Piece
}

A Board represents information about the state of a Go game. Size represents the size of the board, Grid is the storage of Pieces.

func (*Board) Decode

func (b *Board) Decode(str string) (err error)

Initializes a Board from a URL-safe string encoded with Board.Encode

func (*Board) Encode

func (b *Board) Encode() (enc string, err error)

Encodes the Board state into a compressed, base64-encoded URL-safe string enc.

func (*Board) Init

func (b *Board) Init(size int) (err error)

Initializes an empty Board

func (*Board) PrettySVG

func (b *Board) PrettySVG() (svg string)

Creates a string representing an SVG view of the board, suitable for use inline in web templates. Wrap in an external div with particular width/height in CSS to control size. Yay for resolution independence!

func (*Board) PrettyString

func (b *Board) PrettyString() (str string)

Creates pretty string, suitable for use by fmt.Printf or any logging functions. Note that black and white circles are reversed compared to their unicode code points; this assumes your terminal has a dark background.

func (*Board) Score

func (b *Board) Score() (black, white int)

Scores the Board. Scoring follows this algorithm: A color's score = total pieces + total empty pieces completely enclosed by said color. If empty pieces are enclosed by both colors, then empty territory is contested and not added to either score. This method is consistent with a simple version of Chinese area scoring.

func (*Board) ScorePretty

func (b *Board) ScorePretty() (str string)

Makes a string suitable for Sprintf output that declares the winner

func (*Board) SetB

func (b *Board) SetB(x, y int) (err error)

Sets a Piece to black on the Board x, y in range from 1 to Board.Size

func (*Board) SetW

func (b *Board) SetW(x, y int) (err error)

Sets a Piece to white on the Board x, y in range from 1 to Board.Size

type Piece

type Piece struct {
	Black bool
	White bool
	Empty bool
	Up    *Piece //y-1
	Down  *Piece //y+1
	Left  *Piece //x-1
	Right *Piece //x+1
}

A Piece represents information about a piece on the Board. Contains pointers to adjacent pieces. If it's a border, the pointer is nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL