add helper scripts
[mancala] / src / Board.cpp
1 /*
2 Mancala - A Historical Board Game
3 Copyright (C) 2009-2010 A.H.M.Mahfuzur Rahman 65mahfuz90@gmail.com
4 Copyright (c) 2010 Reto Zingg g.d0b3rm4n@gmail.com
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "Board.h"
22
23 #include "math.h"
24
25 #include <QDebug>
26
27 // #include <kdebug.h>
28
29 #include "Cup.h"
30 #include "Kalah.h"
31 #include "Stone.h"
32 #include "GameInfo.h"
33 #include "ThemeManager.h"
34
35
36 Board::Board(GameInfo *gameInfo, ThemeManager *theme)
37     : QGraphicsSvgItem(0)
38 {
39     setSharedRenderer(theme);
40     m_gameInfo = gameInfo;
41     m_theme = theme;
42     setElementId("board");
43     initialSetup();
44     setZValue(1);
45
46     qreal posX,posY,width,height,scaleX,scaleY;
47
48     posX = sceneBoundingRect().x() + sceneBoundingRect().width() * 0.0125; // 0.15
49     posY = sceneBoundingRect().y() + sceneBoundingRect().height() * 0.03; // 0.8
50     width = 1.16 * sceneBoundingRect().width(); // 0.85
51     // height = width / 1.75 ; // 2.5
52     height = 2.6 * sceneBoundingRect().height();
53
54     scaleX = width / ( boundingRect().width() );
55     scaleY = height / ( boundingRect().height() ) ;
56     scale(scaleX,scaleY);
57
58     setPos(posX,posY);
59     
60 //     qDebug() << "posX" << posX;
61 //     qDebug() << "posY" << posY;
62 //     qDebug() << "scaleX" << scaleX;
63 //     qDebug() << "scaleY" << scaleY;
64 //     qDebug() << "width" << width;
65 //     qDebug() << "height" << height;
66 //     qDebug() << "boundingRect width" << boundingRect().width();
67 //     qDebug() << "boundingRect height" << boundingRect().height();
68 //     qDebug() << "sceneBoundingRect().height()" << sceneBoundingRect().height();
69 //     qDebug() << "sceneBoundingRect().width()" << sceneBoundingRect().width();
70 //     qDebug() << "sceneBoundingRect().y()" << sceneBoundingRect().y();
71 //     qDebug() << "sceneBoundingRect().x()" << sceneBoundingRect().x();
72    
73 }
74
75 Board::~Board(){
76     //kDebug() << "Destructor of Board";
77     while(m_cups.count())
78         m_cups.pop_front();
79 }
80
81 void Board::initialSetup(){
82     createKalahs();
83     createCups();
84 }
85
86 void Board::createKalahs(){
87
88     //If the game supports Kalah(store) then create kalah
89     if(m_gameInfo->numKalahs()){
90         m_leftKalah = new Kalah(m_gameInfo, Kalah::Left, this);
91         m_rightKalah = new Kalah(m_gameInfo, Kalah::Right, this);
92     }
93     else return;
94 }
95
96 void Board::createCups(){
97
98     int i = 0;
99
100     //kDebug() << "---------Total in the list before first push---------: " << m_cups.count();
101     for(int row = 0; row < m_gameInfo->numRows(); row++){
102         for(int column = 0; column < m_gameInfo->numCupsPerRow(); column++){
103             //appending to the list of cup
104             m_cups << new Cup(m_gameInfo, i++ , this);
105         }
106     }
107
108 }
109
110 void Board::manipulateStones(QList<Stone*>& currentStoneList){
111
112     int j = 0 , stonesPerCupInitially = m_gameInfo->initialStonesPerCup();
113     Cup* cup;
114     m_stones = currentStoneList;
115
116     foreach(cup,m_cups){
117         for(int i = 0; i < stonesPerCupInitially ; i++ ){
118             cup->addStone(m_stones[j++]);
119         }
120     }
121
122 }
123
124
125 int Board::findCupIndex(QPointF position){
126     Cup* cup;
127     foreach(cup,m_cups){
128
129         if(cup->shape().contains( cup->mapFromScene(position) )){
130             return cup->index();
131         }
132     }
133     return -1;
134 }
135
136
137 void Board::updateChildren(int cupIndex,int kalahIndex,int stoneNo){
138
139     //kDebug() << "Int3 is called";
140
141     for(int i = 0 ; i < stoneNo ; i++){
142         //Remove from cup a stone
143         Stone *stone = m_cups[cupIndex]->updateStoneList();
144
145         if(kalahIndex == Kalah::Left){
146             m_leftKalah->updateStoneList(stone);
147         }
148         else m_rightKalah->updateStoneList(stone);
149         update();
150     }
151     update();
152 }
153
154 //It will remove stone from cupIndex and push it to kalah[index] or cup[index]
155 void Board::updateChildren(int cupIndex,int index,bool isCup){
156
157     Stone* stone = m_cups[cupIndex]->updateStoneList();
158
159     m_isCup = isCup;
160     m_index = index;
161     m_stone = stone;
162
163     slotUpdateChildren();
164     update();
165 }
166
167
168 void Board::slotUpdateChildren(){
169
170     if(m_isCup)
171         m_cups[m_index]->updateStoneList(m_stone);
172
173     else{
174         if(m_index == Kalah::Left)
175             m_leftKalah->updateStoneList(m_stone);
176         else m_rightKalah->updateStoneList(m_stone);
177     }
178 }
179
180