1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <iostream>
#include <string>
#include <limits>
using namespace std;
static inline void clear_cin_errors()
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
static inline bool getDim(int &dimension)
{
cout << "Enter a map size between 3 and 9 inclusive: ";
cin >> dimension;
clear_cin_errors();
if (dimension >=3 && dimension <= 9)
{
return (true);
}
cout << dimension << " map size is incorrect." << endl;
return (false);
}
static inline bool getCoord(const string& axis, const int &dimension, int &coord)
{
cout << axis << endl;
cin >> coord;
clear_cin_errors();
if (coord >=0 && coord <= dimension - 1)
{
return (true);
}
cout << coord << " is incorrect." << endl;
cout << "Point must be a valid position on the map." << endl;
return (false);
}
static inline bool shiftCoord(const string& axis, const int &dimension, int &coord)
{
cout << axis << endl;
int offset = 0;
int tmpCoord = coord;
cin >> offset;
clear_cin_errors();
tmpCoord += offset;
if (tmpCoord >=0 && tmpCoord <= dimension - 1)
{
coord = tmpCoord;
return (true);
}
cout << offset << " is incorrect." << endl;
cout << "Treasure location must be within the confines of the map." << endl;
return (false);
}
static inline int getSelection()
{
cout << endl << "Select: ";
int selection = -1;
cin >> selection;
clear_cin_errors();
return (selection);
}
static inline void Draw(int dimension, int treasureRow, int treasureCol)
{
for (int r = 0; r <= dimension; r++)
{
for (int c = 0; c <= dimension; c++)
{
if(r == treasureRow && c == treasureCol + 1)
{
cout << "X ";
}
else if (r == dimension && c == 0)
{
cout << " ";
}
else if (r < dimension && c == 0)
{
cout << r << " ";
}
else if (r == dimension)
{
cout << c - 1 << " ";
}
else
{
cout << "- ";
}
}
cout << endl;
}
}
int main()
{
int dimension = 0;
while (!getDim(dimension));
int treasureRow = -1;
while (!getCoord("Enter Initial location of X (row): ", dimension, treasureRow));
int treasureCol = -1;
while (!getCoord("Enter Initial location of X (column): ", dimension, treasureCol));
Draw(dimension, treasureRow, treasureCol);
cout << endl;
cout << "Menu options" << endl;
cout << "------------" << endl;
cout << "1) Update treasure coordinates (row/column)." << endl;
cout << "2) Shift X left or right. " << endl;
cout << "3) Shift X up or down." << endl;
cout << "4) Exit program." << endl;
cout << endl;
// take input option from user
int selection;
// switch statement
while ((selection = getSelection()) != 4)
{
switch(selection)
{
case 1:
{
while (!getCoord("Enter updated location of X (row): ", dimension, treasureRow));
while (!getCoord("Enter updated location of X (column): ", dimension, treasureCol));
cout << "Treasure Row: "<<treasureRow << endl;
cout << "Treasure Col: "<<treasureCol << endl;
Draw(dimension, treasureRow, treasureCol);
break;
}
case 2:
{
while (!shiftCoord("Shift X left or right (negative values left, positive values right)", dimension, treasureCol));
cout << "Treasure Col: "<< treasureCol << endl;
Draw(dimension, treasureRow, treasureCol);
break;
}
case 3:
{
while (!shiftCoord("Shift X up or down (negative values down, positive values up):", dimension, treasureRow));
cout << "Treasure Row: " << treasureRow << endl;
Draw(dimension, treasureRow, treasureCol);
break;
}
case 4:
{
cout << "Exiting" << endl;
break;
}
default:
{
break;
}
}
}
cout << "Goodbye, press Enter to continue...\n";
clear_cin_errors();
return (0);
}
|