My Project
pieceStand.cc
Go to the documentation of this file.
1/* pieceStand.cc
2 */
5#include "osl/simpleState.h"
6#include <iostream>
7
8namespace 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
32PieceStand(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
60carryUnchangedAfterAdd(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
69carryUnchangedAfterSub(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
77std::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
88std::ostream& osl::
89PieceStandIO::writeNumbers(std::ostream& os, const PieceStand& stand)
90{
91 for (Ptype ptype: PieceStand::order) {
92 os << stand.get(ptype) << " ";
93 }
94 return os;
95}
96std::istream& osl::
97PieceStandIO::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:
片方の手番の持駒の枚数を記録するクラス.
static const CArray< unsigned char, PTYPE_MAX+1 > shift
void add(Ptype type, unsigned int num=1)
static const unsigned int carryMask
unsigned int testCarries() const
void tryAdd(Ptype type)
加算可能なら加える.
Definition pieceStand.cc:47
static const CArray< unsigned char, PTYPE_MAX+1 > mask
bool canAdd(Ptype type) const
Definition pieceStand.cc:39
PieceStand(unsigned int value=0)
bool carryUnchangedAfterSub(const PieceStand &original, const PieceStand &other) const
Definition pieceStand.cc:69
bool carryUnchangedAfterAdd(const PieceStand &original, const PieceStand &other) const
Definition pieceStand.cc:60
bool atMostOneKind() const
一種類の駒しかない
Definition pieceStand.cc:53
unsigned int get(Ptype type) const
static const CArray< Ptype, 7 > order
持駒の表示で良く使われる順番.
int getIndexLimit(Ptype ptype) const
Definition ptypeTable.h:93
int getIndexMin(Ptype ptype) const
Definition ptypeTable.h:88
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
@ ROOK
Definition basic_type.h:100
@ BISHOP
Definition basic_type.h:99
@ PAWN
Definition basic_type.h:95
@ KNIGHT
Definition basic_type.h:97
@ SILVER
Definition basic_type.h:98
@ GOLD
Definition basic_type.h:94
@ LANCE
Definition basic_type.h:96
const PtypeTable Ptype_Table
Definition tables.cc:97
Player
Definition basic_type.h:8
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
static std::ostream & writeNumbers(std::ostream &, const PieceStand &stand)
持駒の数を空白区切で出力する.
Definition pieceStand.cc:89
static std::istream & readNumbers(std::istream &, PieceStand &stand)
Definition pieceStand.cc:97
static int countBit(Integer mask)
Definition mask.h:160