In: Computer Science
Langton Ant Program:
This assignment will apply Langton's ant a two-dimensional universal turning machine on a 2 dimensional X by X grid where each grid cell is of the color white or black and the ant can be described as one of eight states ie. cell color and orientation.
Create a X by X square grid of “Cell”s that are either black or white. Begin with the “ant” in the center of the grid ( X must be a odd number/integer). Start the ant in a “up” orientation in the center of the board. Perform N steps where each step has to follow some simple rules:
You are to program the `Cell` class according to the following:
Cell color:
The `Cell` class will store the cell's color as a private member variable named `color` of type `CellColor` where `CellColor` is an enumeration data type defined as:
“enum CellColor {white, black};”
Constructor :
The `Cell` class will be constructed using the default constructor i.e. will not accept any arguments. The constructor will set `color` to `white` upon construction.
Cell Color Mutator
The `Cell` class will contain a public member function called `change_color` to change the `color` member. I.e. if `color` is set to `white`, calling `change_color( )` will change the `color` to `black`. The member function `change_color` will return type `void` and will accept no input parameters.
Cell Color Accessors
The `Cell` class will contain two "getter" functions to access `color`.
1.) Must be named `get_color`, will return type `CellColor`, must return the value of `color`, and must accept no input parameters. `get_color must return the value of `Cell`'s `color` member.
The template we will be using to declare the “Cell” class
Cell.hpp:
#include
// Declare an enumeration data type to store the Cell's color
enum CellColor {white, black};
// Declare the Cell Class
class Cell {
public:
// Declare default constructor to initialize color to white
// Declare member function getter for the color (get_color). Returns CellColor
// Declare a member to flip the color (change_color)
// Declare a member to print the string for this color.
// white = "0", black = "1"
private:
// Declare the color of this cell (color) as type CellColor
};
#endif
Cell.cpp:
#include "Cell.hpp"
(This is only part 1. of this assignment so I only need the code for this part I described and not the grid and ant movement yet)
Below is completed code. Let me know if you have any problem or doubt. Thank you.
=====================================================================
Cell.hpp
===================
#ifndef CELL_H
#define CELL_H
#include <string>
// Declare an enumeration data type to store the Cell's
color
enum CellColor { white, black };
// Declare the Cell Class
class Cell {
public:
// default constructor
Cell() {
color = white;
}
// getter
CellColor get_color() {
return color;
}
std::string get_color_string() {
if (color == white)
{
return "0";
}
else {
return "1";
}
}
// mutator
void change_color() {
if (color == white)
{
color = black;
}
else {
color = white;
}
}
private:
CellColor color;
};
#endif
===================
Main.cpp
===================
#include <iostream>
#include "Cell.h"
using namespace std;
int main() {
cout << "Test for Cell class" <<
endl;
cout << "Testing constructor" <<
endl;
// create a cell object
Cell c1;
cout << "Color of cell is: " <<
c1.get_color_string() << endl;
cout << "Testing change_color" <<
endl;
c1.change_color();
cout << "Color of cell is: " <<
c1.get_color_string() << endl;
cout << "Testing change_color" <<
endl;
c1.change_color();
cout << "Color of cell is: " <<
c1.get_color_string() << endl;
cout << "Testing get_color" << endl;
if (c1.get_color() == white) {
cout << "Cell color is white"
<< endl;
}
else {
cout << "Cell color is black"
<< endl;
}
cout << "changing color" << endl;
c1.change_color();
if (c1.get_color() == white) {
cout << "Cell color is white"
<< endl;
}
else {
cout << "Cell color is black"
<< endl;
}
return 0;
}
===================