pieceStand.cc
Go to the documentation of this file.
1 /* pieceStand.cc
2  */
3 #include "osl/bits/pieceStand.h"
4 #include "osl/bits/ptypeTable.h"
5 #include "osl/simpleState.h"
6 #include <iostream>
7 
8 namespace osl
9 {
10  static_assert(sizeof(unsigned int)*/*CHARBITS*/8>=32, "PieceStand");
11 
12  const CArray<Ptype,7> PieceStand::order =
13  {{
15  }};
16 
17  const CArray<unsigned char,PTYPE_MAX+1> PieceStand::shift =
18  {{
19  0,0,0,0,0,0,0,0,
20  28, 24, 18, 14, 10, 6, 3, 0,
21  }};
22  const CArray<unsigned char,PTYPE_MAX+1> PieceStand::mask =
23  {{
24  0,0,0,0,0,0,0,0,
25  (1<<2)-1, (1<<3)-1, (1<<5)-1, (1<<3)-1, (1<<3)-1, (1<<3)-1, (1<<2)-1, (1<<2)-1
26  }};
27 
28  const unsigned int PieceStand::carryMask;
29 }
30 
32 PieceStand(Player pl, const SimpleState& state)
33  : flags(0)
34 {
35  for (Ptype ptype: PieceStand::order)
36  add(ptype, state.countPiecesOnStand(pl, ptype));
37 }
38 
40 {
41  const int max
43  assert(max >= 0);
44  return (static_cast<int>(get(type)) != max);
45 }
46 
48 {
49  if (canAdd(type))
50  add(type);
51 }
52 
54 {
55  return misc::BitOp::countBit(getFlags()) <= 1;
56 }
57 
58 #ifndef MINIMAL
60 carryUnchangedAfterAdd(const PieceStand& original, const PieceStand& other) const
61 {
62  if (original.testCarries() == testCarries())
63  return true;
64  std::cerr << original << " + " << other << " = " << *this << "\n";
65  return false;
66 }
67 
69 carryUnchangedAfterSub(const PieceStand& original, const PieceStand& other) const
70 {
71  if (original.testCarries() == testCarries())
72  return true;
73  std::cerr << original << " - " << other << " = " << *this << "\n";
74  return false;
75 }
76 
77 std::ostream& osl::operator<<(std::ostream& os, osl::PieceStand stand)
78 {
79  os << "(stand";
80  for (Ptype ptype: PieceStand::order)
81  {
82  os << ' ' << stand.get(ptype);
83  }
84  return os << ")";
85 }
86 #endif
87 
88 std::ostream& osl::
89 PieceStandIO::writeNumbers(std::ostream& os, const PieceStand& stand)
90 {
91  for (Ptype ptype: PieceStand::order) {
92  os << stand.get(ptype) << " ";
93  }
94  return os;
95 }
96 std::istream& osl::
97 PieceStandIO::readNumbers(std::istream& is, PieceStand& stand)
98 {
99  stand = PieceStand();
100  for (Ptype ptype: PieceStand::order) {
101  int val;
102  if (is >> val)
103  stand.add(ptype, val);
104  }
105  return is;
106 }
107 
108 /* ------------------------------------------------------------------------- */
109 // ;;; Local Variables:
110 // ;;; mode:c++
111 // ;;; c-basic-offset:2
112 // ;;; End:
void add(Ptype type, unsigned int num=1)
static int countBit(Integer mask)
Definition: mask.h:160
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
Definition: simpleState.h:182
static const CArray< unsigned char, PTYPE_MAX+1 > shift
std::ostream & operator<<(std::ostream &os, Player player)
Definition: basic_type.cc:14
unsigned int get(Ptype type) const
bool atMostOneKind() const
一種類の駒しかない
Definition: pieceStand.cc:53
unsigned int testCarries() const
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
static const CArray< unsigned char, PTYPE_MAX+1 > mask
const PtypeTable Ptype_Table
Definition: tables.cc:97
static std::istream & readNumbers(std::istream &, PieceStand &stand)
Definition: pieceStand.cc:97
bool canAdd(Ptype type) const
Definition: pieceStand.cc:39
bool carryUnchangedAfterSub(const PieceStand &original, const PieceStand &other) const
Definition: pieceStand.cc:69
unsigned int getFlags() const
void tryAdd(Ptype type)
加算可能なら加える.
Definition: pieceStand.cc:47
int getIndexLimit(Ptype ptype) const
Definition: ptypeTable.h:93
static const unsigned int carryMask
static const CArray< Ptype, 7 > order
持駒の表示で良く使われる順番.
Player
Definition: basic_type.h:8
bool carryUnchangedAfterAdd(const PieceStand &original, const PieceStand &other) const
Definition: pieceStand.cc:60
const PieceStand max(PieceStand other) const
種類毎に this と other の持駒の多い方を取る
static std::ostream & writeNumbers(std::ostream &, const PieceStand &stand)
持駒の数を空白区切で出力する.
Definition: pieceStand.cc:89
片方の手番の持駒の枚数を記録するクラス.
int getIndexMin(Ptype ptype) const
Definition: ptypeTable.h:88
PieceStand(unsigned int value=0)