Created
October 23, 2015 17:47
-
-
Save shakked/3dbc83071f1f72348515 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// Programming Assingment 4 - Water Jugs | |
// | |
// Created by Zachary Shakked on 10/21/15. | |
// Copyright © 2015 Zachary Shakked. All rights reserved. | |
// | |
#include <iostream> | |
#include <vector> | |
#include <set> | |
#include <algorithm> | |
using namespace std; | |
class WaterJug; | |
set<WaterJug> memo; | |
class WaterJug { | |
public: | |
int capA, capB, capC; | |
int goalA, goalB, goalC; | |
int currA, currB, currC; | |
void set_values(int capA, int capB, int capC, int goalA, int goalB, int goalC, int currA, int currB, int currC) { | |
this->capA = capA; | |
this->capB = capB; | |
this->capC = capC; | |
this->goalA = goalA; | |
this->goalB = goalB; | |
this->goalC = goalC; | |
this->currA = currA; | |
this->currB = currB; | |
this->currC = currC; | |
this->currentHistory = currentHistory; | |
} | |
string currentHistory; | |
string description() { | |
return "[" + to_string(currA) + "/" + to_string(capA) + ", " + to_string(currB) + "/" + to_string(capB) + ", " + to_string(currC) + "/" + to_string(capC) + "]"; | |
} | |
string goal() { | |
return "[" + to_string(goalA) + "/" + to_string(capA) + ", " + to_string(goalB) + "/" + to_string(capB) + ", " + to_string(goalC) + "/" + to_string(capC) + "]"; | |
} | |
void advance() { | |
vector<WaterJug> newJugs; | |
newJugs.push_back(c_to_a()); | |
newJugs.push_back(b_to_a()); | |
newJugs.push_back(c_to_b()); | |
newJugs.push_back(a_to_b()); | |
newJugs.push_back(b_to_c()); | |
newJugs.push_back(a_to_c()); | |
} | |
bool operator<(WaterJug o) { | |
//I dont know what to do | |
} | |
string hashValue() { | |
return to_string(currA) + to_string(currB) + to_string(currC); | |
} | |
void advanceHelper(vector<WaterJug> jugs) { | |
if (jugs.size() == 0) { | |
cout << "No solution." << endl; | |
} else { | |
vector<WaterJug> futureJugs; | |
for(std::vector<WaterJug>::iterator it = jugs.begin(); it != jugs.end(); ++it) { | |
if (it->is_complete()) { | |
cout << "FINALLY" << endl; | |
cout << "Current History:" << endl << endl; | |
cout << it->currentHistory << endl; | |
return; | |
} else { | |
vector<WaterJug> newJugs; | |
newJugs.push_back(it->c_to_a()); | |
newJugs.push_back(it->b_to_a()); | |
newJugs.push_back(it->c_to_b()); | |
newJugs.push_back(it->a_to_b()); | |
newJugs.push_back(it->b_to_c()); | |
newJugs.push_back(it->a_to_c()); | |
for(std::vector<WaterJug>::iterator newJug = newJugs.begin(); newJug != newJugs.end(); ++newJug) { | |
bool is_in = memo.find(*newJug) != memo.end(); | |
if (!is_in) { | |
memo.insert(*newJug); | |
futureJugs.push_back(*newJug); | |
} | |
} | |
} | |
} | |
} | |
} | |
bool is_complete() { | |
return currA == goalA && currB == goalB && currC == goalC; | |
} | |
WaterJug c_to_a() { | |
cout << description() + " C -> A"<< endl; | |
WaterJug newJug; | |
int currC = this->currC; | |
int currA = this->currA; | |
while (capA != currA && currC != 0) { | |
currA += 1; | |
currC -= 1; | |
} | |
WaterJug jugAfterTransfer; | |
jugAfterTransfer.set_values(capA, capB, capC, goalA, goalB, goalC, currA, currB, currC); | |
jugAfterTransfer.currentHistory = this->currentHistory + "\n" + this->description() + " C -> A"; | |
cout << jugAfterTransfer.description() << endl << endl; | |
return jugAfterTransfer; | |
} | |
WaterJug b_to_a() { | |
cout << description() + " B -> A"<< endl; | |
WaterJug newJug; | |
int currB = this->currB; | |
int currA = this->currA; | |
while (capA != currA && currB != 0) { | |
currA += 1; | |
currB -= 1; | |
} | |
WaterJug jugAfterTransfer; | |
jugAfterTransfer.set_values(capA, capB, capC, goalA, goalB, goalC, currA, currB, currC); | |
jugAfterTransfer.currentHistory = this->currentHistory + "\n" + this->description() + " B -> A"; | |
cout << jugAfterTransfer.description() << endl << endl; | |
return jugAfterTransfer; | |
} | |
WaterJug c_to_b() { | |
cout << description() + " C -> B"<< endl; | |
WaterJug newJug; | |
int currC = this->currC; | |
int currB = this->currB; | |
while (capB != currB && currC != 0) { | |
currB += 1; | |
currC -= 1; | |
} | |
WaterJug jugAfterTransfer; | |
jugAfterTransfer.set_values(capA, capB, capC, goalA, goalB, goalC, currA, currB, currC); | |
jugAfterTransfer.currentHistory = this->currentHistory + "\n" + this->description() + " C -> B"; | |
cout << jugAfterTransfer.description() << endl << endl; | |
return jugAfterTransfer; | |
} | |
WaterJug a_to_b() { | |
cout << description() + " A -> B"<< endl; | |
WaterJug newJug; | |
int currA = this->currA; | |
int currB = this->currB; | |
while (capB != currB && currA != 0) { | |
currB += 1; | |
currA -= 1; | |
} | |
WaterJug jugAfterTransfer; | |
jugAfterTransfer.set_values(capA, capB, capC, goalA, goalB, goalC, currA, currB, currC); | |
jugAfterTransfer.currentHistory = this->currentHistory + "\n" + this->description() + " B -> C"; | |
cout << jugAfterTransfer.description() << endl << endl; | |
return jugAfterTransfer; | |
} | |
WaterJug b_to_c() { | |
cout << description() + " B -> C"<< endl; | |
WaterJug newJug; | |
int currB = this->currB; | |
int currC = this->currC; | |
while (capC != currC && currB != 0) { | |
currC += 1; | |
currB -= 1; | |
} | |
WaterJug jugAfterTransfer; | |
jugAfterTransfer.set_values(capA, capB, capC, goalA, goalB, goalC, currA, currB, currC); | |
jugAfterTransfer.currentHistory = this->currentHistory + "\n" + this->description() + " B -> C"; | |
cout << jugAfterTransfer.description() << endl << endl; | |
return jugAfterTransfer; | |
} | |
WaterJug a_to_c() { | |
cout << description() + " A -> C"<< endl; | |
WaterJug newJug; | |
int currA = this->currA; | |
int currC = this->currC; | |
while (capC != currC && currA != 0) { | |
currC += 1; | |
currA -= 1; | |
} | |
WaterJug jugAfterTransfer; | |
jugAfterTransfer.set_values(capA, capB, capC, goalA, goalB, goalC, currA, currB, currC); | |
jugAfterTransfer.currentHistory = this->currentHistory + "\n" + this->description() + " A -> C"; | |
cout << jugAfterTransfer.description() << endl << endl; | |
return jugAfterTransfer; | |
} | |
}; | |
int main(int argc, const char * argv[]) { | |
WaterJug jug; | |
jug.set_values(3, 5, 8, 0, 2, 6, 0, 0, 8); | |
jug.c_to_a(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment