Question

In: Computer Science

The Gary class is subject to C++ unit testing and therefore has stricter requirements for composition....

The Gary class is subject to C++ unit testing and therefore has stricter requirements for composition. Each required member function will be denoted. (

1) Gary shall be constructed with a parameterized constructor accepting an unsigned integer input parameter representing the size of the board (denote here as BoardSize). Assume that BoardSize is odd! Gary shall initialize his position to be the middle cell of the board, e.g., if the BoardSize is given as 5 Gary would be initialized at index (2,2).

(2) Gary shall contain public member functions which return an unsigned integer type and accept no input named Gary::get_row() and Gary::get_col() which return Gary's row and column position on the board respectively.

(3) Gary shall contain a public member function which returns type void and accepts a Cell pointer called Gary::move(Cell*) which shall (a) alter Gary's orientation based on the Cell's color (b) change the Cell's color (c) move Gary one unit forward in the new orientation

(4) Gary shall contain a public member function which returns type orientation (defined as an enumeration enum orientation {up, right, down, left};) and accepts no input parameters called Gary::get_orientation()

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Here I am attaching code for all files:

  • main.cpp
  • Gary.cpp
  • Gary.hpp
  • Board.cpp
  • Board.hpp
  • Cell.cpp
  • Cell.hpp

main.cpp:

#include "Cell.hpp"
#include "Board.hpp"
#include "Gary.hpp"
using std::cout;
using std::endl;

int main(int ac, char** argv){
Board B(unsigned int 5);
  

  
// unsigned int boardSize; //(from command line arguments)
// unsigned int numberSteps; //(from command line arguments)
// std::string outputFilename; // (from optional command line argument)
//
// if (ac != 4 and ac != 5) {
// cout << "Error: Command line arguments are incorrect. Call program as (1) or (2)"
// << endl;
// return -1;
// }
//
// // Parse the command line arguments
// //filename is not specified
// else if (ac == 4){
// boardSize = atoi(*(argv + 2));
// numberSteps = atoi(*(argv + 3));
// }
//
// //filename is specified
// else{
// boardSize = atoi(*(argv + 2));
// numberSteps = atoi(*(argv + 3));
// outputFilename = *(argv + 4);
// }
//
// cout << "boardSize: " << boardSize << endl;
// cout << "steps: " << numberSteps << endl;
// cout << "output Filename " << outputFilename << endl;
//
// Board B (boardSize);
// Gary G (boardSize);
//
// //G.get_orientation();
// G.get_orientation_string();
// G.get_row();
// G.get_col();
//
// //begin testing turn() function
// Cell c;
// Cell *cptr = &c;
// G.turn(cptr);
// c.change_color();
// G.turn(cptr);
// //end testing turn() function
//
// /*
// else{
// Board B(boardSize);
// if (an output filename is given){
// B.setOutputFilename(outputFilename);
// }
// B.move_gary(numberSteps);
// }
//*/
// vector<int> testVec(3);
return 0;
}

Gary.cpp:

#include "Gary.hpp"
#include <stdio.h>
#include <iostream>

using std::cout;
using std::endl;

Gary::Gary(unsigned int boardSize){
if ((boardSize % 2) == 0){
boardSize = boardSize + 1;
}
  
//initial pos = board center
Position.x = ceil(boardSize/2);
Position.y = ceil(boardSize/2);
//cout << "(x,y)" <<
//init orientation = up
Orientation = up;
cout << "[Gary constructor]\n" << "\t init position: (" << Position.x << "," << Position.y << ")" << endl;
cout << "\t init orientation: " << Orientation << endl;
max = boardSize; // need to store in order to specify when to wrap around
  
}

unsigned int Gary::get_row(){
cout << "[Gary::get_row]\n" << "\t row: " << Position.x << endl;
return Position.x;
}

unsigned int Gary::get_col(){
cout << "[Gary::get_col]\n" << "\t col: " << Position.y << endl;
return Position.y;
}

orientation Gary::get_orientation(){
cout << "[Gary::get_orientation]\n" << "\t orientation: " << Orientation << endl;
return Orientation;
}

void Gary::get_orientation_string(){
cout << "[Gary::get_orientation_string]" << endl;
string orientationString;
//orientation Orientation = get_orientation();
switch(Orientation){
case (up):
cout << "\t orientation: up" << endl;
break;
case (down):
cout << "\t orientation: down" << endl;
break;
case (left):
cout << "\t orientation: left" << endl;
break;
case (right):
cout << "\t orientation: right" << endl;
break;
}
}

void Gary::translate(Cell* cell){
const int maxIndex = max -1;
//translate
switch (Orientation){
case (up):
//check if it's a boundary condition
if (Position.x == 0){
Position.x = maxIndex;
}
else {
++Position.y;
}
break;
case (down):
//check if it's a boundary condition
if (Position.x == maxIndex){
Position.x = 0;
}
else {
--Position.y;
}
break;
case (left):
//check if it's a boundary condition
if (Position.y == 0){
Position.y = maxIndex;
}
else {
--Position.y; //go to left col
}
break;
case (right):
//check if it's a boundary condition
if (Position.y == maxIndex){
Position.y = 0;
}
else {
++Position.y; //go to adj right col
}
break;
}
}

void Gary::turn(Cell* cell){
const int maxIndex = max -1;
cout << "[Gary::turn]" <<endl;
//turn
CellColor color = cell->get_color();
if (color == black){
if (Orientation == up){
if (Position.x != 0){ //not in first row
Orientation = left;
}
}
else if (Orientation == down) {
if (Position.x != maxIndex){ //not in the last row
Orientation = right;
}
}
else if (Orientation == left){
if (Position.y != 0){ //not in first col
Orientation = down;
}
}
else{ // Orientation == right
if (Position.y != maxIndex){ //not in last col
Orientation = up;
}
}
}
if (color == white){
if (Orientation == up){
if (Position.x != 0){ //not in first row
Orientation = right;
}
}
else if (Orientation == down) {
if (Position.x != maxIndex){ //not in the last row
Orientation = left;
}
}
else if (Orientation == left){
if (Position.y != 0){ //not in first col
Orientation = up;
}
}
else{ // Orientation == right
if (Position.y != maxIndex){ //not in last col
Orientation = down;
}
}
cout<< "\t" ;
get_orientation_string();
}
}

void Gary::move(Cell* cell){
  
}

Gary.hpp:

#ifndef Gary_hpp
#define Gary_hpp

#include <stdio.h>
#include "Cell.hpp"
#include <iostream>
#include <vector>
#include <math.h> /* ceil */
struct position{
int x;
int y;
};

enum orientation {up, down, left, right};
class Gary{
public:
// ********* member funcs ******************** //
Gary(unsigned int boardSize);
unsigned int get_row();
unsigned int get_col();
void turn(Cell* cell);
void translate(Cell* cell);
void move(Cell* cell); //move calls turn( ), translate( )
orientation get_orientation();
void get_orientation_string();

// ********* data members ******************** //
position Position;
orientation Orientation;
private:
//void turn(Cell* cell);
unsigned int max;
};

#endif /* GaryClass_hpp */

Board.cpp:


//#include "Cell.hpp"
//#include "Gary.hpp"
//
//#ifndef Board_hpp
//#define Board_hpp
//#include <stdio.h>
//#include <iostream>
//using std::vector;
//
//using std::cout;
//using std::endl;
//using std::vector;
//
//class Board{
// public:
//
// int Size;
// Gary G(unsigned int n);
// vector<vector<Cell>> grid;
// unsigned int n;
//
//public:
//
// Board(unsigned int n){
// int Ans = IsEven(n);
//
// if(Ans == 0){
// n++;
// }
// createGrid();
//// Gary G(unsigned int n);
//
//
//// int Size = n;
// }
// void createGrid(){
// Cell c;
// vector<Cell> x;
//
// for(int ll = 1;ll < n;ll++){
// for(int ii = 1;ii < n;ii++){
// x.push_back(c);
// }
// grid.push_back(x);
// }
// }
// int IsEven(unsigned int x){
// int check = n%2;
// int Ans;
//
// if(check != 0){
// Ans = 1;
// }else{
// Ans = 0;
// }
// return Ans;
// }

//};


Board::Board(unsigned int n){
//check if boardSize is odd
//define a row containing number = boardSize Cell objects
//if n is odd, add 1
cout << "[Board constructor]\n" << "\t n is " << n << endl;
if ((n % 2) == 0){
cout << "\t Board dimension must be an odd number!! Got " << n << " and adding 1 to equal " << n + 1 << endl;
boardSize = n + 1;
}
else{
boardSize = n;
}
cout << "\t new boardSize: " << boardSize << endl;
const int numCells = boardSize;

vector<Cell> rowCell(numCells); //declare a row vector of cell objects
//populate it
for (int i = 0; i<numCells; ++i ){

}
vector<vector<Cell>> Board(numCells, rowCell); //create a vector of row vectors
//check if the board dims are n x n
cout<<"\t rows in board: " << Board.size() <<endl;
cout<<"\t cols in board: " << rowCell.size() <<endl;

}

Board.hpp:

#include "Cell.hpp"
#include "Gary.hpp"

#ifndef Board_hpp
#define Board_hpp
#include <stdio.h>
#include <iostream>
using std::vector;


class Board;
class Board {
public:
Board(unsigned int n);
unsigned int boardSize;
void move_gary(unsigned int numSteps);
//private (once tested)
//Gary gary;
// vector<Cell> rows;
// vector<vector<Cell>> Grid( const int stackedRows, vector<Cell> rows);//private:
//vector<vector<int>> array_2d(rows, vector<int>(cols, 0));
vector<vector<Cell>> Grid( int n, vector<Cell>( int n, Cell));
// ********* data ************//
//Cell cell;


// ********* member funcs ************//

};

#endif /* Board_hpp */

Cell.cpp:

#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
#include "Cell.hpp"
using std::cout;
using std::endl;
using std::string;

Cell::Cell(){
// Default constructor initializes cell color to white
color = white;
}
//setColor

void Cell::change_color(){
//cout<<"[Cell::ChangeColor]"<<endl;
if (color == white){
color = black;
}
else{
color = white;
}
//cout<< "\tchanging cell color to black" <<endl;
}

CellColor Cell::get_color(){
//cout<< "[Cell::get_color]" << endl;
if (color == white){
//cout<< "\tcell color is white"<<endl;
}
else{
//cout<<"\tcell color is black"<<endl;
}
return color;
}

string Cell::get_color_string(){
//cout<< "[Cell::get_color_string]" << endl;
string c;
if (color == white){
c = "0";
}
else{
c = "1";
}
cout<< c << endl;
//cout<<"\tc is "<< c <<endl;
return c;
}


Cell.hpp:

#ifndef HAVEYOUSEENTHSISNAIL_CELL
#define HAVEYOUSEENTHSISNAIL_CELL


#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
//Cell Class Template:
using std::cout;
using std::endl;
using std::string;
// Declare an enumaration data type to store the Cell's color
enum CellColor {white = 0, black = 1};

class Cell {
// Declare the Cell Class
//create ONE cell, and THEN in main() initialize the grid

public:
// Declare default constructor to initialize color to white
Cell();
// Declare member function getter for the color (get_color). Returns CellColor

// Declare a member to flip the color (change_color)
void change_color();
  
// Declare a member to print the string for this color.
// white = "0", black = "1"
CellColor get_color();
  
string get_color_string();
  
//set the color (not req'd)
void setColor(CellColor c){ color = c;}

private:
// Declare the color of this cell (color) as type CellColor
CellColor color;
};

#endif


Related Solutions

Code in C++ Must show: unit testing ------------------------------------ UsedFurnitureItem Create a class named UsedFurnitureItem to represent...
Code in C++ Must show: unit testing ------------------------------------ UsedFurnitureItem Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) //...
Code in C++ Must show: unit testing ------------------------------------ UsedFurnitureItem Create a class named UsedFurnitureItem to represent...
Code in C++ Must show: unit testing ------------------------------------ UsedFurnitureItem Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) //...
Subject: Software, Architecture Design and Testing You are to determine the user requirements for a web...
Subject: Software, Architecture Design and Testing You are to determine the user requirements for a web phone-mail product. The primary purpose of this product is to give phone-mail users (e.g., faculty and staff) the ability to access the functionality of the phone-mail system from a web page. In general, the product should enable users of the phone-mail system to do their usual phone-mail activities via a web page. Evaluate these requirements. Do your requirements satisfy the eight criteria: Understandable, Verifiable,...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based...
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based on client outcomes, scientific evidence and political inputs. How can an RN keep up their practice with the ever changing requirements?
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based...
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based on client outcomes, scientific evidence and political inputs. Discuss one law or regulation that directly impact the practice of the RN?
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based...
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based on client outcomes, scientific evidence and political inputs. Discuss one law or regulation that directly impact the practice of the RN? How can an RN keep up their practice with the ever changing requirements?
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based...
Regulation of healthcare has been subject to many rules, laws and requirements. Requirements continually change based on client outcomes, scientific evidence and political inputs. Discuss one law or regulation that directly impact the practice of the RN? How can an RN keep up their practice with the ever changing requirements?
A construction unit Class C Basic Toolroom has a 110 volt 20A circuit has the following...
A construction unit Class C Basic Toolroom has a 110 volt 20A circuit has the following items: three 200 watt lights, a 2500 watt heating machine, a 1000 watt heating machine and a ¼ hp motor (use 1000 watts/hp). All of these items act as a load on the circuit. As measured in amperes, determine the circuit load.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT