pieceStand.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pieceStand.cc
Go to the documentation of this file.
1 /* pieceStand.cc
2 */
3 #include "osl/pieceStand.h"
4
5 #include "osl/misc/mask.h"
6 #include "osl/ptypeTable.h"
7 #include "osl/state/simpleState.h"
8 #include <boost/static_assert.hpp>
9 #include <boost/foreach.hpp>
10 #include <iostream>
11
12 namespace osl
13 {
14 BOOST_STATIC_ASSERT(sizeof(unsigned int)*/*CHARBITS*/8>=32);
15
16 const CArray<Ptype,7> PieceStand::order =
17 {{
18 ROOK, BISHOP, GOLD, SILVER, KNIGHT, LANCE, PAWN,
19 }};
20
21 const CArray<unsigned char,PTYPE_MAX+1> PieceStand::shift =
22 {{
23 0,0,0,0,0,0,0,0,
24 28, 24, 18, 14, 10, 6, 3, 0,
25 }};
26 const CArray<unsigned char,PTYPE_MAX+1> PieceStand::mask =
27 {{
28 0,0,0,0,0,0,0,0,
29 (1<<2)-1, (1<<3)-1, (1<<5)-1, (1<<3)-1, (1<<3)-1, (1<<3)-1, (1<<2)-1, (1<<2)-1
30 }};
31
32 const unsigned int PieceStand::carryMask;
33 }
34
35 osl::PieceStand::
36 PieceStand(Player pl, const SimpleState& state)
37 : flags(0)
38 {
39 BOOST_FOREACH(Ptype ptype, PieceStand::order)
40 add(ptype, state.countPiecesOnStand(pl, ptype));
41 }
42
43 bool osl::PieceStand::canAdd(Ptype type) const
44 {
45 const int max
46 = Ptype_Table.getIndexLimit(type) - Ptype_Table.getIndexMin(type);
47 assert(max >= 0);
48 return (static_cast<int>(get(type)) != max);
49 }
50
51 void osl::PieceStand::tryAdd(Ptype type)
52 {
53 if (canAdd(type))
54 add(type);
55 }
56
57 bool osl::PieceStand::atMostOneKind() const
58 {
59 return misc::BitOp::countBit(getFlags()) <= 1;
60 }
61
62 #ifndef MINIMAL
63 bool osl::PieceStand::
64 carryUnchangedAfterAdd(const PieceStand& original, const PieceStand& other) const
65 {
66 if (original.testCarries() == testCarries())
67 return true;
68 std::cerr << original << " + " << other << " = " << *this << "\n";
69 return false;
70 }
71
72 bool osl::PieceStand::
73 carryUnchangedAfterSub(const PieceStand& original, const PieceStand& other) const
74 {
75 if (original.testCarries() == testCarries())
76 return true;
77 std::cerr << original << " - " << other << " = " << *this << "\n";
78 return false;
79 }
80
81 std::ostream& osl::operator<<(std::ostream& os, osl::PieceStand stand)
82 {
83 os << "(stand";
84 BOOST_FOREACH(Ptype ptype, PieceStand::order)
85 {
86 os << ' ' << stand.get(ptype);
87 }
88 return os << ")";
89 }
90 #endif
91 /* ------------------------------------------------------------------------- */
92 // ;;; Local Variables:
93 // ;;; mode:c++
94 // ;;; c-basic-offset:2
95 // ;;; End: