HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

A c++ program to solve a Rubik's Cube

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rubikprogramsolvecube

Problem

I wanted to write a c++ program to solve a 3x3 rubik's cube, but failed at it. So I picked up a 2x2 rubik's cube and wrote a program using Blindfold cubing algorithm. Can anyone help me write a more efficient program ?
This is the program I wrote

```
#include
#include
using namespace std;
class Solver
{
string b = "RD' RU' R'U' RU R'F' RU R'U' R'FR DR' ";
string c = "F RU' R'U' RU R'F' RU R'U' R'FR F' ";
string d = "FU' R'U' RU R'F' RU R'U' R'FR2F' ";
string e = "F'D RU' R'U' RU R'F' RU R'U' R'FR D'F ";
string f = "F2D RU' R'U' RU R'F' RU R'U' R'FR D'F2 ";
string g = "RF RU' R'U' RU R'F' RU R'U' R'FR F'R' ";
string h = "D RU' R'U' RU R'F' RU R'U' R'FR D' ";
string i = "U' R'U' RU R'F' RU R'U' R'FR R ";
string j = "R'U' R'U' RU R'F' RU R'U' R'FR R2 ";
string k = "R2U' R'U' RU R'F' RU R'U' R'F ";
string l = "RU' R'U' RU R'F' RU R'U' R'FR ";
string n = "D2 RF RU' R'U' RU R'F' RU R'U' R'FR F'R' D2";
string o = "D'RU' R'U' RU R'F' RU R'U' R'FR D ";
string p = "R'F RU' R'U' RU R'F' RU R'U' R'FR F'R ";
string r = "F2 RU' R'U' RU R'F' RU R'U' R'FR F2 ";
string s = "DR F RU' R'U' RU R'F' RU R'U' R'FR F' R'D' ";
string t = "D2 RU' R'U' RU R'F' RU R'U' R'FR D2 ";
string u = "F RU' R'U' RU R'F' RU R'U' R'FR F' ";
string v = "D'F RU' R'U' RU R'F' RU R'U' R'FR F'D ";
string w = "D2F RU' R'U' RU R'F' RU R'U' R'FR F'D2 ";
string x = "DF RU' R'U' RU R'F' RU R'U' R'FR F'D' ";
public:
string getSol(char letter)
{
switch(letter)
{
case 'b': return b;
case 'c': return c;
case 'd': return d;
case 'e': return e;
case 'f': return f;
case 'g': return g;
case 'h': return h;
case 'i': return i;
case 'j': return j;
case 'k':

Solution

Here are some things that may help you improve your code.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.

Rethink your object design

Right now, the Solver object simply holds two dozen static strings. It has no intelligence and does not, in any sense, know anything about the cube it purports to solve. A better solver might keep a model of the scrambled cube to track its state.

Don't optimize blindly

First, we should keep in mind what Donald Knuth has said about optimization:


"The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming."

Your particular program probably spends 99% of its time doing I/O, so the performance differences among possible alternatives is probably best approximated as zero. If what you're really after is a better algorithm, then that's a different problem.

Don't use std::endl if '\n' will do

Using std::endl emits a \n and flushes the stream. Unless you really need the stream flushed, you can potentially improve the performance of the code by simply emitting '\n' instead of using the potentially more computationally costly std::endl.

Use string concatenation

The code currently includes these lines:

cout<<"-----------------------------------------------"<<endl;
cout<<"|            Cube Solver 2x2 v1.0             |"<<endl;
cout<<"|            Developed by Pavan P             |"<<endl;
cout<<"-----------------------------------------------"<<endl<<endl;
cout<<"Enter the Sequence in small letters: ";


Each of those is a separate call to operator<< but they don't need to be. Another way to write that would be like this:

std::cout <<
    "-----------------------------------------------\n"
    "|            Cube Solver 2x2 v1.0             |\n"
    "|            Developed by Pavan P             |\n"
    "-----------------------------------------------\n"
    "Enter the Sequence in small letters: ";


This reduces the entire sequence to a single call to operator<< because consecutive strings in C++ (and in C, for that matter) are automatically concatenated into a single string by the compiler.

Omit return 0

When a C or C++ program reaches the end of main the compiler will automatically generate code to return 0, so there is no need to put return 0; explicitly at the end of main.

Note: when I make this suggestion, it's almost invariably followed by one of two kinds of comments: "I didn't know that." or "That's bad advice!" My rationale is that it's safe and useful to rely on compiler behavior explicitly supported by the standard. For C, since C99; see ISO/IEC 9899:1999 section 5.1.2.2.3:


[...] a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

For C++, since the first standard in 1998; see ISO/IEC 14882:1998 section 3.6.1:


If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return; statements at the end of a void function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.

So I advocate omitting it; others disagree (often vehemently!) In any case, if you encounter code that omits it, you'll know that it's explicitly supported by the standard and you'll know what it means.

Code Snippets

cout<<"-----------------------------------------------"<<endl;
cout<<"|            Cube Solver 2x2 v1.0             |"<<endl;
cout<<"|            Developed by Pavan P             |"<<endl;
cout<<"-----------------------------------------------"<<endl<<endl;
cout<<"Enter the Sequence in small letters: ";
std::cout <<
    "-----------------------------------------------\n"
    "|            Cube Solver 2x2 v1.0             |\n"
    "|            Developed by Pavan P             |\n"
    "-----------------------------------------------\n"
    "Enter the Sequence in small letters: ";

Context

StackExchange Code Review Q#133923, answer score: 5

Revisions (0)

No revisions yet.