summaryrefslogtreecommitdiff
path: root/Aktywator
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-11-09 13:29:55 +0100
committeremkael <emkael@tlen.pl>2016-11-14 14:46:01 +0100
commitbe59baaa024f8c5f02ec14fe34155e736548046b (patch)
tree3c4f136c234f940d841373c26d1ee5ccbec340d8 /Aktywator
parent64b9dce9855ecbceaa32a7d07f71490d5549fdfb (diff)
* BCDD code for parsing PBN files added
Diffstat (limited to 'Aktywator')
-rw-r--r--Aktywator/Aktywator.csproj3
-rw-r--r--Aktywator/DDTable.cs107
-rw-r--r--Aktywator/PBN.cs36
-rw-r--r--Aktywator/PBNBoard.cs205
-rw-r--r--Aktywator/PBNFile.cs46
5 files changed, 375 insertions, 22 deletions
diff --git a/Aktywator/Aktywator.csproj b/Aktywator/Aktywator.csproj
index b153254..14bb190 100644
--- a/Aktywator/Aktywator.csproj
+++ b/Aktywator/Aktywator.csproj
@@ -81,6 +81,7 @@
<DependentUpon>ChooseTournament.cs</DependentUpon>
</Compile>
<Compile Include="Common.cs" />
+ <Compile Include="DDTable.cs" />
<Compile Include="HandRecord.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
@@ -96,6 +97,8 @@
<DependentUpon>MysqlSettings.cs</DependentUpon>
</Compile>
<Compile Include="PBN.cs" />
+ <Compile Include="PBNBoard.cs" />
+ <Compile Include="PBNFile.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Setting.cs" />
diff --git a/Aktywator/DDTable.cs b/Aktywator/DDTable.cs
new file mode 100644
index 0000000..38e0fbd
--- /dev/null
+++ b/Aktywator/DDTable.cs
@@ -0,0 +1,107 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Runtime.InteropServices;
+using System.Text.RegularExpressions;
+
+namespace Aktywator
+{
+ class DDTableInvalidException : FieldNotFoundException
+ {
+ public DDTableInvalidException() : base() { }
+ public DDTableInvalidException(String msg) : base(msg) { }
+ }
+
+ class DDTable
+ {
+ public static char[] DENOMINATIONS = { 'C', 'D', 'H', 'S', 'N' };
+ public static char[] PLAYERS = { 'N', 'E', 'S', 'W' };
+
+ private PBNBoard board;
+
+ private int[,] getEmptyTable()
+ {
+ int[,] result = new int[4, 5];
+ for (int i = 0; i < 4; i++)
+ {
+ for (int j = 0; j < 5; j++)
+ {
+ result[i, j] = -1;
+ }
+ }
+ return result;
+ }
+
+ private int[,] validateTable(int[,] table)
+ {
+ foreach (int t in table)
+ {
+ if (t > 13 || t < 0)
+ {
+ throw new DDTableInvalidException("Invalid number of tricks: " + t.ToString());
+ }
+ }
+ return table;
+ }
+
+ public DDTable(PBNBoard board)
+ {
+ this.board = board;
+ }
+
+ public int[,] GetJFRTable()
+ {
+ int[,] result = this.getEmptyTable();
+ String ability = this.board.GetAbility();
+ MatchCollection abilities = this.board.ValidateAbility(ability);
+ foreach (Match playerAbility in abilities)
+ {
+ char player = playerAbility.Groups[1].Value[0];
+ int playerID = Array.IndexOf(PLAYERS, player);
+ int denomID = 4;
+ foreach (char tricks in playerAbility.Groups[2].Value.ToCharArray())
+ {
+ result[playerID, denomID] = (tricks > '9') ? (tricks - 'A' + 10) : (tricks - '0');
+ denomID--;
+ }
+ }
+ return this.validateTable(result);
+ }
+
+ public int[,] GetPBNTable()
+ {
+ List<String> table = this.board.GetOptimumResultTable();
+ List<Match> parsedTable = this.board.ValidateOptimumResultTable(table);
+ int[,] result = this.getEmptyTable();
+ foreach (Match lineMatch in parsedTable)
+ {
+ char player = lineMatch.Groups[1].Value[0];
+ char denom = lineMatch.Groups[2].Value[0];
+ int tricks = Int16.Parse(lineMatch.Groups[3].Value);
+ int playerID = Array.IndexOf(PLAYERS, player);
+ int denomID = Array.IndexOf(DENOMINATIONS, denom);
+ result[playerID, denomID] = tricks;
+ }
+ return this.validateTable(result);
+ }
+
+ public int[,] GetDDTable()
+ {
+ try
+ {
+ return this.GetJFRTable();
+ }
+ catch (FieldNotFoundException)
+ {
+ try
+ {
+ return this.GetPBNTable();
+ }
+ catch (FieldNotFoundException)
+ {
+ return null;
+ }
+ }
+ }
+ }
+}
diff --git a/Aktywator/PBN.cs b/Aktywator/PBN.cs
index ad7ac99..f6d5393 100644
--- a/Aktywator/PBN.cs
+++ b/Aktywator/PBN.cs
@@ -2,12 +2,14 @@
using System.Collections.Generic;
using System.Text;
using System.IO;
+using System.Windows.Forms;
namespace Aktywator
{
class PBN
{
public HandRecord[] handRecords;
+ public DDTable[] ddTables;
protected int lowBoard;
protected int highBoard;
private int _count;
@@ -19,33 +21,23 @@ namespace Aktywator
public PBN(string filename, int lowBoard, int highBoard)
{
this.handRecords = new HandRecord[highBoard + 1];
+ this.ddTables = new DDTable[highBoard + 1];
- StreamReader f = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read));
- try
+ this.lowBoard = lowBoard;
+ this.highBoard = highBoard;
+
+ this._count = 0;
+ PBNFile pbn = new PBNFile(filename);
+ foreach (PBNBoard board in pbn.Boards)
{
- int board = lowBoard;
- bool canBeRead = false;
- _count = 0;
- while (!f.EndOfStream && (board <= highBoard))
+ int boardNo = Int32.Parse(board.GetNumber());
+ if (lowBoard <= boardNo && boardNo <= highBoard)
{
- string line = f.ReadLine();
- if (line.Trim() == "[Board \"" + board + "\"]")
- canBeRead = true;
- else if (canBeRead && (line.Substring(0, 6) == "[Deal "))
- {
- line = line.Substring(line.IndexOf(':') + 1);
- line = line.Substring(0, line.IndexOf('"'));
- handRecords[board] = new HandRecord(line);
- canBeRead = false;
- _count++;
- board++;
- }
+ this.handRecords[boardNo] = new HandRecord(board.GetLayout());
+ this.ddTables[boardNo] = new DDTable(board);
+ this._count++;
}
}
- finally
- {
- f.Close();
- }
}
}
diff --git a/Aktywator/PBNBoard.cs b/Aktywator/PBNBoard.cs
new file mode 100644
index 0000000..605630c
--- /dev/null
+++ b/Aktywator/PBNBoard.cs
@@ -0,0 +1,205 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace Aktywator
+{
+
+ class PBNField
+ {
+ public String Key;
+ public String Value;
+ public String RawField;
+
+ public PBNField() { }
+
+ public PBNField(String key, String value)
+ {
+ this.Key = key;
+ this.Value = value;
+ this.RawField = String.Format("[{0} \"{1}\"]", this.Key, this.Value);
+ }
+
+ public PBNField(String rawData)
+ {
+ this.RawField = rawData;
+ }
+ }
+
+ class FieldNotFoundException : Exception
+ {
+ public FieldNotFoundException() : base() { }
+ public FieldNotFoundException(String msg) : base(msg) { }
+ }
+
+ class PBNBoard
+ {
+ public List<PBNField> Fields;
+
+ private bool? hasOptimumResultTable = null;
+ private bool? hasAbility = null;
+
+ private static Regex linePattern = new Regex(@"\[(.*) ""(.*)""\]");
+ private static Regex abilityPattern = new Regex(@"\b([NESW]):([0-9A-D]{5})\b");
+ private static Regex optimumResultTablePattern = new Regex(@"^([NESW])\s+([CDHSN])T?\s+(\d+)$");
+
+ public PBNBoard(List<string> lines)
+ {
+ this.Fields = new List<PBNField>();
+ foreach (String line in lines)
+ {
+ PBNField field = new PBNField();
+ field.RawField = line;
+ Match lineParse = PBNBoard.linePattern.Match(line);
+ if (lineParse.Success)
+ {
+ field.Key = lineParse.Groups[1].Value;
+ field.Value = lineParse.Groups[2].Value;
+ }
+ this.Fields.Add(field);
+ }
+ }
+
+ public bool HasField(String key)
+ {
+ foreach (PBNField field in this.Fields)
+ {
+ if (key.Equals(field.Key))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String GetField(String key)
+ {
+ foreach (PBNField field in this.Fields)
+ {
+ if (key.Equals(field.Key))
+ {
+ return field.Value;
+ }
+ }
+ throw new FieldNotFoundException(key + " field not found");
+ }
+
+ public String GetLayout()
+ {
+ return this.GetField("Deal");
+ }
+
+ public String GetNumber()
+ {
+ return this.GetField("Board");
+ }
+
+ public String GetVulnerable()
+ {
+ return this.GetField("Vulnerable");
+ }
+
+ public String GetDealer()
+ {
+ return this.GetField("Dealer");
+ }
+
+ public MatchCollection ValidateAbility(String ability)
+ {
+ MatchCollection matches = PBNBoard.abilityPattern.Matches(ability);
+ if (matches.Count != 4)
+ {
+ this.hasAbility = false;
+ throw new DDTableInvalidException("Invalid Ability line: " + ability);
+ }
+ List<String> players = new List<String>();
+ foreach (Match match in matches)
+ {
+ if (players.Contains(match.Groups[1].Value))
+ {
+ this.hasAbility = false;
+ throw new DDTableInvalidException("Duplicate entry in Ability: " + match.Groups[0].Value);
+ }
+ else
+ {
+ players.Add(match.Groups[1].Value);
+ }
+ }
+ this.hasAbility = true;
+ return matches;
+ }
+
+ public String GetAbility()
+ {
+ return this.GetField("Ability");
+ }
+
+ public String GetOptimumResult()
+ {
+ return this.GetField("OptimumResult");
+ }
+
+ public List<Match> ValidateOptimumResultTable(List<String> table)
+ {
+ List<Match> matches = new List<Match>();
+ List<String> duplicates = new List<String>();
+ foreach (String line in table)
+ {
+ Match match = PBNBoard.optimumResultTablePattern.Match(line);
+ if (!match.Success)
+ {
+ this.hasOptimumResultTable = false;
+ throw new DDTableInvalidException("Invalid OptimumResultTable line: " + line);
+ }
+ String position = match.Groups[1].Value + " - " + match.Groups[2].Value;
+ if (duplicates.Contains(position))
+ {
+ this.hasOptimumResultTable = false;
+ throw new DDTableInvalidException("Duplicate OptimumResultTable line: " + line);
+ }
+ else
+ {
+ duplicates.Add(position);
+ }
+ matches.Add(match);
+ }
+ this.hasOptimumResultTable = true;
+ return matches;
+ }
+
+ public List<String> GetOptimumResultTable()
+ {
+ bool fieldFound = false;
+ List<String> result = new List<String>();
+ foreach (PBNField field in this.Fields)
+ {
+ if ("OptimumResultTable".Equals(field.Key))
+ {
+ fieldFound = true;
+ }
+ else
+ {
+ if (fieldFound)
+ {
+ if (field.Key == null)
+ {
+ result.Add(field.RawField);
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+ }
+ if (!fieldFound)
+ {
+ this.hasOptimumResultTable = false;
+ throw new FieldNotFoundException("OptimumResultTable field not found");
+ }
+ return result;
+ }
+
+ }
+}
diff --git a/Aktywator/PBNFile.cs b/Aktywator/PBNFile.cs
new file mode 100644
index 0000000..044630a
--- /dev/null
+++ b/Aktywator/PBNFile.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.IO;
+
+namespace Aktywator
+{
+ class PBNFile
+ {
+ public List<PBNBoard> Boards;
+
+ private String filename;
+
+ public PBNFile(String filename)
+ {
+ this.filename = filename;
+ this.Boards = new List<PBNBoard>();
+ String[] fileContents = File.ReadAllLines(this.filename);
+ List<String> contents = new List<String>();
+ foreach (String line in fileContents) {
+ contents.Add(line.Trim());
+ }
+ List<String> lines = new List<String>();
+ foreach (String line in contents)
+ {
+ if (line.Length == 0)
+ {
+ if (lines.Count > 0)
+ {
+ this.Boards.Add(new PBNBoard(lines));
+ lines = new List<String>();
+ }
+ }
+ else
+ {
+ lines.Add(line);
+ }
+ }
+ if (lines.Count > 0)
+ {
+ this.Boards.Add(new PBNBoard(lines));
+ }
+ }
+
+ }
+}