move.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
move.cc
Go to the documentation of this file.
1 #include "osl/piece.h"
2 #include "osl/move.h"
3 #include "osl/state/simpleState.h"
4 #include "osl/squareCompressor.h"
5 #include "move-phash.c"
6 #include <boost/static_assert.hpp>
7 #include <iostream>
8
9 namespace osl
10 {
11 BOOST_STATIC_ASSERT(sizeof(Move) == 4);
12 } //namespace osl
13
14 bool osl::Move::isValid() const
15 {
16 if (! isNormal())
17 return false;
18 const Square from = this->from();
19 if (! from.isValid())
20 return false;
21 const Square to = this->to();
22 if (! to.isOnBoard())
23 return false;
24 return osl::isValid(ptype())
25 && osl::isValid(capturePtype())
26 && capturePtype()!=KING
27 && osl::isValid(player());
28 }
29
30 const osl::Move osl::Move::rotate180() const
31 {
32 if (isPass())
33 return Move::PASS(alt(player()));
34 if (! isNormal())
35 return *this;
36 return Move(from().rotate180Safe(), to().rotate180(), ptype(),
37 capturePtype(), isPromotion(), alt(player()));
38 }
39
40 std::ostream& osl::operator<<(std::ostream& os,const Move move)
41 {
42 if (move == Move::DeclareWin())
43 return os << "MOVE_DECLARE_WIN";
44 if (move.isInvalid())
45 return os << "MOVE_INVALID";
46 if (move.isPass())
47 return os << "MOVE_PASS";
48 const Player turn = move.player();
49 if (move.isValid())
50 {
51 if (move.from().isPieceStand())
52 {
53 os << "Drop(" << turn << "," << move.ptype() << "," << move.to() << ")";
54 }
55 else
56 {
57 const Ptype capture_ptype=move.capturePtype();
58 os << "Move(" << turn << "," << move.ptype() << ","
59 << move.from() << "->" << move.to() ;
60 if (move.promoteMask())
61 os << ",promote";
62 if (capture_ptype != PTYPE_EMPTY)
63 os << ",capture=" << capture_ptype;
64 os << ")";
65 }
66 }
67 else
68 {
69 os << "InvalidMove " << move.from() << " " << move.to()
70 << " " << move.ptypeO() << " " << move.oldPtypeO()
71 << " " << move.promoteMask()
72 << " " << move.capturePtype() << "\n";
73 }
74 return os;
75 }
76
77 unsigned int osl::Move::hash() const
78 {
79 assert(capturePtype() == PTYPE_EMPTY);
80 return move_phash(intValue());
81 }
82
83 const osl::Move osl::Move::
84 fromMove16(Move16 move16, const SimpleState& state)
85 {
86 if (move16==MOVE16_NONE)
87 return Move();
88 Player turn=state.turn();
89 Square to=SquareCompressor::melt((move16>>8)&0x7f);
90 if((move16&0x80)!=0){
91 Ptype ptype=(Ptype)(move16-0x80);
92 return Move(to,ptype,turn);
93 }
94 Square from=SquareCompressor::melt(move16&0x7f);
95 Ptype ptype=state[from].ptype();
96 Ptype capture_ptype=state[to].ptype();
97 bool is_promote=(move16&0x8000)!=0;
98 if(is_promote)
99 return Move(from,to,::osl::promote(ptype),capture_ptype,true,turn);
100 else
101 return Move(from,to,ptype,capture_ptype,false,turn);
102 }
103 osl::Move16 osl::Move::toMove16() const
104 {
105 if (isInvalid())
106 return MOVE16_NONE;
107 if (isDrop())
108 return Move16(0x80+(uint16_t)ptype()+((SquareCompressor::compress(to()))<<8));
109 if (isPromotion())
110 return Move16(SquareCompressor::compress(from())+(SquareCompressor::compress(to())<<8)+0x8000);
111 return Move16(SquareCompressor::compress(from())+(SquareCompressor::compress(to())<<8));
112 }
113
114
115 // ;;; Local Variables:
116 // ;;; mode:c++
117 // ;;; c-basic-offset:2
118 // ;;; End: