Advertisement
Guest User

frogger

a guest
Jul 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.92 KB | None | 0 0
  1. #include <termios.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <unistd.h> //usleep
  6. //#include <curses.h> //for kbhit and getch
  7.  
  8. #define ANSI_COLOR_BLACK   "\x1b[30m"
  9. #define ANSI_COLOR_RED     "\x1b[31m"
  10. #define ANSI_COLOR_GREEN   "\x1b[32m"
  11. #define ANSI_COLOR_BLUE    "\x1b[34m"
  12. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  13. #define ANSI_COLOR_CYAN    "\x1b[36m"
  14. #define ANSI_COLOR_RESET   "\x1b[0m"
  15. #define clear_console() printf("\033[H\033[J")
  16. #define input_without_enter() system ("/bin/stty raw")//get input without pressing enter
  17. #define input_usual() system ("/bin/stty cooked");//usual input mode
  18. #define true 1
  19. #define false 0
  20. #define LEFT 0
  21. #define RIGHT 1
  22. #define FORWARD 2
  23. #define NONE 2
  24. // '#' - frog, 'r' - right car, 'l' - left car ,'*' - empty
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <sys/select.h>
  29. #include <termios.h>
  30.  
  31. const char numbers_arr[10][5][3]= {
  32.     {   //0
  33.         {'o','o','o'},
  34.         {'o','*','o'},
  35.         {'o','*','o'},
  36.         {'o','*','o'},
  37.         {'o','o','o'}
  38.     },
  39.     {   //1
  40.         {'*','*','o'},
  41.         {'*','o','o'},
  42.         {'*','*','o'},
  43.         {'*','*','o'},
  44.         {'*','*','o'}
  45.     },
  46.     {   //2
  47.         {'o','o','o'},
  48.         {'*','*','o'},
  49.         {'*','o','*'},
  50.         {'o','*','*'},
  51.         {'o','o','o'}
  52.     },
  53.     {   //3
  54.         {'o','o','o'},
  55.         {'*','*','o'},
  56.         {'o','o','o'},
  57.         {'*','*','o'},
  58.         {'o','o','o'}
  59.     },
  60.     {   //4
  61.         {'o','*','o'},
  62.         {'o','*','o'},
  63.         {'o','o','o'},
  64.         {'*','*','o'},
  65.         {'*','*','o'}
  66.     },
  67.     {   //5
  68.         {'o','o','o'},
  69.         {'o','*','*'},
  70.         {'o','o','o'},
  71.         {'*','*','o'},
  72.         {'o','o','o'}
  73.     },
  74.     {   //6
  75.         {'o','o','o'},
  76.         {'o','*','*'},
  77.         {'o','o','o'},
  78.         {'o','*','o'},
  79.         {'o','o','o'}
  80.     },
  81.     {   //7
  82.         {'o','o','o'},
  83.         {'*','*','o'},
  84.         {'*','o','*'},
  85.         {'*','o','*'},
  86.         {'*','o','*'}
  87.     },
  88.     {   //8
  89.         {'o','o','o'},
  90.         {'o','*','o'},
  91.         {'o','o','o'},
  92.         {'o','*','o'},
  93.         {'o','o','o'}
  94.     },
  95.     {   //9
  96.         {'o','o','o'},
  97.         {'o','*','o'},
  98.         {'o','o','o'},
  99.         {'*','*','o'},
  100.         {'o','o','o'}
  101.     }
  102. };
  103.  
  104.  
  105.  
  106. struct termios orig_termios;
  107.  
  108. void reset_terminal_mode()
  109. {
  110.     tcsetattr(0, TCSANOW, &orig_termios);
  111. }
  112.  
  113. void set_conio_terminal_mode()
  114. {
  115.     struct termios new_termios;
  116.  
  117.     /* take two copies - one for now, one for later */
  118.     tcgetattr(0, &orig_termios);
  119.     memcpy(&new_termios, &orig_termios, sizeof(new_termios));
  120.  
  121.     /* register cleanup handler, and set the new terminal mode */
  122.     atexit(reset_terminal_mode);
  123.     cfmakeraw(&new_termios);
  124.     tcsetattr(0, TCSANOW, &new_termios);
  125. }
  126.  
  127. int kbhit()
  128. {
  129.     struct timeval tv = { 0L, 0L };
  130.     fd_set fds;
  131.     FD_ZERO(&fds);
  132.     FD_SET(0, &fds);
  133.     return select(1, &fds, NULL, NULL, &tv);
  134. }
  135.  
  136. int getch()
  137. {
  138.     int r;
  139.     unsigned char c;
  140.     if ((r = read(0, &c, sizeof(c))) < 0) {
  141.         return r;
  142.     } else {
  143.         return c;
  144.     }
  145. }
  146.  
  147. struct frog_pos {
  148.     int    row;//from 0 to 7
  149.     int    col;//from 0 to 15
  150. };
  151. struct frog_pos frog_pos;
  152. int is_shifting=false;
  153. int game_arr[8][16];
  154. int score=0;
  155.  
  156. void set_num(int number,int pos) {//pos = {1,2,3,4}
  157.     int start_col=(pos-1)*(4);
  158.     if(pos>2)start_col+=1;
  159.     for(int row=1; row<6; row++) {
  160.         for(int col=start_col; col<start_col+3; col++) {//every number's width is 4 pixels
  161.             game_arr[row][col]=numbers_arr[number][row-1][col-start_col];
  162.         }
  163.     }
  164. }
  165.  
  166. void generate_cars(int row) { //generate car (direction, start_pos(!),length(!))
  167.     //none?
  168.     int direction=rand() % 2;
  169.     int length=1+(rand() % 2);
  170.     int start_pos=rand() % (16-length+1);//-length
  171.     for(int i=0;i<length;i++){
  172.     if(direction==LEFT) {//<--
  173.         game_arr[row][start_pos+i]='l';
  174.     }
  175.     else if (direction==RIGHT) {//-->
  176.         game_arr[row][start_pos+i]='r';
  177.     }
  178. }
  179. }
  180.  
  181. void init_game() {
  182.     for(int row=0; row<8; row++) {//init game_arr
  183.         for(int col=0; col<16; col++) {
  184.             game_arr[row][col]= '*';
  185.         }
  186.         if(row!=0&&row!=7) {
  187.             generate_cars(row);
  188.         }
  189.     }
  190.     game_arr[7][8]= '#';//frog starting position
  191.     frog_pos.row=7;
  192.     frog_pos.col=8;
  193.     score=0;
  194. }
  195.  
  196. void output_arr() {
  197.     reset_terminal_mode();
  198.     clear_console();
  199.     for(int row=0; row<8; row++) {
  200.         for(int col=0; col<16; col++) {
  201.             if(game_arr[row][col]=='#') {
  202.                 printf("%s%c%s ", ANSI_COLOR_GREEN,'#',ANSI_COLOR_RESET);
  203.             }
  204.             /*else if(game_arr[row][col]=='l'||game_arr[row][col]=='r') {
  205.                 printf("%s%c%s ", ANSI_COLOR_RED,'o',ANSI_COLOR_RESET);
  206.             }*/
  207.             else if(game_arr[row][col]=='o') {
  208.                 printf("%s%c%s ", ANSI_COLOR_RED,'#',ANSI_COLOR_RESET);
  209.             }
  210.             else if(game_arr[row][col]=='l') {
  211.                 printf("%s%c%s ", ANSI_COLOR_RED,'#',ANSI_COLOR_RESET);
  212.             }
  213.             else if(game_arr[row][col]=='r') {
  214.                 printf("%s%c%s ", ANSI_COLOR_RED,'#',ANSI_COLOR_RESET);
  215.             }
  216.             else {
  217.                 printf("%s%c%s ", ANSI_COLOR_BLUE,'*',ANSI_COLOR_RESET);
  218.                 // printf("%c ",time_arr[row][col]);
  219.             }
  220.         }
  221.         printf("\n");
  222.     }
  223.     set_conio_terminal_mode();
  224. }
  225.  
  226. void display_score(){
  227.     set_num((score/1000)%10,1);
  228.     set_num((score/100)%10,2);
  229.     set_num((score/10)%10,3);
  230.     set_num(score%10,4);
  231.     output_arr();
  232.     char wait =getchar();
  233.     wait=wait;
  234.     }
  235.  
  236. void clear_arr() {
  237.      for(int row=0; row<8; row++) {//init game_arr
  238.         for(int col=0; col<16; col++) {
  239.             game_arr[row][col]= '*';
  240.         }
  241.     }
  242. }
  243.  
  244. void reset_arr() {
  245.     usleep(200*1000);
  246.     clear_arr();
  247.     display_score();
  248.     clear_arr();
  249.     init_game();
  250. }
  251.  
  252. void reset_win_arr() {//todo add win msg
  253.     clear_arr();
  254.     display_score();
  255.     clear_arr();
  256.     init_game();
  257. }
  258.  
  259.  
  260.  
  261. void car_move_n_check_lose(int direction,int row,int col) {
  262.     int move_col;
  263.     if(direction==LEFT) {
  264.         if(col==0) {
  265.             move_col=15;
  266.         }
  267.         else {
  268.             move_col=col-1;
  269.         }
  270.     }
  271.     if(direction==RIGHT) {
  272.         if(col==15) {
  273.             move_col=0;
  274.         }
  275.         else {
  276.             move_col=col+1;
  277.         }
  278.     }
  279.     if(game_arr[row][move_col]=='#') { //lose
  280.         reset_arr();
  281.     }
  282.     else {
  283.         char temp=game_arr[row][col];
  284.         game_arr[row][col]=game_arr[row][move_col];
  285.         game_arr[row][move_col]=temp;
  286.  
  287.     }
  288. }
  289.  
  290.  
  291. void update_arr() {
  292.     for(int row=0; row<8; row++) {
  293.         for(int col=0; col<16; col++) {
  294.             if(game_arr[row][col]=='r'){break;}//time economy
  295.             if(game_arr[row][col]=='l') { //<--
  296.                 car_move_n_check_lose(LEFT,row,col);
  297.             }
  298.         }
  299.        for(int col=15; col>=0; col--) {
  300.             if(game_arr[row][col]=='l'){break;}//time economy
  301.             if(game_arr[row][col]=='r') { //<--
  302.                 car_move_n_check_lose(RIGHT,row,col);
  303.             }
  304.         }
  305.     }
  306. }
  307.  
  308.  
  309.  
  310. void shift_arr() { //shift array down and then generate new cars
  311.     //is_shifting=true;
  312.     output_arr();
  313.     for(int i=0; i<7; i++) { //shift array 7 times down
  314.         for(int row=7; row>=0; row--) {//init game_arr
  315.             for(int col=0; col<16; col++) {
  316.                 if(row==0) { //generate last empty row
  317.                     game_arr[row][col]='*';
  318.                 }
  319.                 else {
  320.                     game_arr[row][col]=game_arr[row-1][col];
  321.                 }
  322.             }
  323.             if(row==0&&i!=6) { //generate new cars
  324.                 generate_cars(row);
  325.             }
  326.         }
  327.         usleep(200*1000);//microseconds (1000 times smaller than milliseconds )
  328.         output_arr();
  329.     }
  330.     //is_shifting=false;
  331. }
  332.  
  333. int frog_is_lose(int direction) {
  334.     switch(direction) {
  335.     case LEFT:
  336.         if(game_arr[frog_pos.row][frog_pos.col-1]=='r'||game_arr[frog_pos.row][frog_pos.col-1]=='l') {
  337.             return true;
  338.         }
  339.         return false;
  340.  
  341.     case RIGHT:
  342.         if(game_arr[frog_pos.row][frog_pos.col+1]=='r'||game_arr[frog_pos.row][frog_pos.col+1]=='l') {
  343.             return true;
  344.         }
  345.         return false;
  346.  
  347.     case FORWARD:
  348.         if(game_arr[frog_pos.row-1][frog_pos.col]=='r'||game_arr[frog_pos.row-1][frog_pos.col]=='l') {
  349.             return true;
  350.         }
  351.         return false;
  352.     }
  353.     return false;
  354. }
  355.  
  356. void move_forward() { //move along row
  357.     //shift arr after moving forward
  358.     if(frog_pos.row>0) {
  359.         if(frog_is_lose(FORWARD)==true) { //lose
  360.             reset_arr();
  361.         }
  362.         else {
  363.             char temp=game_arr[frog_pos.row][frog_pos.col];
  364.             game_arr[frog_pos.row][frog_pos.col]=game_arr[frog_pos.row-1][frog_pos.col];
  365.             game_arr[frog_pos.row-1][frog_pos.col]=temp;
  366.             frog_pos.row--;
  367.             score++;
  368.             if(score==9999){//WIN MSG
  369.                 reset_win_arr();
  370.                 }
  371.         }
  372.     }
  373.     if(frog_pos.row==0) { //shifting array to new location
  374.         shift_arr();
  375.         frog_pos.row=7;
  376.     }
  377. }
  378.  
  379. void move_left() {//move along col (--)
  380.     if(frog_pos.col>0) {
  381.         if(frog_is_lose(LEFT)==true) { //lose
  382.             reset_arr();
  383.         }
  384.         else {
  385.             char temp=game_arr[frog_pos.row][frog_pos.col];
  386.             game_arr[frog_pos.row][frog_pos.col]=game_arr[frog_pos.row][frog_pos.col-1];
  387.             game_arr[frog_pos.row][frog_pos.col-1]=temp;
  388.             frog_pos.col--;
  389.         }
  390.     }
  391. }
  392.  
  393. void move_right() {//move along col (++)
  394.     if(frog_pos.col<15) {
  395.         if(frog_is_lose(RIGHT)==true) { //lose
  396.             reset_arr();
  397.         }
  398.         else {
  399.             char temp=game_arr[frog_pos.row][frog_pos.col];
  400.             game_arr[frog_pos.row][frog_pos.col]=game_arr[frog_pos.row][frog_pos.col+1];
  401.             game_arr[frog_pos.row][frog_pos.col+1]=temp;
  402.             frog_pos.col++;
  403.         }
  404.     }
  405. }
  406.  
  407. /*void get_pressed_key() {
  408.     input_without_enter();
  409.     char key = getchar();
  410.     input_usual();
  411.     switch(key) {
  412.     case 'w':
  413.         move_forward();
  414.         break;
  415.     case 'a':
  416.         move_left();
  417.         break;
  418.     case 'd':
  419.         move_right();
  420.         break;
  421.     }
  422.     output_arr();
  423. }*/
  424. void get_pressed_key() {
  425.  
  426.     char key = getch();
  427.     switch(key) {
  428.     case 'w':
  429.         move_forward();
  430.         break;
  431.     case 'a':
  432.         move_left();
  433.         break;
  434.     case 'd':
  435.         move_right();
  436.         break;
  437.     }
  438.     output_arr();
  439. }
  440.  
  441.  
  442. int main(int argc, char **argv)
  443. {
  444.     srand(time(NULL)); //init rand
  445.     set_conio_terminal_mode();
  446.     init_game();
  447.     output_arr();
  448.  
  449.     while(1) {
  450.         while (!kbhit()) {//no click
  451.             update_arr();
  452.             usleep(200*1000);
  453.             output_arr();
  454.         }
  455.         get_pressed_key();
  456.     }
  457.     return 0;
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement