1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. #include "ASMProcedures.h"
  8.  
  9. using namespace std;
  10.  
  11. // PROTO types
  12. void doGame();
  13.  
  14. // it will uses blocks every time
  15. const int BLOCK = 219;        // ascii block char
  16.  
  17. const int max_y = 20;
  18. const int max_x = 20;
  19.  
  20. const int BLACK = 14;          // color black
  21. const int BLUE  = 1;          // blue
  22. const int RED   = 4;          // red
  23. const int GREEN = 2;          // green
  24. const int EMPTY = 9;          // light blue
  25.  
  26. int screen[max_x][max_y];
  27.  
  28. int maze[max_x][max_y] =
  29. {
  30.      EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLUE,  EMPTY, EMPTY, EMPTY,
  31.  
  32. BLUE,  EMPTY, EMPTY, BLUE,  EMPTY, EMPTY, EMPTY, EMPTY, BLACK,
  33.      EMPTY, EMPTY, EMPTY, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK,
  34.  
  35. BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK,
  36.      EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
  37.  
  38.      EMPTY, EMPTY, EMPTY, BLACK, BLUE , BLUE , BLUE , EMPTY, EMPTY, EMPTY, EMPTY,
  39.  
  40. EMPTY, BLUE , BLUE , BLUE,  BLUE , BLUE , EMPTY, EMPTY, EMPTY,
  41.      EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, BLUE , BLUE  ,EMPTY,
  42.  
  43. EMPTY, BLUE , EMPTY, EMPTY, EMPTY, BLUE , BLUE , BLUE , BLUE,
  44.      EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY,
  45.  
  46.      EMPTY, EMPTY, EMPTY, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK,
  47.  
  48. BLACK, BLUE , BLUE , BLUE  ,BLUE , BLUE , EMPTY, EMPTY, EMPTY,
  49.  
  50. BLACK, EMPTY, EMPTY, EMPTY, EMPTY, BLUE,  EMPTY, EMPTY, EMPTY,
  51.      EMPTY, EMPTY, EMPTY, EMPTY, BLUE , BLUE , BLUE  ,BLUE , BLUE , EMPTY, EMPTY,
  52.  
  53. BLACK, EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY, EMPTY,
  54.      EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY,
  55.  
  56. BLACK, EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY, EMPTY,
  57.      BLUE , BLUE,  EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY,
  58.  
  59.      EMPTY, BLUE , EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY,
  60.  
  61.      EMPTY, BLUE , BLUE , BLUE , EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY,
  62.  
  63.      EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY, EMPTY, EMPTY, BLACK, BLACK, BLACK,
  64.  
  65. BLACK, BLUE  ,BLUE , BLUE , BLUE  ,BLUE , EMPTY, EMPTY, EMPTY,
  66.      EMPTY, EMPTY, EMPTY, BLUE , BLACK, BLACK, BLACK, BLACK, BLACK, EMPTY, EMPTY,
  67.  
  68.      EMPTY, EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY,
  69.  
  70.      EMPTY, EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, BLUE , BLUE , BLUE ,
  71.  
  72. BLUE , EMPTY, EMPTY, EMPTY, EMPTY, BLUE  ,EMPTY, EMPTY, EMPTY,
  73.      EMPTY, EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
  74.  
  75. BLUE , EMPTY, EMPTY, EMPTY, EMPTY, BLUE , EMPTY, EMPTY, EMPTY,
  76.      EMPTY, EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
  77.  
  78.      BLACK, BLACK, BLACK, BLACK, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
  79.  
  80. };
  81.  
  82. struct avatarType
  83. {
  84.      int x;
  85.      int y;
  86. } avatar, endPT, startPT, dest;
  87.  
  88. // game score and such
  89.      int score = 0;
  90.      char input;
  91.      int timer = 0;
  92.      int temp;
  93.      int roll;
  94.      int elapTicks;
  95.      double elapMilli, elapSeconds, elapMinutes;
  96.      bool done = false;
  97.      bool validInput = false;
  98.  
  99.      int topScore = 0;
  100. //     string fileName = "topscore.txt";
  101.      string playerName = "anonymous";
  102.      string topName = playerName;
  103.  
  104.      
  105.  
  106. int main()
  107. {
  108.      // READ FROM FILE (if exists)
  109.      ifstream OpenFile("topscore.txt");
  110.  
  111.      if (! OpenFile.eof() )
  112.      {
  113.           cout << "High scored read in from data file " << endl;
  114.           OpenFile >> topName;
  115.           OpenFile >> topScore;
  116.           cout << "High score was " << topScore << " by " << topName << endl << endl;
  117.      }
  118.      else
  119.           cout << "Error opening high score file, possibly doesnt exist" << endl;
  120.      OpenFile.close();
  121.  
  122.      doWait();
  123.  
  124.      // MENU LOOP
  125.      do
  126.      {
  127.      // GOAL :: Input player name, selection of options, help screen, play game, exit
  128.  
  129. game
  130.  
  131.      // draw menu
  132.      clearscreen();
  133.      cout << "MENU:" << endl
  134.           << "1. enter player name" << endl
  135.           << "2. options" << endl
  136.           << "3. help" << endl
  137.           << "4. PLAY game" << endl
  138.           << "5. exit" << endl << endl;
  139.  
  140.      cin >> input;
  141.      switch(input)
  142.      {
  143.      case '1': {
  144.                     cout << endl
  145.                          << "Please enter your name:";
  146.                     cin >> playerName;
  147.                     cout << endl << endl;
  148.                     cout << "New player name is now: " << playerName;
  149.                     doWait();
  150.                }; break;
  151.      case '2':{
  152.                     cout << endl
  153.                          << "sorry, there currently are no options to set"
  154.                          << endl
  155.                          << "but I'll show you the top score:" << endl
  156.                          << topName << " has a top score of " << topScore << endl
  157.                          << endl;
  158.                     doWait();
  159.               }; break;
  160.      case '3':{
  161.                     cout << endl
  162.                          << endl
  163.                          << "HELP!" << endl
  164.                          << "Movement keys" << endl
  165.                          << "A/a   move left" << endl
  166.                          << "D/d   move right" << endl
  167.                          << "W/w   move up" << endl
  168.                          << "S/s   move down" << endl
  169.                          << endl
  170.                          << "  RED squares +20 points" << endl
  171.                          << "GREEN squares +10 points" << endl
  172.                          << endl
  173.                          << "The timer is subtracted from your end score, so be
  174.  
  175. quick!"
  176.                          << endl << endl;
  177.                     doWait();
  178.               }; break;
  179.      case '4':{
  180.                     // play game
  181.                     doGame();
  182.               }; break;
  183.      case '5':{
  184.                     cout << "exiting" << endl;
  185.  
  186.                     // write high score to file, including users name
  187.                     // topScore and topName --> to file fileName
  188.                     ofstream saveFile("topscore.txt");
  189.                     saveFile << topName << endl;
  190.                     saveFile << topScore << endl;
  191.                     saveFile.close();
  192.  
  193.                     doWait();
  194.  
  195.                     exit(0);
  196.               }; break;
  197.      }
  198.  
  199.      } while (1==1);
  200.  
  201.      // DONE
  202. }
  203.  
  204. void doGame()
  205. {
  206.      // set starting location of avatar and end point of maze
  207.      startPT.x = 0;
  208.      startPT.y = 19;
  209.      avatar = startPT;
  210.      endPT.x = 19;
  211.      endPT.y = 0;
  212.  
  213.      // starting values
  214.      score = 0;
  215.      done = false;
  216.      validInput = false;
  217.  
  218.      // setup the maze
  219.      for (int x=0; x < max_x; x++)
  220.           for (int y=0; y < max_y; y++)
  221.           {
  222.                temp = maze[x][y];
  223.  
  224.                if ( (temp != EMPTY) && (x != startPT.x) && (y != startPT.y) )
  225.                {
  226.                     if (temp == BLUE)
  227.                          roll = 1 + rand() % 5;
  228.                     else
  229.                          roll = 1 + rand() % 10;
  230.  
  231.                     if (roll == 2)  
  232.                          temp = GREEN;
  233.                     if (roll == 1)
  234.                          temp = RED;
  235.                }
  236.  
  237.                screen[x][y] = temp;
  238.           }
  239.  
  240.      clock_t Begin, End;             //initialize Begin and End for the timer
  241.      Begin = clock() * CLK_TCK;      //start the timer
  242.  
  243.      // clear the screen
  244.      clearscreen();    
  245.  
  246. // DO
  247.      while (! done)
  248.      {
  249.           // show the timer
  250.           End = clock() * CLK_TCK;                // stop the timer    
  251.           elapMilli = (End - Begin)/1000;         // milliseconds from Begin to End
  252.           elapSeconds = elapMilli/1000;           // seconds from Begin to End
  253.           showTimer(elapSeconds);
  254.  
  255.           // draw the maze
  256.           for (int x=0; x < max_x; x++)
  257.                for (int y=0; y < max_y; y++)
  258.                     drawBlock(x,y,screen[x][y]);
  259.  
  260.           // print current score
  261.           showScore(score - elapSeconds);
  262.  
  263.           // show avatar
  264.           drawAvatar(avatar.x,avatar.y);
  265.  
  266.           // get user movement keys
  267.           validInput = false;
  268.           dest = avatar;
  269.  
  270.           input = getInput();
  271.           switch(input)
  272.           {
  273.                case 97:
  274.                case 65:{ // a or A (LEFT)
  275.                          dest.x--;    
  276.                          validInput = true;
  277.                        }; break;
  278.                case 119:
  279.                case 87:{ // w or W (UP)
  280.                          dest.y--;    
  281.                          validInput = true;
  282.                        }; break;
  283.                case 100:
  284.                case 68:{ // d or D (RIGHT)
  285.                          dest.x++;
  286.                          validInput = true;
  287.                        }; break;
  288.                case 115:
  289.                case 83:{ // s or S (DOWN)
  290.                          dest.y++;
  291.                          validInput = true;
  292.                        }; break;
  293.                case 120:
  294.                case 88:{ // x or X
  295.                          done = true;
  296.                        }; break;
  297.           }
  298.  
  299.           // check for valid values
  300.           if (( dest.x < 0) || (dest.x >= max_x))
  301.                validInput = false;
  302.           if (( dest.y < 0) || (dest.y >= max_y))
  303.                validInput = false;
  304.  
  305.           if (validInput)
  306.           {
  307.                // check the destination square to see if its valid
  308.                if (screen[dest.x][dest.y] != EMPTY)
  309.                {
  310.                     avatar = dest;
  311.                     // check if on red/green square - increase score
  312.                     if (screen[avatar.x][avatar.y] == RED)
  313.                     {
  314.                          score += 10;
  315.                          screen[avatar.x][avatar.y] = maze[avatar.x][avatar.y];
  316.                     }
  317.                     if (screen[avatar.x][avatar.y] == GREEN)
  318.                     {
  319.                          score += 20;
  320.                          screen[avatar.x][avatar.y] = maze[avatar.x][avatar.y];
  321.                     }
  322.  
  323.                     // check if at end of maze - then EXIT
  324.                     if ((avatar.x == endPT.x) && ((avatar.y == endPT.y)))
  325.                          done = true;
  326.                }
  327.           }              
  328.      }
  329. // REPEAT
  330.  
  331.      // show the timer
  332.      End = clock() * CLK_TCK;                // stop the timer    
  333.      elapMilli = (End - Begin)/1000;         // milliseconds from Begin to End
  334.      elapSeconds = elapMilli/1000;           // seconds from Begin to End
  335.      showTimer(elapSeconds);
  336.  
  337.      // print current score
  338.      score -= elapSeconds;                   // lower the score by the time it took
  339.      showScore(score);
  340.  
  341.      // show avatar
  342.      drawAvatar(avatar.x,avatar.y);
  343.  
  344.      if ((avatar.x == endPT.x) && ((avatar.y == endPT.y)))
  345.      {
  346.           doWin();
  347.  
  348.           // only save top score if you won
  349.           if (score > topScore)
  350.           {
  351.                topScore = score;
  352.                topName = playerName;
  353.           }
  354.      }
  355.      else
  356.           doWait();
  357.  
  358.      return;
  359. }