Thread: Rubix

  1. #1

    Unhappy Rubix

    I got a question from my friends sister's boyfriend. He made a rubix game in C++ and at that time I was looking for new program Idea's he said give it a try so I did only to find out I find it nearly impossible to do.

    This is how far I got:

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <windows.h>
    #include <conio.c>
    #include <time.h>
    void drawred();
    void drawbrown();
    void drawyellow();
    void drawwhite();
    void drawpurple();
    void drawgreen();
    void drawside();
    void cl(int);
    char* square = "|###|\n|###|\n|###|";
    int random(int,int);
    
    int main()
    {
     drawside();
     getch();
     return 0;
    }
    int random(int low, int high) 
    {
    srand(time(NULL));
    return low + rand()%(high-low+1);
    }
    void cl(int cl){
    switch(cl)
    {
     case 1:
     drawred();
     break;
     case 2:
     drawred();
     break;
     case 3:
     drawred();
     break;
     case 4:
     drawred();
     break;
     case 5:
     drawred();
     break;
     case 6:
     drawred();
     break;
    }
    }
    void drawred(){textcolor(RED);cout<<square<<"\n";}
    void drawbrown(){textcolor(BROWN);cout<<square<<"\n";}
    void drawyellow(){textcolor(YELLOW);cout<<square<<"\n";}
    void drawwhite(){textcolor(WHITE);cout<<square<<"\n";}
    void drawpurple(){textcolor(MAGENTA);cout<<square<<"\n";}
    void drawgreen(){textcolor(GREEN);cout<<square<<"\n";}
    void drawside()
    {
     cl(random(1,6));
     cout<<endl;
     cl(random(1,6));
     cout<<endl;
     cl(random(1,6));
     cout<<endl;
    }
    This prints 3 squares down wards in ALL THE SAME COLOR which is not what i intented it to do I want them to all appear in different colors but some how they wont. Can anybody help me out here and what would be the best way to make a full side. Thanx

    -Devouring One-

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void cl(int cl){
    switch(cl)
    {
    case 1:
    drawred();
    break;
    case 2:
    drawred();
    break;
    case 3:
    drawred();
    break;
    case 4:
    drawred();
    break;
    case 5:
    drawred();
    break;
    case 6:
    drawred();
    break;
    }
    }
    Do you see the problem yet? Here's a hint: do you see the word RED in there a few too many times?

    On a side note, your "#include"s make me want to cry.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    My include's I know its just Dev C++ wont allow me to do it without the .h it gives me errors like windows no such directory. So i just do windows.h and it works. Its not like it makes alot of difference!

    Anyway here is my revised code:

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <windows.h>
    #include <conio.c>
    #include <time.h>
    int arraybox[7][10];
    void drawred();
    void drawbrown();
    void drawyellow();
    void drawwhite();
    void drawpurple();
    void drawgreen();
    void drawside();
    void cl(int);
    char* square = "\n|###|\n|###|\n|###|";
    int random(int,int);
    
    int main()
    {
     drawside();
     getch();
     return 0;
    }
    int random(int low, int high) 
    {
    srand(time(NULL));
    return low + rand()%(high-low+1);
    }
    void cl(int cl){
    switch(cl)
    {
     case 1:
     drawred();
     break;
     case 2:
     drawbrown();
     break;
     case 3:
     drawyellow();
     break;
     case 4:
     drawwhite();
     break;
     case 5:
     drawpurple();
     break;
     case 6:
     drawbrown();
     break;
    }
    }
    void drawred(){
    textcolor(RED);for( int l = 0; l < 7; l ++){
    cout<<endl;
    for( int w = 0; w< 10; w ++){
    cout<<static_cast<char>(arraybox[l][w] = 177);
    }
    }}
    void drawbrown(){
    textcolor(BROWN);for( int l = 0; l < 7; l ++){
    cout<<endl;
    for( int w = 0; w< 10; w ++){
    cout<<static_cast<char>(arraybox[l][w] = 177);
    }
    }}
    void drawyellow(){textcolor(YELLOW);for( int l = 0; l < 7; l ++){
    cout<<endl;
    for( int w = 0; w< 10; w ++){
    cout<<static_cast<char>(arraybox[l][w] = 177);
    }
    }}
    void drawwhite(){textcolor(WHITE);for( int l = 0; l < 7; l ++){
    cout<<endl;
    for( int w = 0; w< 10; w ++){
    cout<<static_cast<char>(arraybox[l][w] = 177);
    }
    }}
    void drawpurple(){textcolor(MAGENTA);for( int l = 0; l < 7; l ++){
    cout<<endl;
    for( int w = 0; w< 10; w ++){
    cout<<static_cast<char>(arraybox[l][w] = 177);
    }
    }}
    void drawgreen(){textcolor(GREEN);for( int l = 0; l < 7; l ++){
    cout<<endl;
    for( int w = 0; w< 10; w ++){
    cout<<static_cast<char>(arraybox[l][w] = 177);
    }
    }}
    void drawside()
    {
     cl(random(1,6));
    }
    I know how to make three squares down:

    Code:
    cl(random(1,6));
    cl(random(1,6));
    cl(random(1,6));
    but what if I want them along side:

    |###| |###| |###|
    |###| |###| |###|
    |###| |###| |###|

    It wont alow me! I tried everything!

    [EDIT]
    And even if I do:
    Code:
    cl(random(1,6));
    cout<<endl;
    cl(random(1,6));
    cout<<endl;
    cl(random(1,6));
    It gives me three the same square's
    [/EDIT]
    Last edited by devour89; 12-17-2002 at 04:05 AM.
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I assume that every square is supposed to be a (potentially) a different color? If so, you don't want to draw a whole line in one shot.

    Instead, you need to do something like:
    Code:
    void DrawSquare( int c )
    {
        SetColor( c );
        cout << "###";
    }
    void DrawLine( int c1, int c2, int c3 )
    {
        cout << "|"; 
        DrawSquare( c1, );
        cout << "|";
        DrawSquare( c2, );
        cout << "|";
        DrawSquare( c3, );
        cout << "|" << endl;
    }
    That should get you started. Basicly, you pass it the colors for each of the boxes in that row. Remember, you have to draw the full row, then you draw then next row, and finally the third. So, if it were all red:

    DrawLine( RED, RED, RED );
    DrawLine( RED, RED, RED );
    DrawLine( RED, RED, RED );

    That would draw a solid red side.

    Then let's say you rotate the column up one, as you would with a normal Rubix Cube. We'll say it's the left most column, and that it would turn the left most column Yellow.

    DrawLine( YELLOW, RED, RED );
    DrawLine( YELLOW, RED, RED );
    DrawLine( YELLOW, RED, RED );

    And so on...

    The problem is basicly that you're using a single string, and the string itself contains the newline character: '\n'.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Thanx man I used your idea and perfected it a bit. I will now start the other sides and think of the user interface.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rubix Cube Contest
    By The Brain in forum Contests Board
    Replies: 24
    Last Post: 01-15-2005, 05:33 PM
  2. Anyone good with a Rubix Cube?
    By Nakeerb in forum C++ Programming
    Replies: 7
    Last Post: 11-05-2002, 05:59 PM