Avatar of genephage

genephage's solution

to Go Counting in the C# Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Count the scored points on a Go board.

In the game of go (also known as baduk, igo, cờ vây and wéiqí) points are gained by completely encircling empty intersections with your stones. The encircled intersections of a player are known as its territory.

Write a function that determines the territory of each player. You may assume that any stones that have been stranded in enemy territory have already been taken off the board.

Multiple empty intersections may be encircled at once and for encircling only horizontal and vertical neighbours count. In the following diagram the stones which matter are marked "O" and the stones that don't are marked "I" (ignored). Empty spaces represent empty intersections.

+----+
|IOOI|
|O  O|
|O OI|
|IOI |
+----+

To be more precise an empty intersection is part of a player's territory if all of its neighbours are either stones of that player or empty intersections that are part of that player's territory.

For more information see wikipedia or Sensei's Library.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

GoCountingTest.cs

// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;
using System;
using System.Collections.Generic;

public class GoCountingTest
{
    [Fact]
    public void Black_corner_territory_on_5x5_board()
    {
        var coordinate = (0, 1);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        var actual = sut.Territory(coordinate);
        var expected = (Owner.Black, new[] { (0, 0), (0, 1), (1, 0) });
        Assert.Equal(expected.Item1, actual.Item1);
        Assert.Equal(expected.Item2, actual.Item2);
    }

    [Fact(Skip = "Remove to run test")]
    public void White_center_territory_on_5x5_board()
    {
        var coordinate = (2, 3);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        var actual = sut.Territory(coordinate);
        var expected = (Owner.White, new[] { (2, 3) });
        Assert.Equal(expected.Item1, actual.Item1);
        Assert.Equal(expected.Item2, actual.Item2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Open_corner_territory_on_5x5_board()
    {
        var coordinate = (1, 4);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        var actual = sut.Territory(coordinate);
        var expected = (Owner.None, new[] { (0, 3), (0, 4), (1, 4) });
        Assert.Equal(expected.Item1, actual.Item1);
        Assert.Equal(expected.Item2, actual.Item2);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_stone_and_not_a_territory_on_5x5_board()
    {
        var coordinate = (1, 1);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        var actual = sut.Territory(coordinate);
        var expected = (Owner.None, new ValueTuple<int,int>[0]);
        Assert.Equal(expected.Item1, actual.Item1);
        Assert.Equal(expected.Item2, actual.Item2);
    }

    [Fact(Skip = "Remove to run test")]
    public void Invalid_because_x_is_too_low_for_5x5_board()
    {
        var coordinate = (-1, 1);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        Assert.Throws<ArgumentException>(() => sut.Territory(coordinate));
    }

    [Fact(Skip = "Remove to run test")]
    public void Invalid_because_x_is_too_high_for_5x5_board()
    {
        var coordinate = (5, 1);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        Assert.Throws<ArgumentException>(() => sut.Territory(coordinate));
    }

    [Fact(Skip = "Remove to run test")]
    public void Invalid_because_y_is_too_low_for_5x5_board()
    {
        var coordinate = (1, -1);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        Assert.Throws<ArgumentException>(() => sut.Territory(coordinate));
    }

    [Fact(Skip = "Remove to run test")]
    public void Invalid_because_y_is_too_high_for_5x5_board()
    {
        var coordinate = (1, 5);
        var board = 
            "  B  \n" +
            " B B \n" +
            "B W B\n" +
            " W W \n" +
            "  W  ";
        var sut = new GoCounting(board);
        Assert.Throws<ArgumentException>(() => sut.Territory(coordinate));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_territory_is_the_whole_board()
    {
        var board = " ";
        var sut = new GoCounting(board);
        var actual = sut.Territories();
        var expected = new Dictionary<Owner, ValueTuple<int,int>[]>
        {
            [Owner.Black] = new ValueTuple<int,int>[0],
            [Owner.White] = new ValueTuple<int,int>[0],
            [Owner.None] = new[] { (0, 0) }
        };
        Assert.Equal(expected.Keys, actual.Keys);
        Assert.Equal(expected[Owner.Black], actual[Owner.Black]);
        Assert.Equal(expected[Owner.White], actual[Owner.White]);
        Assert.Equal(expected[Owner.None], actual[Owner.None]);
    }

    [Fact(Skip = "Remove to run test")]
    public void Two_territory_rectangular_board()
    {
        var board = 
            " BW \n" +
            " BW ";
        var sut = new GoCounting(board);
        var actual = sut.Territories();
        var expected = new Dictionary<Owner, ValueTuple<int,int>[]>
        {
            [Owner.Black] = new[] { (0, 0), (0, 1) },
            [Owner.White] = new[] { (3, 0), (3, 1) },
            [Owner.None] = new ValueTuple<int,int>[0]
        };
        Assert.Equal(expected.Keys, actual.Keys);
        Assert.Equal(expected[Owner.Black], actual[Owner.Black]);
        Assert.Equal(expected[Owner.White], actual[Owner.White]);
        Assert.Equal(expected[Owner.None], actual[Owner.None]);
    }

    [Fact(Skip = "Remove to run test")]
    public void Two_region_rectangular_board()
    {
        var board = " B ";
        var sut = new GoCounting(board);
        var actual = sut.Territories();
        var expected = new Dictionary<Owner, ValueTuple<int,int>[]>
        {
            [Owner.Black] = new[] { (0, 0), (2, 0) },
            [Owner.White] = new ValueTuple<int,int>[0],
            [Owner.None] = new ValueTuple<int,int>[0]
        };
        Assert.Equal(expected.Keys, actual.Keys);
        Assert.Equal(expected[Owner.Black], actual[Owner.Black]);
        Assert.Equal(expected[Owner.White], actual[Owner.White]);
        Assert.Equal(expected[Owner.None], actual[Owner.None]);
    }
}
using System;
using System.Collections.Generic;

public enum GoPlayer
{
    None,
    Black,
    White
}

public class GoCounting
{
    public GoCounting(string input)
    {
        throw new NotImplementedException("You need to implement this function.");
    }

    public Tuple<GoPlayer, IEnumerable<Tuple<int, int>>> TerritoryFor(Tuple<int, int> coord)
    {
        throw new NotImplementedException("You need to implement this function.");
    }

    public Dictionary<GoPlayer, IEnumerable<Tuple<int, int>>> Territories()
    {
        throw new NotImplementedException("You need to implement this function.");
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?