Question

In: Computer Science

Langton Ant Program: This assignment will apply Langton's ant a two-dimensional universal turning machine on a...

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:

  1. If the ant is on a white tile turn 90° clockwise, then change the color of the tile & move forward one unit
  2. If the ant is on a black tile turn 90° counterclockwise, change the color of the tile and then move forward one unit.

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.

  1. Must be named `get_color_string`, must return type `std::string`, and must accept no input. `get_color_string` must return "1" if `color` is `black` and must return "0" if `color` is `white`.

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)

Solutions

Expert Solution

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;
}

===================


Related Solutions

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. These methods MUST be your original code.   Your program should output all the values in the array and then output the average high and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT