• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kblackbox

  • sources
  • kde-4.14
  • kdegames
  • kblackbox
kbbgamedoc.cpp
Go to the documentation of this file.
1 //
2 // KBlackBox
3 //
4 // A simple game inspired by an emacs module
5 //
6 /***************************************************************************
7  * Copyright (c) 1999-2000, Robert Cimrman *
8  * cimrman3@students.zcu.cz *
9  * *
10  * Copyright (c) 2007, Nicolas Roffet *
11  * nicolas-kde@roffet.com *
12  * *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the *
26  * Free Software Foundation, Inc., *
27  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA *
28  ***************************************************************************/
29 
30 #include "kbbgamedoc.h"
31 
32 
33 
34 
35 
36 #include "kbbballsonboard.h"
37 #include "kbbtutorial.h"
38 
39 
40 
41 //
42 // Constructor / Destructor
43 //
44 
45 KBBGameDoc::KBBGameDoc(KBBMainWindow *parent, KBBTutorial* tutorial) : QObject(parent)
46 {
47  setRunning(false);
48  m_columns = 1;
49  m_rows = 1;
50  m_tutorial = tutorial;
51 
52  m_random.setSeed(0);
53 
54  m_balls = new KBBBallsOnBoard(this, m_columns, m_rows);
55  m_ballsPlaced = new KBBBallsOnBoard(this, m_columns, m_rows);
56  connect(m_ballsPlaced, SIGNAL(changes()), parent, SLOT(updateStats()));
57 }
58 
59 
60 
61 //
62 // Public
63 //
64 
65 int KBBGameDoc::columns() const
66 {
67  return m_columns;
68 }
69 
70 
71 void KBBGameDoc::gameOver()
72 {
73  // Clear
74  setRunning(false);
75 
76  // Compute final score
77  if (m_ballsPlaced->numberOfBallsNotIn(m_balls)>0)
78  setScore(SCORE_LOST);
79 }
80 
81 
82 bool KBBGameDoc::gameReallyStarted()
83 {
84  return m_gameReallyStarted;
85 }
86 
87 
88 bool KBBGameDoc::mayShootRay(const int incomingPosition) const
89 {
90  if (m_tutorial->isVisible() && !m_tutorial->mayShootRay(incomingPosition))
91  return false;
92  else
93  return true;
94 }
95 
96 
97 void KBBGameDoc::newGame(int balls, int columns, int rows)
98 {
99  clean(columns, rows);
100 
101  // Puts the balls in the black box on random positions.
102  int boxPos;
103  for (int i = 0; i < balls; i++) {
104  do {
105  boxPos = m_random.getLong(m_columns * m_rows);
106  } while (m_balls->contains(boxPos));
107  m_balls->add(boxPos);
108  }
109 }
110 
111 
112 int KBBGameDoc::numberOfBallsPlaced()
113 {
114  return m_ballsPlaced->count();
115 }
116 
117 
118 int KBBGameDoc::numberOfBallsToPlace()
119 {
120  return m_balls->count();
121 }
122 
123 
124 int KBBGameDoc::rows() const
125 {
126  return m_rows;
127 }
128 
129 
130 int KBBGameDoc::score()
131 {
132  return m_score;
133 }
134 
135 
136 int KBBGameDoc::shootRay( int borderPosition )
137 {
138  int outgoingBorderPosition = m_balls->oppositeBorderPosition(borderPosition);
139 
140  if ((outgoingBorderPosition == HIT_POSITION) || (borderPosition == outgoingBorderPosition))
141  setScore(m_score + SCORE_ONE);
142  else
143  setScore(m_score + SCORE_TWO);
144 
145  if (!m_tutorial->isVisible())
146  setRunning(true);
147  emit updateStats();
148 
149  return outgoingBorderPosition;
150 }
151 
152 
153 void KBBGameDoc::startTutorial()
154 {
155  clean(KBBTutorial::COLUMNS, KBBTutorial::ROWS);
156  m_balls->add(16);
157  m_balls->add(21);
158  m_balls->add(33);
159  m_tutorial->setStep(1);
160  m_tutorial->start();
161 }
162 
163 
164 void KBBGameDoc::timeChanged()
165 {
166  setScore(m_score+1);
167 }
168 
169 
170 
171 //
172 // Private
173 //
174 
175 void KBBGameDoc::clean(const int columns, const int rows)
176 {
177  m_columns = columns;
178  m_rows = rows;
179 
180  // Clear
181  setRunning(false);
182  m_ballsPlaced->newBoard(m_columns, m_rows);
183  setScore(-1); // -1 because a signal "timeChanged" will be send at the beginning and the score will be set to 0.
184 
185  m_balls->newBoard(m_columns, m_rows);
186 }
187 
188 
189 void KBBGameDoc::setRunning(const bool r)
190 {
191  m_gameReallyStarted = r;
192  emit isRunning(r);
193 }
194 
195 
196 void KBBGameDoc::setScore( int n )
197 {
198  if (n<1000)
199  m_score = n;
200  else
201  m_score = 999;
202  emit updateStats();
203 }
204 
205 #include "kbbgamedoc.moc"
KBBGameDoc::SCORE_TWO
static const int SCORE_TWO
Definition: kbbgamedoc.h:69
KBBGameDoc::KBBGameDoc
KBBGameDoc(KBBMainWindow *parent, KBBTutorial *tutorial)
Constructor.
Definition: kbbgamedoc.cpp:45
KBBGameDoc::gameReallyStarted
bool gameReallyStarted()
Check if the player started to play.
Definition: kbbgamedoc.cpp:82
KBBGameDoc::SCORE_LOST
static const int SCORE_LOST
Definition: kbbgamedoc.h:67
QWidget::isVisible
bool isVisible() const
kbbgamedoc.h
KBBTutorial::COLUMNS
static int const COLUMNS
Number of columns in the tutorial.
Definition: kbbtutorial.h:64
KBBGameDoc::shootRay
int shootRay(int borderPosition)
Shoot a ray.
Definition: kbbgamedoc.cpp:136
KBBTutorial::ROWS
static int const ROWS
Number of rows in the tutorial.
Definition: kbbtutorial.h:69
KBBGameDoc::updateStats
void updateStats()
KBBGameDoc::m_balls
KBBBallsOnBoard * m_balls
Definition: kbbgamedoc.h:144
KBBBallsOnBoard::oppositeBorderPosition
int oppositeBorderPosition(int borderPosition)
Compute the opposite border position of the given position.
Definition: kbbballsonboard.cpp:145
kbbballsonboard.h
KBBGameDoc::numberOfBallsPlaced
int numberOfBallsPlaced()
Number of balls the user placed on the board.
Definition: kbbgamedoc.cpp:112
KBBGameDoc::SCORE_ONE
static const int SCORE_ONE
Definition: kbbgamedoc.h:68
QObject
KBBTutorial
Tutorial widget.
Definition: kbbtutorial.h:51
KBBGameDoc::HIT_POSITION
static const int HIT_POSITION
When a laser ray enter the black box, it exits on a defined border position, except if the laser ray ...
Definition: kbbgamedoc.h:65
KBBTutorial::start
void start()
Start the tutorial Start or restart the tutorial at the 1st step.
Definition: kbbtutorial.cpp:251
KBBGameDoc::timeChanged
void timeChanged()
Definition: kbbgamedoc.cpp:164
kbbtutorial.h
KBBBallsOnBoard::newBoard
void newBoard(const int columns, const int rows)
Define a new board and remove all balls.
Definition: kbbballsonboard.cpp:124
KBBGameDoc::newGame
void newGame(int balls, int columns, int rows)
Create new board game and initialize game.
Definition: kbbgamedoc.cpp:97
KBBBallsOnBoard::contains
bool contains(int boxPosition)
Check if there is a ball at the given position in the black box.
Definition: kbbballsonboard.cpp:112
KBBGameDoc::mayShootRay
bool mayShootRay(const int incomingPosition) const
Definition: kbbgamedoc.cpp:88
KBBBallsOnBoard::numberOfBallsNotIn
int numberOfBallsNotIn(KBBBallsOnBoard *otherBoard)
Compares 2 boards and return the number of differences.
Definition: kbbballsonboard.cpp:133
KBBMainWindow
Main window of the game KBlackBox.
Definition: kbbmainwindow.h:49
KBBTutorial::mayShootRay
bool mayShootRay(const int incomingPosition)
May the player shoot the ray?
Definition: kbbtutorial.cpp:131
KBBGameDoc::columns
int columns() const
Get the number of columns.
Definition: kbbgamedoc.cpp:65
KBBGameDoc::m_ballsPlaced
KBBBallsOnBoard * m_ballsPlaced
Definition: kbbgamedoc.h:145
KBBGameDoc::startTutorial
void startTutorial()
Initialize the tutorial.
Definition: kbbgamedoc.cpp:153
KBBTutorial::setStep
void setStep(const int step)
Go to the given tutorial step.
Definition: kbbtutorial.cpp:155
KBBBallsOnBoard::add
void add(int boxPosition)
Add a ball on this board.
Definition: kbbballsonboard.cpp:78
KBBBallsOnBoard::count
int count()
Number of balls on this board.
Definition: kbbballsonboard.cpp:118
KBBGameDoc::isRunning
void isRunning(bool)
KBBGameDoc::numberOfBallsToPlace
int numberOfBallsToPlace()
Number of balls the user has to place on the board.
Definition: kbbgamedoc.cpp:118
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KBBGameDoc::score
int score()
Get current score.
Definition: kbbgamedoc.cpp:130
KBBGameDoc::gameOver
void gameOver()
Stop the game, show solution and compute final score.
Definition: kbbgamedoc.cpp:71
KBBBallsOnBoard
Set of balls (or various objects) with positions on the board.
Definition: kbbballsonboard.h:62
KBBGameDoc::rows
int rows() const
Get the number of rows.
Definition: kbbgamedoc.cpp:124
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kblackbox

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

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal