Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "gridworld.h"
- #include <string>
- #include <iostream>
- namespace gridworld {
- Tile::Tile(bool w, bool e, std::string eText) {
- wall = w;
- event = e;
- eventText = eText;
- }
- Tile::Tile(bool w) {
- wall = w;
- event = false;
- eventText = "";
- }
- Tile::Tile() {
- wall = true;
- event = false;
- eventText = "";
- }
- Map::Map() {
- charX = 2;
- charY = 7;
- map[0][0] = Tile();
- map[0][1] = Tile();
- map[0][2] = Tile();
- map[0][3] = Tile();
- map[0][4] = Tile();
- map[0][5] = Tile();
- map[0][6] = Tile();
- map[0][7] = Tile();
- map[1][7] = Tile();
- map[2][7] = Tile();
- map[3][7] = Tile();
- map[4][7] = Tile();
- map[5][7] = Tile();
- map[6][7] = Tile();
- map[7][7] = Tile();
- map[1][0] = Tile();
- map[2][0] = Tile();
- map[3][0] = Tile();
- map[4][0] = Tile();
- map[5][0] = Tile();
- map[6][0] = Tile();
- map[7][0] = Tile();
- map[7][1] = Tile();
- map[7][3] = Tile();
- map[7][4] = Tile();
- map[7][5] = Tile();
- map[7][6] = Tile();
- map[4][1] = Tile();
- map[4][2] = Tile();
- map[4][3] = Tile();
- map[4][4] = Tile();
- map[4][5] = Tile();
- map[2][5] = Tile();
- map[3][5] = Tile();
- map[5][5] = Tile();
- map[1][3] = Tile();
- map[2][3] = Tile();
- map[1][1] = Tile(false, true, "You found the gold!");
- map[3][1] = Tile(false, true, "You were eaten by a grue!");
- map[5][1] = Tile(false, true, "The ceiling collapses, you die.");
- map[6][3] = Tile(false, true, "You are bitten by a snake");
- map[2][1] = Tile(false);
- map[6][1] = Tile(false);
- map[1][2] = Tile(false);
- map[2][2] = Tile(false);
- map[3][2] = Tile(false);
- map[5][2] = Tile(false);
- map[6][2] = Tile(false);
- map[3][3] = Tile(false);
- map[5][3] = Tile(false);
- map[1][4] = Tile(false);
- map[2][4] = Tile(false);
- map[3][4] = Tile(false);
- map[5][4] = Tile(false);
- map[6][4] = Tile(false);
- map[1][5] = Tile(false);
- map[6][5] = Tile(false);
- map[1][6] = Tile(false);
- map[2][6] = Tile(false);
- map[3][6] = Tile(false);
- map[4][6] = Tile(false);
- map[5][6] = Tile(false);
- map[6][6] = Tile(false);
- map[2][7] = Tile(false);
- }
- std::string Map::possibleExits() {
- std::string exits = "";
- for (int i = -1; i <= 1; i++) {
- for (int j = -1; j <= 1; j++) {
- if (legalExit(charX + i, charY + j) == true) {
- //if this is a legal exit
- //figure out which direction this is, and return it.
- switch (i) {
- case -1:
- if (exits == "") {
- exits += "W";
- } else {
- exits += ", W";
- }
- break;
- case 1:
- if (exits == "") {
- exits += "E";
- } else {
- exits += ", E";
- }
- break;
- //this is not a wall, it is an exit!
- }
- switch (j) {
- case -1:
- if (exits == "") {
- exits += "N";
- } else {
- exits += ", N";
- }
- break;
- case 1:
- if (exits == "") {
- exits += "S";
- } else {
- exits += ", S";
- }
- break;
- }
- }
- }
- }
- return exits;
- }
- bool Map::legalExit(int x, int y) {
- if ((charX - x + charY - y == -1) or (charX - x + charY - y == 1)) {
- switch (x - charX) {
- case -1:
- return legalExit('W');
- break;
- case 1:
- return legalExit('E');
- break;
- }
- switch (y - charY) {
- case -1:
- return legalExit('N');
- break;
- case 1:
- return legalExit('S');
- break;
- }
- }
- return false;
- }
- bool Map::legalExit(char dir) {
- switch (dir) {
- case 'W':
- if (charX - 1 > 0) {
- if (map[charX - 1][charY].wall == false) {
- return true;
- }
- }
- break;
- case 'E':
- if (charX + 1 <= 7) {
- if (map[charX + 1][charY].wall == false) {
- return true;
- }
- }
- break;
- case 'N':
- if (charY - 1 > 0) {
- if (map[charX][charY - 1].wall == false) {
- return true;
- }
- }
- break;
- case 'S':
- if (charY + 1 <= 7) {
- if (map[charX][charY + 1].wall == false) {
- return true;
- }
- }
- break;
- }
- return false;
- }
- void Map::moveChar(char dir) {
- if (legalExit(dir)) {
- switch (dir) {
- case 'W':
- charX -= 1;
- break;
- case 'E':
- charX += 1;
- break;
- case 'N':
- charY -= 1;
- break;
- case 'S':
- charY += 1;
- break;
- }
- }
- }
- std::string Map::finishEvent() {
- if (finish()) {
- return map[charX][charY].eventText;
- } else
- return "";
- }
- void Display::exits(Map map) {
- //print possible exits
- std::cout << "You are at " << map.charX << "," << map.charY << std::endl;
- std::cout << "Possible exits are " << map.possibleExits() << std::endl
- << "> ";
- }
- void Display::finish(Map map) {
- //print character location message
- std::cout << map.finishEvent() << std::endl;
- }
- void Display::intro() {
- std::cout
- << "Welcome to Gridworld! Quantized Excitement! Fate is waiting for you"
- << std::endl;
- std::cout
- << "Valid commands: N, S, E and W for directions, Q to quit the game."
- << std::endl;
- }
- void Display::quit() {
- std::cout << "Goodbye!" << std::endl;
- }
- bool InputManager::runTurn(Map map) {
- std::string s;
- std::cin >> s;
- std::cin.ignore();
- //convert input (n, N, north, North should all be valid, and converted to a single character 'N')
- char c = 'X';
- if ((s == "n") or (s == "N")) {
- c = 'N';
- }
- if ((s == "e") or (s == "E")) {
- c = 'E';
- }
- if ((s == "s") or (s == "S")) {
- c = 'S';
- }
- if ((s == "w") or (s == "W")) {
- c = 'W';
- }
- if ((s == "q") or (s == "Q")) {
- return true; //QUIT
- }
- map.moveChar(c);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement