2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
13 #include <core/behaviour.h>
14 #include <core/move.h>
15 #include <core/defaultpoolcollection.h>
16 #include <core/defaultpool.h>
21 namespace RandomlessEntropy
{
23 State::State(const IBehaviour
* behaviour
,
26 , m_behaviour(behaviour
)
28 DefaultPoolCollection
* c
= new DefaultPoolCollection
;
29 // only Chaos/Black has a pool
30 c
->addPool(Black::self(), new DefaultPool(Black::self()));
34 State::State(const State
& other
)
37 , m_board(other
.m_board
)
38 , m_turn(other
.m_turn
)
39 , m_behaviour(other
.m_behaviour
)
41 , m_pools(other
.m_pools
) { }
43 State::~State() { delete m_pools
; }
45 IState
* State::clone() const {
46 State
* s
= new State(*this);
47 s
->m_pools
= m_pools
->clone();
52 // Chaos starts, board is empty
53 m_turn
= Black::self();
54 // FIXME: all pieces in pool
55 IPool
* pool
= pools()->pool(Black::self());
56 pool
->insert(-1, Piece(Black::self(), Stone::color(0)));
57 pool
->insert(-1, Piece(Black::self(), Stone::color(1)));
58 pool
->insert(-1, Piece(Black::self(), Stone::color(2)));
61 const Board
* State::board() const {
65 Board
* State::board() {
69 const IColor
* State::turn() const {
73 void State::setTurn(const IColor
* turn
) {
77 bool State::equals(IState
* other
) const {
78 return m_board
.equals(other
->board()) &&
79 m_turn
== other
->turn();
82 void State::assign(const IState
* other
) {
83 m_board
= *other
->board();
84 m_turn
= other
->turn();
86 const IPoolCollection
* pools
= other
->pools();
89 m_pools
= pools
->clone();
93 void State::move(const Move
& m
) {
94 if (m
.drop() != Piece()) {
95 board()->set(m
.dst(), m
.drop());
96 pools()->pool(m
.drop().color())->take(m
.drop());
99 const Piece piece
= m_board
.get(m
.src());
100 if (piece
== Piece()) return;
102 behaviour()->move(m_delegator
, m
);
105 behaviour()->advanceTurn(m_delegator
);
108 int State::rank(int n
, const IColor
* turn
) const {
109 if (turn
== White::self())
110 return m_board
.size().y
- n
- 1;
115 IPoolCollection
* State::pools() { return m_pools
; }
116 const IPoolCollection
* State::pools() const { return m_pools
; }
118 const IBehaviour
* State::behaviour() const { return m_behaviour
; }
120 void State::setDelegator(IState
* delegator
) { m_delegator
= delegator
; }
122 Component
* State::clone(const IBehaviour
* behaviour
,
123 const Point
& size
) const {
124 return new State(behaviour
, size
);
127 } // namespace RandomlessEntropy