C中的老虎机(gotoxy)

时间:2014-12-29 08:25:52

标签: c

#include <stdio.h>
#include <time.h>
#include <windows.h>

int intSlot1, intSlot2, intSlot3;

void fnGotoXY(short x, short y);
void fnSlotMachine();
void fnSlot1();
void fnSlot2();
void fnSlot3();

int main(){

        srand( time(0) );
        fnSlotMachine();
        fnSlot1();
        fnSlot2();
        fnSlot3();

}

void fnGotoXY(short x, short y){

        COORD pos = {x, y};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

void fnSlotMachine(){

        fnGotoXY(5, 5);
        printf(" x^---------------------------^x\n");
        printf("      |oOoOoOoOoOoOoOoOoOoOoOoOoOoOo|\n");
        printf("      \\_____________________________/\n");
        printf("      /__$$$__\\  /__$$$__\\  /__$$$__\\");
        fnGotoXY(5, 12);
        printf(" <*^*^*^*>  <*^*^*^*>  <*^*^*^*>");


}

void fnSlot1(){

        while(1){
                Sleep(50);
                fnGotoXY(5, 9);
                intSlot1 = rand() % 9;
                printf(" | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
                fnGotoXY(2, 10);
                printf("    | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
                fnGotoXY(2, 11);
                printf("    | %i %i %i |",   intSlot1, intSlot1, intSlot1);
        }      
}

void fnSlot2(){

        while(1){
                Sleep(50);
                fnGotoXY(17, 9);
                intSlot2 = rand() % 9;
                printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
                fnGotoXY(17, 10);
                printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
                fnGotoXY(17, 11);
                printf("| %i %i %i |",   intSlot2, intSlot2, intSlot2);
        }
}

void fnSlot3(){

        while(1){
                Sleep(50);
                fnGotoXY(27, 9);
                intSlot3 = rand() % 9;
                printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
                fnGotoXY(27, 10);
                printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
                fnGotoXY(27, 11);
                printf("| %i %i %i |",   intSlot3, intSlot3, intSlot3);
        }
}

所以,我的问题是关于gotoxy。放入while(1)循环后,其他插槽不打印。希望得到一些回应。提前谢谢!

2 个答案:

答案 0 :(得分:1)

while(1)函数中删除fnSlotN循环,而不是:

while(1)
{
    fnSlot1();
    fnSlot2();
    fnSlot3();
}

答案 1 :(得分:1)

所有功能都有无限循环。如果您输入一个函数,则永远不会返回。

考虑在主函数中放置它。

#include <stdio.h>
#include <time.h>
...

int main(){

        srand( time(0) );
        fnSlotMachine();
        while(1) {
          fnSlot1();
          fnSlot2();
          fnSlot3(); 
        }
}

...    
void fnSlot1(){

            Sleep(50);
            fnGotoXY(5, 9);
            intSlot1 = rand() % 9;
            printf(" | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
            fnGotoXY(2, 10);
            printf("    | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
            fnGotoXY(2, 11);
            printf("    | %i %i %i |",   intSlot1, intSlot1, intSlot1);

}

void fnSlot2(){


            Sleep(50);
            fnGotoXY(17, 9);
            intSlot2 = rand() % 9;
            printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
            fnGotoXY(17, 10);
            printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
            fnGotoXY(17, 11);
            printf("| %i %i %i |",   intSlot2, intSlot2, intSlot2);

}

void fnSlot3(){


            Sleep(50);
            fnGotoXY(27, 9);
            intSlot3 = rand() % 9;
            printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
            fnGotoXY(27, 10);
            printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
            fnGotoXY(27, 11);
            printf("| %i %i %i |",   intSlot3, intSlot3, intSlot3);

}