Commit 3e2ee13f authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Dissemination version.

parent 709f270a
Loading
Loading
Loading
Loading

CMakeLists.txt

0 → 100644
+9 −0
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.31)
project(hw2_config)

set(CMAKE_CXX_STANDARD 17)

add_executable(exec
        main.cpp
        impl/Enums.cpp
        impl/Rubk.cpp)
+2 −92
Original line number Diff line number Diff line
# HomeworkTwo
# Homework Two Configurations



## Getting started

To make it easy for you to get started with GitLab, here's a list of recommended next steps.

Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!

## Add your files

* [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
* [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:

```
cd existing_repo
git remote add origin https://agile.bu.edu/gitlab/configs/ec330/homeworks/homeworktwo.git
git branch -M master
git push -uf origin master
```

## Integrate with your tools

* [Set up project integrations](https://agile.bu.edu/gitlab/configs/ec330/homeworks/homeworktwo/-/settings/integrations)

## Collaborate with your team

* [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
* [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
* [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
* [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
* [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)

## Test and Deploy

Use the built-in continuous integration in GitLab.

* [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
* [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
* [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
* [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
* [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)

***

# Editing this README

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.

## Suggestions for a good README

Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.

## Name
Choose a self-explaining name for your project.

## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.

## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.

## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.

## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.

## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.

## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.

## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.

## Contributing
State if you are open to contributions and what your requirements are for accepting them.

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.

## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.

## License
For open source projects, say how it is licensed.

## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
Configuration files for homework two.
 No newline at end of file

impl/Enums.cpp

0 → 100644
+35 −0
Original line number Diff line number Diff line
//
// Created by Ari on 2/9/26.
//

#include "../include/Enums.h"

#include <ostream>
using namespace std;

std::ostream& operator<<(ostream& os, const Color clr) {
  return os << ColorToChar(clr);
}

char ColorToChar(Color clr) {
  return ColorToCharArray[static_cast<size_t>(clr)];
}

Color CharToColor(const char ch) {
  switch (std::toupper(static_cast<unsigned char>(ch))) {
    case 'W':
      return Color::WHITE;
    case 'Y':
      return Color::YELLOW;
    case 'R':
      return Color::RED;
    case 'O':
      return Color::ORANGE;
    case 'B':
      return Color::BLUE;
    case 'G':
      return Color::GREEN;
    default:
      throw std::invalid_argument("invalid color character");
  }
}
 No newline at end of file

impl/Rubk.cpp

0 → 100644
+273 −0
Original line number Diff line number Diff line
//
// Created by Ari on 2/4/17.
//

#include "../include/Rubk.h"

#include <iostream>
#include <stdexcept>

#include "../include/Enums.h"

// HELPER FUNCTIONS
/**
 * @param line The line to examine
 * @return true iff line consists only of space-like characters
 */
bool isBlank(string line) {
  return all_of(line.begin(),
                line.end(), // keep going until a blank line or eof
                [](unsigned char ch) { return std::isspace(ch); });
}

void faceHelper(Rubk &result, istringstream &fps, FaceName theFace,
                string line) {
  int col = 0, row = 0;
  bool lineOk;

  do {
    // look for a non-blank line
    while (isBlank(line))
      getline(fps, line);

    for (auto ch : line) {
      if (!std::isspace(ch)) {
        result.setCellColor(theFace, Face::Row(row), Face::Column(col),
                            CharToColor(ch));
        col++;
      }
    }
    row++;
    col = 0; // reset for next line
    lineOk = static_cast<bool>(getline(fps, line));
  } while (lineOk && row < result.getLen());
}

/**
 * Cycles through four positions for a single index (based on ChatGPT
 * optimization).
 */
void cycle4(const Rubk &src, Rubk &dst, FaceName f1, Face::Row r1,
            Face::Column c1, FaceName f2, Face::Row r2, Face::Column c2,
            FaceName f3, Face::Row r3, Face::Column c3, FaceName f4,
            Face::Row r4, Face::Column c4) {
  Color tmp = src.getCellColor(f1, r1, c1);

  dst.setCellColor(f1, r1, c1, src.getCellColor(f2, r2, c2));
  dst.setCellColor(f2, r2, c2, src.getCellColor(f3, r3, c3));
  dst.setCellColor(f3, r3, c3, src.getCellColor(f4, r4, c4));
  dst.setCellColor(f4, r4, c4, tmp);
}

/**
 * Concatenates 4 faces to the output stream.
 * Output is undefined if the faces do not have the same dimensions.
 * @param os The stream onto which the faces are output.
 */
void concatFaces(std::ostream &os, const Face &face1, const Face &face2,
                 const Face &face3, const Face &face4) {
  for (int xx = 0; xx < face1.faceLen(); xx++) {
    Face::Row xxRow(xx);
    os << face1.printRow(xxRow) << " " << face2.printRow(xxRow) << " "
       << face3.printRow(xxRow) << " " << face4.printRow(xxRow) << " " << endl;
  }
}

std::ostream &operator<<(std::ostream &os, const Rubk &cube) {
  return os << cube.printFlattened();
}

// RUBK IMPLEMENTATIONS
Rubk::Rubk(int len) : cubeLen(len) {
  _faces.emplace(FaceName::TOP, Face(Color::RED, cubeLen));
  _faces.emplace(FaceName::LEFT, Face(Color::BLUE, cubeLen));
  _faces.emplace(FaceName::FRONT, Face(Color::WHITE, cubeLen));
  _faces.emplace(FaceName::RIGHT, Face(Color::RED, cubeLen));
  _faces.emplace(FaceName::AFT, Face(Color::BLUE, cubeLen));
  _faces.emplace(FaceName::BOTTOM, Face(Color::WHITE, cubeLen));
}

Rubk::Rubk(Rubk const &other) {
  cubeLen = other.cubeLen;
  _faces = other._faces;
}

Rubk::Rubk(const string &flatProfile) {
  std::istringstream fps(flatProfile); // a stream version of the flatProfile

  // get cube dimensions and setup faces
  string line;
  // dimension length is the number of non-space characters in the non-blank
  // first line
  while (isBlank(line)) {
    getline(fps, line);
  }
  cubeLen =
      static_cast<int>(count_if(line.begin(), line.end(), [](unsigned char ch) {
        return !std::isspace(ch);
      }));
  _faces.emplace(FaceName::TOP, Face(Color::RED, cubeLen));
  _faces.emplace(FaceName::LEFT, Face(Color::BLUE, cubeLen));
  _faces.emplace(FaceName::FRONT, Face(Color::WHITE, cubeLen));
  _faces.emplace(FaceName::RIGHT, Face(Color::RED, cubeLen));
  _faces.emplace(FaceName::AFT, Face(Color::BLUE, cubeLen));
  _faces.emplace(FaceName::BOTTOM, Face(Color::WHITE, cubeLen));

  // read the top tranche
  faceHelper(*this, fps, FaceName::TOP, line);

  // read the middle tranche
  getline(fps, line);
  int col = 0, row = 0;
  bool lineOk;
  do {
    // look for a non-blank line
    while (isBlank(line))
      getline(fps, line);

    // read the characters of the line
    auto theFace = FaceName::LEFT;
    for (auto ch : line) {
      if (!std::isspace(ch)) {
        setCellColor(theFace, Face::Row(row), Face::Column(col),
                     CharToColor(ch));
        if (++col == cubeLen) {
          col = 0;
          switch (theFace) {
          case FaceName::LEFT:
            theFace = FaceName::FRONT;
            break;
          case FaceName::FRONT:
            theFace = FaceName::RIGHT;
            break;
          case FaceName::RIGHT:
            theFace = FaceName::AFT;
            break;
          default:
            break;
          }
        }
      }
    }
    row++;
    col = 0; // reset for next line
    lineOk = static_cast<bool>(getline(fps, line));
  } while (lineOk && row < cubeLen);

  // read the bottom tranche
  getline(fps, line);
  faceHelper(*this, fps, FaceName::BOTTOM, line);
}

bool Rubk::isUnscrambled() const {
  throw logic_error("Rubk::isUnscrambled() has not yet been implemented");
}

Rubk Rubk::rotate(bool direction, FaceName theFaceName) const {
  Rubk result(*this);
  for (int xx = 0; xx < cubeLen; xx++)
    for (int yy = 0; yy < cubeLen; yy++)
      result.setCellColor(
          theFaceName, Face::Row(direction ? xx : cubeLen - 1 - xx),
          Face::Column(direction ? cubeLen - 1 - yy : yy),
          getCellColor(theFaceName, Face::Row(yy), Face::Column(xx)));
  return result;
}

Rubk Rubk::makeMove(CubeMoves theMove) const {
  constexpr Face::Column leftCol(0);
  constexpr Face::Row topRow(0);
  const Face::Column rightCol(cubeLen - 1);
  const Face::Row bottomRow(cubeLen - 1);

  Rubk result(*this);
  switch (theMove) {
  case LeftDown: { // front left column goes down
    for (int yy = 0; yy < cubeLen; ++yy) {
      Face::Row r(yy);
      Face::Row rr(cubeLen - 1 - yy);

      cycle4(*this, result, FaceName::FRONT, r, leftCol, FaceName::TOP, r,
             leftCol, FaceName::AFT, rr, rightCol, // reversed
             FaceName::BOTTOM, r, leftCol);
    }

    result = result.rotate(true, FaceName::LEFT);
    break;
  }

  case RightDown: { // front right column goes down
    for (int yy = 0; yy < cubeLen; ++yy) {
      const Face::Row r(yy);
      const Face::Row rr(cubeLen - 1 - yy);

      cycle4(*this, result, FaceName::FRONT, r, rightCol, FaceName::TOP, r,
             rightCol, FaceName::AFT, rr, leftCol, // back reversed
             FaceName::BOTTOM, r, rightCol);
    }
    result = result.rotate(false, FaceName::RIGHT);
    break;
  }

  case TopLeft: { // top row goes left
    for (int yy = 0; yy < cubeLen; ++yy) {
      Face::Column c(yy);

      cycle4(*this, result, FaceName::FRONT, topRow, c, FaceName::RIGHT, topRow,
             c, FaceName::AFT, topRow, c, FaceName::LEFT, topRow, c);
    }

    result = result.rotate(true, FaceName::TOP);
    break;
  }

  case BottomLeft: { // bottom row goes left
    for (int yy = 0; yy < cubeLen; ++yy) {
      const Face::Column c(yy);

      cycle4(*this, result, FaceName::FRONT, bottomRow, c, FaceName::RIGHT,
             bottomRow, c, FaceName::AFT, bottomRow, c, FaceName::LEFT,
             bottomRow, c);
    }
    result = result.rotate(false, FaceName::BOTTOM);
    break;
  }

  default:
    throw out_of_range("ERR:  I don't know how to do that yet ... sorry!");
  }
  return result;
}

Color Rubk::getCellColor(FaceName theFaceName, Face::Row theRow,
                         Face::Column theColumn) const {
  return getFace(theFaceName).get(theRow, theColumn);
}

void Rubk::setCellColor(FaceName theFaceName, Face::Row theRow,
                        Face::Column theColumn, Color newColor) {
  getFace(theFaceName).set(theRow, theColumn, newColor);
}

string Rubk::printFlattened() const {
  stringstream os; // the output stream

  Face BLANK_FACE(Color::BLANK_, cubeLen); // a blank face

  // output the cube
  // ... row 0
  concatFaces(os, BLANK_FACE, getFace(FaceName::TOP), BLANK_FACE, BLANK_FACE);
  os << endl;

  // ... row 1
  concatFaces(os, getFace(FaceName::LEFT), getFace(FaceName::FRONT),
              getFace(FaceName::RIGHT), getFace(FaceName::AFT));
  os << endl;

  // ... row 2
  concatFaces(os, BLANK_FACE, getFace(FaceName::BOTTOM), BLANK_FACE,
              BLANK_FACE);
  os << endl;

  return os.str();
}
 No newline at end of file

main.cpp

0 → 100644
+35 −0
Original line number Diff line number Diff line
#include <iostream>
#include "include/Rubk.h"

/**
 * A sample use of the {@link Rubk} methods.
 */
int main() {
  const std::string cubeProfile = R"(
       W W W
       W W W
       W W W

O O O  G G G  R R R  B B B
O O O  G G G  R R R  B B B
O O O  G G G  R R R  B B B

       Y Y Y
       Y Y Y
       Y Y Y
)";

  Rubk test(cubeProfile);           // create a cube
  cout << "Original cube:" << endl << test << endl;
  test=test.makeMove(LeftDown);     // make a move
  cout << "Moved LeftDown:" << endl << test << endl;
  test=test.makeMove(RightDown);    // make another move
  cout << "Moved RightDown:" << endl << test << endl;
  test=test.makeMove(TopLeft);      // make yet another move
  cout << "Moved TopLeft:" << endl << test << endl;
  test=test.makeMove(BottomLeft);   // make a final move
  cout << "Moved BottomLeft:" << endl << test << endl;

  cout << test << "is " << (test.isUnscrambled()?"unscrambled":"scrambled") << endl;         // print out the result
  return 0;
}
 No newline at end of file