-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thomas7.cpp
116 lines (106 loc) · 2.51 KB
/
Thomas7.cpp
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
// Assignment 7 - COSC 1410
// TA: Can Cao
// Author: Micah Thomas
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
enum DIRECTION {EAST = 1, WEST = 2, NORTH = 3, SOUTH = 4};
const int BOARD_SIZE = 15;
const int CENTER = (BOARD_SIZE / 2) - 1;
void init();
int getSteps();
void printSim(int board[][BOARD_SIZE], int x, int y);
int runSim(int board[][BOARD_SIZE], int steps, int &i, int &j);
bool walk(int &x, int &y, DIRECTION dir, int onBoard[][BOARD_SIZE]);
int main() {
int board[BOARD_SIZE][BOARD_SIZE] = {0}, steps = 0, i = 0, total = 0;
init();
steps = getSteps();
board[CENTER][CENTER] = 1;
int x = CENTER, y = CENTER;
while (steps > 0) {
i = runSim(board, steps, x, y);
total += i;
printSim(board, x, y);
cout << "\nTotal Steps: " << total;
if (i != steps) {
cout << "\nPress any key to continue ...";
cin.get();
cin.get();
return 0;
} else
steps = getSteps();
}
return 0;
}
void init() {
int seed;
cout << "Seed: ";
cin >> seed;
srand(seed);
}
int getSteps() {
int steps;
cout << "\nNumber of steps (0 to stop): ";
cin >> steps;
return steps;
}
void printSim(int board[][BOARD_SIZE], int i, int j) {
cout << "\n\n";
for (int x = 0; x < BOARD_SIZE; x++) {
cout << "\t";
for (int y = 0; y < BOARD_SIZE; y++) {
if (x == i && y == j)
cout << "x";
else
cout << "-";
}
cout << "\t\t";
for (int y = 0; y < BOARD_SIZE; y++)
cout << " " << board[x][y] << " ";
cout << endl;
}
}
int runSim(int board[][BOARD_SIZE], int steps, int &x, int &y) {
for (int i = steps; i > 0; i--) {
DIRECTION dir = static_cast<DIRECTION>(rand() % 4 + 1);
if (!walk(x, y, dir, board)) {
cout << "\nWalk over in " << (steps - i + 1) << " steps!\n";
return (steps - i + 1);
}
}
return steps;
}
bool walk(int &x, int &y, DIRECTION dir, int onBoard[][BOARD_SIZE]) {
int i = x, j = y;
cout << "\nWalking from (" << x << "," << y << ")";
switch (dir) {
case NORTH:
y -= 1;
cout << " NORTH ";
break;
case SOUTH:
y += 1;
cout << " SOUTH ";
break;
case EAST:
x += 1;
cout << " EAST ";
break;
case WEST:
x -= 1;
cout << " WEST ";
break;
default:
break;
}
cout << " to (" << x << "," << y << ")";
if ( (x > 14) || (y > 14) || (x < 0) || (y < 0)) {
cout << "\nCan't walk outside of box";
x = i; y = j;
return false;
}
onBoard[y][x] += 1;
return true;
}