1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* main.cpp v 1.0
* 2x2x2 Cube Solver console interface (c) 2005 by Eric Dietz
* notes: readme.txt email: root@wrongway.org
*/
// includes
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
// namespace
using namespace std;
// local includes
#include "cu2.h"
#include "config.h"
// shared variables
const char *version = "1.0";
const int N = cu2::N;
cu2 thecube;
bool randm = false;
bool verbal = false;
int inmode = 0;
char coltochar[7];
// declarations
void myrenderscreen(cu2 c);
int chartocol(char c);
void pressenter();
void sendsolution();
int readcommandline(string cmdln);
// display cube on screen
void myrenderscreen(cu2 c)
{
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) printf(" ");
printf(" ");
for (int j = 1; j <= N; j++)
printf(" %c", coltochar[c.cube[j][N+1][N+1-i]]);
printf("\n");
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++)
printf(" %c", coltochar[c.cube[0][N+1-i][N+1-j]]);
printf(" ");
for (int j = 1; j <= N; j++)
printf(" %c", coltochar[c.cube[j][N+1-i][0]]);
printf(" ");
for (int j = 1; j <= N; j++)
printf(" %c", coltochar[c.cube[N+1][N+1-i][j]]);
printf(" ");
for (int j = 1; j <= N; j++)
printf(" %c", coltochar[c.cube[N+1-j][N+1-i][N+1]]);
printf("\n");
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) printf(" ");
printf(" ");
for (int j = 1; j <= N; j++)
printf(" %c", coltochar[c.cube[j][0][i]]);
printf("\n");
}
}
// return the number associated with a given char
int chartocol(char c)
{
for (int i = 1; i <= 6; i++)
if (coltochar[i] == c)
return i;
return 0;
}
// make user press enter to continue
void pressenter()
{
printf("*** press enter to continue ***");
fflush(stdout);
fgetc(stdin);
}
// display the solution on the screen
void sendsolution()
{
string cmd = "", txt;
int lin = 0;
for (int i = 1; i <= N * N; i++)
cmd += " ";
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
cmd.at(((i-1)*N+(j-1)) ) = coltochar[thecube.cube[j][N+1][N+1-i] ];
cmd.at(((i-1)*N+(j-1))+N*N ) = coltochar[thecube.cube[0][N+1-i][N+1-j] ];
cmd.at(((i-1)*N+(j-1))+N*N*2) = coltochar[thecube.cube[j][N+1-i][0] ];
cmd.at(((i-1)*N+(j-1))+N*N*3) = coltochar[thecube.cube[N+1][N+1-i][j] ];
cmd.at(((i-1)*N+(j-1))+N*N*4) = coltochar[thecube.cube[N+1-j][N+1-i][N+1]];
cmd.at(((i-1)*N+(j-1))+N*N*5) = coltochar[thecube.cube[j][0][i] ];
}
}
if (inmode == 1)
{
cmd.insert(N*N*5," D:");
cmd.insert(N*N*4," B:");
cmd.insert(N*N*3," R:");
cmd.insert(N*N*2," F:");
cmd.insert(N*N ," L:");
cmd.insert(0 , "U:");
}
printf("200 cube solved ok.\n");
printf("101 version %s (%s)\n", version, cu2::version);
printf("202 %i moves %i groups", thecube.mov[0], thecube.MOV);
for (int i = 1; i <= thecube.MOV; i++)
printf(" %i", thecube.mov[i]);
printf("\n");
printf("220 starting diagram:\n");
//thecube.renderscreen();
myrenderscreen(thecube);
printf("221 diagram end.\n");
printf("210 sending solution:\n");
if (verbal && thecube.mov[0] > 0)
pressenter();
for (int i = 1; i <= thecube.mov[0]; i++)
{
if (verbal)
{
txt = "";
switch (thecube.solution.at(i*3-3))
{
case 'U': txt += "top"; break;
case 'D': txt += "bottom"; break;
case 'L': txt += "left"; break;
case 'R': txt += "right"; break;
case 'F': txt += "front"; break;
case 'B': txt += "back"; break;
}
txt += " ";
switch (thecube.solution.at(i*3-2))
{
case 'L': txt += "left"; break;
case 'R': txt += "right"; break;
case 'U': txt += "up"; break;
case 'D': txt += "down"; break;
case 'C': txt += "clockwise"; break;
case 'A': txt += "anticlockwise"; break;
}
printf("%s\n", txt.c_str());
lin++;
if (lin >= 24)
{
lin = 0;
pressenter();
}
}
else
{
printf("%c%c, ", thecube.solution.at(i*3-3), thecube.solution.at(i*3-2));
}
}
if (!verbal)
printf("\n");
if (verbal && lin > 0)
pressenter();
printf("211 completed solution.\n");
thecube.dosolution();
#ifdef SHOWENDINGDIAGRAM
printf("220 ending diagram:\n");
//thecube.renderscreen();
myrenderscreen(thecube);
printf("221 diagram end.\n");
#endif // SHOWENDINGDIAGRAM
if (randm)
printf("203 command-line: cu2 %s\n", cmd.c_str());
printf("201 terminating successfully.\n");
}
// take the command-line and make a cube of it
int readcommandline(string cmdln)
{
string cmd = cmdln;
int numused = 0, isused;
#ifdef SENDVERBALSOLUTION
verbal = true;
#endif // SENDVERBALSOLUTION
thecube.resetcube();
for (int i = 1; i <= 6; i++)
coltochar[i] = '\0';
if
(cmd == "random"
// || cmd == "" // remark on completion
)
{
// 1,2,3,4,5,6 or W,R,B,O,G,Y or something else?
/*
for (int i = 1; i <= 6; i++)
coltochar[i] = i + '0';
*/
coltochar[1] = 'W'; coltochar[2] = 'R'; coltochar[3] = 'B';
coltochar[4] = 'O'; coltochar[5] = 'G'; coltochar[6] = 'Y';
thecube.scramblecube();
randm = true;
inmode = 1;
return 0;
}
if ((int)cmd.find(" ") >= 0)
{
// NEW routine to deal with optional "U: D: L: R: F: B: " command lines
int u,d,l,r,f,b,max=0,min=0;
u = cmd.find("U:"); if (u < 0) u = cmd.find("u:");
d = cmd.find("D:"); if (d < 0) d = cmd.find("d:");
l = cmd.find("L:"); if (l < 0) l = cmd.find("l:");
r = cmd.find("R:"); if (r < 0) r = cmd.find("r:");
f = cmd.find("F:"); if (f < 0) f = cmd.find("f:");
b = cmd.find("B:"); if (b < 0) b = cmd.find("b:");
if (u < min) min = u; if (u > max) max = u;
if (d < min) min = d; if (d > max) max = d;
if (l < min) min = l; if (l > max) max = l;
if (r < min) min = r; if (r > max) max = r;
if (f < min) min = f; if (f > max) max = f;
if (b < min) min = b; if (b > max) max = b;
if (min < 0 || max > (int)cmd.length() - N * N - 2)
return 1;
cmd = cmdln.substr(u+2,N*N);
cmd += cmdln.substr(l+2,N*N);
cmd += cmdln.substr(f+2,N*N);
cmd += cmdln.substr(r+2,N*N);
cmd += cmdln.substr(b+2,N*N);
cmd += cmdln.substr(d+2,N*N);
}
if (cmd.length() < N * N * 6)
return 1;
// NEW routine to allow ANY 6 chars to suffice for the cube!!=)
for (int i = 0; i < N * N * 6; i++)
{
isused = 0;
for (int j = 1; j <= 6; j++)
if (coltochar[j] == cmd.at(i))
isused = 1;
if (!isused)
{
numused++;
if (numused > 6)
return 1;
coltochar[numused] = cmd.at(i);
}
}
if (numused < 6)
return 1;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
thecube.cube[j][N+1][N+1-i] = chartocol(cmd.at(((i-1)*N+(j-1)) ));
thecube.cube[0][N+1-i][N+1-j] = chartocol(cmd.at(((i-1)*N+(j-1))+N*N ));
thecube.cube[j][N+1-i][0] = chartocol(cmd.at(((i-1)*N+(j-1))+N*N*2));
thecube.cube[N+1][N+1-i][j] = chartocol(cmd.at(((i-1)*N+(j-1))+N*N*3));
thecube.cube[N+1-j][N+1-i][N+1] = chartocol(cmd.at(((i-1)*N+(j-1))+N*N*4));
thecube.cube[j][0][i] = chartocol(cmd.at(((i-1)*N+(j-1))+N*N*5));
}
}
return 0;
}
// entry point
int main(int argc, char *argv[])
{
string cmdln = "";
if (argc >= 7)
{
inmode = 1;
cmdln = argv[1];
for (int i = 2; i <= 6; i++)
{
cmdln += " ";
cmdln += argv[i];
}
}
else if (argc >= 2)
{
inmode = 0;
cmdln = argv[1];
}
srand(time(NULL)); // UNremark upon completion
int in = 0, out = 0, retcode = 0;
in = readcommandline(cmdln);
if (in == 0)
{
//thecube.shorten = cu2::SHORTEN_STRIP_ALL; // remark on completion
out = thecube.solvecube();
if (out == 0)
{
sendsolution();
}
else
{
printf("500 ERROR: solver failed for the following reason:\n");
if (out == cu2::ERR_NOTINITED)
printf("511 ERROR: mysterious not-inited error - something is fishy.\n");
else if (out == cu2::ERR_MISPAINTED)
printf("512 ERROR: cubelet error - incorrect cubelets - cube mispainted.\n");
else if (out == cu2::ERR_NONDESCRIPT)
printf("513 ERROR: nondescript error from findsolution() - cube misassembled.\n");
else if (out == cu2::ERR_PARITY_CORNER_ROTATION)
printf("514 ERROR: parity error - corner rotation - cube misassembled.\n");
else if (out == cu2::ERR_PARITY_CORNER_BACKWARD)
printf("515 ERROR: parity error - backward corner - cube mispainted.\n");
else
printf("502 ERROR: unexpected error - solvecube() returned %i.\n", out);
printf("101 version %s (%s)\n", version, cu2::version);
printf("220 your diagram:\n");
//thecube.renderscreen();
myrenderscreen(thecube);
printf("221 diagram end.\n");
printf("501 terminating unsuccessfully.\n");
retcode = 1;
}
}
else
{
printf("500 ERROR: solver failed for the following reason:\n");
printf("510 ERROR: non-protocol input entered.\n");
printf("101 version %s (%s)\n", version, cu2::version);
printf(
"\
400 syntax:\n\
cu2 <cube-layout>\n\
cu2 random\n\
example command-lines:\n\
cu2 U:1111 L:3322 F:4433 R:5544 B:2255 D:6666\n\
cu2 WWWWOOBBGGOORRGGBBRRYYYY\n\
cu2 random\n\
notes:\n\
- hint: try using the random parameter to get a feel for how to input cubes.\n\
- read readme.txt (included with this distribution) for protocol details.\n\
- use a terminal with scrollback feature to use this program! (or pipe it)\n\
cu2 by Eric Dietz - root@wrongway.org\n\
401 end of notes.\n\
"
);
#ifdef SHOWBADCOMMANDLINE
if (!(cmdln == "" || cmdln == "help" ))
{
printf("unrecognized command-line input:\n");
if (cmdln.length() <= N * N * 6 * 2)
printf("%s\n", cmdln.c_str());
}
#endif // SHOWBADCOMMANDLINE
printf("501 terminating unsuccessfully.\n");
retcode = -1;
}
#ifdef WAITFORENTERTOEND
printf("press enter to continue...\n");
fgetc(stdin);
#endif // WAITFORENTERTOEND
return retcode;
}
//
|