Giter Site home page Giter Site logo

gosharp's Introduction

GoSharp

A C# class library for the game of Go (a.k.a Igo, Weiqi, Baduk).

Table of Content

Starting a New Game

To start a new game, create a GameInfo object, specify the handicap, the board size, the starting player and the komi in it. Then create a Game object using the GameInfo parameter constructor.

Example:

GameInfo gi = new GameInfo() { Handicap = 2, StartingPlayer = Content.White };
Game game = new Game(gi);

Making a Move

To make a move, call the MakeMove method on a game object. The method will return a new game object representing the state of the game after the move. The current implementation accepts all moves, including suicide, overwrite and super-ko violating moves.

As shown, there are overloads of the MakeMove method which set an out bool parameter with true or false depending on the legality of the move.

Example:

bool legal;
Game postMove = game.MakeMove(3, 3, out legal);

Variations

To start a variation sub-game, call MakeMove on any Game node in the tree. All variations are recorded in the game tree, and may be exported to SGF.

Setting Up a Position

To set up a board position without going through all the moves, you can use setup moves instead. Setup moves are not checked for legality; they can overwrite, create groups with no liberties, etc. They are used, for example, to set up a life-and-death go problem, or to show common patterns that appear in game. To use setup moves call the SetupMove method on a game object.

Example 1: Setting up a free-placed handicap:

GameInfo gi = new GameInfo() { StartingPlayer = Content.White };
Game g = new Game(gi);
g.SetupMove(3, 3, Content.Black); // Place a black stone on the lower left star point.
g.SetupMove(9, 9, Content.Black); // Place a black stone in the center of the board.
g.SetupMove(3, 15, Content.Black); // Place a black stone in the upper left star point.
Console.WriteLine(g.SerializeToSGF(null));

When exporting to SGF, the setup moves will appear correctly:

(;FF[4]PL[W]AB[dd][jj][dp])

gosharp's People

Contributors

nerai avatar paviad avatar yazgoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gosharp's Issues

Confused what this library is for

I'm new to the world of GO game programming. I was looking for a GO engine written in C# to use in a Unity Android app and found this project. Is this project just for creating GO protocol strings? or is it an engine? or just to see if a move is valid?

License

There is currently no license set. I suggest adding a permissive one like MIT.

Handle different charsets

Currently, the library assumes ASCII at all times. (I hope I did not miss anything.) Out of interest, I just checked and KGS seems to store SGFs in UTF8.

It misses the CA property of the SGF standard.

Property:	CA
Propvalue:	simpletext
Propertytype:	root
Function:	Provides the used charset for SimpleText and Text type.
		Default value is 'ISO-8859-1' aka 'Latin1'.
		Only charset names (or their aliases) as specified in RFC 1345
		(or updates thereof) are allowed.
		Basically this field uses the same names as MIME messages in
		their 'charset=' field (in Content-Type).
		RFC's can be obtained via FTP from DS.INTERNIC.NET,
		NIS.NSF.NET, WUARCHIVE.WUSTL.EDU, SRC.DOC.IC.AC.UK
		or FTP.IMAG.FR.

I am currently working on this issue for another project. If I find the time I will create a PR.

Capture count not correct in all cases

Sometime when capturing a large group the stone count is wrong, noticed when captured 14 stones. Had 8 captured, the variable said I had captured 31 not 22.

Whitespace

My usual whitespace settings are completely different from what is mainly used in this codebase, so it is a bit cumbersome sometimes. I'm not sure how you feel about this @paviad, so I would like to ask for clarification what should be kept as is and what may be changed. I'm willing to adapt.

  1. Should tabs be used for indentation (but not formatting)?
    I definitely think so, but I am not too religious about it.

  2. Line breaks unix style (LF) or windows style (CRLF)?
    I love LF, which is currently used. Some commonly used tools sometimes break with them, but it seems it was mostly fixed in VS 2017, so I'd suggest keeping it like this.

  3. Space before opening parenthesis?
    foo (2) instead of foo(2)
    It is not currently applied. I find it easier to read.

  4. Some braces on same line?
    Currently all braces are on a new line. A more dense format (braces on new line only for classes and methods but not if, for, lambdas etc.) seems easier to read to me. Especially as there are no long, complex lines in this project that would need space around them to be readable.

  5. UTF8 BOM
    UTF8 does not require a byte order marker. I suggest removing it.

Regardless of the decisions, it would be useful to have the relevant settings available in the repo so they can be imported into VS (and CodeMaid) easily. I can submit these for VS17. I'm currently abroad so I don't have VS13 and 15 with me.

Request, Disable Illegal Move

Hi, We continue to use GoSharp.

If a MakeMoves is illegal, I would like a method that can safely undo it ,
or a method that doesn't record the move in the first place if it's illegal.

Question about sgf support

Been using the sgf loader to succesfully load both tsumego files and pro-games. Are any of the tags saved somewhere such as player names, result etc? Also node comments with specific intersections that should be marked?

Thanks for the great work on this. Really usefull!

Cannot have two branches both beginning with pass move

The Game class stores moves in a dictionary. If two moves map to the same key, this leads to an exception.

Passes use a singleton instance as key, so they are affected.

Regular moves use custom Point objects, which override GetHashCode but not equals, so they are not affected. I'm not sure if this is an unintended side effect.

Example SGF:

(;GM[1]FF[4]CA[UTF-8]AP[Kibble:0]ST[2]
SZ[19]
;B[]
(;W[]C[first])
(;W[]C[second])
)

Bug in capture

Just wanted to let you know that there looks to be a bug in the capture code. When capturing by playing on a point with no liberties both black and white stones are removed.

SetUpMoves reset problem in restoring the board from SGF.

Hi, paviad.
I found a problem with restoring the board from SGF.

If SetUpMoves is done on the first board and there are no branches in SGF, it works fine, but if there is a branch move at Root, the stones set by SetUpMoves disappear and the board is reset. (Board._content becomes Empty).
I would appreciate a correction.

Work well

(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2022-11-17]AB[aa][ab][ac][ad][ae][af][ag][ah][ai][aj]AE[br][bs]AW[ao][an][al][am][as][ar][aq][ap][ak];B[dj];W[fj];B[hj];W[jj])

Bug(add variant into root)

(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2022-11-17]AB[aa][ab][ac][ad][ae][af][ag][ah][ai][aj]AE[br][bs]AW[ao][an][al][am][as][ar][aq][ap][ak](;B[dj];W[fj];B[hj];W[jj])(;B[dl];W[fl];B[hl];W[jl]))
private static void CreateGameTree(SGFGameTree root, Game p) {
            
    // ~~~~~~~

    // I think that perhaps there is a processing problem here.
    foreach (var r in root.GameTrees) {
        CreateGameTree(r, p);
    }
}

Get all unique groups

Hi is there a way to get all unique groups on the board currently? From what I can see you need to check each point if it contains a group?

what's difference Go and GoSharpCore?

Thank you for creating such a wonderful library.
The code in the root directory (namespace go) and the code in the GoSharpCore directory (namespace GoSharpCore) are similar and almost identical.
What is the difference between them and how should I use them depending on the situation?

How to replay an SGF game?

Which methods should be used to jump to the next / previous nodes in an SGF game? Do I need to write new methods or is there built-in support to browse an SGF game?

How do I switch turns for each move?

How do I switch turns for each move?
I have written the following code.

GameInfo gi = new GameInfo() { Handicap = 0, StartingPlayer = Content.Black, Komi = 6.5 };
Game game = new Game(gi);

bool legal;
game.MakeMove(3, 3, out legal);
game.MakeMove(16, 16, out legal);

Debug.Log(game.SerializeToSGF(null));

What I get is this kind of SGF. It is a variation move of Black's first move and does not advance the number of moves.

(;SZ[19](;B[dd])(;B[qq]))

What I want is an SGF like this one.

(;SZ[19](;B[dd];W[qq]))

ConvertFromSGF does not handle upper case letters

The method is only designed for lower case letters. Games with large boards use capital letters.

The first letter designates the column (left to right), the second the row (top to bottom). The upper left part of the board is used for smaller boards, e.g. letters "a"-"m" for 13*13.
A pass move is shown as '[]' or alternatively as '[tt]' (only for boards <= 19x19), i.e. applications should be able to deal with both representations. '[tt]' is kept for compatibility with FF[3].
Using lowercase letters only the maximum board size is 26x26.

In FF[4] it is possible to specify board sizes upto 52x52. In this case uppercase letters are used to represent points from 27-52, i.e. 'a'=1 ... 'z'=26 , 'A'=27 ... 'Z'=52

Example SGF:

(;GM[1]FF[4]CA[UTF-8]AP[CGoban:3]ST[2]
SZ[37]HA[9]AB[dd][sd][Hd][ds][ss][Hs][dH][sH][HH]RE[W+228.50]
;W[]
;B[II]
;W[]
;B[uJ]
;W[tJ])

GoSharp/Point.cs

Lines 80 to 85 in 4f9a889

public static Point ConvertFromSGF(string sgf)
{
if (sgf == "") return Game.PassMove;
var bb = ASCIIEncoding.ASCII.GetBytes(sgf);
return new Point(bb[0] - 97, bb[1] - 97);
}

License information

I want to use this library to create a mobile go application with Unity. It will be a free application with in-app purchases. What are the terms of use?

Illegal moves allowed

Looks like you are allowed to fill you last liberty if it's a group of more than one stone. 1 stone is not allowed to play a suicide move but a group is.

SGFProperty.ReadValue does not respect backslash in front of closing bracket

This kind of comment in an SGF trips the property reader: C[a [3d?\]: hi]

When the C property value is read, the closing bracket is always assumed to be the end of the comment, even though it is preceded by a backslash here.

GoSharp/SGFProperty.cs

Lines 82 to 93 in 4c7a2d5

private void ReadValue(TextReader sr)
{
char c = (char)sr.Read();
if (c != '[')
throw new InvalidDataException("Property value doesn't begin with a '['.");
string val = "";
while ((c = (char)sr.Read()) != ']')
{
val += c;
}
Values.Add(new SGFPropValue(val.Trim()));
}

This affects files from KGS. I am looking at the spec right now to check if the backslash is standard conforming or not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.