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
|
#include <cstdlib>
#include <iostream>
#include <cmath>
#include "ant.h"
using namespace std;
/*
This program is designed to run a board approximately 20 by 20 spaces. A symbol
* is placed to represent the ant on the board (seen as "^", ">", "<", "v" depending
* upon the directional orientation.
* The ant moves according to various key inputs and will gather food pieces
* randomly scattered across the board (seen as "F"). This food will add to its
* overall energy level by five.
*/
ant a;
//This boolean is designed to find the closest piece of food on the board by comparing
//the position of the ant to the location of every piece of food, designated by the
//letter "F," and print the exact coordinates of the closest one.
bool ClosestFood(int board[20][20], ant &, int *cx, int *cy) {
double closest, closenow;
bool found = false;
int ax = a.GetX();
int ay = a.GetY();
int dx, dy;
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 20; x++) {
if (board[x][y] == 1) {
dx = x - ax;
dy = y - ay;
closenow = sqrt(dx*dx + dy*dy);
if (found == false) {
found == true;
*cx = x;
*cy = y;
closest = closenow;
}
else if (closenow < closest) {
*cx = x;
*cy = y;
closest = closenow;
}
}
}
}
return found;
}
int main(int argc, char** argv) {
srand(time(NULL));
int cx, cy;
int food = 10;
int board[20][20];
ant a;
char action;
/*This first pair of for loops is designed to initially set up the board
as a 20x20 board with food scattered in random positions while other areas
are displayed otherwise (as a ".").*/
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 20; x++) {
int food_appear = (rand() % food) + 1;
if (food_appear > 1) {
board[x][y] == 0;
}
else {
board[x][y] == 1;
}
}
}
/*This next section will set the initial appearance of the board with its
border edges, food, ant piece, and blank spaces. It will change the energy
of the ant should it run upon the food pieces.*/
for (int x = 0; x < 20; x++) {
cout << "-";
}
cout << endl;
for (int y = 0; y < 20; y++) {
cout << "|";
for (int x = 0; x < 20; x++) {
if (a.GetX() == x && a.GetY() == y) {
a.Draw();
}
else {
if (board[x][y] == 1) {
cout << "F";
}
else {
cout << ".";
}
}
if (board[x][y] == 1 && a.GetX() == x && a.GetY() == y) {
a.ChangeEnergy();
board[x][y] == 0;
}
}
cout << "|";
cout << endl;
}
for (int x = 0; x < 20; x++) {
cout << "-";
}
cout << endl;
ClosestFood(board[20][20], a, &cx, &cy);
cout << ClosestFood << endl;
/*The following is for entry of the string for movement as well as reprinting the board for each
consecutive movement in the string. Movement is done by entering "a" for left, "s" for right,
and "w" to go forward.
All movement occurs on the board, and after each movement the board is repeated so as to show
the new position of the ant, as well as if food has disappeared (should the ant have passed
over the top of a food piece during the previous turn).
At the end of every string of movements, the program will print the final energy of the ant.*/
while(a.GetEnergy() >= 1) {
cout << "Move left(a), right(s), or forward(w)?" << endl;
cin >> action;
switch (action) {
case 'a':
a.Left();
break;
case 's':
a.Right();
break;
case 'w':
a.Forward();
break;
}
for (int x = 0; x < 20; x++) {
cout << "-";
}
cout << endl;
for (int y = 0; y < 20; y++) {
cout << "|";
for (int x = 0; x < 20; x++) {
if (a.GetX() == x && a.GetY() == y) {
a.Draw();
}
else {
if (board[x][y] == 1) {
cout << "F";
}
else {
cout << ".";
}
}
if (board[x][y] == 1 && a.GetX() == x && a.GetY() == y) {
a.ChangeEnergy();
board[x][y] == 0;
}
}
cout << "|";
cout << endl;
}
for (int x = 0; x < 20; x++) {
cout << "-";
}
cout << endl;
cout << a.GetEnergy();
cout << endl;
}
return 0;
}
|