pieceStand.cc
Go to the documentation of this file.
/* pieceStand.cc
*/
#include "osl/bits/pieceStand.h"
#include "osl/bits/ptypeTable.h"
#include "osl/simpleState.h"
#include <iostream>
namespace osl
{
static_assert(sizeof(unsigned int)*/*CHARBITS*/8>=32, "PieceStand");
const CArray<Ptype,7> PieceStand::order =
{{
ROOK, BISHOP, GOLD, SILVER, KNIGHT, LANCE, PAWN,
}};
const CArray<unsigned char,PTYPE_MAX+1> PieceStand::shift =
{{
0,0,0,0,0,0,0,0,
28, 24, 18, 14, 10, 6, 3, 0,
}};
const CArray<unsigned char,PTYPE_MAX+1> PieceStand::mask =
{{
0,0,0,0,0,0,0,0,
(1<<2)-1, (1<<3)-1, (1<<5)-1, (1<<3)-1, (1<<3)-1, (1<<3)-1, (1<<2)-1, (1<<2)-1
}};
const unsigned int PieceStand::carryMask;
}
osl::PieceStand::
PieceStand(Player pl, const SimpleState& state)
: flags(0)
{
for (Ptype ptype: PieceStand::order)
add(ptype, state.countPiecesOnStand(pl, ptype));
}
bool osl::PieceStand::canAdd(Ptype type) const
{
const int max
= Ptype_Table.getIndexLimit(type) - Ptype_Table.getIndexMin(type);
assert(max >= 0);
return (static_cast<int>(get(type)) != max);
}
void osl::PieceStand::tryAdd(Ptype type)
{
if (canAdd(type))
add(type);
}
bool osl::PieceStand::atMostOneKind() const
{
return misc::BitOp::countBit(getFlags()) <= 1;
}
#ifndef MINIMAL
bool osl::PieceStand::
carryUnchangedAfterAdd(const PieceStand& original, const PieceStand& other) const
{
if (original.testCarries() == testCarries())
return true;
std::cerr << original << " + " << other << " = " << *this << "\n";
return false;
}
bool osl::PieceStand::
carryUnchangedAfterSub(const PieceStand& original, const PieceStand& other) const
{
if (original.testCarries() == testCarries())
return true;
std::cerr << original << " - " << other << " = " << *this << "\n";
return false;
}
std::ostream& osl::operator<<(std::ostream& os, osl::PieceStand stand)
{
os << "(stand";
for (Ptype ptype: PieceStand::order)
{
os << ' ' << stand.get(ptype);
}
return os << ")";
}
#endif
std::ostream& osl::
PieceStandIO::writeNumbers(std::ostream& os, const PieceStand& stand)
{
for (Ptype ptype: PieceStand::order) {
os << stand.get(ptype) << " ";
}
return os;
}
std::istream& osl::
PieceStandIO::readNumbers(std::istream& is, PieceStand& stand)
{
stand = PieceStand();
for (Ptype ptype: PieceStand::order) {
int val;
if (is >> val)
stand.add(ptype, val);
}
return is;
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
pieceStand.h
osl::CArray
Definition container.h:20
osl::PieceStand
片方の手番の持駒の枚数を記録するクラス.
Definition bits/pieceStand.h:38
osl::PieceStand::shift
static const CArray< unsigned char, PTYPE_MAX+1 > shift
Definition bits/pieceStand.h:44
osl::PieceStand::add
void add(Ptype type, unsigned int num=1)
Definition bits/pieceStand.h:68
osl::PieceStand::carryMask
static const unsigned int carryMask
Definition bits/pieceStand.h:42
osl::PieceStand::testCarries
unsigned int testCarries() const
Definition bits/pieceStand.h:137
osl::PieceStand::tryAdd
void tryAdd(Ptype type)
加算可能なら加える.
Definition pieceStand.cc:47
osl::PieceStand::mask
static const CArray< unsigned char, PTYPE_MAX+1 > mask
Definition bits/pieceStand.h:45
osl::PieceStand::max
const PieceStand max(PieceStand other) const
種類毎に this と other の持駒の多い方を取る
Definition bits/pieceStand.h:171
osl::PieceStand::canAdd
bool canAdd(Ptype type) const
Definition pieceStand.cc:39
osl::PieceStand::PieceStand
PieceStand(unsigned int value=0)
Definition bits/pieceStand.h:48
osl::PieceStand::flags
unsigned int flags
Definition bits/pieceStand.h:46
osl::PieceStand::carryUnchangedAfterSub
bool carryUnchangedAfterSub(const PieceStand &original, const PieceStand &other) const
Definition pieceStand.cc:69
osl::PieceStand::carryUnchangedAfterAdd
bool carryUnchangedAfterAdd(const PieceStand &original, const PieceStand &other) const
Definition pieceStand.cc:60
osl::PieceStand::atMostOneKind
bool atMostOneKind() const
一種類の駒しかない
Definition pieceStand.cc:53
osl::PieceStand::getFlags
unsigned int getFlags() const
Definition bits/pieceStand.h:165
osl::PieceStand::get
unsigned int get(Ptype type) const
Definition bits/pieceStand.h:131
osl::PieceStand::order
static const CArray< Ptype, 7 > order
持駒の表示で良く使われる順番.
Definition bits/pieceStand.h:41
osl::SimpleState
Definition simpleState.h:35
osl::SimpleState::countPiecesOnStand
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
Definition simpleState.h:182
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::ROOK
@ ROOK
Definition basic_type.h:100
osl::BISHOP
@ BISHOP
Definition basic_type.h:99
osl::PAWN
@ PAWN
Definition basic_type.h:95
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::SILVER
@ SILVER
Definition basic_type.h:98
osl::GOLD
@ GOLD
Definition basic_type.h:94
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::Player
Player
Definition basic_type.h:8
osl::operator<<
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
ptypeTable.h
simpleState.h
osl::PieceStandIO::writeNumbers
static std::ostream & writeNumbers(std::ostream &, const PieceStand &stand)
持駒の数を空白区切で出力する.
Definition pieceStand.cc:89
osl::PieceStandIO::readNumbers
static std::istream & readNumbers(std::istream &, PieceStand &stand)
Definition pieceStand.cc:97
osl::misc::BitOp::countBit
static int countBit(Integer mask)
Definition mask.h:160