 # ChrisPritchard's solution

## to Go Counting in the C# Track

Published at Oct 16 2018 · 0 comments
Instructions
Test suite
Solution

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.

## 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 System;
using System.Collections.Generic;
using Xunit;

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, Array.Empty<(int, int)>());
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, (int, int)[]>
{
[Owner.Black] = Array.Empty<(int, int)>(),
[Owner.White] = Array.Empty<(int, int)>(),
[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, (int, int)[]>
{
[Owner.Black] = new[] { (0, 0), (0, 1) },
[Owner.White] = new[] { (3, 0), (3, 1) },
[Owner.None] = Array.Empty<(int, int)>()
};
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, (int, int)[]>
{
[Owner.Black] = new[] { (0, 0), (2, 0) },
[Owner.White] = Array.Empty<(int, int)>(),
[Owner.None] = Array.Empty<(int, int)>()
};
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;
using System.Linq;

public enum Owner
{
None, Black, White
}

public class GoCounting
{
private string[] board;

public GoCounting(string input) => board = input.Split('\n');

public (Owner owner, IEnumerable<(int, int)> tiles) Territory((int x, int y) coord)
{
if(coord.x < 0 || coord.y < 0 || coord.y >= board.Length || coord.x >= board.Length)
throw new ArgumentException();

var start = OwnerFor(coord);
if(start != Owner.None)
return (Owner.None, Enumerable.Empty<(int, int)>());

var extent = Extent(coord.x, coord.y);
var owners = extent
.Select(o => o.owner)
.Where(o => o != Owner.None)
.Distinct();
var areEmpty = extent
.Where(o => o.owner == Owner.None)
.Select(o => (x: o.x, y: o.y))
.OrderBy(o => o.x).ThenBy(o => o.y);
return (owners.Count() == 1 ? owners.First() : Owner.None, areEmpty);
}

private IEnumerable<(int x, int y, Owner owner)> Extent(int x, int y)
{
var soFar = new Dictionary<(int x, int y), Owner> { { (x, y), Owner.None } };
while(neighbours.Any())
{
var withOwners = neighbours.Select(o => (x: o.x, y: o.y, owner: OwnerFor(o))).ToList();

neighbours = withOwners
.Where(o => o.owner == Owner.None)
.Where(o => !soFar.ContainsKey((o.x, o.y)))
.ToArray();
}

return soFar.Select(o => (o.Key.x, o.Key.y, o.Value));
}

private IEnumerable<(int x, int y)> Adjacent(int x, int y)
=> new (int dx, int dy)[] { (-1, 0), (1, 0), (0, -1), (0, 1) }
.Select(o => (x: x + o.dx, y: y + o.dy))
.Where(o => o.x >= 0 && o.y >= 0 && o.y < board.Length && o.x < board.Length);

private Owner OwnerFor(char c) => c == ' ' ? Owner.None : c == 'W' ? Owner.White : Owner.Black;

private Owner OwnerFor((int x, int y) coord) => OwnerFor(board[coord.y][coord.x]);

public Dictionary<Owner, IEnumerable<(int, int)>> Territories()
{
var result = new Dictionary<Owner, IEnumerable<(int, int)>>
{
[Owner.Black] = Array.Empty<(int, int)>(),
[Owner.White] = Array.Empty<(int, int)>(),
[Owner.None] = Array.Empty<(int, int)>()
};

var allTiles =
Enumerable.Range(0, board.Length).SelectMany(y =>
Enumerable.Range(0, board.Length).Select(x => Territory((x, y))));
var hasTerritory =
allTiles.GroupBy(o => o.owner)
.ToDictionary(o => o.Key,
o => o.SelectMany(t => t.tiles).Distinct());

foreach(var owner in hasTerritory.Keys)
result[owner] = hasTerritory[owner];

return result;
}
}``````