Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4. #include <SDL/SDL.h>
  5.  
  6. int selected_field,
  7.     chosen_colour[4],
  8.     round_number;
  9. bool enforce_endturn;
  10. float colour_set[8][3],
  11.     rows[6],
  12.     columns[8],
  13.     rows_interval,
  14.     cols_interval;
  15. SDL_Event event;
  16.  
  17. void init();
  18. void draw_square(float x1,float y1, float x2, float y2, float c1, float c2, float c3);
  19. void draw_doneround(int col_no,int hypoth,int resp);
  20. void display_end_image(int comb, bool won);
  21. int verify_answer(int proposition , int solution); // odpowiedz gracza i dobre rozwiazanie zapisane jako czterocyfrowe l. naturalne
  22. void get_keyboard_events(bool additionals);
  23.  
  24. int main(int argc,char* argv[])
  25. {
  26.     printf("dupa");
  27.     selected_field = 0,
  28.     round_number = 0;
  29.     enforce_endturn = false;
  30.     rows_interval = 95.0f/600.0f;
  31.     cols_interval = 95.0f/800.0f;
  32.     int hypothesis,
  33.         combination = 0,
  34.         power = 1000,
  35.         c,
  36.         response=0;
  37.     bool game_is_on = true;
  38.     init();
  39.     glutInit(&argc,argv);
  40.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  41.     glutInitWindowSize(800, 600);
  42.     glutCreateWindow(argv[0]);
  43.     glClearColor(0.0f,0.0f,0.0f,0.0f);
  44.     glClearDepth(1.0);
  45.     glMatrixMode(GL_PROJECTION);
  46.     glLoadIdentity();
  47.     gluOrtho2D(0.0f,1.0f,0.0f,1.0f);
  48.     glMatrixMode(GL_MODELVIEW);
  49.     printf("dupa2");
  50.     for (int i = 0 ; i < 4 ; ++i)
  51.     {
  52.         c = (rand() % 6) + 1;
  53.         combination += c*power;
  54.         power /= 10;
  55.     }
  56.     // DO TUTAJ JEST OK
  57.     power = 1000;
  58.     while(game_is_on)
  59.     {
  60.         printf("guwno");
  61.         get_keyboard_events(true); // zrob klawiature
  62.         if (enforce_endturn) // zweryfikuj kombinacje
  63.         {
  64.             /*
  65.             hypothesis = 0;
  66.             for (int i = 0 ; i < 4 ; ++i)
  67.             {
  68.                 hypothesis += power * chosen_colour[i];
  69.                 power /= 10;
  70.             }
  71.             response = verify_answer(hypothesis,combination);
  72.             */
  73.             if (response == 40 || round_number == 5) game_is_on = false;
  74.             else
  75.             {
  76.                 draw_doneround(round_number,hypothesis,response);
  77.                 for (int i = 0 ; i < 4 ; ++i) chosen_colour[i] = 1;
  78.                 selected_field = 0;
  79.                 enforce_endturn = false;
  80.                 power = 1000;
  81.                 ++round_number;
  82.             }
  83.         }
  84.         // wyswietl obecna hipoteze TODO
  85.         glFlush();
  86.     }
  87.     if(response == 40) display_end_image(combination,true);
  88.     else display_end_image(combination,false);
  89.     while(!game_is_on)
  90.     {
  91.         get_keyboard_events(false);
  92.         glFlush();
  93.     }
  94.     return 0;
  95. }
  96. void init()
  97. {
  98.     // numery komorek w rzedach
  99.     rows[0] = 25.0f/600.0f;
  100.     rows[1] = 120.0f/600.0f;
  101.     rows[2] = 215.0f/600.0f;
  102.     rows[3] = 310.0f/600.0f;
  103.     rows[4] = 405.0f/600.0f;
  104.     rows[5] = 500.0f/600.0f;
  105.     // numery komorek w kolumnach
  106.     columns[0] = 25.0f/800.0f;
  107.     columns[1] = 120.0f/800.0f;
  108.     columns[2] = 215.0f/800.0f;
  109.     columns[3] = 310.0f/800.0f;
  110.     columns[4] = 415.0f/800.0f;
  111.     columns[5] = 510.0f/800.0f;
  112.     columns[6] = 605.0f/800.0f;
  113.     columns[7] = 700.0f/800.0f;
  114.     // czarny
  115.     colour_set[0][0] = 0.0f;
  116.     colour_set[0][1] = 0.0f;
  117.     colour_set[0][2] = 0.0f;
  118.     // czerwony
  119.     colour_set[1][0] = 1.0f;
  120.     colour_set[1][1] = 0.0f;
  121.     colour_set[1][2] = 0.0f;
  122.     // zolty
  123.     colour_set[2][0] = 1.0f;
  124.     colour_set[2][1] = 1.0f;
  125.     colour_set[2][2] = 0.0f;
  126.     // zielony
  127.     colour_set[3][0] = 0.0f;
  128.     colour_set[3][1] = 1.0f;
  129.     colour_set[3][2] = 0.0f;
  130.     // niebieski
  131.     colour_set[4][0] = 0.0f;
  132.     colour_set[4][1] = 0.0f;
  133.     colour_set[4][2] = 1.0f;
  134.     // pomaranczowy
  135.     colour_set[5][0] = 0.98f;
  136.     colour_set[5][1] = 0.625f;
  137.     colour_set[5][2] = 0.12f;
  138.     // cyan
  139.     colour_set[6][0] = 0.0f;
  140.     colour_set[6][1] = 1.0f;
  141.     colour_set[6][2] = 1.0f;
  142.     // bialy
  143.     colour_set[7][0] = 1.0f;
  144.     colour_set[7][1] = 1.0f;
  145.     colour_set[7][2] = 1.0f;
  146.     for (int i = 0 ; i < 4 ; ++i) chosen_colour[i] = 1;
  147. }
  148. void display_end_image(int comb, bool won)
  149. {
  150.     glColor3f(0.0f,0.0f,0.0f);
  151.     glBegin(GL_TRIANGLES);
  152.         glVertex2f(0.0f,0.0f);
  153.         glVertex2f(1.0f,0.0f);
  154.         glVertex2f(0.0f,1.0f);
  155.  
  156.         glVertex2f(1.0f,0.0f);
  157.         glVertex2f(1.0f,1.0f);
  158.         glVertex2f(0.0f,1.0f);
  159.     glEnd();
  160.  
  161.     // TODO
  162.     // narysuj bialy prostokat na srodku
  163.     // dostaw trojkat z gory/dolu
  164.     // wyrysuj poprawna kombinacje
  165.    
  166. }
  167. void draw_square(float x1,float y1, float x2, float y2, float c1, float c2, float c3)
  168. {
  169.     glColor3f(c1,c2,c3);
  170.     glBegin(GL_TRIANGLES);
  171.         glVertex2f(x1,y1);
  172.         glVertex2f(x2,y1);
  173.         glVertex2f(x1,y2);
  174.  
  175.         glVertex2f(x1,y2);
  176.         glVertex2f(x2,y1);
  177.         glVertex2f(x2,y2);
  178.     glEnd();
  179. }
  180. void draw_doneround(int col_no,int hypoth,int resp)
  181. {
  182.     int hypo[4],
  183.         power = 1000,
  184.         xmin,
  185.         xmax,
  186.         ymin,
  187.         ymax;
  188.     for (int i = 0 ; i < 4 ; ++i)
  189.     {
  190.         hypo[i] = (hypoth - (hypoth % power) ) / hypoth;
  191.         power /= 10;
  192.     }
  193.     for (int i = 0 ; i < 4 ; ++i)
  194.     {
  195.         xmin = rows[i] + (10.0f/95.0f)*rows_interval;
  196.         xmax = rows[i] + (65.0f/95.0f)*rows_interval;
  197.         ymin = columns[col_no] + (10.0f/95.0f)*cols_interval;
  198.         ymax = columns[col_no] + (65.0f/95.0f)*cols_interval;
  199.         draw_square(xmin,ymin,xmax,ymax,colour_set[hypo[i]][0],colour_set[hypo[i]][1],colour_set[hypo[i]][2]);
  200.     }
  201.     // TODO czarne i biale
  202. }
  203. int verify_answer(int proposition , int solution) // odpowiedz gracza i dobre rozwiazanie zapisane jako czterocyfrowe l. naturalne
  204. {
  205.     int prop[4],
  206.         sol[4],
  207.         power = 1000,
  208.         answer = 0;
  209.  
  210.     for (int i = 0 ; i < 4 ; ++i)
  211.     {
  212.         prop[i] = (proposition - (proposition % power) ) / power;
  213.         sol[i] = (solution - (solution % power) ) / power;
  214.         power /= 10;
  215.         if (prop[i] == sol[i])
  216.         {
  217.             prop[i] = -1;
  218.             sol[i] = -1;
  219.             answer += 10;
  220.         }
  221.     }
  222.     for (int i = 0 ; i < 4 ; ++i) // czas wyznaczyc ilosc bialych kamykow
  223.     {
  224.         if (prop[i] > 0)
  225.         {
  226.             for (int j = 0 ; j < 4 ; ++j)
  227.             {
  228.                 if (prop[i] == sol[j])
  229.                 {
  230.                     prop[i] = -1;
  231.                     sol[j] = -1;
  232.                     answer += 1;
  233.                     j = 5;
  234.                 }
  235.             }
  236.         }
  237.     }
  238.     return answer; // dwucyfrowa liczba naturalna, 10*czarne + biale
  239. }
  240. void get_keyboard_events(bool additionals)
  241. {
  242.     if(SDL_PollEvent(&event)) // czy cos sie zdarzylo
  243.     {
  244.         if (event.type == SDL_KEYDOWN && additionals)
  245.         {
  246.             switch(event.key.keysym.sym)
  247.             {
  248.                 case SDLK_q: exit(0); break;
  249.                 case SDLK_a: selected_field = (selected_field - 1) % 4; break;
  250.                 case SDLK_d: selected_field = (selected_field + 1) % 4; break;
  251.                 case SDLK_1: chosen_colour[selected_field] = 1; break;
  252.                 case SDLK_2: chosen_colour[selected_field] = 2; break;
  253.                 case SDLK_3: chosen_colour[selected_field] = 3; break;
  254.                 case SDLK_4: chosen_colour[selected_field] = 4; break;
  255.                 case SDLK_5: chosen_colour[selected_field] = 5; break;
  256.                 case SDLK_6: chosen_colour[selected_field] = 6; break;
  257.                 case SDLK_SPACE: enforce_endturn = true; break;
  258.             }
  259.         }
  260.         else if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q) exit(0);
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement