Libsaki
Core library of Pancake Mahjong
who.h
1 #ifndef SAKI_WHO_H
2 #define SAKI_WHO_H
3 
4 #include "../util/assume.h"
5 #include "../util/int_iter.h"
6 
7 
8 
9 namespace saki
10 {
11 
12 
13 
14 class Who
15 {
16 public:
17  static constexpr int HUMAN_INDEX = 0;
18 
19  Who() : mWho(NOBODY) {}
20 
21  explicit constexpr Who(int who)
22  : mWho(who)
23  {
24  }
25 
26  explicit Who(unsigned who)
27  : mWho(static_cast<decltype(mWho)>(who))
28  {
29  assert(0 <= mWho && mWho <= NOBODY);
30  }
31 
32  Who(const Who &copy) = default;
33 
34  int index() const
35  {
36  assert(validSomebody(mWho));
37  assume_opt_out(validSomebody(mWho));
38  return mWho;
39  }
40 
41  unsigned uIndex() const
42  {
43  return static_cast<unsigned>(index());
44  }
45 
46  Who right() const
47  {
48  assert(validSomebody(mWho));
49  assume_opt_out(validSomebody(mWho));
50  return Who((mWho + 1) % 4);
51  }
52 
53  Who left() const
54  {
55  assert(validSomebody(mWho));
56  assume_opt_out(validSomebody(mWho));
57  return Who((mWho + 3) % 4);
58  }
59 
60  Who cross() const
61  {
62  assert(validSomebody(mWho));
63  assume_opt_out(validSomebody(mWho));
64  return Who((mWho + 2) % 4);
65  }
66 
67  Who byDice(int dice) const
68  {
69  assert(1 <= dice && dice <= 12);
70  assume_opt_out(1 <= dice && dice <= 12);
71  return Who((mWho + dice - 1) % 4);
72  }
73 
74  Who byTurn(int turn) const
75  {
76  return Who((mWho + turn) % 4);
77  }
78 
84  int looksAt(Who tar) const
85  {
86  assert(validSomebody(mWho));
87  assume_opt_out(validSomebody(mWho));
88  assert(validSomebody(tar.mWho));
89  assume_opt_out(validSomebody(tar.mWho));
90  assert(mWho != tar.mWho);
91  assume_opt_out(mWho != tar.mWho);
92  return 3 - (tar.mWho - mWho + 4) % 4;
93  }
94 
100  int turnFrom(Who from) const
101  {
102  assert(validSomebody(mWho));
103  assume_opt_out(validSomebody(mWho));
104  assert(validSomebody(from.mWho));
105  assume_opt_out(validSomebody(from.mWho));
106  return (4 + mWho - from.mWho) % 4;
107  }
108 
109  bool somebody() const
110  {
111  return mWho != NOBODY;
112  }
113 
114  bool nobody() const
115  {
116  return mWho == NOBODY;
117  }
118 
119  bool human() const
120  {
121  return mWho == HUMAN_INDEX;
122  }
123 
124  bool operator==(Who rhs) const
125  {
126  return mWho == rhs.mWho;
127  }
128 
129  bool operator!=(Who rhs) const
130  {
131  return !(*this == rhs);
132  }
133 
134  Who &operator=(const Who &assign) = default;
135 
136 private:
137  static constexpr bool validSomebody(int w) noexcept
138  {
139  return 0 <= w && w < 4;
140  }
141 
142 private:
143  static const int NOBODY = 4;
144 
145  int mWho;
146 };
147 
148 
149 
150 namespace whos
151 {
152 
153 
154 
155 constexpr IntRange<Who, 4> ALL4;
156 
157 constexpr Who HUMAN(Who::HUMAN_INDEX);
158 
159 
160 
161 } // namespace whos
162 
163 
164 
165 } // namespace saki
166 
167 
168 
169 #endif // SAKI_WHO_H
Definition: int_iter.h:54
int turnFrom(Who from) const
Turning count from another players.
Definition: who.h:100
Definition: ai.cpp:18
int looksAt(Who tar) const
Relative position of anohter player.
Definition: who.h:84
Definition: who.h:14