summaryrefslogtreecommitdiff
path: root/Aktywator/DDTable.cs
blob: 5de9c3b6f2cf40f31872e2441177adf2625186bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 = { 'S', 'H', 'D', 'C', 'N' };
        private static char[] JFR_DENOMS = {'N', 'S', 'H', 'D', 'C'};
        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;
                int denom = 0;
                foreach (char tricks in playerAbility.Groups[2].Value.ToCharArray())
                {
                    denomID = Array.IndexOf(DENOMINATIONS, JFR_DENOMS[denom]);
                    result[playerID, denomID] = (tricks > '9') ? (tricks - 'A' + 10) : (tricks - '0');
                    denom++;
                }
            }
            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;
                }
            }
        }
    }
}