Rubik's Cube - Mini Project

Hey guys, today I am posting the mini project I did in my 6th semester(B.E.). This code is written in Visual C/C++ which is graphical simulation of the Rubik's Cube.




/*
Rubik's Cube Mini Project 
By: Aakash Jha & Ashok Kumar Shrestha 
*/
#include "stdlib.h"
#include <cmath>
#include<stdio.h>
#include<string.h>
#include "glut.h"

#define UP '1'
#define DOWN '2'
#define LEFT '3'
#define RIGHT '4'
#define FRONT '5'
#define BACK '6'
#define UP2 'q'
#define DOWN2 'w'
#define LEFT2 'e'
#define RIGHT2 'r'
#define FRONT2 't'
#define BACK2 'y'
#define UP3 'a'
#define DOWN3 's'
#define LEFT3 'd'
#define RIGHT3 'f'
#define FRONT3 'g'
#define BACK3 'h'

#define SHUFFLE 'h'
#define CHANGEMOVE 'd'
#define CHANGEMOVE2 'a'
#define MAKEMOVE 's'
#define MAKEMOVE2 'w' 
#define RESTART 'r'
#define TEXTID      3
#define ESCAPE 27


static int begin;

void OnSize(int x, int y);
void OnDraw();
void OnChar(unsigned char k, int x, int y);
void OnDrag(int, int);
void OnClick(int, int, int, int);

int dragstartx, dragstarty;
double theta = .35;
double phi = .25;
double theta0, phi0;
bool Shuffling;
//bool MoveClick=false;
//int mousex,mousey;
int CurrentMove = 0;
int f = 2;

/*
modified
*/

float mod_color[9][3] = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 1.0, 0.0 }, { 0.0, 1.0, 1.0 }, { 1.0, 0.0, 1.0 }, { 1.0, 1.0, 1.0 }, { 0.5, 0.5, 0.0 }, { 0.1, 0.2, 0.3 } };
int count = 0;

class Matrix
{
public:
double l[3][3]; //[row][column]
};

class Vector3D
{
public:
Vector3D() :x(0), y(0), z(0){}
double x, y, z;
void MultiplyBy(Matrix m)
{
double xnew, ynew, znew;
xnew = m.l[0][0] * x + m.l[1][0] * y + m.l[2][0] * z;
ynew = m.l[0][1] * x + m.l[1][1] * y + m.l[2][1] * z;
znew = m.l[0][2] * x + m.l[1][2] * y + m.l[2][2] * z;
x = xnew; y = ynew; z = znew;
}
Vector3D& operator+=(Vector3D p){ x += p.x; y += p.y; z += p.z; return *this; }
};

class Face
{
public:
void Draw()
{
glColor3d(r, g, b);
glBegin(GL_QUADS);
for (int n = 0; n<4; ++n)
glVertex3d(v[n].x, v[n].y, v[n].z);
glEnd();
}
void MultiplyBy(Matrix m){ for (int n = 0; n<4; ++n)v[n].MultiplyBy(m); }
Vector3D Centre()
{
Vector3D ret;
for (int n = 0; n<4; ++n)
ret += v[n];
return ret;
}
double r, g, b;
Vector3D v[4];
};

class Cube
{
public:
void Draw(){
for (int n = 0; n<6; ++n)
{

face[n].Draw();
}//draws 6 faces of one cube 
}



Vector3D Centre()
{
Vector3D ret;
for (int n = 0; n<6; ++n)
ret += face[n].Centre();
return ret;
}
void MultiplyBy(Matrix m){ for (int n = 0; n<6; ++n)face[n].MultiplyBy(m); }
Face face[6];
};

class Rubiks
{
public:
void Reset()
{
for (int n = 0; n<27; ++n)
{
int xi = n % 3 - 1;
int yi = ((n - xi) / 3) % 3 - 1;
int zi = ((n - xi) / 3) / 3 - 1;
double x = xi*1.1;
double y = yi*1.1;
double z = zi*1.1;


cube[n].face[0].r = .8;
cube[n].face[0].v[0].x = x - .5; //Bottom
cube[n].face[0].v[0].y = y - .5;
cube[n].face[0].v[0].z = z - .5;
cube[n].face[0].v[1].x = x + .5;
cube[n].face[0].v[1].y = y - .5;
cube[n].face[0].v[1].z = z - .5;
cube[n].face[0].v[2].x = x + .5;
cube[n].face[0].v[2].y = y + .5;
cube[n].face[0].v[2].z = z - .5;
cube[n].face[0].v[3].x = x - .5;
cube[n].face[0].v[3].y = y + .5;
cube[n].face[0].v[3].z = z - .5;


cube[n].face[1].r = 1;
cube[n].face[1].g = .5;
cube[n].face[1].b = 0;
cube[n].face[1].v[0].x = x - .5; //Top
cube[n].face[1].v[0].y = y - .5;
cube[n].face[1].v[0].z = z + .5;

cube[n].face[1].v[1].x = x + .5;
cube[n].face[1].v[1].y = y - .5;
cube[n].face[1].v[1].z = z + .5;

cube[n].face[1].v[2].x = x + .5;
cube[n].face[1].v[2].y = y + .5;
cube[n].face[1].v[2].z = z + .5;

cube[n].face[1].v[3].x = x - .5;
cube[n].face[1].v[3].y = y + .5;
cube[n].face[1].v[3].z = z + .5;


cube[n].face[2].r = cube[n].face[2].b = cube[n].face[2].g = .9;
cube[n].face[2].v[0].x = x - .5; //Left
cube[n].face[2].v[0].y = y - .5;
cube[n].face[2].v[0].z = z - .5;

cube[n].face[2].v[1].x = x - .5;
cube[n].face[2].v[1].y = y - .5;
cube[n].face[2].v[1].z = z + .5;

cube[n].face[2].v[2].x = x - .5;
cube[n].face[2].v[2].y = y + .5;
cube[n].face[2].v[2].z = z + .5;

cube[n].face[2].v[3].x = x - .5;
cube[n].face[2].v[3].y = y + .5;
cube[n].face[2].v[3].z = z - .5;


cube[n].face[3].r = cube[n].face[3].g = .95;
cube[n].face[3].b = 0;
cube[n].face[3].v[0].x = x + .5; //Right
cube[n].face[3].v[0].y = y - .5;
cube[n].face[3].v[0].z = z - .5;

cube[n].face[3].v[1].x = x + .5;
cube[n].face[3].v[1].y = y - .5;
cube[n].face[3].v[1].z = z + .5;

cube[n].face[3].v[2].x = x + .5;
cube[n].face[3].v[2].y = y + .5;
cube[n].face[3].v[2].z = z + .5;

cube[n].face[3].v[3].x = x + .5;
cube[n].face[3].v[3].y = y + .5;
cube[n].face[3].v[3].z = z - .5;


cube[n].face[4].b = 1;
cube[n].face[4].r = cube[n].face[4].g = 0;
cube[n].face[4].v[0].x = x - .5; //Front
cube[n].face[4].v[0].y = y - .5;
cube[n].face[4].v[0].z = z - .5;

cube[n].face[4].v[1].x = x - .5;
cube[n].face[4].v[1].y = y - .5;
cube[n].face[4].v[1].z = z + .5;

cube[n].face[4].v[2].x = x + .5;
cube[n].face[4].v[2].y = y - .5;
cube[n].face[4].v[2].z = z + .5;

cube[n].face[4].v[3].x = x + .5;
cube[n].face[4].v[3].y = y - .5;
cube[n].face[4].v[3].z = z - .5;


cube[n].face[5].g = .6;
cube[n].face[5].b = cube[n].face[5].r = 0;
cube[n].face[5].v[0].x = x - .5; //Back
cube[n].face[5].v[0].y = y + .5;
cube[n].face[5].v[0].z = z - .5;

cube[n].face[5].v[1].x = x - .5;
cube[n].face[5].v[1].y = y + .5;
cube[n].face[5].v[1].z = z + .5;

cube[n].face[5].v[2].x = x + .5;
cube[n].face[5].v[2].y = y + .5;
cube[n].face[5].v[2].z = z + .5;

cube[n].face[5].v[3].x = x + .5;
cube[n].face[5].v[3].y = y + .5;
cube[n].face[5].v[3].z = z - .5;

for (int f = 0; f<6; ++f)
for (int v = 0; v<4; ++v)
if (fabs(cube[n].face[f].v[v].x) < 1
&& fabs(cube[n].face[f].v[v].y) < 1
&& fabs(cube[n].face[f].v[v].z) < 1)
{
cube[n].face[f].r = 0;
cube[n].face[f].g = 0;
cube[n].face[f].b = 0;
}

/*
int face_int = 0;
if (n<9)
{
/*
0-8 --> buttom layer cube number
9-17 --> middle layer cube number
18-26 --> top layer cube number
red:| 6 3 0| int face_int = 0; if (n<9)
| 7 4 1|
| 8 5 2|
blue:| 18 9  0| int face_int = 4; if (n<3||( n<12&& n>8)|| (n<21& n>17))
| 19 10 1|
| 20 11 2|
white:| 24 15 6| int face_int = 2; if (n%3==0)
| 21 12 3|
| 18  9 0|
orange:| 24 21 18|  int face_int = 1; if (n>17&&n<27)
| 25 22 19|
| 26 23 20|
yellow:| 26 17 8| int face_int = 3;if (n == 2 || n == 5 || n == 8 || n == 11 || n == 14 || n == 17 || n == 20 || n == 23 || n == 26)
| 23 14 5|
| 20 11 2|
green:| 24 15 6| int face_int = 5; if ((n<9&&n>5) || (n<18 && n>14) || (n>23 && n<27))
| 25 16 7|
| 26 17 8|



cube[n].face[face_int].r = mod_color[count % 9][0];
cube[n].face[face_int].g = mod_color[count % 9][1];
cube[n].face[face_int].b = mod_color[count % 9][2];
count++;
if (count >= 9) count = 0;

}
*/
}
}
void Draw()
{
for (int n = 0; n<27; ++n) cube[n].Draw();
}

void Shuffle()
{
Shuffling = true;
for (int n = 0; n<50; ++n)
{
int r = rand() % 6;
char k;
if (r == 0) k = UP;
if (r == 1) k = DOWN;
if (r == 2) k = LEFT;
if (r == 3) k = RIGHT;
if (r == 4) k = FRONT;
if (r == 5) k = BACK;
MakeMove(k, true);
}
Shuffling = false;
}
void MakeMove(char k, bool fast)
{
Matrix m;
for (int x = 0; x<3; ++x)
for (int y = 0; y<3; ++y)
m.l[x][y] = 0;

double ang = fast ? 3.14159265 / 10.0 : 3.14159265 / 50.0;

switch (k)
{
case(LEFT) :
case(LEFT2) :
case(LEFT3) :
m.l[0][0] = 1;
m.l[1][1] = cos(ang);
m.l[1][2] = sin(ang);
m.l[2][1] = -sin(ang);
m.l[2][2] = cos(ang);
break;
case(RIGHT) :
case(RIGHT2) :
case(RIGHT3) :
m.l[0][0] = 1;
m.l[1][1] = cos(ang);
m.l[1][2] = -sin(ang);
m.l[2][1] = sin(ang);
m.l[2][2] = cos(ang);
break;
case(UP) :
case(UP2) :
case(UP3) :
 m.l[2][2] = 1;
m.l[0][0] = cos(ang);
m.l[0][1] = -sin(ang);
m.l[1][0] = sin(ang);
m.l[1][1] = cos(ang);
break;
case(DOWN) :
case(DOWN2) :
case(DOWN3) :
m.l[2][2] = 1;
m.l[0][0] = cos(ang);
m.l[0][1] = sin(ang);
m.l[1][0] = -sin(ang);
m.l[1][1] = cos(ang);
break;
case(FRONT) :
case(FRONT2) :
case(FRONT3) :
m.l[1][1] = 1;
m.l[0][0] = cos(ang);
m.l[0][2] = sin(ang);
m.l[2][0] = -sin(ang);
m.l[2][2] = cos(ang);
break;
case(BACK) :
case(BACK2) :
case(BACK3) :
m.l[1][1] = 1;
m.l[0][0] = cos(ang);
m.l[0][2] = -sin(ang);
m.l[2][0] = sin(ang);
m.l[2][2] = cos(ang);
break;
}

long t0 = glutGet(GLUT_ELAPSED_TIME);
for (int t = 0; t<(fast ? 5 : 25); ++t)
{
for (int n = 0; n<27; ++n)
switch (k)
{
case(LEFT) :
case(RIGHT3) :
if (cube[n].Centre().x<-1) cube[n].MultiplyBy(m);
break;
case(RIGHT) :
case(LEFT3) :
if (cube[n].Centre().x>+1) cube[n].MultiplyBy(m);
break;
case(LEFT2) :
case(RIGHT2) :
if (cube[n].Centre().x>-1 && cube[n].Centre().x<+1) cube[n].MultiplyBy(m);
break;
case(UP) :
case(DOWN3) :
if (cube[n].Centre().z>+1) cube[n].MultiplyBy(m);
break;
case(DOWN) :
case(UP3) :
if (cube[n].Centre().z<-1) cube[n].MultiplyBy(m);
break;
case(UP2) :
case(DOWN2) :
if (cube[n].Centre().z<+1 && cube[n].Centre().z>-1) cube[n].MultiplyBy(m);
break;
case(FRONT) :
case(BACK3) :
if (cube[n].Centre().y<-1) cube[n].MultiplyBy(m);
break;
case(BACK) :
case(FRONT3) :
if (cube[n].Centre().y>+1) cube[n].MultiplyBy(m);
break;
case(FRONT2) :
case(BACK2) :
if (cube[n].Centre().y<+1 && cube[n].Centre().y>-1) cube[n].MultiplyBy(m);
break;
}
OnDraw();
while (glutGet(GLUT_ELAPSED_TIME)<t0 + t * 10);
}
}
Cube cube[27];
} RCube;


void DrawTextXY(double x, double y, double z, double scale, char *s)
{
int i;

glPushMatrix();
glTranslatef(x, y, z);
glScalef(scale, scale, scale);
for (i = 0; i < strlen(s); i++)
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, s[i]);
// glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,s[i]);
glPopMatrix();
}

void menu1()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glClearColor(0, 0, 0, 0.0);

glTranslatef(0.0, 0.0, -6.0);
glTranslatef(0.0, -1.3, 0.0);

glColor3f(1.00, 0.20, 0.10);
glLoadName(TEXTID);

DrawTextXY(-3.5, 3, 0.0, 0.003, "     RUBIK'S CUBE ");

glColor3f(0.6, 0.8, 0.7);
DrawTextXY(-1.1, 2.4, 0.0, 0.0014, "    MENU ");

glColor3f(1.0, 0.8, 0.4);
DrawTextXY(-1.25, 2.1, 0.0, 0.001, "     1 : PROCEED ");
DrawTextXY(-1.25, 1.9, 0.0, 0.001, "     2 : HELP ");
DrawTextXY(-1.25, 1.7, 0.0, 0.001, "     3 : EXIT ");
DrawTextXY(-1.25, 1.5, 0.0, 0.001, "     4 : BACK");
glFlush(); //Finish rendering
glutSwapBuffers();

}

void menu2()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glClearColor(0, 0, 0, 1.0);


glTranslatef(0.0, 0.0, -6.0);
glTranslatef(0.0, -1.3, 0.0);

glColor3f(0.6, 0.8, 0.7);
DrawTextXY(-4.1, 3, 0.0, 0.003, "       HELP SECTION ");
glColor3f(1.0, 0.8, 0.4);
DrawTextXY(-1.75, 2.4, 0.0, 0.0014, "  Keyboard Controls : ");
glColor3f(0.9, 0.8, 0.9);
DrawTextXY(-1.7, 2.1, 0.0, 0.001, "  Shuffle the Cube : h ");
DrawTextXY(-1.7, 1.7, 0.0, 0.001, "  Move Grid Forward : w ");
DrawTextXY(-1.7, 1.5, 0.0, 0.001, "  Move Grid Backward : s ");
DrawTextXY(-1.7, 1.1, 0.0, 0.001, "  Move Face Forward : a ");
DrawTextXY(-1.7, 0.9, 0.0, 0.001, "  Move Face Backward : d ");
DrawTextXY(-2.441, 0.6, 0.0, 0.001, "  Free View : Click any mouse button and Drag");
DrawTextXY(-2.4, 0.4, 0.0, 0.001, "  Return to Main Screen from output : ESCAPE");


glColor3f(0.9, 0.9, 0.8);
DrawTextXY(-1, -0.4, 0.0, 0.001, "  Press any KEY ... ");


glFlush(); //Finish rendering


glutSwapBuffers();

}

void cover()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glClearColor(0, 0, 0, 0.0);

glTranslatef(0.0, 0.0, -6.0);
glTranslatef(0.0, -1.3, 0.0);

glColor3f(1.00, 0.20, 0.10);
glLoadName(TEXTID);

DrawTextXY(-1.7, 3.5, 0.0, 0.001, "   GRAPHICAL IMPLEMENTATION OF  ");

glColor3f(0.6, 0.8, 0.7);
DrawTextXY(-2.34, 3, 0.0, 0.0014, "    RUBIK'S CUBE USING OPENGL ");

/* glColor3f(1.0,0.8,0.4);
DrawTextXY(-1.25,2.1,0.0,0.001,"     1 : PROCEED ");
DrawTextXY(-1.25,1.9,0.0,0.001,"     2 : HELP ");
DrawTextXY(-1.25,1.7,0.0,0.001,"     3 : EXIT ");
*/
glColor3f(0.7, 0.6, 0.1);
DrawTextXY(-0.9, 1.7, 0.0, 0.0007, "     Submitted by : ");

glColor3f(1.0, 0.5, 0.0);
DrawTextXY(-2.4, 1.2, 0.0, 0.001, "     AAKASH JHA ");
DrawTextXY(-0.5, 1.2, 0.0, 0.001, "        ASHOK KUMAR SHRESTHA ");
glColor3f(0.7, 0.8, 0.6);
DrawTextXY(-2.5, 0.95, 0.0, 0.001, "     (1BO11CS001) ");
DrawTextXY(.15, 0.95, 0.0, 0.001, "     (1BO11CS007) ");

glColor3f(0.7, 0.6, 0.1);
DrawTextXY(-1.25, 0, 0.0, 0.0007, "     Under the guidance of : ");
glColor3f(1.0, 0.8, 0.4);
DrawTextXY(-1.3, -.2, 0.0, 0.001, "     MRS PADMAVATHI");
DrawTextXY(-1.23, -.35, 0.0, 0.0007, "     Asst. Prof.,Dept. of CSE ");
DrawTextXY(-1.0, -.55, 0.0, 0.001, "       BrCE");
glColor3f(0.3, 0.3, 0.3);
DrawTextXY(-1, -1, 0.0, 0.0008, "     Press any key to continue... ");

glFlush(); //Finish rendering
glutSwapBuffers();
}

void Dis()
{
if (f == 0)
menu1();
else if (f == 1)
menu2();
else if (f == 2)
cover();
else
{
OnDraw();

}

}


int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000, 1000);
glutCreateWindow("Rubik's Cube");

glutDisplayFunc(Dis);
glutKeyboardFunc(OnChar);
glutReshapeFunc(OnSize);
//glutDisplayFunc(OnDraw);

glutMotionFunc(OnDrag);
glutMouseFunc(OnClick);

RCube.Reset();

glutMainLoop();
return 0;
}

void OnSize(int x, int y)
{

glViewport(0, 0, x, y);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 1, 100);
glMatrixMode(GL_MODELVIEW);

glEnable(GL_DEPTH_TEST);
// glDrawBufffer(GL_BACK);
}


void OnDraw()
{
glClearColor(.75, .75, .75, 0); // background color

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
gluLookAt(7 * sin(phi)*cos(theta), -7 * cos(phi)*cos(theta), 7 * sin(theta), 0, 0, 0, 0, 0, 1);


glPushMatrix();
glLineWidth(3);
glColor3d(0, 0, 0); // color of the arrow
if (CurrentMove<3)
{
glScaled(1.1, 3.6, 3.6);

glTranslated(CurrentMove - 1, 0, 0);

glBegin(GL_LINES);
glVertex3d(0, -.25, .6); //Little pointy arrows
glVertex3d(0, +.25, .6);
glVertex3d(0, -.25, .6);
glVertex3d(.1, -.15, .6);
glVertex3d(0, -.25, .6);
glVertex3d(-.1, -.15, .6);

glVertex3d(0, -.25, -.6);
glVertex3d(0, +.25, -.6);
glVertex3d(0, +.25, -.6);
glVertex3d(.1, +.15, -.6);
glVertex3d(0, +.25, -.6);
glVertex3d(-.1, +.15, -.6);

glVertex3d(0, .6, -.25);
glVertex3d(0, .6, +.25);
glVertex3d(0, .6, +.25);
glVertex3d(.1, .6, +.15);
glVertex3d(0, .6, +.25);
glVertex3d(-.1, .6, +.15);

glVertex3d(0, -.6, -.25);
glVertex3d(0, -.6, +.25);
glVertex3d(0, -.6, -.25);
glVertex3d(.1, -.6, -.15);
glVertex3d(0, -.6, -.25);
glVertex3d(-.1, -.6, -.15);
glEnd();
}
if (CurrentMove >= 3 && CurrentMove<6)
{
glScaled(3.6, 1.1, 3.6);
glTranslated(0, CurrentMove - 4, 0);

glBegin(GL_LINES);
glVertex3d(-.25, 0, .6); //Little pointy arrows
glVertex3d(+.25, 0, .6);
glVertex3d(-.25, 0, .6);
glVertex3d(-.15, .1, .6);
glVertex3d(-.25, 0, .6);
glVertex3d(-.15, -.1, .6);

glVertex3d(-.25, 0, -.6);
glVertex3d(+.25, 0, -.6);
glVertex3d(+.25, 0, -.6);
glVertex3d(+.15, .1, -.6);
glVertex3d(+.25, 0, -.6);
glVertex3d(+.15, -.1, -.6);

glVertex3d(.6, 0, -.25);
glVertex3d(.6, 0, +.25);
glVertex3d(.6, 0, +.25);
glVertex3d(.6, .1, +.15);
glVertex3d(.6, 0, +.25);
glVertex3d(.6, -.1, +.15);

glVertex3d(-.6, 0, -.25);
glVertex3d(-.6, 0, +.25);
glVertex3d(-.6, 0, -.25);
glVertex3d(-.6, .1, -.15);
glVertex3d(-.6, 0, -.25);
glVertex3d(-.6, -.1, -.15);
glEnd();
}
if (CurrentMove >= 6)
{
glScaled(3.6, 3.6, 1.1);
glTranslated(0, 0, CurrentMove - 7);

glBegin(GL_LINES);
glVertex3d(-.25, .6, 0); //Little pointy arrows
glVertex3d(+.25, .6, 0);
glVertex3d(-.25, .6, 0);
glVertex3d(-.15, .6, .1);
glVertex3d(-.25, .6, 0);
glVertex3d(-.15, .6, -.1);

glVertex3d(-.25, -.6, 0);
glVertex3d(+.25, -.6, 0);
glVertex3d(+.25, -.6, 0);
glVertex3d(+.15, -.6, .1);
glVertex3d(+.25, -.6, 0);
glVertex3d(+.15, -.6, -.1);

glVertex3d(.6, -.25, 0);
glVertex3d(.6, +.25, 0);
glVertex3d(.6, +.25, 0);
glVertex3d(.6, +.15, .1);
glVertex3d(.6, +.25, 0);
glVertex3d(.6, +.15, -.1);

glVertex3d(-.6, -.25, 0);
glVertex3d(-.6, +.25, 0);
glVertex3d(-.6, -.25, 0);
glVertex3d(-.6, -.15, .1);
glVertex3d(-.6, -.25, 0);
glVertex3d(-.6, -.15, -.1);
glEnd();
}
glutWireCube(1);
glPopMatrix();

RCube.Draw();

glutSwapBuffers();

}

void OnChar(unsigned char key, int x, int y)
{
if (f == 0) //main page
{

switch (key)
{
case 13:
case '1': f = 3; break; //fountain
case '2': f = 1; break; //help
case '3': exit(0); //exit
case '4': case 'b': f = 2; break;
case ESCAPE: exit(0);

glutPostRedisplay();
}
Dis();
}
else if (f == 1)  //help page
{
if (key == 'b' || key == 'B')
f = 0;
else
f = 3;
glutPostRedisplay();
}
else if (f == 2) //cover page
{
f = 0;
}
else
{
int k = key;

if (k == 27)
{
f = 0;
}
else
{
if (k == SHUFFLE){ RCube.Shuffle(); return; }
if (k == RESTART){ RCube.Reset(); return; }
if (k == CHANGEMOVE)
{
++CurrentMove;
CurrentMove %= 9;
glutPostRedisplay();
return;
}
if (k == CHANGEMOVE2)
{
--CurrentMove;
if (CurrentMove<0) CurrentMove = 8;
glutPostRedisplay();
return;
}
if (k == MAKEMOVE)
{
if (CurrentMove == 0) RCube.MakeMove(LEFT, false);
if (CurrentMove == 1) RCube.MakeMove(LEFT2, false);
if (CurrentMove == 2) RCube.MakeMove(LEFT3, false);
if (CurrentMove == 3) RCube.MakeMove(FRONT, false);
if (CurrentMove == 4) RCube.MakeMove(FRONT2, false);
if (CurrentMove == 5) RCube.MakeMove(FRONT3, false);
if (CurrentMove == 6) RCube.MakeMove(DOWN, false);
if (CurrentMove == 7) RCube.MakeMove(DOWN2, false);
if (CurrentMove == 8) RCube.MakeMove(DOWN3, false);
return;
}
if (k == MAKEMOVE2)
{
if (CurrentMove == 0) RCube.MakeMove(RIGHT3, false);
if (CurrentMove == 1) RCube.MakeMove(RIGHT2, false);
if (CurrentMove == 2) RCube.MakeMove(RIGHT, false);
if (CurrentMove == 3) RCube.MakeMove(BACK3, false);
if (CurrentMove == 4) RCube.MakeMove(BACK2, false);
if (CurrentMove == 5) RCube.MakeMove(BACK, false);
if (CurrentMove == 6) RCube.MakeMove(UP3, false);
if (CurrentMove == 7) RCube.MakeMove(UP2, false);
if (CurrentMove == 8) RCube.MakeMove(UP, false);
return;
}
}
}
Dis();
}

void OnDrag(int x, int y)
{
theta = (y - dragstarty) / 100.0 + theta0;
phi = (dragstartx - x) / 50.0 + phi0;

if (theta<-1.57) theta = -1.57;
if (theta>1.57) theta = 1.57;
glutPostRedisplay();
}

void OnClick(int b, int s, int x, int y)
{
if (s == GLUT_DOWN)
{
theta0 = theta;
phi0 = phi;
dragstartx = x;
dragstarty = y;
}
}



Comments

Popular posts from this blog

Wizard Quiz: Privacy Policy

Sorting Hat: Privacy Policy

mTranslator