home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / GAME.CPP < prev    next >
C/C++ Source or Header  |  1992-02-18  |  1KB  |  51 lines

  1. // GAME.CPP--Example from Chapter 3 "An Introduction to C++"
  2.  
  3. #include <stdlib.h>
  4. #include <iostream.h>
  5. #include <conio.h>
  6.  
  7. const DODGERS = 0;
  8. const GIANTS = 1;
  9.  
  10. void main(void)
  11. {
  12.    int scoreboard [2][9];    // An array two rows by nine columns
  13.    int team, inning;
  14.    int score, total;
  15.  
  16.    randomize();              // Initialize random number generator
  17.  
  18.    // Generate the scores
  19.    for (team = DODGERS; team <= GIANTS; team++) {
  20.       for (inning = 0; inning < 9; inning++) {
  21.          score = random(3);
  22.          if (score == 2)             // 1/3 chance to score at least a run
  23.             score = random(3) + 1;   // 1 to 3 runs
  24.          if (score == 3)
  25.             score = random(7) + 1;   // Simulates chance of a big
  26.                                      // inning of 1 to 7 runs
  27.          scoreboard[team][inning] = score;
  28.       }
  29.    }
  30.  
  31.    // Print the scores
  32.    cout << "\nInning\t1   2   3   4   5   6   7   8   9  Total\n";
  33.    cout << "Dodgers\t";
  34.    total = 0;
  35.    for (inning = 0; inning <= 8; inning++) {
  36.       score = scoreboard[DODGERS][inning];
  37.       total += score;
  38.       cout << score << "   ";
  39.    }
  40.    cout << total << "\n";
  41.  
  42.    cout << "Giants\t";
  43.    total = 0;
  44.    for (inning = 0; inning < 9; inning++) {
  45.       score = scoreboard[GIANTS][inning];
  46.       total += score;
  47.       cout << score << "   ";
  48.    }
  49.    cout << total << "\n" ;
  50. }
  51.