Skip to content

Instantly share code, notes, and snippets.

@mattflo
Created August 29, 2012 01:50
Show Gist options
  • Save mattflo/3505991 to your computer and use it in GitHub Desktop.
Save mattflo/3505991 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace describe_MacalaKata
{
public class Mancala : List<int>
{
public void Sow(int well)
{
if (well == 6 || well == 13) return;
var opponentGoal = well < 7 ? 13 : 6;
var seeds = this[well];
this[well] = 0;
while (seeds > 0)
{
var idx = ++well;
if (idx == opponentGoal) continue;
if (this.Count-1 < idx) idx = well = 0;
this[idx]++;
seeds--;
}
}
public override string ToString()
{
var player1s = string.Join("\t", this.Take(6));
var player2s = string.Join("\t", this.Skip(7).Take(6).Reverse());
return this[13].ToString() + "\t" + player2s + Environment.NewLine + "\t" + player1s + "\t" + this[6];
}
}
[TestFixture]
public class run
{
Mancala game;
private Random rand;
[SetUp]
public void setup()
{
game = new Mancala { 4, 4, 4, 4, 4, 4, 0,
4, 4, 4, 4, 4, 4, 0 };
rand = new Random();
}
[Test]
public void try_a_game()
{
for (int i = 0; i < 10; i++)
{
game.Sow(Player1Move());
Console.WriteLine(game);
game.Sow(Player2Move());
Console.WriteLine(game);
}
}
[Test]
public void run_entire_game()
{
while (!(game.Take(6).All(well => well == 0) || game.Skip(7).Take(6).All(well => well == 0)))
{
game.Sow(Player1Move());
Console.WriteLine(game);
game.Sow(Player2Move());
Console.WriteLine(game);
}
var player1Score = game.Take(7).Sum();
var player2Score = game.Skip(7).Take(7).Sum();
Console.WriteLine("Player 1: {0}", player1Score);
Console.WriteLine("Player 2: {0}", player2Score);
}
[Test]
public void player_one_valid_plays()
{
Enumerable.Range(0, 30).ToList().ForEach(i => Console.WriteLine(Player1Move()));
}
[Test]
public void player_two_valid_plays()
{
Enumerable.Range(0, 30).ToList().ForEach(i => Console.WriteLine(Player2Move()));
}
[Test]
public void print_game_status()
{
Console.WriteLine(game);
}
private int Player1Move()
{
var move = rand.Next(6);
Console.WriteLine("Player 1 move {0}", move + 1);
return move;
}
private int Player2Move()
{
var move = rand.Next(7, 13);
Console.WriteLine("Player 2 move {0}", move - 6);
return move;
}
}
[TestFixture]
public class describe_valid_plays
{
Mancala game;
[SetUp]
public void setup()
{
game = new Mancala { 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 1 };
}
[Test]
public void player_1s_goal_cannot_be_sowed()
{
game.Sow(6);
Assert.That(game[7], Is.EqualTo(0));
}
[Test]
public void player_2s_goal_cannot_be_sowed()
{
game.Sow(13);
Assert.That(game[0], Is.EqualTo(0));
}
[Test]
public void sowing_empty_wells_does_nothing()
{
game.Sow(0);
Assert.That(game[1], Is.EqualTo(0));
}
}
[TestFixture]
public class describe_skipping_player_1s_goal
{
Mancala game;
[SetUp]
public void setup()
{
game = new Mancala { 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 8, 0 };
game.Sow(12);
}
[Test]
public void opponents_goal_is_skipped()
{
Assert.That(game[6], Is.EqualTo(0));
}
[Test]
public void seeding_continues_with_player_2s_first_goal()
{
Assert.That(game[7], Is.EqualTo(1));
}
}
[TestFixture]
public class describe_skipping_player_2s_goal
{
Mancala game;
[SetUp]
public void setup()
{
game = new Mancala { 0, 0, 0, 0, 0, 8, 0,
0, 0, 0, 0, 0, 0, 0 };
game.Sow(5);
}
[Test]
public void opponents_goal_is_skipped()
{
Assert.That(game[13], Is.EqualTo(0));
}
[Test]
public void seeding_continues_with_player_1s_first_goal()
{
Assert.That(game[0], Is.EqualTo(1));
}
}
[TestFixture]
public class describe_simple_seeding
{
Mancala game;
[SetUp]
public void setup()
{
game = new Mancala { 0, 3, 1, 0 };
game.Sow(1);
}
[Test]
public void empties_the_sowed_well()
{
Assert.That(game[1], Is.EqualTo(0));
}
[Test]
public void adds_1_to_the_next_well()
{
Assert.That(game[2], Is.EqualTo(2));
}
[Test]
public void seeds_the_next_next_well()
{
Assert.That(game[3], Is.EqualTo(1));
}
[Test]
public void seeding_wraps_around()
{
Assert.That(game[0], Is.EqualTo(1));
}
}
}
------ Test started: Assembly: MacalaKata.dll ------
Player 1 move 2
0 4 4 4 4 4 4
4 0 5 5 5 5 0
Player 2 move 1
0 4 5 5 5 5 0
4 0 5 5 5 5 0
Player 1 move 3
0 4 5 5 5 5 1
4 0 0 6 6 6 1
Player 2 move 3
1 5 6 6 0 5 1
5 0 0 6 6 6 1
Player 1 move 2
1 5 6 6 0 5 1
5 0 0 6 6 6 1
Player 2 move 1
1 5 6 6 0 6 0
5 0 0 6 6 6 1
Player 1 move 2
1 5 6 6 0 6 0
5 0 0 6 6 6 1
Player 2 move 3
1 5 6 6 0 6 0
5 0 0 6 6 6 1
Player 1 move 1
1 5 6 6 0 6 0
0 1 1 7 7 7 1
Player 2 move 2
2 6 7 7 1 0 0
1 1 1 7 7 7 1
Player 1 move 3
2 6 7 7 1 0 0
1 1 0 8 7 7 1
Player 2 move 3
2 6 7 8 0 0 0
1 1 0 8 7 7 1
Player 1 move 2
2 6 7 8 0 0 0
1 0 1 8 7 7 1
Player 2 move 5
3 7 0 8 0 0 0
2 1 2 9 8 7 1
Player 1 move 1
3 7 0 8 0 0 0
0 2 3 9 8 7 1
Player 2 move 1
3 7 0 8 0 0 0
0 2 3 9 8 7 1
Player 1 move 4
3 8 1 9 1 1 1
0 2 3 0 9 8 2
Player 2 move 2
3 8 1 9 2 0 1
0 2 3 0 9 8 2
Player 1 move 1
3 8 1 9 2 0 1
0 2 3 0 9 8 2
Player 2 move 6
4 0 1 9 2 0 2
1 3 4 1 10 9 2
Player 1 move 6
4 1 2 10 3 1 3
2 4 4 1 10 0 3
Player 2 move 2
4 1 2 10 4 0 3
2 4 4 1 10 0 3
Player 1 move 5
4 2 3 11 5 1 4
3 5 4 1 0 1 4
Player 2 move 5
5 3 0 11 5 1 4
4 5 4 1 0 1 4
Player 1 move 5
5 3 0 11 5 1 4
4 5 4 1 0 1 4
Player 2 move 2
5 3 0 11 6 0 4
4 5 4 1 0 1 4
Player 1 move 3
5 3 0 11 6 0 4
4 5 0 2 1 2 5
Player 2 move 3
6 4 1 12 0 0 4
5 6 0 2 1 2 5
Player 1 move 2
6 4 1 12 0 0 5
5 0 1 3 2 3 6
Player 2 move 4
7 5 2 0 1 1 6
6 1 2 4 3 4 6
Player 1 move 4
7 5 2 0 1 1 7
6 1 2 0 4 5 7
Player 2 move 6
8 0 2 0 1 1 7
7 2 3 1 4 5 7
Player 1 move 5
8 0 2 0 1 2 8
7 2 3 1 0 6 8
Player 2 move 1
9 1 3 1 2 3 0
8 3 3 1 0 6 8
Player 1 move 1
9 1 3 1 2 4 1
0 4 4 2 1 7 9
Player 2 move 5
10 2 0 1 2 4 1
1 4 4 2 1 7 9
Player 1 move 1
10 2 0 1 2 4 1
0 5 4 2 1 7 9
Player 2 move 2
10 3 1 2 3 0 1
0 5 4 2 1 7 9
Player 1 move 1
10 3 1 2 3 0 1
0 5 4 2 1 7 9
Player 2 move 3
10 4 2 3 0 0 1
0 5 4 2 1 7 9
Player 1 move 3
10 4 2 3 0 0 1
0 5 0 3 2 8 10
Player 2 move 5
11 5 0 3 0 0 1
0 5 0 3 2 8 10
Player 1 move 4
11 5 0 3 0 0 1
0 5 0 0 3 9 11
Player 2 move 6
12 0 0 3 0 0 1
1 6 1 1 3 9 11
Player 1 move 1
12 0 0 3 0 0 1
0 7 1 1 3 9 11
Player 2 move 5
12 0 0 3 0 0 1
0 7 1 1 3 9 11
Player 1 move 3
12 0 0 3 0 0 1
0 7 0 2 3 9 11
Player 2 move 1
12 0 0 3 0 1 0
0 7 0 2 3 9 11
Player 1 move 5
12 0 0 3 0 1 1
0 7 0 2 0 10 12
Player 2 move 6
12 0 0 3 0 1 1
0 7 0 2 0 10 12
Player 1 move 5
12 0 0 3 0 1 1
0 7 0 2 0 10 12
Player 2 move 1
12 0 0 3 0 2 0
0 7 0 2 0 10 12
Player 1 move 1
12 0 0 3 0 2 0
0 7 0 2 0 10 12
Player 2 move 4
13 1 1 0 0 2 0
0 7 0 2 0 10 12
Player 1 move 4
13 1 1 0 0 2 0
0 7 0 0 1 11 12
Player 2 move 6
14 0 1 0 0 2 0
0 7 0 0 1 11 12
Player 1 move 3
14 0 1 0 0 2 0
0 7 0 0 1 11 12
Player 2 move 5
14 1 0 0 0 2 0
0 7 0 0 1 11 12
Player 1 move 5
14 1 0 0 0 2 0
0 7 0 0 0 12 12
Player 2 move 6
15 0 0 0 0 2 0
0 7 0 0 0 12 12
Player 1 move 5
15 0 0 0 0 2 0
0 7 0 0 0 12 12
Player 2 move 5
15 0 0 0 0 2 0
0 7 0 0 0 12 12
Player 1 move 2
15 0 0 0 0 3 1
0 0 1 1 1 13 13
Player 2 move 4
15 0 0 0 0 3 1
0 0 1 1 1 13 13
Player 1 move 4
15 0 0 0 0 3 1
0 0 1 0 2 13 13
Player 2 move 3
15 0 0 0 0 3 1
0 0 1 0 2 13 13
Player 1 move 4
15 0 0 0 0 3 1
0 0 1 0 2 13 13
Player 2 move 1
15 0 0 0 0 4 0
0 0 1 0 2 13 13
Player 1 move 5
15 0 0 0 0 4 0
0 0 1 0 0 14 14
Player 2 move 3
15 0 0 0 0 4 0
0 0 1 0 0 14 14
Player 1 move 6
15 1 1 1 1 5 1
1 1 2 1 1 1 16
Player 2 move 3
15 1 1 2 0 5 1
1 1 2 1 1 1 16
Player 1 move 2
15 1 1 2 0 5 1
1 0 3 1 1 1 16
Player 2 move 5
15 2 0 2 0 5 1
1 0 3 1 1 1 16
Player 1 move 3
15 2 0 2 0 5 1
1 0 0 2 2 2 16
Player 2 move 1
15 2 0 2 0 6 0
1 0 0 2 2 2 16
Player 1 move 4
15 2 0 2 0 6 0
1 0 0 0 3 3 16
Player 2 move 5
15 2 0 2 0 6 0
1 0 0 0 3 3 16
Player 1 move 4
15 2 0 2 0 6 0
1 0 0 0 3 3 16
Player 2 move 1
15 2 0 2 0 6 0
1 0 0 0 3 3 16
Player 1 move 6
15 2 0 2 0 7 1
1 0 0 0 3 0 17
Player 2 move 2
16 3 1 3 1 0 1
2 1 0 0 3 0 17
Player 1 move 5
16 3 1 3 1 0 2
2 1 0 0 0 1 18
Player 2 move 2
16 3 1 3 1 0 2
2 1 0 0 0 1 18
Player 1 move 5
16 3 1 3 1 0 2
2 1 0 0 0 1 18
Player 2 move 4
17 4 2 0 1 0 2
2 1 0 0 0 1 18
Player 1 move 6
17 4 2 0 1 0 2
2 1 0 0 0 0 19
Player 2 move 2
17 4 2 0 1 0 2
2 1 0 0 0 0 19
Player 1 move 3
17 4 2 0 1 0 2
2 1 0 0 0 0 19
Player 2 move 1
17 4 2 0 2 1 0
2 1 0 0 0 0 19
Player 1 move 2
17 4 2 0 2 1 0
2 0 1 0 0 0 19
Player 2 move 3
17 4 3 1 0 1 0
2 0 1 0 0 0 19
Player 1 move 3
17 4 3 1 0 1 0
2 0 0 1 0 0 19
Player 2 move 1
17 4 3 1 0 1 0
2 0 0 1 0 0 19
Player 1 move 4
17 4 3 1 0 1 0
2 0 0 0 1 0 19
Player 2 move 5
18 5 0 1 0 1 0
3 0 0 0 1 0 19
Player 1 move 4
18 5 0 1 0 1 0
3 0 0 0 1 0 19
Player 2 move 1
18 5 0 1 0 1 0
3 0 0 0 1 0 19
Player 1 move 2
18 5 0 1 0 1 0
3 0 0 0 1 0 19
Player 2 move 4
18 5 1 0 0 1 0
3 0 0 0 1 0 19
Player 1 move 1
18 5 1 0 0 1 0
0 1 1 1 1 0 19
Player 2 move 2
18 5 1 0 1 0 0
0 1 1 1 1 0 19
Player 1 move 3
18 5 1 0 1 0 0
0 1 0 2 1 0 19
Player 2 move 2
18 5 1 0 1 0 0
0 1 0 2 1 0 19
Player 1 move 6
18 5 1 0 1 0 0
0 1 0 2 1 0 19
Player 2 move 3
18 5 1 1 0 0 0
0 1 0 2 1 0 19
Player 1 move 6
18 5 1 1 0 0 0
0 1 0 2 1 0 19
Player 2 move 2
18 5 1 1 0 0 0
0 1 0 2 1 0 19
Player 1 move 1
18 5 1 1 0 0 0
0 1 0 2 1 0 19
Player 2 move 3
18 5 1 1 0 0 0
0 1 0 2 1 0 19
Player 1 move 3
18 5 1 1 0 0 0
0 1 0 2 1 0 19
Player 2 move 4
18 5 2 0 0 0 0
0 1 0 2 1 0 19
Player 1 move 1
18 5 2 0 0 0 0
0 1 0 2 1 0 19
Player 2 move 5
19 6 0 0 0 0 0
0 1 0 2 1 0 19
Player 1 move 1
19 6 0 0 0 0 0
0 1 0 2 1 0 19
Player 2 move 3
19 6 0 0 0 0 0
0 1 0 2 1 0 19
Player 1 move 1
19 6 0 0 0 0 0
0 1 0 2 1 0 19
Player 2 move 6
20 0 0 0 0 0 0
1 2 1 3 2 0 19
Player 1: 28
Player 2: 20
1 passed, 0 failed, 0 skipped, took 1.00 seconds (NUnit 2.6.0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment