Tentative Randomless-Entropy variant.
[tagua/yd.git] / src / variants / randomless-entropy / state.cpp
blob93dc97c6274890c7a76633b0b1d58b9baee26d31
1 /*
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.
9 */
11 #include "state.h"
13 #include <core/behaviour.h>
14 #include <core/move.h>
15 #include <core/defaultpoolcollection.h>
16 #include <core/defaultpool.h>
18 #include "colors.h"
19 #include "stone.h"
21 namespace RandomlessEntropy {
23 State::State(const IBehaviour* behaviour,
24 const Point& size)
25 : m_board(size)
26 , m_behaviour(behaviour)
27 , m_delegator(this) {
28 DefaultPoolCollection* c = new DefaultPoolCollection;
29 // only Chaos/Black has a pool
30 c->addPool(Black::self(), new DefaultPool(Black::self()));
31 m_pools = c;
34 State::State(const State& other)
35 : IState()
36 , DefaultState()
37 , m_board(other.m_board)
38 , m_turn(other.m_turn)
39 , m_behaviour(other.m_behaviour)
40 , m_delegator(this)
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();
48 return s;
51 void State::setup() {
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 {
62 return &m_board;
65 Board* State::board() {
66 return &m_board;
69 const IColor* State::turn() const {
70 return m_turn;
73 void State::setTurn(const IColor* turn) {
74 m_turn = 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();
87 if (pools) {
88 delete m_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());
98 } else {
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;
111 else
112 return n;
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