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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include<iostream>
using namespace std;
int main()
{
//island to walk across -1 is water, 3 is a bridge, 0 are the walk ways, and 2 is a flower
int island[13][13]={{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }};
int randomNum;
int count = 0;
int stopper = 0;
int x = 6;
int y = 0;
int cost=0;
int averageCost;
//variables to count how many times made it to the other bridge or rescued
int madeIt = 0;
int rescued = 0;
for (count = 0; count < 5000;)
{
//resets the island, stopper, and x,y to original values
int island[13][13] = { { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };
stopper = 0;
//starting point on the island
x = 6;
y = 0;
do
{
randomNum = rand() % 100 + 1;
if (randomNum <= 44)
{
//this is to step forward
y += 1;
if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
cost += 5;
}
else if (island[x][y] == -1)
{
stopper = -1;
count++;
rescued++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
madeIt++;
}
}
else if (randomNum > 44 && randomNum <= 65)
{
//this steps right
x += 1;
if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
cost += 5;
}
else if (island[x][y] == -1)
{
stopper = -1;
count++;
rescued++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
madeIt++;
}
}
else if (randomNum > 65 && randomNum <= 90)
{
//this steps left
x -= 1;
if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
cost += 5;
}
else if (island[x][y] == -1)
{
stopper = -1;
count++;
rescued++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
madeIt++;
}
}
else if (randomNum > 90 && randomNum <= 100)
{
//this steps backwards
y -= 1;
if (island[x][y] == 2 || island[x][y] == 1)
{
island[x][y] -= 1;
cost += 5;
}
else if (island[x][y] == -1)
{
stopper = -1;
count++;
rescued++;
}
else if (island[x][y] == 3)
{
stopper = 3;
count++;
madeIt++;
}
}
else
{
cout << "error" << endl;
}
} while (stopper != 3 || stopper != -1);
cout << stopper << endl;
}
averageCost = cost / 5000;
cout << "Cost of walking on island" << averageCost << endl;
cout << "Times rescued in water" << rescued << endl;
cout << "Times made it to a bridge" << madeIt << endl;
return 0;
}
|