• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kblackbox

kbbgamedoc.cpp

Go to the documentation of this file.
00001 //
00002 // KBlackBox
00003 //
00004 // A simple game inspired by an emacs module
00005 //
00006 /***************************************************************************
00007  *   Copyright (c) 1999-2000, Robert Cimrman                               *
00008  *   cimrman3@students.zcu.cz                                              *
00009  *                                                                         *
00010  *   Copyright (c) 2007, Nicolas Roffet                                    *
00011  *   nicolas-kde@roffet.com                                                *
00012  *                                                                         *
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   (at your option) any later version.                                   *
00018  *                                                                         *
00019  *   This program is distributed in the hope that it will be useful,       *
00020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00022  *   GNU General Public License for more details.                          *
00023  *                                                                         *
00024  *   You should have received a copy of the GNU General Public License     *
00025  *   along with this program; if not, write to the                         *
00026  *   Free Software Foundation, Inc.,                                       *
00027  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
00028  ***************************************************************************/
00029 
00030 #include "kbbgamedoc.h"
00031 
00032 
00033 
00034 
00035 
00036 #include "kbbballsonboard.h"
00037 #include "kbbtutorial.h"
00038 
00039 
00040 
00041 //
00042 // Constructor / Destructor
00043 //
00044 
00045 KBBGameDoc::KBBGameDoc(KBBMainWindow *parent, KBBTutorial* tutorial) : QObject(parent)
00046 {
00047     setRunning(false);
00048     m_columns = 1;
00049     m_rows = 1;
00050     m_tutorial = tutorial;
00051     
00052     m_random.setSeed(0);
00053     
00054     m_balls = new KBBBallsOnBoard(this, m_columns, m_rows);
00055     m_ballsPlaced = new KBBBallsOnBoard(this, m_columns, m_rows);
00056     connect(m_ballsPlaced, SIGNAL(changes()), parent, SLOT(updateStats()));
00057 }
00058 
00059 
00060 
00061 //
00062 // Public
00063 //
00064 
00065 int KBBGameDoc::columns() const
00066 {
00067     return m_columns;
00068 }
00069 
00070 
00071 void KBBGameDoc::gameOver()
00072 {
00073     // Clear
00074     setRunning(false);
00075     
00076     // Compute final score
00077     if (m_ballsPlaced->numberOfBallsNotIn(m_balls)>0)
00078         setScore(SCORE_LOST);
00079 }
00080 
00081 
00082 bool KBBGameDoc::gameReallyStarted()
00083 {
00084     return m_gameReallyStarted;
00085 }
00086 
00087 
00088 bool KBBGameDoc::mayShootRay(const int incomingPosition) const
00089 {
00090     if (m_tutorial->isVisible() && !m_tutorial->mayShootRay(incomingPosition))
00091         return false;
00092     else
00093         return true;
00094 }
00095 
00096 
00097 void KBBGameDoc::newGame(int balls, int columns, int rows)
00098 {
00099     clean(columns, rows);
00100 
00101     // Puts the balls in the black box on random positions.
00102     int boxPos;
00103     for (int i = 0; i < balls; i++) {
00104         do {
00105             boxPos = m_random.getLong(m_columns * m_rows);
00106         } while (m_balls->contains(boxPos));
00107         m_balls->add(boxPos);
00108     }
00109 }
00110 
00111 
00112 int KBBGameDoc::numberOfBallsPlaced()
00113 {
00114     return m_ballsPlaced->count();
00115 }
00116 
00117 
00118 int KBBGameDoc::numberOfBallsToPlace()
00119 {
00120     return m_balls->count();
00121 }
00122 
00123 
00124 int KBBGameDoc::rows() const
00125 {
00126     return m_rows;
00127 }
00128 
00129 
00130 int KBBGameDoc::score()
00131 {
00132     return m_score;
00133 }
00134 
00135 
00136 int KBBGameDoc::shootRay( int borderPosition )
00137 {
00138     int outgoingBorderPosition = m_balls->oppositeBorderPosition(borderPosition);
00139     
00140     if ((outgoingBorderPosition == HIT_POSITION) || (borderPosition == outgoingBorderPosition))
00141         setScore(m_score + SCORE_ONE);
00142     else
00143         setScore(m_score + SCORE_TWO);
00144     
00145     if (!m_tutorial->isVisible())
00146         setRunning(true);
00147     emit updateStats();
00148 
00149     return outgoingBorderPosition;
00150 }
00151 
00152 
00153 void KBBGameDoc::startTutorial()
00154 {
00155     clean(KBBTutorial::COLUMNS, KBBTutorial::ROWS);
00156     m_balls->add(16);
00157     m_balls->add(21);
00158     m_balls->add(33);
00159     m_tutorial->setStep(1);
00160     m_tutorial->start();
00161 }
00162 
00163 
00164 void KBBGameDoc::timeChanged()
00165 {
00166     setScore(m_score+1);
00167 }
00168 
00169 
00170 
00171 //
00172 // Private
00173 //
00174 
00175 void KBBGameDoc::clean(const int columns, const int rows)
00176 {
00177     m_columns = columns;
00178     m_rows = rows;
00179 
00180     // Clear
00181     setRunning(false);
00182     m_ballsPlaced->newBoard(m_columns, m_rows);
00183     setScore(-1); // -1 because a signal "timeChanged" will be send at the beginning and the score will be set to 0.
00184 
00185     m_balls->newBoard(m_columns, m_rows);
00186 }
00187 
00188 
00189 void KBBGameDoc::setRunning(const bool r)
00190 {
00191     m_gameReallyStarted = r;
00192     emit isRunning(r);
00193 }
00194 
00195 
00196 void KBBGameDoc::setScore( int n )
00197 {
00198     if (n<1000)
00199         m_score = n;
00200     else
00201         m_score = 999;
00202     emit updateStats();
00203 }
00204 
00205 #include "kbbgamedoc.moc"

kblackbox

Skip menu "kblackbox"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal