Vous êtes sur la page 1sur 4

/* * Jonathan Snider * Lab #15 * CS 120 - Section 2 * 12/6/2011 * */ /* * Ants move on their own, pick up food.

* New ant is generated when ant eats food. * */ /* * System Includes */ #include <cstdlib> #include <iostream> #include <cmath> /* * User Includes */ #include "ant.h" #include "node.h" using namespace std; /* * Quick print function to show current position and direction * Pos is listed as an x,y coordinate Pos: x, y * * param myAnt The ant class created in main(); */ void printInfo(ant my_ant) { cout << " || Pos: " << my_ant.getX() << ", " << my_ant.getY(); cout << " || Dir: " << my_ant.print(); cout << " || Energy: " << my_ant.getEnergy() << endl; } /*Fills the board with 0's. 10% void create(int b[20][20]){ for (int y = 0; y < 20; for (int x = 0; x < b[x][y] = 0; if(rand()%100 < b[x][y] } } } } /*Prints the board. Shows the ant at a specific point, "F" for where food is or "." in blank.*/ void print(int b[20][20], ant a){ for (int y = 0; y < 20; y++) { chance of a 1 which is where food is*/ y++){ 20; x++){ 10){ = 1;

cout << "|"; for (int x = 0; x < 20; x++) { if (a.getX() == x && a.getY() == y) { cout << a.print(); } else if(b[x][y] == 1){ cout << "F"; } else if(b[x][y] == 0){ cout << "."; } } cout << "|\n"; } cout << "-----------------------------------------------------" << endl; } /*Checks if there's an ant at x,y*/ ant *isant(int x, int y, node *n){ while(n != NULL){ //// while not at the end of the linked list if(n->get_ant()->getX() == x && n->get_ant()->getY() == y){ return n->get_ant(); // if yes, return a pointer to that //ant } n = n->get_next(); //if not, move down linked list } return NULL; } /*Prints board with a linked list of ants.*/ void print2 (int b[20][20], node *p){ ant *to_print; for(int y = 0; y < 20; y++){ for (int x = 0; x < 20; x++) { //go through board to_print = isant(x,y,p); // check if there's an ant at x,y if(to_print != NULL){ cout << to_print->print(); // prints ant } else if(b[x][y] == 1){ cout << "F"; } else if(b[x][y] == 0){ cout << "."; } } cout << "|\n"; } cout << "-----------------------------------------------------" << endl; } /*Finds closest food, prints location and distance of food.*/ void find_closest(int b[20][20], ant a, int &cx, int &cy){ float closest_distance = 1000000; float distance; 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()); if(distance < closest_distance){ closest_distance = distance;

cx = x; cy = y; } } } } cout << "Closest food is at " << cx << ", " << cy << ". " << "It is " << closest_distance << " spaces away." << endl; } /*void move_all(node *n, int b[20][20]){ while (n !=NULL){ n->get_ant()->move(b); n= n->get_next(); } }*/ /* * */ int main(int argc, char** argv) { int x, y; //for finding closest food int cx, cy; int board[20][20]; //creates array for board // srand(time(NULL)); //seeds random variable for placing food create(board); /*walking through a list of ants, ants move to food, spawn new ant, and print*/ node *first = new node(new ant); while(1){ print2(board, first); node *curr = first; while (curr != NULL){ find_closest(board, *(curr->get_ant()), cx, cy); curr->get_ant()->move(cx, cy); x = curr->get_ant()->getX(); y = curr->get_ant()->getY(); if(board[x][y] == 1){ //if the ant is on food curr->get_ant()->changeEnergy(5); // increase energy node* temp = new node(new ant); //creates new ant node temp->set_next(first); first = temp; temp = NULL; board[x][y] = 0; //removes food } curr = curr->get_next(); cin.get(); cin.clear(); //print2(board, first); } }

return 0; }

Vous aimerez peut-être aussi