File: GembloQGeometry.cpp

package info (click to toggle)
pentobi 26.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 4,068 kB
  • sloc: cpp: 25,986; javascript: 897; xml: 57; makefile: 13; sh: 11
file content (164 lines) | stat: -rw-r--r-- 4,343 bytes parent folder | download | duplicates (5)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//-----------------------------------------------------------------------------
/** @file libpentobi_base/GembloQGeometry.cpp
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#include "GembloQGeometry.h"

#include <map>
#include <memory>
#include "libboardgame_base/MathUtil.h"

namespace libpentobi_base {

using namespace std;
using libboardgame_base::mod;

//-----------------------------------------------------------------------------

GembloQGeometry::GembloQGeometry(unsigned nu_players)
{
    unsigned height;
    if (nu_players == 2)
    {
        height = 22;
        m_edge = 4;
    }
    else if (nu_players == 3)
    {
        height = 26;
        m_edge = 6;
    }
    else
    {
        LIBBOARDGAME_ASSERT(nu_players == 4);
        height = 28;
        m_edge = 13;
    }
    Geometry::init(2 * height, height);
}

const GembloQGeometry& GembloQGeometry::get(unsigned nu_players)
{
    static map<unsigned, shared_ptr<GembloQGeometry>> s_geometry;

    auto pos = s_geometry.find(nu_players);
    if (pos != s_geometry.end())
        return *pos->second;
    auto geometry = make_shared<GembloQGeometry>(nu_players);
    s_geometry.insert({nu_players, geometry});
    return *geometry;
}

auto GembloQGeometry::get_adj_coord(int x, int y) const -> AdjCoordList
{
    AdjCoordList l;
    l.push_back({x + 1, y});
    l.push_back({x - 1, y});
    switch (get_point_type(x, y))
    {
    case 0:
    case 3:
        l.push_back({x, y - 1});
        break;
    case 1:
    case 2:
        l.push_back({x, y + 1});
        break;
    }
    return l;
}

auto GembloQGeometry::get_diag_coord(int x, int y) const -> DiagCoordList
{
    // See Geometry::get_diag_coord() about advantageous ordering of the list
    DiagCoordList l;
    switch (get_point_type(x, y))
    {
    case 0:
        l.push_back({x + 2, y - 1});
        l.push_back({x - 1, y + 1});
        l.push_back({x - 1, y - 1});
        l.push_back({x, y + 1});
        l.push_back({x + 3, y});
        l.push_back({x - 2, y + 1});
        l.push_back({x + 1, y + 1});
        l.push_back({x + 3, y - 1});
        l.push_back({x - 2, y});
        l.push_back({x + 2, y});
        l.push_back({x + 1, y - 1});
        break;
    case 1:
        l.push_back({x - 2, y + 1});
        l.push_back({x + 1, y - 1});
        l.push_back({x + 1, y + 1});
        l.push_back({x, y - 1});
        l.push_back({x - 3, y});
        l.push_back({x + 2, y - 1});
        l.push_back({x - 1, y - 1});
        l.push_back({x - 3, y + 1});
        l.push_back({x + 2, y});
        l.push_back({x - 2, y});
        l.push_back({x - 1, y + 1});
        break;
    case 2:
        l.push_back({x - 2, y - 1});
        l.push_back({x + 3, y + 1});
        l.push_back({x - 1, y + 1});
        l.push_back({x, y - 1});
        l.push_back({x + 3, y});
        l.push_back({x + 2, y + 1});
        l.push_back({x + 1, y - 1});
        l.push_back({x - 2, y});
        l.push_back({x + 2, y});
        l.push_back({x - 1, y - 1});
        l.push_back({x + 1, y + 1});
        break;
    case 3:
        l.push_back({x - 3, y - 1});
        l.push_back({x + 2, y + 1});
        l.push_back({x + 1, y - 1});
        l.push_back({x, y + 1});
        l.push_back({x - 3, y});
        l.push_back({x - 2, y - 1});
        l.push_back({x - 1, y + 1});
        l.push_back({x + 2, y});
        l.push_back({x - 2, y});
        l.push_back({x + 1, y + 1});
        l.push_back({x - 1, y - 1});
        break;
    }
    return l;
}

unsigned GembloQGeometry::get_period_x() const
{
    return 4;
}

unsigned GembloQGeometry::get_period_y() const
{
    return 2;
}

unsigned GembloQGeometry::get_point_type(int x, int y) const
{
    return mod(x + 2 * static_cast<int>(y % 2 != 0), 4);
}

bool GembloQGeometry::init_is_onboard(unsigned x, unsigned y) const
{
    auto width = get_width();
    auto height = get_height();
    unsigned dy = min(y, height - y - 1);
    unsigned min_x = (width - 4 * m_edge) / 2 - 1 > 2 * dy ?
                (width - 4 * m_edge) / 2 - 1 - 2 * dy : 0;
    unsigned max_x = width - min_x - 1;
    return x >= min_x && x <= max_x;
}

//-----------------------------------------------------------------------------

} // namespace libpentobi_base