From 505871b85700f3dba65cebd9d1fd6c83076a0a9a Mon Sep 17 00:00:00 2001 From: dodikk Date: Sat, 19 Dec 2015 15:30:48 +0200 Subject: [PATCH] Added turn selector strategies --- .../Assets/ReversiKit/GreedyTurnSelector.cs | 21 ++++++++++++++++++ .../Assets/ReversiKit/RandomTurnSelector.cs | 22 +++++++++++++++++++ .../ReversiKit/ReversiKit/ReversiKit.csproj | 6 +++++ 3 files changed, 49 insertions(+) create mode 100644 Reversi/Assets/ReversiKit/GreedyTurnSelector.cs create mode 100644 Reversi/Assets/ReversiKit/RandomTurnSelector.cs diff --git a/Reversi/Assets/ReversiKit/GreedyTurnSelector.cs b/Reversi/Assets/ReversiKit/GreedyTurnSelector.cs new file mode 100644 index 0000000..0f119c8 --- /dev/null +++ b/Reversi/Assets/ReversiKit/GreedyTurnSelector.cs @@ -0,0 +1,21 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +namespace ReversiKit +{ + public class GreedyTurnSelector : ITurnSelector + { + public IReversiTurn SelectBestTurnOnBoard( + IEnumerable validTurns, + IBoardState board) + { + IReversiTurn result = + validTurns.OrderByDescending(t => t.PositionsOfFlippedItems.Count()) + .First(); + + return result; + } + } +} + diff --git a/Reversi/Assets/ReversiKit/RandomTurnSelector.cs b/Reversi/Assets/ReversiKit/RandomTurnSelector.cs new file mode 100644 index 0000000..bd62f32 --- /dev/null +++ b/Reversi/Assets/ReversiKit/RandomTurnSelector.cs @@ -0,0 +1,22 @@ +using System; +using System.Linq; +using System.Collections.Generic; + + +namespace ReversiKit +{ + public class RandomTurnSelector : ITurnSelector + { + public IReversiTurn SelectBestTurnOnBoard( + IEnumerable validTurns, + IBoardState board) + { + Random rand = new Random(); + int index = rand.Next(0, validTurns.Count()); + + IReversiTurn result = validTurns.ElementAt(index); + return result; + } + } +} + diff --git a/Reversi/ReversiKit/ReversiKit/ReversiKit/ReversiKit.csproj b/Reversi/ReversiKit/ReversiKit/ReversiKit/ReversiKit.csproj index d2c2b7d..4a98644 100644 --- a/Reversi/ReversiKit/ReversiKit/ReversiKit/ReversiKit.csproj +++ b/Reversi/ReversiKit/ReversiKit/ReversiKit/ReversiKit.csproj @@ -71,6 +71,12 @@ Impl\TurnCalculator.cs + + Impl\GreedyTurnSelector.cs + + + Impl\RandomTurnSelector.cs +