kblackbox
kbbgamedoc.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "kbbgamedoc.h"
00031
00032
00033
00034
00035
00036 #include "kbbballsonboard.h"
00037 #include "kbbtutorial.h"
00038
00039
00040
00041
00042
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
00063
00064
00065 int KBBGameDoc::columns() const
00066 {
00067 return m_columns;
00068 }
00069
00070
00071 void KBBGameDoc::gameOver()
00072 {
00073
00074 setRunning(false);
00075
00076
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
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
00173
00174
00175 void KBBGameDoc::clean(const int columns, const int rows)
00176 {
00177 m_columns = columns;
00178 m_rows = rows;
00179
00180
00181 setRunning(false);
00182 m_ballsPlaced->newBoard(m_columns, m_rows);
00183 setScore(-1);
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"