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
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.
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.
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/>.
24 #include <QGraphicsScene>
25 #include <QGraphicsSvgItem>
26 // #include <kdebug.h>
33 Kalah::Kalah(GameInfo* info, Kalah::side p, QGraphicsSvgItem *parent)
34 : QGraphicsSvgItem(parent){
38 m_board = qgraphicsitem_cast<Board*>(parent);
40 setSharedRenderer(m_board->renderer());
41 setElementId("kalah");
49 //The Drawing of Kalah Items
50 void Kalah::initialSetup(){
52 qreal gapWidth = m_board->boundingRect().width() * ( 0.2 / ( m_gameInfo->numCupsPerRow() + m_gameInfo->numKalahs() + 1 ) );
54 m_size.setWidth( m_board->boundingRect().width() * 0.8 / ( m_gameInfo->numCupsPerRow() + m_gameInfo->numKalahs() ) );
55 m_size.setHeight( m_board->boundingRect().height() * 0.8 );
57 //kalahWidth and cupWidth are same
59 m_pos.setX( m_board->boundingRect().x() + gapWidth );
60 else m_pos.setX( m_board->boundingRect().x() + gapWidth + ( gapWidth + m_size.width() ) * ( m_gameInfo->numCupsPerRow() + 1 ) );
61 m_pos.setY( m_board->boundingRect().y() + m_board->boundingRect().height() * 0.1 );
63 //scaling the kalah object
64 qreal kalahScaleX = m_size.width() / boundingRect().width();
65 qreal kalahScaleY = m_size.height()/ boundingRect().height();
67 m_mid_point.setX( m_pos.x() + m_size.width() / 2 );
68 m_mid_point.setY( m_pos.y() + m_size.height() / 2 );
71 scale(kalahScaleX,kalahScaleY);
74 void Kalah::setupText(){
76 m_text = new QGraphicsSimpleTextItem("0",m_board);
77 m_text->setFont( QFont("Comic Sans MS", 24, QFont::Bold) );
78 m_text->setPen(QPen(QColor("darkslateblue"), 1.5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
79 QLinearGradient gradient(0, 0, 0, 100);
80 gradient.setColorAt(0.0, "mediumslateblue");
81 gradient.setColorAt(1.0, "cornsilk");
82 m_text->setBrush(gradient);
84 int textX = m_pos.x() + m_size.width();
85 textX -= m_text->boundingRect().size().width() + 15;
87 int textY = m_pos.y() + m_size.height();
88 textY -= m_text->boundingRect().size().height() - 15;
90 m_text->scale(0.8,0.8);
91 m_text->setPos(textX,textY);
93 m_text->setVisible(false);
96 void Kalah::updateStoneList(Stone* stoneItem){
98 //kDebug() << "In Kalah" ;
101 m_text->setVisible(false);
102 m_stone.append(stoneItem);
104 //calculate stone postion and place it
105 stonePos = calculateStonePosition(stoneItem);
106 stoneItem->animateStones(QPointF(stonePos->x(),stonePos->y()));
108 //we want to change the text showing ball number
109 if( m_stone.count() ){
111 m_text->setText( QString("%1").arg(m_stone.count() ) );
112 m_text->setVisible(true);
117 //kDebug() << "Returning";
121 //calculate stonePostion in Kalah
122 QPointF* Kalah::calculateStonePosition(Stone* stone){
124 QPointF *returnPoint = new QPointF();
126 qreal diameterCup = qMin(m_size.width(),m_size.height());
128 int initialLength = qrand() % (int) diameterCup / 3;
129 int angle = qrand() % 360;
131 qreal posX = middlePoint().x() + initialLength * cos(angle * M_PI / 180);
132 qreal posY = middlePoint().y() + initialLength * sin(angle * M_PI / 180);
134 if( ( posX + stone->size().width()) > ( m_pos.x() + 0.7 * m_size.width() ) )
135 posX -= stone->size().width();
136 else if( posX < ( m_pos.x()+ 0.3 * m_size.width() ) )
137 posX += stone->size().width();
139 if( ( posY + stone->size().height() ) > ( m_pos.y() + 0.7 * m_size.height() ) )
140 posY -= stone->size().height();
141 else if( posY < (m_pos.y() + 0.3 < m_size.height() ) )
142 posY += stone->size().height();
144 returnPoint->setX(posX);
145 returnPoint->setY(posY);