Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.31 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2. //Program: Skeleton for Task 1c � group assignment
  3. //Author: Pascale Vacher
  4. //Last updated: 24 February 2017
  5. //---------------------------------------------------------------------------
  6.  
  7. //---------------------------------------------------------------------------
  8. //----- include libraries
  9. //---------------------------------------------------------------------------
  10.  
  11. //include standard libraries
  12. //BEAR IS SO COOL
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <conio.h>
  16. #include <cassert>
  17. #include <string>
  18. #include <sstream>
  19. #include <vector>
  20. #include <fstream>
  21. using namespace std;
  22.  
  23. //include our own libraries
  24. #include "ConsoleUtils.h"   //for Clrscr, Gotoxy, etc.
  25.  
  26. //---------------------------------------------------------------------------
  27. //----- define constants
  28. //---------------------------------------------------------------------------
  29.  
  30. //defining the size of the grid
  31. const int  SIZEX(16);       //horizontal dimension
  32. const int  SIZEY(11);       //vertical dimension
  33. //defining symbols used for display of the grid and content
  34. const char BEAR('@');       //bear
  35. const char TUNNEL(' ');     //tunnel
  36. const char WALL('#');
  37. const char BOMB('0');       //Bomb
  38. const char TRIGGER('T');    //Trigger
  39. const char EXIT('X');       //Exit//border
  40. //defining the command letters to move the bear on the maze
  41. const int  UP(72);          //up arrow
  42. const int  DOWN(80);        //down arrow
  43. const int  RIGHT(77);       //right arrow
  44. const int  LEFT(75);        //left arrow
  45.  
  46. //defining the other command letters
  47. const char QUIT('Q');       //to end the game
  48. //score
  49.  
  50.  
  51. struct Item {
  52.     int x, y;
  53.     char symbol;
  54. };
  55.  
  56. ifstream inUsername;
  57. ofstream outUsername;
  58. ifstream inPlayerScore;
  59. ofstream outPlayerScore;
  60. //---------------------------------------------------------------------------
  61. //----- run game
  62. //---------------------------------------------------------------------------
  63.  
  64.  
  65. int main()
  66. {
  67.     int score(0); //Change this from a global!
  68.     int PreviousScore(0);
  69.     string playerName;
  70.  
  71.     void mainMenu(string&);
  72.  
  73.     //calls the main menu
  74.     mainMenu(playerName);
  75.  
  76.     //function declarations (prototypes)
  77.     void initialiseGame(char g[][SIZEX], char m[][SIZEX], vector<Item>& bears);
  78.     void paintGame(const char g[][SIZEX], string mess, vector<Item>& theBears, const char BEAR, string& playerName, bool cheatMode, int& score);
  79.     bool wantsToQuit(const int key);
  80.     bool isArrowKey(const int k);
  81.     bool cheatMode = false;
  82.     int  getKeyPress();
  83.     void updateGameData(char g[][SIZEX], char m[][SIZEX], vector<Item>& theBears, int key, string& mess, bool cheatMode, int& z, int& score);
  84.     void updateGrid(char g[][SIZEX], const char m[][SIZEX], vector<Item>& bears);
  85.     void endProgram();
  86.     //local variable declarations
  87.     int z = 0;
  88.     char grid[SIZEY][SIZEX];
  89.     //grid for display
  90.  
  91.     char maze[SIZEY][SIZEX];
  92.     //structure of the maze
  93.  
  94.     Item bear = { 1, 0, BEAR };
  95.     Item bearTwo = { 1, 1, BEAR };
  96.     Item bearThree = { 1, 2, BEAR };
  97.     vector<Item> theBears = { bear, bearTwo, bearThree };//Bears vector
  98.  
  99.     string message("LET'S START...");   //current message to player
  100.  
  101.     //action...
  102.     initialiseGame(grid, maze, theBears);   //initialise grid (incl. walls & bear)
  103.     paintGame(grid, message, theBears, BEAR, playerName, cheatMode, score);         //display game info, modified grid & messages
  104.     int key(getKeyPress());             //read in  selected key: arrow or letter command
  105.     while (!wantsToQuit(key))           //while user does not want to quit
  106.     {
  107.         if (key == 'C')
  108.         {
  109.             cheatMode = !cheatMode;
  110.             message = "CHEAT MODE ON!";
  111.             cout << '\a';
  112.             score = 500;
  113.         }
  114.         else
  115.         {
  116.             if (isArrowKey(key))
  117.             {
  118.                 updateGameData(grid, maze, theBears, key, message, cheatMode, z, score);        //move bear in that direction
  119.                 updateGrid(grid, maze, theBears);                   //update grid information
  120.             }
  121.             else
  122.             {
  123.                 message = "INVALID KEY!";   //set 'Invalid key' message
  124.  
  125.             }
  126.         }
  127.         paintGame(grid, message, theBears, BEAR, playerName, cheatMode, score);     //display game info, modified grid & messages
  128.         key = getKeyPress();            //display menu & read in next option
  129.     }
  130.     endProgram();                       //display final message
  131.     return 0;
  132. }
  133.  
  134. void mainMenu(string& playerName)
  135. {
  136.     void fileInName(string& playerName);
  137.     void showMessage(WORD backColour, WORD textColour, int x, int y, const string message);
  138.     showMessage(clRed, clYellow, 40, 10, "3 Bears Project 2016-17");
  139.     showMessage(clRed, clYellow, 40, 11, "Team DAB 2.0");
  140.     showMessage(clBlue, clWhite, 40, 12, "Player Name: ");
  141.     outUsername.open("PlayerName.txt", ios::out);
  142.     cin >> playerName;      //allows the player to enter his name
  143.     SelectBackColour(clBlack);      //resets the background colour to black when the screen clears
  144.     fileInName(playerName);
  145.     Clrscr();
  146.  
  147. }
  148.  
  149. //---------------------------------------------------------------------------
  150. //----- initialise game state
  151. //---------------------------------------------------------------------------
  152.  
  153. void initialiseGame(char grid[][SIZEX], char maze[][SIZEX], vector<Item>& theBears)
  154. { //initialise grid & place bear in middle
  155.     void setInitialMazeStructure(char maze[][SIZEX], vector<Item>& theBears);
  156.     void setInitialDataFromMaze(char maze[][SIZEX], vector<Item>& theBears);
  157.     void updateGrid(char g[][SIZEX], const char m[][SIZEX], vector<Item>& theBears);
  158.  
  159.     setInitialMazeStructure(maze, theBears);        //initialise maze
  160.     setInitialDataFromMaze(maze, theBears); //initialise bear's position
  161.     updateGrid(grid, maze, theBears);       //prepare grid
  162. }
  163.  
  164. void setInitialMazeStructure(char maze[][SIZEX], vector<Item>& theBears)
  165. { //set the position of the walls in the maze
  166.     //initialise maze configuration
  167.     int initialMaze[SIZEY][SIZEX]   //local array to store the maze structure
  168.         = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  169.         { 1, 0, 3, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 1 },
  170.         { 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
  171.         { 1, 0, 1, 0, 1, 5, 0, 0, 0, 3, 0, 1, 0, 1, 0, 1 },
  172.         { 1, 2, 1, 0, 1, 3, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1 },
  173.         { 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
  174.         { 1, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1 },
  175.         { 1, 0, 1, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
  176.         { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1 },
  177.         { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  178.         { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
  179.     // with 1 for wall, 0 for tunnel, etc.
  180.     //copy into maze structure
  181.     for (int row(0); row < SIZEY; ++row)
  182.     {
  183.         for (int col(0); col < SIZEX; ++col)
  184.         {
  185.             switch (initialMaze[row][col])
  186.             {
  187.             case 0: maze[row][col] = TUNNEL; break;
  188.             case 1: maze[row][col] = WALL; break;
  189.             case 2: maze[row][col] = BEAR; break;
  190.             case 3: maze[row][col] = BOMB; break;
  191.             case 4: maze[row][col] = TRIGGER; break;
  192.             case 5: maze[row][col] = EXIT; break;
  193.             }
  194.         }
  195.     }
  196. }
  197. void setInitialDataFromMaze(char maze[][SIZEX], vector<Item>& theBears)
  198. { //extract bear's coordinates from initial maze info
  199.     for (int row(0); row < SIZEY; ++row)
  200.         for (int col(0); col < SIZEX; ++col)
  201.             for (int i = 0; i < theBears.size(); i++)
  202.                 switch (maze[row][col])
  203.             {
  204.                 case BEAR:
  205.                 {
  206.                     theBears[i].x = col;
  207.                     theBears[i].y = row;
  208.                     maze[row][col] = TUNNEL;
  209.                 }
  210.                 break;
  211.                 //will work for other items too
  212.             }
  213. }
  214.  
  215. //---------------------------------------------------------------------------
  216. //----- update grid state
  217. //---------------------------------------------------------------------------
  218.  
  219. void updateGrid(char grid[][SIZEX], const char maze[][SIZEX], vector<Item>& theBears)
  220. { //update grid configuration after each move
  221.     void setMaze(char g[][SIZEX], const char b[][SIZEX]);
  222.     void placeBears(char g[][SIZEX], vector<Item>& theBears);
  223.  
  224.  
  225.     setMaze(grid, maze);    //reset the empty maze configuration into grid
  226.     placeBears(grid, theBears);
  227.  
  228.     //set bear in grid
  229. }
  230.  
  231. void setMaze(char grid[][SIZEX], const char maze[][SIZEX])
  232. { //reset the empty/fixed maze configuration into grid
  233.     for (int row(0); row < SIZEY; ++row)
  234.         for (int col(0); col < SIZEX; ++col)
  235.             grid[row][col] = maze[row][col];
  236. }
  237.  
  238. void placeBears(char g[][SIZEX], vector<Item>& theBears)
  239. { //place bear at its new position in grid
  240.     for (int i = 0; i < theBears.size(); i++) //for Loop for passing x and y into bears vector
  241.  
  242.         g[theBears[i].y][theBears[i].x] = theBears[i].symbol;
  243. }
  244.  
  245. void triggerBombs(char grid[][SIZEX], char maze[][SIZEX], vector<Item>& theBears)
  246. {
  247.     void updateGrid(char grid[][SIZEX], const char maze[][SIZEX], vector<Item>& theBears);
  248.  
  249.     for (int row(0); row < SIZEY; ++row)
  250.     {
  251.         for (int col(0); col < SIZEX; ++col)
  252.         {
  253.             if (maze[row][col] == BOMB)
  254.             {
  255.                 maze[row][col] = TUNNEL;
  256.             }
  257.  
  258.         }
  259.     }
  260.  
  261.     updateGrid(grid, maze, theBears);
  262. }
  263.  
  264. string previousScore(string PlayerName, int& score)
  265. {
  266.     string previousscore;
  267.     ifstream myfile(PlayerName + ".txt");
  268.     if (myfile.is_open())
  269.     {
  270.         myfile >> score;
  271.         previousscore = score;
  272.         myfile.close();
  273.  
  274.     }
  275.     else {
  276.         previousscore = "500";
  277.     }
  278.     return previousscore;
  279. }
  280. //---------------------------------------------------------------------------
  281. //----- move the bear
  282. //---------------------------------------------------------------------------
  283. void updateGameData(char g[][SIZEX], char maze[][SIZEX], vector<Item>& theBears, const int key, string& mess, bool cheatMode, int& z, int& score)
  284. { //move bear in required direction
  285.  
  286.     bool isArrowKey(const int k);
  287.     void setKeyDirection(int k, int& dx, int& dy, int& score);
  288.     void triggerBombs(char maze[][SIZEX], char grid[][SIZEX], vector<Item>& theBears);
  289.     void showMessage(const WORD backColour, const WORD textColour, int x, int y, const string message);
  290.  
  291.     //reset message to blank
  292.     mess = "                                         ";     //reset message to blank
  293.  
  294.     //calculate direction of movement for given key
  295.     int dx(0), dy(0);
  296.     setKeyDirection(key, dx, dy, score);
  297.     for (int i = 0; i < theBears.size(); i++)
  298.         //check new target position in grid and update game data (incl. bear coordinates) if move is possible
  299.         switch (g[theBears[i].y + dy][theBears[i].x + dx])
  300.     {           //...depending on what's on the target position in grid...
  301.         case TUNNEL:            //can move
  302.             theBears[i].y += dy;        //go in that Y direction
  303.             theBears[i].x += dx;        //go in that X direction
  304.  
  305.             break;
  306.         case WALL:              //hit a wall and stay there
  307.             cout << '\a';       //beep the alarm
  308.             mess = "CANNOT GO THERE!";
  309.             break;
  310.  
  311.         case BEAR:
  312.             cout << '\a';       //beep the alarm
  313.             mess = "CANNOT GO THERES!";
  314.             break;
  315.  
  316.         case EXIT:
  317.        
  318.             cout << '\a';
  319.             theBears.erase(theBears.begin() + i);
  320.             mess = "BEAR SAVED!";
  321.             Sleep(50);
  322.             z++;
  323.             if (z >= 3)
  324.             {
  325.                 showMessage(clBlack, clGreen, 40, 9, "  All Bears rescued! You win!     ");
  326.             }
  327.            
  328.             showMessage(clBlack, clGreen, 15 + z, 2, "@");
  329.  
  330.             break;
  331.        
  332.  
  333.         case TRIGGER:
  334.         {
  335.             if (cheatMode == false)
  336.             {
  337.        
  338.             theBears[i].y += dy;        //go in that Y direction
  339.             theBears[i].x += dx;        //go in that X direction
  340.             triggerBombs(g, maze, theBears);
  341.             mess = "Trigger activated!";
  342.            
  343.             }
  344.             else
  345.             {
  346.                 theBears[i].y += dy;        //go in that Y direction
  347.                 theBears[i].x += dx;        //go in that X direction
  348.             }
  349.  
  350.             break;
  351.         }
  352.  
  353.         case BOMB:
  354.         {
  355.             if (cheatMode == false)
  356.             {
  357.                 cout << '\a';                                       //Play an audio alert to let the player know
  358.                 maze[theBears[i].y + dy][theBears[i].x + dx] = ' '; //delete the bomb by modifying the maze
  359.                 theBears.erase(theBears.begin() + i);               //remove the bear from the vector
  360.                 mess = "BOMB HIT!";                                 //message the player that a bombs been hit
  361.             }
  362.             else
  363.             {
  364.                 theBears[i].y += dy;        //go in that Y direction
  365.                 theBears[i].x += dx;        //go in that X direction
  366.             }
  367.             break;
  368.         }
  369.     }
  370. }
  371. //---------------------------------------------------------------------------
  372. //----- process key
  373. //---------------------------------------------------------------------------
  374. void setKeyDirection(const int key, int& dx, int& dy, int& score)
  375. { //calculate direction indicated by key
  376.     bool isArrowKey(const int k);
  377.  
  378.     assert(isArrowKey(key));
  379.     if (isArrowKey(key) == true){
  380.         score++;
  381.     }
  382.  
  383.     switch (key)    //...depending on the selected key...
  384.     {
  385.     case LEFT:      //when LEFT arrow pressed...
  386.         dx = -1;    //decrease the X coordinate
  387.         dy = 0;
  388.         break;
  389.     case RIGHT:     //when RIGHT arrow pressed...
  390.         dx = +1;    //increase the X coordinate
  391.         dy = 0;
  392.         break;
  393.     case UP:        //when UP arrow pressed...
  394.         dx = 0;
  395.         dy = -1;    //decrease the Y coordinate
  396.         break;
  397.     case DOWN:      //when DOWN arrow pressed...
  398.         dx = 0;
  399.         dy = +1;    //increase the Y coordinate
  400.         break;
  401.  
  402.     }
  403. }
  404.  
  405. int getKeyPress()
  406. { //get key or command (in uppercase) selected by user
  407.     //KEEP THIS FUNCTION AS GIVEN
  408.     int keyPressed;
  409.     keyPressed = _getch();          //read in the selected arrow key or command letter
  410.     while (keyPressed == 224)       //ignore symbol following cursor key
  411.         keyPressed = _getch();
  412.     return toupper(keyPressed);     //return it in uppercase
  413. }
  414.  
  415. bool isArrowKey(const int key)
  416. {   //check if the key pressed is an arrow key (also accept 'K', 'M', 'H' and 'P')
  417.     return (key == LEFT) || (key == RIGHT) || (key == UP) || (key == DOWN);
  418. }
  419. bool wantsToQuit(const int key)
  420. {   //check if the user wants to quit (when key is 'Q' or 'q')
  421.     return toupper(key) == QUIT;
  422. }
  423.  
  424. //---------------------------------------------------------------------------
  425. //----- display info on screen
  426. //---------------------------------------------------------------------------
  427.  
  428. string tostring(char x) {
  429.     std::ostringstream os;
  430.     os << x;
  431.     return os.str();
  432. }
  433. void showMessage(const WORD backColour, const WORD textColour, int x, int y, const string message)
  434. {
  435.     Gotoxy(x, y);
  436.     SelectBackColour(backColour);
  437.     SelectTextColour(textColour);
  438.     cout << message;
  439. }
  440. void paintGame(const char g[][SIZEX], string mess, vector<Item>& theBears, const char BEAR, string& playerName, bool cheatMode, int& score)
  441. { //display game title, messages, maze, bear and other items on screen
  442.     string tostring(char x);
  443.     void showMessage(const WORD backColour, const WORD textColour, int x, int y, const string message);
  444.     void paintGrid(const char g[][SIZEX]);
  445.     string previousScore(string name, int& score);
  446.  
  447.     //display Player Name
  448.     showMessage(clWhite, clRed, 40, 0, playerName);
  449.  
  450.     //display game title
  451.     showMessage(clBlack, clYellow, 0, 0, "___GAME___");
  452.     showMessage(clWhite, clRed, 40, 0, "FoP Task 1c: February 2017");
  453.  
  454.     //display menu options available
  455.     showMessage(clRed, clYellow, 40, 3, "TO MOVE USE KEYBOARD ARROWS ");
  456.     showMessage(clRed, clYellow, 40, 4, "TO QUIT ENTER 'Q'           ");
  457.     showMessage(clRed, clYellow, 40, 4, "TO ENTER CHEAT MODE ENTER 'C'          ");
  458.  
  459.     //print auxiliary messages if any
  460.     showMessage(clBlack, clWhite, 40, 8, mess); //display current message
  461.  
  462.     //display rescued bears
  463.  
  464.     showMessage(clBlack, clYellow, 0, 2, "Rescued Bears: ");
  465.     string cheatModeActive;
  466.     if (cheatMode == true)
  467.     {
  468.         cheatModeActive = "Yes";
  469.     }
  470.     else
  471.     {
  472.         cheatModeActive = "No";
  473.     }
  474.     showMessage(clBlack, clYellow, 0, 3, "Cheat mode: " + cheatModeActive);
  475.  
  476.    
  477.     //previousscore
  478.  
  479.     string previousscore = previousScore(playerName, score);
  480.  
  481.     showMessage(clBlack, clYellow, 40, 6, "PREVIOUS SCORE: " + previousscore);
  482.  
  483.     showMessage(clBlack, clYellow, 40, 5, "SCORE: ");
  484.     cout << score;
  485.  
  486.     //print auxiliary messages if any
  487.     showMessage(clBlack, clWhite, 40, 8, mess); //display current message
  488.  
  489.     // display grid contents
  490.     paintGrid(g);
  491. }
  492.  
  493.  
  494. void paintGrid(const char g[][SIZEX])
  495. { //display grid content on screen
  496.     SelectBackColour(clBlack);
  497.     SelectTextColour(clWhite);
  498.     Gotoxy(0, 6);
  499.     for (int row(0); row < SIZEY; ++row)
  500.     {
  501.         for (int col(0); col < SIZEX; ++col)
  502.             cout << g[row][col];    //output cell content
  503.         cout << endl;
  504.     }
  505. }
  506.  
  507. void endProgram()
  508. {
  509.     void showMessage(const WORD backColour, const WORD textColour, int x, int y, const string message);
  510.     showMessage(clRed, clYellow, 40, 8, "");    //hold output screen until a keyboard key is hit
  511.     system("pause");
  512.  
  513. }
  514. ////////////////////////////////////////////////////////file in/out functions
  515. void fileInName(string& playerName){
  516.     outUsername << "Username: ";
  517.     outUsername << playerName + "   ";
  518.     if (outUsername.fail()){
  519.         cout << "\nAn error has ocurred when openiong the file.";
  520.     }
  521.     outUsername.close();
  522. }
  523. void fileInScore(int& score){
  524.     outPlayerScore << "Score: ";
  525.     outPlayerScore << score;
  526.     if (outPlayerScore.fail()){
  527.         cout << "\nAn error has occured when opening the file.";
  528.     }
  529.     outPlayerScore.close();
  530.  
  531. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement