Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey I need help with my code, the code I need help with making is the display solutions, but before doing that you will have

Hey I need help with my code, the code I need help with making is the display solutions, but before doing that you will have to make the jumble board(making jumble board function before the display function makes it easier to make the display function so pls try and make the jumble board function) the instructions to make the code are in the picture below, and use try_move function to make the jumble board function,

here are instructions for display_solutions:class Scrambler needs a member function display_solution, which accepts no arguments and re- turns a string showing the solution to the game. This is the solution from the start, not from the current state of the board, by basically reversing theJumble-Board algorithm. Here is my code which is correct and works:#include #include #include #include #include #include #include #include // include for rand() function #include #include // std::cout #include // include for time() function

using namespace std;

class Scrambler{ public: Scrambler(const string& path, const int size); string str() const; void scramblerinit(); void try_move(const string& cmd); bool is_over() const; string display_solution() const; vector get_words() const;

private: vector> board; int size; int boardSize; vector words; string path; };

Scrambler :: Scrambler(const string& path, const int size){ this->size = size; this->path = path; ifstream file; file.open(path); for(string lines; getline(file,lines);){ words.push_back(lines); } for(int j = 0; j < size; j++){ vector rows; for(int i = 0; i < size; i++){ rows.push_back(' '); } board.push_back(rows); } scramblerinit(); }

void Scrambler:: scramblerinit(){ for(int j = 0; j < size; j++){ int spaceRemain = board.at(j).size(); int space; int i = 0; while(spaceRemain > 2){ int randNum = rand() % words.size(); string wordRand = words[randNum]; space = spaceRemain - wordRand.size(); if(wordRand.size() <= spaceRemain && space != 3){ for(char character: wordRand){ board.at(j).at(i) = character; i++; spaceRemain--; } i++; spaceRemain--; } } } }

string Scrambler:: str()const{ string rboard; string seperate; int count; count = 1;

for(int j = 0; j <= size; j++){ if(j == 1){ rboard = " "; } rboard += " " + to_string(j); } rboard += " "; for(int i = 0; i<4 * size; i++){ seperate += "-"; } for(vector rows: board){ rboard += " " + seperate + " "; rboard += to_string(count) + " | "; int a; string strchar; for(a = 0; a < rows.size() - 1; a ++){ strchar = rows.at(a); rboard += strchar + " | "; } strchar = rows.at(a); rboard += strchar + " |"; rboard += " "; count ++; } rboard += " " + seperate; return rboard; }

void Scrambler::try_move(const string& cmd) { if(cmd[0]=='r'){ int r = cmd[1]-'0'-1; int dir = cmd[2]=='l'?1:-1; if(dir==1){ int temp = board[r][0]; for(int c=0; c=1; c--) board[r][c] = board[r][c+dir]; board[r][0] = temp; } }else{ int c = cmd[1]-'0'-1; int dir = cmd[2]=='u'?1:-1; if(dir==1){ int temp = board[0][c]; for(int r=0; r=1; r--) board[r][c] = board[r+dir][c]; board[0][c] = temp; } }

} vector Scrambler::get_words() const { vector boardwords; string currentwords; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (isalpha(board.at(i).at(j))) { currentwords += board.at(i).at(j); } else if (!currentwords.empty()) { boardwords.push_back(currentwords); currentwords.clear(); } } if (!currentwords.empty()) { boardwords.push_back(currentwords); currentwords.clear(); } } return boardwords; }

bool Scrambler::is_over() const { return false; }

string Scrambler::display_solution() const { return "1+1"; }

int main() { Scrambler scrambler("dictionary(1).txt", 7); cout << "Initial board: " << scrambler.str() << endl;

scrambler.try_move("c2d"); cout << "Board after c2d move: " << scrambler.str() << endl;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Evaluate nPr . 1. 5P2 2. 6P6 3. 12P2 4. 6P5

Answered: 1 week ago