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
|
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
#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.
*/
//This void 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.
void find_closest(int b[20][20], ant a, int &ax, int &ay){
float closest_distance = 1000;
float distance;
//int ax,ay;
for(int y = 0; y < 20; y++){
for(int x = 0; x < 20; x++){
if(b[x][y] == 1){
distance = abs(x-a.GetX()) + abs(y-a.GetY());
//cout << x << " " << y << " " << distance << endl;
if(distance < closest_distance){
closest_distance = distance;
ax = x;
ay = y;
}
}
}
}
}
int main(int argc, char** argv) {
srand(time(NULL));
int x, y;
int b, c;
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;
}
}
}
/*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.*/
/*
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;
}
*/
while(a.GetEnergy() > 0){
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;
find_closest(board,a,x,y);
cout << "Closest Location: " << x << ", " << y << endl; //The exact coordinates of the closest food.
b = x;
c = y;
a.Move(b, c);
cout << endl;
usleep(100000);
}
return 0;
}
|