/*
What: Instant Insanity Game source in Turbo C. GPL(C)
=======================
Col1 Col4*4
3 3 3 3 Row1
405 405 405 405 Row2
1 1 1 1 Row3
2 2 2 2 Row4
=======================
c:\mosh\games>tcc -If:\tcc30\include -Lf:\tcc30\lib instanti.cpp
*/
#include "cubeinit.cpp"
int cube[4][6]; /* four cubes with six faces */
enum { W,R,G,B } virtual_colors;
static int screen_colors[5] = {WHITE, RED,GREEN,BLUE,YELLOW};
void setpuzzle(int p){
int i,k;
static int puzzle[1][4][6] = {
{{W,W,W,G,R,B},{G,W,G,R,B,R},{W,W,B,R,G,B},{B,W,G,G,B,R}},
};
for(i=0;i<6;i++)
for(k=0;k<4;k++)
cube[k][i]=puzzle[p%(sizeof(puzzle)/sizeof(cube))][k][i];
}
int sameface(int cube[4][6]){ /* return 0, if all sides have different colors */
int i,j,k,dup=0;
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
for(k=0;k<4;k++)
dup += (cube[i][k]==cube[j][k]);
return dup;
}
void putcharc(int row, int kol, int c){
int t;
textcolor(screen_colors[c]);
mvaddch(row,kol,'0'+c);
}
void drawcubes(int cc){
int i;
clrscr();
for(i=0;i<4;i++){
putcharc(4+i*4 ,1,cube[i][3]);
putcharc(4+i*4 ,2,cube[i][0]);
putcharc(4+i*4-1 ,3,cube[i][4]);
putcharc(4+i*4+1 ,3,cube[i][5]);
putcharc(4+i*4 ,3,cube[i][1]);
putcharc(4+i*4 ,4,cube[i][2]);
if(i==cc)
putcharc(4+i*4,5,4);
}
}
void upcube(int cc){
int t=cube[cc][3];
cube[cc][3]=cube[cc][0]; cube[cc][0]=cube[cc][1];
cube[cc][1]=cube[cc][2]; cube[cc][2]=t;
}
void rocube(int cc){
int t=cube[cc][3];
cube[cc][3]=cube[cc][4]; cube[cc][4]=cube[cc][1];
cube[cc][1]=cube[cc][5]; cube[cc][5]=t;
}
void main(){
chtype ch=' ';
int cc=0; /* current cube to operate on */
initscr(); cbreak(); noecho(); refresh();
setpuzzle(0);
drawcubes(cc);
do{
textcolor(FGCOL); mvaddch(24,0,':');
clrtoeol(); mvaddch(24,1,ch); ch = mvgetch(24,2); clrtoeol();
amessage("ok.");
switch(ch){
case 'L': if(--cc<0)cc=3; break;
case 'R': if(++cc>3)cc=0; break;
case 'l': upcube(cc); break;
case 'r': upcube(cc); upcube(cc); upcube(cc); break;
case 'u': rocube(cc); break;
case 'd': rocube(cc); rocube(cc); rocube(cc); break;
case '1': cc=0; break;
case '2': cc=1; break;
case '3': cc=2; break;
case '4': cc=3; break;
}
if( sameface(cube) == 0 ){
printf("Done\n");
}
drawcubes(cc);
}while(ch!='q');
clear(); refresh();
endwin();
}