 ## to Go Counting in the C# Track

Published at Jul 24 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
{

public GoCounting(string board)
{
_board = board.Split('\n');
X = _board.First().Length;
Y = _board.Length;
}

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

public Dictionary<Owner, IEnumerable<(int, int)>> Territories()
{
var everyPointTerritory = new List<((int, int) coor, (Owner owner, (int, int)[] arr) pair)>();
for (int i = 0; i < X; i++)
for (int j = 0; j < Y; j++)
var resultDict = new Dictionary<Owner, IEnumerable<(int, int)>>() { { Owner.Black, new(int, int) }, { Owner.White, new(int, int) }, { Owner.None, new(int, int) }, };
foreach (var vt in everyPointTerritory)
{
var AttatchNewTerritory = resultDict[vt.pair.owner].ToList();
resultDict[vt.pair.owner] = AttatchNewTerritory.Distinct();
}
return resultDict;
}

public (Owner, (int, int)[]) Territory((int, int) coordinate)
{
bool inGameZone = coordinate.Item1 >= 0 && coordinate.Item1 < X && coordinate.Item2 >= 0 &&
coordinate.Item2 < Y;
if (!inGameZone) throw new ArgumentException();
bool isTerritoryMark = !"WB".Contains(_board[coordinate.Item2][coordinate.Item1]);
if (!isTerritoryMark) return (Owner.None, new(int, int));

var terri = new List<((int, int) point, bool isChecked)>();
var around = AroundTerritory(' ', coordinate);
while (terri.Any(m => !m.isChecked))
{
var newAround = new List<(int, int)>();
for (int i = 0; i < terri.Count; i++)
{
newAround = AroundTerritory(' ', terri[i].point);
terri[i] = (terri[i].point, true);
}
terri.AddRange(newAround.Except(terri.Select(s => s.point)).Select(s => (s, false)));
}
var owner = CheckOwner(terri);
return (owner, terri.Select(s => s.point).OrderBy(m => m).ToArray());
}

private Owner CheckOwner(List<((int, int) point, bool isChecked)> myc)
{
var blacks = new List<(int, int)>();
var whites = new List<(int, int)>();
foreach (var m in myc)
{
}
var owner = Owner.None;
if (blacks.Count > 0 && whites.Count == 0)
owner = Owner.Black;
if (blacks.Count == 0 && whites.Count > 0)
owner = Owner.White;
return owner;
}

private List<(int, int)> AroundTerritory(char mark, (int x, int y) p)
{
var around = new List<(int, int)>();

if (p.x - 1 >= 0)
{
if (_board[p.y][p.x - 1] == mark)
}
if (p.x + 1 < X)
{
if (_board[p.y][p.x + 1] == mark)
}
if (p.y - 1 >= 0)
{
if (_board[p.y - 1][p.x] == mark)
}
if (p.y + 1 < Y)
{
if (_board[p.y + 1][p.x] == mark)
}
return around;
}
}

/*
*   What I learn ?
*
*   1. create a collect list.  start from coordinate and set isCheck = true;
*   2.  check 4 direction (x,y) , add to collect list , but set isCheck = false;
*   3. repeat check collect until all collect item , with isCheck == true.  then
*      stop collect.
*
*   4.1  use collect to pick blacks, whites
*   4.2 count whits/blacks.  then we can know winner.
*
*
*   -------------------------------------
*   5.1 for every point calculate territory and owner.
*   5.2 append and distinct result.
*
*  */``````