af2ae84d0ddf8d230d6c7d31398db66a2295bf5e
[tagua/yd.git] / src / variants / crazyhouse / state.cpp
blobaf2ae84d0ddf8d230d6c7d31398db66a2295bf5e
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 */
10 #include "state.h"
11 #include "global.h"
13 #include <core/behaviour.h>
14 #include <core/board.h>
15 #include <core/color.h>
16 #include <core/defaultpoolcollection.h>
17 #include <core/defaultpool.h>
18 #include <core/move.h>
19 #include <core/piece.h>
20 #include <core/taguacast.h>
22 namespace Crazyhouse {
24 State::State(IState* state)
25 : Delegators::State(state) {
26 DefaultPoolCollection* c = new DefaultPoolCollection;
27 for (int i = 0; i < 2; i++)
28 c->addPool(players[i], new DefaultPool(players[i]));
29 m_pools = c;
32 State::~State() { delete m_pools; }
34 IState* State::clone() const {
35 State* s = new State(m_state->clone());
36 s->m_pools = m_pools->clone();
37 return s;
40 void State::assign(const IState* other) {
41 m_state->assign(other);
42 const IPoolCollection* pools = other->pools();
43 if (pools) {
44 delete m_pools;
45 m_pools = pools->clone();
49 void State::move(const Move& m) {
50 if (m.drop() != Piece()) {
51 Piece captured = board()->get(m.dst());
52 board()->set(m.dst(), m.drop());
53 pools()->pool(m.drop().color())->take(m.drop());
55 // handle capturing by drop: some variants could use it
56 if (captured != Piece()) {
57 if (captured.get("promoted").toBool()) {
58 captured.setType(pawn);
60 pools()->pool(behaviour()->opponent(captured.color()))
61 ->insert(-1, captured);
64 behaviour()->advanceTurn(this);
66 else {
67 m_state->move(m);
68 if (m.promotion()) {
69 Piece promoted = board()->get(m.dst());
70 promoted.set("promoted", true);
71 board()->set(m.dst(), promoted);
76 const IPoolCollection* State::pools() const { return m_pools; }
77 IPoolCollection* State::pools() { return m_pools; }
79 } // namespace Crazyhouse