pong: end game when max score reached
Stefan Schuermans

Stefan Schuermans commited on 2019-06-16 13:24:22
Showing 3 changed files, with 44 additions and 4 deletions.

... ...
@@ -29,6 +29,8 @@ namespace Blinker {
29 29
 
30 30
 /// descriptor for delay value
31 31
 Pong::ValueDescr const Pong::c_delayDescr = { 200, 100, 500 };
32
+/// descriptor for maximum score value
33
+Pong::ValueDescr const Pong::c_maxScoreDescr = { 9, 1, 99 };
32 34
 
33 35
 /// operator connection name suffix for left player's connection
34 36
 std::string const Pong::c_opConnSuffixLeft = "/left";
... ...
@@ -50,9 +52,10 @@ Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
50 52
   m_fileScoreColor(dirBase.getFile("scoreColor")),
51 53
   m_fileGoalColor(dirBase.getFile("goalColor")),
52 54
   m_fileDelay(dirBase.getFile("delay")),
55
+  m_fileMaxScore(dirBase.getFile("maxScore")),
53 56
   m_ballColor(), m_lineColor(), m_padColor(), m_computerColor(),
54 57
   m_scoreColor(), m_goalColor(),
55
-  m_delay(c_delayDescr.default_),
58
+  m_delay(c_delayDescr.default_), m_maxScore(c_maxScoreDescr.default_),
56 59
   m_pConnLeft(NULL), m_pConnRight(NULL),
57 60
   m_ballPosX(-1), m_ballPosY(-1), m_ballDirX(0), m_ballDirY(0),
58 61
   m_padSize(0), m_leftPosY(0), m_rightPosY(0), m_leftDelay(0), m_rightDelay(0),
... ...
@@ -96,7 +99,8 @@ bool Pong::updateConfigGame()
96 99
       colorUpdate(m_fileComputerColor, m_computerColor) ||
97 100
       colorUpdate(m_fileScoreColor, m_scoreColor) ||
98 101
       colorUpdate(m_fileGoalColor, m_goalColor) ||
99
-      valueUpdate(m_fileDelay, c_delayDescr, m_delay)) {
102
+      valueUpdate(m_fileDelay, c_delayDescr, m_delay) ||
103
+      valueUpdate(m_fileMaxScore, c_maxScoreDescr, m_maxScore)) {
100 104
     ret = true;
101 105
   }
102 106
 
... ...
@@ -232,6 +236,7 @@ void Pong::reinitialize()
232 236
   color2data(m_fileGoalColor, m_goalColor);
233 237
   // get values
234 238
   valueFromFile(m_fileDelay, c_delayDescr, m_delay);
239
+  valueFromFile(m_fileMaxScore, c_maxScoreDescr, m_maxScore);
235 240
 
236 241
   // reset scores
237 242
   m_scoreLeft = 0;
... ...
@@ -307,10 +312,19 @@ void Pong::timeCall()
307 312
 
308 313
     --m_goalDelay;
309 314
 
310
-    // new ball when delay is over
315
+    // delay is over
311 316
     if (m_goalDelay <= 0) {
317
+        // maximum score reached
318
+        if (m_scoreLeft >= (int)m_maxScore ||
319
+            m_scoreRight >= (int)m_maxScore) {
320
+          gameOver();
321
+          return; // no gameOver() calls deactivate(), no need to redraw
322
+        }
323
+        // game continues with new ball
324
+        else {
312 325
           startBall();
313
-        return; // startBall calls redraw() and planTimeCall()
326
+          return; // startBall() calls redraw() and planTimeCall()
327
+        }
314 328
     }
315 329
 
316 330
   }
... ...
@@ -608,5 +622,23 @@ void Pong::startBall()
608 622
   planTimeCall();
609 623
 }
610 624
 
625
+
626
+/// game over: close player connections and deactivate
627
+void Pong::gameOver()
628
+{
629
+  // close open operator connections
630
+  if (m_pConnLeft) {
631
+    m_pConnLeft->close();
632
+    m_pConnLeft = NULL;
633
+  }
634
+  if (m_pConnRight) {
635
+    m_pConnRight->close();
636
+    m_pConnRight = NULL;
637
+  }
638
+
639
+  // deactivate game
640
+  deactivate();
641
+}
642
+
611 643
 } // namespace Blinker
612 644
 
... ...
@@ -162,10 +162,15 @@ protected:
162 162
   /// start ball
163 163
   void startBall();
164 164
 
165
+  /// game over: close player connections and deactivate
166
+  void gameOver();
167
+
165 168
 protected:
166 169
 
167 170
   /// descriptor for delay value
168 171
   static ValueDescr const c_delayDescr;
172
+  /// descriptor for maximum score value
173
+  static ValueDescr const c_maxScoreDescr;
169 174
 
170 175
   /// operator connection name suffix for left player's connection
171 176
   static std::string const c_opConnSuffixLeft;
... ...
@@ -179,6 +184,7 @@ protected:
179 184
   ColorFile m_fileScoreColor;    ///< color file for score when ball is moving
180 185
   ColorFile m_fileGoalColor;     ///< color file for score during goal
181 186
   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
187
+  UIntFile  m_fileMaxScore;      ///< file for maximum score (end of game)
182 188
 
183 189
   ColorData    m_ballColor;     ///< ball color
184 190
   ColorData    m_lineColor;     ///< center line color
... ...
@@ -187,6 +193,7 @@ protected:
187 193
   ColorData    m_scoreColor;    ///< score color when ball is moving
188 194
   ColorData    m_goalColor;     ///< score color during goal
189 195
   unsigned int m_delay;         ///< initial delay in ms per frame (ball speed)
196
+  unsigned int m_maxScore;      ///< maximum score (end of game)
190 197
 
191 198
   OpConn *m_pConnLeft;  ///< operator connection left player (or NULL)
192 199
   OpConn *m_pConnRight; ///< operator connection right player (or NULL)
193 200