C++ programming You will design, implement, and test a program that implements a
ID: 3686405 • Letter: C
Question
C++ programming You will design, implement, and test a program that implements a simulation of Langton’s Ant. For a brief explanation consider Wikipedia: https://en.wikipedia.org/wiki/Langton%27s_ant. Note that is can be considered a cellular automaton. This means that you have an array or matrix of cells. Each turn or step the value of each cell may change based upon a simple rule. For the Ant there are two simple rules.
The rules are: 1. In a white square, turn right 90 degrees and change the square to black.
2. In a black square, turn left 90 degrees and change the square to white.
You can use a blank character for a white space. You can use the number sign (“#”) for black. And for the Ant? An asterisk of course (“*”). J Left ? Right? Those are relative directions! How are you going to keep track of the direction the Ant is facing? How will you “remember” what color the current cell occupied by the Ant is or was? You will created an Ant class to help organize, hold, and manipulate this information. Your program will prompt the user to enter the number of rows and columns for the 2D array. Use those values to create a dynamic array of Ant objects. You should also prompt the user for the number of steps. Suggest values to the user. Notice in the Wikipedia article they mention several hundred and 10000. Use functional decomposition to develop functions to validate the input. What parameters and criteria do these functions need? You should start by asking the user for the starting location of the Ant. You should add the option for the user to have a random starting location.
Your program should display each step.
Explanation / Answer
The program code is shown below:
Header file
class lAnt
{
public:
/// Direction, NESW.
enum Direction
{
lDIR_NORTH, lDIR_SOUTH, lDIR_WEST, lDIR_EAST
};
/**
* Constructor.
*/
lAnt();
/**
* Destructor.
*/
~lAnt();
void turnLeft();
/**
* Turns right.
*/
void turnRight();
/**
* Getting current x positon of ant.
*
*/
int getX();
/**
* Getting current y positon of ant.
*
*/
int getY();
/**
* Setting x positon of ant.
*
*/
void lsetX(int lx);
/**
* Setting y positon of ant.
*
*/
void lsetY(int ly);
private:
/// direction current
Direction lm_direction;
/// x position current.
int lm_x;
// ly position current.
int lm_y;
};
class lPlane
{
public:
/// plane width.
static const int lWIDTH = 30;
/// plane height.
static const int lHEIGHT = 18;
/// space 'on' check ?
static const int lON = 1;
/// space 'off' check?
static const int lOFF = 0;
/**
* Constructor.
*/
lPlane();
/**
* Destructor.
*/
~lPlane();
void lsetAnt(lAnt*);
void lflipColor(lAnt*);
int lgetColorAt(lAnt*);
int getWidth();
int getHeight();
void output();
void checkWrap();
void moveAnt();
private:
int lm_plane[lHEIGHT][lWIDTH];
lAnt* m_ant;
};
#include "langton.hpp"
#include <iostream>
using namespace std;
lAnt::lAnt()
{
lm_direction = lDIR_NORTH;
}
lAnt::~lAnt()
{
}
int lAnt::getX()
{
return lm_x;
}
int lAnt::getY()
{
return lm_y;
}
void lAnt::lsetX(int lx)
{
lm_x = lx;
}
void lAnt::lsetY(int ly)
{
lm_y = ly;
}
void lAnt::turnRight()
{
// turn 90 degress right.
switch (lm_direction)
{
case lDIR_NORTH: lm_direction = lDIR_EAST; lm_x += 1; break; // moving array's right
case lDIR_EAST: lm_direction = lDIR_SOUTH; lm_y += 1; break; // moving array's down
case lDIR_SOUTH: lm_direction = lDIR_WEST; lm_x -= 1; break; // moving array's left
case lDIR_WEST: lm_direction = lDIR_NORTH; lm_y -= 1; break; // moving array's up.
default: lm_direction = lDIR_NORTH; break;
}
}
void lAnt::turnLeft()
{
// turn 90 degress left.
switch (lm_direction)
{
case lDIR_NORTH: lm_direction = lDIR_WEST; lm_x -= 1; break;
case lDIR_WEST: lm_direction = lDIR_SOUTH; lm_y += 1; break;
case lDIR_SOUTH: lm_direction = lDIR_EAST; lm_x += 1; break;
case lDIR_EAST: lm_direction = lDIR_NORTH; lm_y -= 1; break;
default: lm_direction = lDIR_NORTH; break;
}
}
//-----------------------------------------------------------------------------
lPlane::lPlane()
{
// initialization of array with 0 values.
for (int li = 0; li < lPlane::lHEIGHT; li++)
{
for (int lj = 0; lj < lPlane::lWIDTH; lj++)
{
lm_plane[li][lj] = 0;
}
}
}
lPlane::~lPlane()
{
}
void lPlane::lsetAnt(lAnt* ant)
{
m_ant = ant;
}
void lPlane::lflipColor(lAnt* ant)
{
int lcolor = lm_plane[ant->getY()][ant->getX()];
if(lcolor == lPlane::lOFF)
{
lm_plane[ant->getY()][ant->getX()] = lPlane::lON;
}
else
{
lm_plane[ant->getY()][ant->getX()] = lPlane::lOFF;
}
}
int lPlane::lgetColorAt(lAnt* ant)
{
return lm_plane[ant->getY()][ant->getX()];
}
void lPlane::moveAnt()
{
lflipColor(m_ant); // flipping lcolor where current ant exists.
if(lgetColorAt(m_ant) == lPlane::lOFF)
{
// turn left if lcolor is off.
m_ant->turnLeft();
}
else if(lgetColorAt(m_ant) == lPlane::lON)
{
// turn right if on.
m_ant->turnRight();
}
checkWrap();
}
void lPlane::checkWrap()
{
// making sure we wrapped around things
if(m_ant->getX() < 0)
{
m_ant->lsetX(lPlane::lWIDTH - 1);
}
if(m_ant->getY() < 0)
{
m_ant->lsetY(lPlane::lHEIGHT - 1);
}
if(m_ant->getX() > lPlane::lWIDTH - 1)
{
m_ant->lsetX(0);
}
if(m_ant->getY() > lPlane::lHEIGHT - 1)
{
m_ant->lsetY(0);
}
}
void lPlane::output()
{
// display
for(int li = 0; li < lPlane::lHEIGHT; li++)
{
for(int lj = 0; lj < lPlane::lWIDTH; lj++)
{
if(lm_plane[li][lj] == lPlane::lOFF)
{
cout << "."; // print dot for lOFF colors
}
else if (lm_plane[li][lj] == lPlane::lON)
{
cout << "*"; // print asterisk for lON colors
}
}
cout << ' ';
}
}
#include <iostream>
#include "langton.hpp"
using namespace std;
int main(int argc, char* argv[])
{
lAnt* ant = new lAnt();
ant->lsetX(lPlane::lWIDTH / 2);
ant->lsetY(lPlane::lHEIGHT / 2); // starting position.
lPlane* plane = new lPlane();
plane->lsetAnt(ant);
while(true)
{
plane->output();
plane->moveAnt();
cout << ' ';
usleep(10000);
}
delete ant;
delete plane;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.