#include #include #include //todo: //add robot //challenges: //populate, addrobot, and addkitten could all be put into one function int in = 0; int objects = 30; //1-30 are objects, 31 is kitten, 32 is robot int x[32]; int y[32]; //int x[(objects + 2)]; int y[(objects + 2)]; the + 2 makes room for kitten and robot, but these should be excluded from for loops char object_symbol[32]; //char object_symbol[(objects + 2)]; int object_color[32]; //int object_color[(objects + 2)]; int object_brightness[32]; //int object_brightness[(objects + 2)]; int populate_stop = 1; int addrobot_stop = 1; int addkitten_stop = 1; time_t t; //30 objects, kitten, and robot //\033[%d;%dH move cursor to spot //\033[%dm color text void populate() { for(int i = 0; i < objects; i++) { //initial position randomization x[i] = (rand() % 128); y[i] = (rand() % 64); object_symbol[i] = ((rand() % 93) + 32); //ansi 33-126 object_color[i] = ((rand() % 8) + 29); //ansi escape code color 30-37 object_brightness[i] = (rand() % 2); //brightness for color value } while(populate_stop) { //check if multiple positions are the same, if so then re-randomize those positions for(int i = 0; i < objects; i++) { for(int j = 0; j < objects; j++) { if((x[i] == x[j]) && (y[i] == y[j])) { x[i] = (rand() % 128); y[i] = (rand() % 64); } else { populate_stop = 0; } } } } for (int i = 0; i < objects; i++) { //move cursor to screen position and place object if (object_brightness) { printf("\033[%d;%dH\033[%d;1m%c\033[0m",(y[i] + 3),(x[i] + 1),object_color[i],(object_symbol[i])); } else { printf("\033[%d;%dH\033[%dm%c\033[0m",(y[i] + 3),(x[i] + 1),object_color[i],(object_symbol[i])); } } } void addkitten() { x[31] = (rand() % 128); y[31] = (rand() % 64); object_symbol[31] = ((rand() % 93) + 32); //ansi 33-126 object_color[31] = ((rand() % 8) + 29); //ansi escape code color 30-37 object_brightness[31] = (rand() % 2); //brightness for color value while(addkitten_stop) { for(int i = 0; i < objects; i++) { for(int j = 0; j < objects; j++) { if((x[31] == x[j]) && (y[31] == y[j])) { x[31] = (rand() % 128); y[31] = (rand() % 64); } else { addkitten_stop = 0; } } } } if (object_brightness) { //debug these kitten printers have a white background (;47) printf("\033[%d;%dH\033[%d;47;1m%c\033[0m",(y[31] + 3),(x[31] + 1),object_color[31],(object_symbol[31])); } else { printf("\033[%d;%dH\033[%d;47m%c\033[0m",(y[31] + 3),(x[31] + 1),object_color[31],(object_symbol[31])); } } void addrobot() { x[32] = (rand() % 128); y[32] = (rand() % 64); object_symbol[32] = 35; //robot is always a bright white # object_color[32] = 37; object_brightness[32] = 1; while(addrobot_stop) { for(int i = 0; i < objects; i++) { for(int j = 0; j < objects; j++) { if((x[32] == x[j]) && (y[32] == y[j])) { x[32] = (rand() % 128); y[32] = (rand() % 64); } else { addrobot_stop = 0; } } } } printf("\033[%d;%dH\033[%d;1m%c\033[0m",(y[32] + 3),(x[32] + 1),object_color[32],(object_symbol[32])); } void moverobot(int direction) { switch(direction) { case 72 : //up printf("\033[%d;%dH ",(y[32] + 3),(x[32] + 1)); //clear robot position for(int i = 0; i < (objects + 1); i++) { //objects + 1 to account for kitten if((x[32] == x[i]) && ((y[32] - 1) == y[i])) { //trying to have same coords as an object wprintf("\033[d\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back one space and then place robot break; } else { wprintf("\033[d\033[a\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back and up one space and then place robot break; } } case 80 : //down printf("\033[%d;%dH ",(y[32] + 3),(x[32] + 1)); //clear robot position for(int i = 0; i < (objects + 1); i++) { //objects + 1 to account for kitten if((x[32] == x[i]) && ((y[32] - 1) == y[i])) { //trying to have same coords as an object wprintf("\033[d\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back one space and then place robot break; } else { wprintf("\033[d\033[b\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back and up one space and then place robot break; } } case 75 : //left printf("\033[%d;%dH ",(y[32] + 3),(x[32] + 1)); //clear robot position for(int i = 0; i < (objects + 1); i++) { //objects + 1 to account for kitten if((x[32] == x[i]) && ((y[32] - 1) == y[i])) { //trying to have same coords as an object wprintf("\033[d\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back one space and then place robot break; } else { wprintf("\033[d\033[d\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back and up one space and then place robot break; } } case 77 : //right printf("\033[%d;%dH ",(y[32] + 3),(x[32] + 1)); //clear robot position for(int i = 0; i < (objects + 1); i++) { //objects + 1 to account for kitten if((x[32] == x[i]) && ((y[32] - 1) == y[i])) { //trying to have same coords as an object wprintf("\033[d\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back one space and then place robot break; } else { wprintf("\033[d\033[c\033[%d;1m%c\033[0m",object_color[32],object_symbol[32]); //move cursor back and up one space and then place robot break; } } /*printf("\033[%d;%dH ",(y[32] + 3),(x[32] + 1)); //for all objects including kitten //if robot coords + 1 in wahatever direction equal the coords of something else then dont move, otherwise set the coods to that position for(int i = 0; i < (objects + 1); i++) { //objects + 1 to account for kitten if(((x[32] + 1) == x[i]) && (y[32] == y[i])) { //trying to have same coords as an object printf("\033[%d;%dH\033[%d;1m%c\033[0m",(y[32]),(x[32]),object_color[32],(object_symbol[32])); break; } else { x[32] = x[32] + 1; printf("\033[%d;%dH\033[%d;1m%c\033[0m",(y[32] + 3),(x[32] + 1),object_color[32],(object_symbol[32])); break; } }*/ } } int main() { { //initialization srand(time(t)); system("mode con: cols=128 lines=66"); } { //start screen printf("\033[8;8Hb\033[9;8Hr\033[10;8Hu\033[12;8Hh"); getch(); system("cls"); } { //initial population printf("\033[1;1Hinitialization"); printf("\033[2;1H--------------------------------------------------------------------------------------------------------------------------------"); populate(); addkitten(); addrobot(); } while(in != 27) { in = getch(); if (in == 0 || in == 0xE0) in = getch(); //got dang ol windows makes you call functions and arrows keys twice to get the input switch(in) { case 72 : //up moverobot(in); case 80 : //down moverobot(in); case 75 : //left moverobot(in); case 77 : //right moverobot(in); /*default : printf("pizdec");*/ } } }