I am creating a program that involves a simple agent navigating a grid world to discover food. I am having trouble creating a unique class for the agent (which contains data members that represent its perception of the world - direction of the smell of the food, a visual 5x7 field which contains barracades and other world objects, and the agent's energy ). The agent receives these in the form of a single string from a socket buffer and then sends its response based on it's reasoning resulting from the current state of the environment (which changes every time it makes a decision- so it is a stimulus-response agent).
I am very new at programming (probably obviously) and would appreciate some code critique and/or ideas on how to make this program better. I am specifically having troubles with moving my existing implementation to an agent class in a .h file. I also would like to include the 2d vector representing the agent's visual field in that class and maybe add data members to it which represent front, back, left, right etc. Thank you for taking the time to look : )
/* agentnavigation
*/
using namespace std;
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <iomanip>
#include "connection.cpp"
#include "agent.h"
int main( )
{
string role;
int sock;
/* establishing role as base */
role = "base\n";
/* to connect to the environment of Maeden, connect to the Madenport server
by creating an intance of the socket class and sending your role.
Socket constructer returns the agent's id. */
sock = connect(role);
//
string Message = Receive(sock);
/**************** agent information *********************/
char id = Message.at(0);
char dir = Message.at(2);
stack<char> inventory;
char gd[35];
int it = 0; //iterator for grid
char ground;
string message("");
//put this into agent class
stack<char> s;
char* myposition;
int endlines = 0;
/* get input */
for (int i = 1; i < Message.size(); i++)
{
if ( Message.at(i) == '\n') { endlines++; }
if ( Message.at(i) == '"' && !s.empty()) { s.pop(); }
if ( Message.at(i) == '"' && s.empty() )
{
s.push( Message.at(i));
if( endlines == 2 && Message.at(i+1) != '(' && Message.at(i+1) != ')') { inventory.push( Message.at(i+1) );}
if( endlines == 3 && Message.at(i+1) != '(' && Message.at(i+1) != ')' ) //fill grid array up with data
{
{
gd[it] = (Message.at(i+1));
it++;
}
}
}
/* arrive at the point in the buffer which shows what is on the ground */
if ( endlines == 4 && Message.at(i+1) != '(' )
{
if (Message.at(i+1) == ')') ground = ' ';
else ground = Message.at(i+1);
}
}
// cout << "inventory of size " << inventory.size() << " contains: " << inventory.top() << '\n';
cout << "grid of size " << it << ":" << '\n';
// for ( int i=0; i< it; i++)
// {
// cout << i << gd[i] << '\n';
// }
// cout << "id: " << id << '\n';
// cout << "direction: " << dir << '\n';
// cout << "ground: "" << ground << """<<'\n';
/********** visual field ****************/
vector<vector<char> > grid ( 5, vector<char> (7) );
int i = 0;
while (i < it)
{
for (int k=0; k<5; k++)
{
for (int j = 0; j < 7; ++j)
{
grid[k][j] = gd[i];
i++;
}
}
}
for ( int i = 0; i < 5; i++ )
{
for ( int j = 0; j < 7; j++ )
cout << '|' << grid[i][j] <<'|';
cout<<'\n';
}
/********** end of agent attribute intialization****************/
Send("r\n", sock);
//HELP: having trouble with using agent class functions
//agent mouse(role , id);
//mouse.interpret( Receive(sock) );
//mouse.respond();
Receive(sock);
//cout << "Received sock...in whileloop before Send() \n";
return 0;
}
/*
* agent.h
*
*
*/
#ifndef AGENT_H
#define AGENT_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class agent
{
public:
agent();
agent (string role, char i);
~agent();
void initagent(string r, char i) ;
string role() const { return myrole; }
struct world
{
char gradient;
bool passedCheese;
char ground;
bool onCheese;
/*** HELP : I would like to consolidate these into a visual grid
representation structure using the vector matrix from the
main ( vector< vector<char> > grid;) ***/
/* bool blockedLeft;
bool blockedRight;
bool blockedFront; */
char inventory;
};
/** HELP: knowlege base structure representation( functions consisting of if/else statements ??) **/
/*return response functions */
string myrole;
char myId;
char energy;
};
#endif /*"agent.h"*/