In: Computer Science
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()
Working code implemented in C++ and appropriate comments provided for better understanding.
Here I am attaching code for all files:
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