Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ problem to arrays the following classic game, Towers of Hanoi. Homework #3:

ID: 3851758 • Letter: C

Question

C++ problem to arrays the following classic game, Towers of Hanoi.

Homework #3: Towers of Hanoi Overview In this homework you will m Towers of Hanoi. This is a mathematical The game starts with the disks placed in the leftmost stack. The disks are placed in ascending order, this is, the smallest disk at the top. The goal of this game is to move the entire leftmost stack to the rightmost stack. ake use of arrays and control flow structures to solve the classic game l game that consists of three stacks and a number of disks. You must adhere to the following game rules: Only one disk can be moved at a time. *Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack. Only the uppermost disks can be moved. No disk may be placed on top of a smaller disk. Binary Representation This problem can be solved in a number of ways. We are going to solve it through a simple binary representation. These are the rules for the model: There is one bit for each disk The most significant bit represents the largest disk. The least significant b it represents the smallest disk. .Start reading the bits from the right. The first bit to have a value of 1 is the source disk (or the disk to be moved) The second bit to have a value of 1 is the destination disk If the number of zeroes in between the first and second bit is even (or zero) then move the · source disk on top of the destination disk. If the number of zeroes in between the first and second bit is odd then move the source disk on top of the stack not containing the destination disk. * If there is only one bit with the value of 1 move the disk to any stack you see fit. In the end, all the disks from the leftmost stack must be placed in the rightmost stack. Let's assume we have only three disks (D3, D2, D1), then we would have something like this:

Explanation / Answer

main.cpp

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <iostream>
#include <bitset>
#include <streambuf>
#include <sstream>
#include <numeric>
#include <functional>
#include <string>
#include <stdexcept>
#include <cmath>
#include <algorithm>

using namespace std;

string coutText;                            // -- DO NOT MODIFY --
stringstream coutBuffer;                    // -- DO NOT MODIFY --
streambuf* coutOld;                         // -- DO NOT MODIFY --
streambuf* cinBackup;                       // -- DO NOT MODIFY --

void startRecording();                      // -- DO NOT MODIFY --
void stopRecording();                       // -- DO NOT MODIFY --
void injectInput(istringstream& input);     // -- DO NOT MODIFY --
void clearInput();                          // -- DO NOT MODIFY --

istringstream userInput("");                // -- DO NOT MODIFY --

void studentCode(){
    injectInput(userInput);
    startRecording();
  
    // Student code goes here
  
    stopRecording();
}

// -------------------------------------------
// -- DO NOT MODIFY FROM THIS POINT ONWARDS --
// -------------------------------------------

void injectInput(istringstream& input){
    cinBackup = cin.rdbuf();
    cin.rdbuf(input.rdbuf());
}

void clearInput(){
    cin.rdbuf(cinBackup);
    cin.ignore(INT_MAX);
}

void startRecording(){
    coutOld = std::cout.rdbuf(coutBuffer.rdbuf());
    cout.flush();
    coutText = "";
    coutBuffer.str("");
}

void stopRecording(){
    coutText = coutBuffer.str();
    std::cout.rdbuf(coutOld);
}

TEST_CASE("Test Case 1: Invalid Input"){
    userInput.str("2 ");
    studentCode();
    string test_validOutput = R"(Enter_number_of_disks_(3-10):_Need_at_least_3_disks!)";
    string difference = "";
    std::string::size_type minimumSize = min(coutText.size(), test_validOutput.size());
  
    for(std::string::size_type i = 0; i < minimumSize; ++i) {
        difference.append(std::string(1, coutText[i]));
        difference.append(" vs ");
        difference.append(std::string(1, test_validOutput[i]));
        difference.append(" ");
    }
  
    INFO(" ");
    INFO("Given output characters: " << coutText.size() << " " <<
         "Expected output characters: " << test_validOutput.size() <<
         " Printing given vs. expected: " << difference);
  
    REQUIRE(coutText == test_validOutput);
}

TEST_CASE("Test Case 2: Invalid Input"){
    userInput.str("11 ");
    studentCode();
    string test_validOutput = R"(Enter_number_of_disks_(3-10):_Need_at_most_10_disks!)";
    string difference = "";
    std::string::size_type minimumSize = min(coutText.size(), test_validOutput.size());
  
    for(std::string::size_type i = 0; i < minimumSize; ++i) {
        difference.append(std::string(1, coutText[i]));
        difference.append(" vs ");
        difference.append(std::string(1, test_validOutput[i]));
        difference.append(" ");
    }
  
    INFO(" ");
    INFO("Given output characters: " << coutText.size() << " " <<
         "Expected output characters: " << test_validOutput.size() <<
         " Printing given vs. expected: " << difference);
  
    REQUIRE(coutText == test_validOutput);
}

TEST_CASE("Test Case 3: Three Disks"){
    userInput.str("3 ");
    studentCode();
    string test_validOutput = R"(Enter_number_of_disks_(3-10):_Maximum_number_of_operations:_7

Steps:
Move_'1'_from_Tower_1_to_Tower_3
Move_'11'_from_Tower_1_to_Tower_2
Move_'1'_from_Tower_3_to_Tower_2
Move_'111'_from_Tower_1_to_Tower_3
Move_'1'_from_Tower_2_to_Tower_1
Move_'11'_from_Tower_2_to_Tower_3
Move_'1'_from_Tower_1_to_Tower_3

Tower_1
0
0
0

Tower_2
0
0
0

Tower_3
1
11
111)";
    string difference = "";
    std::string::size_type minimumSize = min(coutText.size(), test_validOutput.size());
  
    for(std::string::size_type i = 0; i < minimumSize; ++i) {
        difference.append(std::string(1, coutText[i]));
        difference.append(" vs ");
        difference.append(std::string(1, test_validOutput[i]));
        difference.append(" ");
    }
  
    INFO(" ");
    INFO("Given output characters: " << coutText.size() << " " <<
         "Expected output characters: " << test_validOutput.size() <<
         " Printing given vs. expected: " << difference);
  
    REQUIRE(coutText == test_validOutput);
}

TEST_CASE("Test Case 4: Four Disks"){
    userInput.str("4 ");
    studentCode();
    string test_validOutput = R"(Enter_number_of_disks_(3-10):_Maximum_number_of_operations:_15

Steps:
Move_'1'_from_Tower_1_to_Tower_2
Move_'11'_from_Tower_1_to_Tower_3
Move_'1'_from_Tower_2_to_Tower_3
Move_'111'_from_Tower_1_to_Tower_2
Move_'1'_from_Tower_3_to_Tower_1
Move_'11'_from_Tower_3_to_Tower_2
Move_'1'_from_Tower_1_to_Tower_2
Move_'1111'_from_Tower_1_to_Tower_3
Move_'1'_from_Tower_2_to_Tower_3
Move_'11'_from_Tower_2_to_Tower_1
Move_'1'_from_Tower_3_to_Tower_1
Move_'111'_from_Tower_2_to_Tower_3
Move_'1'_from_Tower_1_to_Tower_2
Move_'11'_from_Tower_1_to_Tower_3
Move_'1'_from_Tower_2_to_Tower_3

Tower_1
0
0
0
0

Tower_2
0
0
0
0

Tower_3
1
11
111
1111)";
  
    string difference = "";
    std::string::size_type minimumSize = min(coutText.size(), test_validOutput.size());
  
    for(std::string::size_type i = 0; i < minimumSize; ++i) {
        difference.append(std::string(1, coutText[i]));
        difference.append(" vs ");
        difference.append(std::string(1, test_validOutput[i]));
        difference.append(" ");
    }
  
    INFO(" ");
    INFO("Given output characters: " << coutText.size() << " " <<
         "Expected output characters: " << test_validOutput.size() <<
         " Printing given vs. expected: " << difference);
  
    REQUIRE(coutText == test_validOutput);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote