Advertisement
vikhik

gridworld gridworld.cpp

Jul 8th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include "gridworld.h"
  2.  
  3. #include <string>
  4. #include <iostream>
  5.  
  6. namespace gridworld {
  7.  
  8. Tile::Tile(bool w, bool e, std::string eText) {
  9.     wall = w;
  10.     event = e;
  11.     eventText = eText;
  12. }
  13.  
  14. Tile::Tile(bool w) {
  15.     wall = w;
  16.     event = false;
  17.     eventText = "";
  18. }
  19.  
  20. Tile::Tile() {
  21.     wall = true;
  22.     event = false;
  23.     eventText = "";
  24. }
  25.  
  26. Map::Map() {
  27.     charX = 2;
  28.     charY = 7;
  29.  
  30.     map[0][0] = Tile();
  31.     map[0][1] = Tile();
  32.     map[0][2] = Tile();
  33.     map[0][3] = Tile();
  34.     map[0][4] = Tile();
  35.     map[0][5] = Tile();
  36.     map[0][6] = Tile();
  37.     map[0][7] = Tile();
  38.     map[1][7] = Tile();
  39.     map[2][7] = Tile();
  40.     map[3][7] = Tile();
  41.     map[4][7] = Tile();
  42.     map[5][7] = Tile();
  43.     map[6][7] = Tile();
  44.     map[7][7] = Tile();
  45.     map[1][0] = Tile();
  46.     map[2][0] = Tile();
  47.     map[3][0] = Tile();
  48.     map[4][0] = Tile();
  49.     map[5][0] = Tile();
  50.     map[6][0] = Tile();
  51.     map[7][0] = Tile();
  52.     map[7][1] = Tile();
  53.     map[7][3] = Tile();
  54.     map[7][4] = Tile();
  55.     map[7][5] = Tile();
  56.     map[7][6] = Tile();
  57.     map[4][1] = Tile();
  58.     map[4][2] = Tile();
  59.     map[4][3] = Tile();
  60.     map[4][4] = Tile();
  61.     map[4][5] = Tile();
  62.     map[2][5] = Tile();
  63.     map[3][5] = Tile();
  64.     map[5][5] = Tile();
  65.     map[1][3] = Tile();
  66.     map[2][3] = Tile();
  67.  
  68.     map[1][1] = Tile(false, true, "You found the gold!");
  69.     map[3][1] = Tile(false, true, "You were eaten by a grue!");
  70.     map[5][1] = Tile(false, true, "The ceiling collapses, you die.");
  71.     map[6][3] = Tile(false, true, "You are bitten by a snake");
  72.  
  73.     map[2][1] = Tile(false);
  74.     map[6][1] = Tile(false);
  75.     map[1][2] = Tile(false);
  76.     map[2][2] = Tile(false);
  77.     map[3][2] = Tile(false);
  78.     map[5][2] = Tile(false);
  79.     map[6][2] = Tile(false);
  80.     map[3][3] = Tile(false);
  81.     map[5][3] = Tile(false);
  82.     map[1][4] = Tile(false);
  83.     map[2][4] = Tile(false);
  84.     map[3][4] = Tile(false);
  85.     map[5][4] = Tile(false);
  86.     map[6][4] = Tile(false);
  87.     map[1][5] = Tile(false);
  88.     map[6][5] = Tile(false);
  89.     map[1][6] = Tile(false);
  90.     map[2][6] = Tile(false);
  91.     map[3][6] = Tile(false);
  92.     map[4][6] = Tile(false);
  93.     map[5][6] = Tile(false);
  94.     map[6][6] = Tile(false);
  95.     map[2][7] = Tile(false);
  96. }
  97.  
  98. std::string Map::possibleExits() {
  99.     std::string exits = "";
  100.     for (int i = -1; i <= 1; i++) {
  101.         for (int j = -1; j <= 1; j++) {
  102.             if (legalExit(charX + i, charY + j) == true) {
  103.                 //if this is a legal exit
  104.                 //figure out which direction this is, and return it.
  105.                 switch (i) {
  106.                 case -1:
  107.                     if (exits == "") {
  108.                         exits += "W";
  109.                     } else {
  110.                         exits += ", W";
  111.                     }
  112.                     break;
  113.                 case 1:
  114.                     if (exits == "") {
  115.                         exits += "E";
  116.                     } else {
  117.                         exits += ", E";
  118.                     }
  119.                     break;
  120.                     //this is not a wall, it is an exit!
  121.                 }
  122.                 switch (j) {
  123.                 case -1:
  124.                     if (exits == "") {
  125.                         exits += "N";
  126.                     } else {
  127.                         exits += ", N";
  128.                     }
  129.                     break;
  130.                 case 1:
  131.                     if (exits == "") {
  132.                         exits += "S";
  133.                     } else {
  134.                         exits += ", S";
  135.                     }
  136.                     break;
  137.                 }
  138.             }
  139.         }
  140.     }
  141.  
  142.     return exits;
  143. }
  144.  
  145. bool Map::legalExit(int x, int y) {
  146.     if ((charX - x + charY - y == -1) or (charX - x + charY - y == 1)) {
  147.         switch (x - charX) {
  148.         case -1:
  149.             return legalExit('W');
  150.             break;
  151.         case 1:
  152.             return legalExit('E');
  153.             break;
  154.         }
  155.         switch (y - charY) {
  156.         case -1:
  157.             return legalExit('N');
  158.             break;
  159.         case 1:
  160.             return legalExit('S');
  161.             break;
  162.         }
  163.     }
  164.     return false;
  165. }
  166.  
  167. bool Map::legalExit(char dir) {
  168.     switch (dir) {
  169.     case 'W':
  170.         if (charX - 1 > 0) {
  171.             if (map[charX - 1][charY].wall == false) {
  172.                 return true;
  173.             }
  174.         }
  175.         break;
  176.     case 'E':
  177.         if (charX + 1 <= 7) {
  178.             if (map[charX + 1][charY].wall == false) {
  179.                 return true;
  180.             }
  181.         }
  182.         break;
  183.     case 'N':
  184.         if (charY - 1 > 0) {
  185.             if (map[charX][charY - 1].wall == false) {
  186.                 return true;
  187.             }
  188.         }
  189.         break;
  190.     case 'S':
  191.         if (charY + 1 <= 7) {
  192.             if (map[charX][charY + 1].wall == false) {
  193.                 return true;
  194.             }
  195.         }
  196.         break;
  197.     }
  198.     return false;
  199. }
  200.  
  201. void Map::moveChar(char dir) {
  202.     if (legalExit(dir)) {
  203.         switch (dir) {
  204.         case 'W':
  205.             charX -= 1;
  206.             break;
  207.         case 'E':
  208.             charX += 1;
  209.             break;
  210.         case 'N':
  211.             charY -= 1;
  212.             break;
  213.         case 'S':
  214.             charY += 1;
  215.             break;
  216.         }
  217.     }
  218. }
  219.  
  220. std::string Map::finishEvent() {
  221.     if (finish()) {
  222.         return map[charX][charY].eventText;
  223.     } else
  224.         return "";
  225. }
  226.  
  227. void Display::exits(Map map) {
  228.     //print possible exits
  229.     std::cout << "You are at " << map.charX << "," << map.charY << std::endl;
  230.     std::cout << "Possible exits are " << map.possibleExits() << std::endl
  231.             << "> ";
  232. }
  233.  
  234. void Display::finish(Map map) {
  235.     //print character location message
  236.     std::cout << map.finishEvent() << std::endl;
  237. }
  238.  
  239. void Display::intro() {
  240.     std::cout
  241.             << "Welcome to Gridworld! Quantized Excitement! Fate is waiting for you"
  242.             << std::endl;
  243.     std::cout
  244.             << "Valid commands: N, S, E and W for directions, Q to quit the game."
  245.             << std::endl;
  246. }
  247.  
  248. void Display::quit() {
  249.     std::cout << "Goodbye!" << std::endl;
  250. }
  251.  
  252. bool InputManager::runTurn(Map map) {
  253.     std::string s;
  254.     std::cin >> s;
  255.     std::cin.ignore();
  256.  
  257.     //convert input (n, N, north, North should all be valid, and converted to a single character 'N')
  258.     char c = 'X';
  259.  
  260.     if ((s == "n") or (s == "N")) {
  261.         c = 'N';
  262.     }
  263.     if ((s == "e") or (s == "E")) {
  264.         c = 'E';
  265.     }
  266.     if ((s == "s") or (s == "S")) {
  267.         c = 'S';
  268.     }
  269.     if ((s == "w") or (s == "W")) {
  270.         c = 'W';
  271.     }
  272.     if ((s == "q") or (s == "Q")) {
  273.         return true; //QUIT
  274.     }
  275.  
  276.     map.moveChar(c);
  277.     return false;
  278. }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement