Grid.cpp 3.1 KB
Newer Older
Noah Olsen's avatar
Noah Olsen committed
1
// "Copyright [year] <Copyright Owner>"
Noah Olsen's avatar
commit  
Noah Olsen committed
2 3 4 5 6 7 8 9 10 11 12
#include <cstdlib>
#include <iostream>
using namespace std;

#include "Grid.h"

// randomly generate grid and start score at 0
// Ensure player and enemies start at different places

// define the functions for grid
char Grid::getDirection() {
Noah Olsen's avatar
Noah Olsen committed
13 14 15 16 17 18
    cout << "Where do you want to go?" << endl;
    cout << "Please enter u for up, r for right, d for down, l for left, or m "
            "for a random direction."
         << endl;
    cin >> direction;
    return direction;
Noah Olsen's avatar
commit  
Noah Olsen committed
19
}
Noah Olsen's avatar
Noah Olsen committed
20

Noah Olsen's avatar
commit  
Noah Olsen committed
21
// 2D array using subtraction in direction
Noah Olsen's avatar
Noah Olsen committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*int Grid::move(int GridRows, int GridColumns, char direction) {
 if (direction == 'u') {
   if ((room == 3) || (room == 2) || (room == 1)) {
   } else {
     room = room - 3;
   }
 } else if (direction == 'r') {
   if ((room == 3) || (room == 6) || (room == 9)) {
   } else {
     room = room + 1;
   }
 } else if (direction == 'd') {
   if ((room == 7) || (room == 8) || (room == 9)) {
   } else {
     room = room + 3;
   }
 } else if (direction == 'l') {
   if ((room == 1) || (room == 4) || (room == 7)) {
   } else {
     room = room - 1;
   }
 } else if (direction == 'm') {
   room = move(room, getRandDirection());
 } else {
   cout << "Invalid entry. Please input valid entry. ";
   room = move(room, getDirection());
 }
 return room;
}*/
int Grid::move(int GridRows, int GridColumns, char direction) {
    if (direction == 'u') {
        if ((GridRows == 4) || (GridRows == 3) || (GridRows == 2) ||
            (GridRows == 1)) {
        } else {
            GridRows += 1;
        }
    } else if (direction == 'r') {
        if ((GridColumns == 4) || (GridColumns == 8) || (GridColumns == 12) ||
            (GridColumns == 16)) {
        } else {
            GridColumns += 1;
        }
    } else if (direction == 'd') {
        if ((GridRows == 13) || (GridRows == 14) || (GridRows == 15) ||
            (GridRows == 16)) {
        } else {
            GridRows -= 1;
        }
    } else if (direction == 'l') {
        if ((GridColumns == 1) || (GridColumns == 5) || (GridColumns == 9) ||
            (GridColumns == 13)) {
        } else {
            GridColumns -= 1;
        }
    } else if (direction == 'm') {
        move(GridRows, GridColumns, getRandDirection());
Noah Olsen's avatar
commit  
Noah Olsen committed
78 79 80 81
    }
}

char Grid::getRandDirection() {
Noah Olsen's avatar
Noah Olsen committed
82 83 84 85 86 87 88 89 90 91
    otherDirection = rand() % 5 + 1;
    if (otherDirection == 5) {
        return 'u';
    } else if (otherDirection == 3) {
        return 'r';
    } else if (otherDirection == 4) {
        return 'd';
    } else {
        return 'l';
    }
Noah Olsen's avatar
commit  
Noah Olsen committed
92 93
}

Noah Olsen's avatar
Noah Olsen committed
94 95 96 97 98 99 100 101 102 103
void Grid::printGrid(int GridRows, int GridColumns) {
    char grid[GridRows][GridColumns];
    for (int i = 0; i < GridRows; i++) {
        for (int j = 0; j < GridColumns; j++) {
            grid[i][j] = '.';
            cout << grid[i][j];
        }
        cout << endl;
    }
}
Noah Olsen's avatar
commit  
Noah Olsen committed
104 105 106 107 108 109 110 111 112 113 114 115
/*void Grid::printGrid(int player, int bosses) {
  char grid[10];
  for (int i = 0; i < 10; i++) {
    grid[i] = '.';
  }
  grid[player] = 'P';
  grid[bosses] = 'B';
  cout << grid[1] << grid[2] << grid[3] << endl;
  cout << grid[4] << grid[5] << grid[6] << endl;
  cout << grid[7] << grid[8] << grid[9] << endl;
}*/
// 2D array print inside a nested loop