pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

binaryfire private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Binary_Fire on Thu 22 May 20:27
report abuse | download | new post

  1. //X si 0 21/05/2008
  2.  
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <GL/glut.h>
  6. #include <GL/gl.h>
  7. #include <cstring>
  8. #include <ctime>
  9.  
  10. typedef struct pozitie { int x,y,stare_castig; };
  11.  
  12. #define PCP 2   //COMPUTER PLAYER
  13. #define HUP 1   //HUMAN    PLAYER
  14.  
  15. #define CFG_FILE "config.cfg"
  16.  
  17. int mat[4][4];
  18. int step,flagStopPlay;
  19.  
  20. int CurrWindowWidth,CurrWindowHeight;
  21.  
  22. void printString(char*);
  23. void startGame();
  24. void initGame();
  25. void keyboardInput(unsigned char,int,int);
  26. void mouseInput(int,int,int,int);
  27. void drawMove(int,int,int);
  28. void renderArena();
  29. int see_winner(int);
  30. pozitie choosePC(int,int);
  31. void resizeScene(int,int);
  32.  
  33. int see_winner(int mat[][4])
  34. {
  35.         int i,j;
  36.  
  37.         //fixez o linie si ma uit sa vad daca e egala
  38.         for ( i = 1; i < 4; ++i )
  39.                 if ( mat[i][1] == mat[i][2] &&  mat[i][2] == mat[i][3] && mat[i][1] != 0 )
  40.                         return mat[i][1];
  41.  
  42.         //fixez o coloana si ma uit sa vad daca e egala
  43.         for ( i = 1; i < 4; ++i )
  44.                 if ( mat[1][i] == mat[2][i] && mat[2][i] == mat[3][i] && mat[1][i] != 0 )
  45.                         return mat[1][i];
  46.  
  47.         //ma uit pe diagonale
  48.         if ( mat[1][1] == mat[2][2] && mat[2][2] == mat[3][3] && mat[1][1] != 0 )
  49.                 return mat[1][1];
  50.         if ( mat[3][1] == mat[2][2] && mat[2][2] == mat[1][3] && mat[3][1] != 0 )
  51.                 return mat[3][1];
  52.  
  53.         //daca nu gasesc un castigator returnez 0
  54.         return 0;
  55. }
  56.  
  57. pozitie choosePC(int boardState[][4],int side,int start)
  58. {
  59.         int i,j,castigator;
  60.         int change_side[3] = { 0, 2, 1 };
  61.         int castig[4][4];
  62.         pozitie aux;
  63.         int dim,pozx[10],pozy[10];
  64.     int posMove;
  65.    
  66.         for ( i = 1; i <= 3; ++i )
  67.         for ( j = 1; j <= 3; ++j )
  68.                 if ( boardState[i][j] == 0 )
  69.                 {
  70.                         boardState[i][j] = side;        //am gasit o pozitie libera, o marchez cu jucatorul curent
  71.                        
  72.                         castigator = see_winner(boardState);    //ma uit sa vad daca nu cumva castiga deja
  73.                         if ( castigator == side )       
  74.                         {
  75.                                 //atunci marchez pozitia ca fiind pierzatoare pentru adversar
  76.                                 castig[i][j] = -1;
  77.                         }
  78.                         else
  79.                         {
  80.                                 //daca nu a fost gasit castigator atunci analizez acest subarbore si ii retin starea de castig
  81.                                 castig[i][j] = choosePC(boardState,change_side[side],0).stare_castig;
  82.                         }
  83.  
  84.                         boardState[i][j] = 0;   
  85.                 }
  86.                 else
  87.                         castig[i][j] = 5;       //am pus o valoare mica diferita de -1,0,1 ca sa nu aiba asa valori urate in ea
  88.  
  89.         //caut pozitiile acum
  90.         //daca am una pierzatoare pentru adversar va fi castigatoare pentru cel curent -> side
  91.         dim = 0;
  92.        
  93.         for ( i = 1; i <= 3; ++i )
  94.         for ( j = 1; j <= 3; ++j )
  95.                 if ( boardState[i][j] == 0 && castig[i][j] == -1 )
  96.                 {
  97.                         ++dim;
  98.                         pozx[dim] = i; pozy[dim] = j;
  99.                 }
  100.         if ( dim > 0 )
  101.         {
  102.                 //aleg random o pozitie
  103.                 posMove = rand()%dim + 1;
  104.                 aux.x = pozx[ posMove ];
  105.                 aux.y = pozy[ posMove ];
  106.                 aux.stare_castig = 1;
  107.                 return aux;
  108.         }
  109.  
  110.         //daca nu am gasit atunci caut o pozitie de remiza
  111.         dim = 0;
  112.         for ( i = 1; i <= 3; ++i )
  113.         for ( j = 1; j <= 3; ++j )
  114.                 if ( boardState[i][j] == 0 && castig[i][j] == 0 )
  115.                 {
  116.                         ++dim;
  117.                         pozx[dim] = i; pozy[dim] = j;
  118.                 }
  119.         if ( dim > 0 )
  120.         {
  121.                 //aleg random o pozitie
  122.                 posMove = rand()%dim + 1;
  123.                 aux.x = pozx[ posMove ];
  124.                 aux.y = pozy[ posMove ];
  125.                 aux.stare_castig = 0;
  126.                 return aux;
  127.         }
  128.  
  129.         //altfel pot alege doar o pozitie castigatoare pentru adversar in concluzie pierd eu [side]
  130.         for ( i = 1; i <= 3; ++i )
  131.         for ( j = 1; j <= 3; ++j )
  132.                 if ( boardState[i][j] == 0 )
  133.                 {
  134.                         ++dim;
  135.                         pozx[dim] = i; pozy[dim] = j;
  136.                 }
  137.         if ( dim > 0 )
  138.         {
  139.                 //aleg random o pozitie
  140.                 posMove = rand()%dim + 1;
  141.                 aux.x = pozx[ posMove ];
  142.                 aux.y = pozy[ posMove ];
  143.                 aux.stare_castig = -1;
  144.                 return aux;
  145.         }
  146.  
  147.         //daca nu am gasit nici o pozitie libera inseamna ca este remiza
  148.         aux.x = aux.y = 0;
  149.         aux.stare_castig = 0;
  150.        
  151.         return aux;
  152. }
  153.  
  154. void resizeScene(int w,int h)
  155. {
  156.         //folosesc functia "resizeScene" pentru a reinitializa fereastra in caz de
  157.         //schimbare a marimii
  158.  
  159.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  160.  
  161.         CurrWindowWidth = w;
  162.         CurrWindowHeight = h;
  163.  
  164.         glViewport(0, 0, w, h);
  165.  
  166.         glMatrixMode(GL_PROJECTION);
  167.  
  168.         //resetez totul
  169.         glLoadIdentity();
  170.        
  171.         glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
  172.  
  173.         renderArena();
  174. }
  175.  
  176. void init()
  177. {
  178.         glClearColor(0.0, 0.0, 0.0, 0.0);
  179.         glColor3f(0.0,0.0,1.0);
  180.  
  181.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  182.  
  183.         //initializez tipul matricei folosite pentru vizualizare
  184.         glMatrixMode(GL_PROJECTION);
  185.  
  186.         //initializez matricea
  187.         glLoadIdentity();
  188.  
  189.         //initializez sistemul ortogonal
  190.         glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
  191. }
  192.  
  193. void keyboardInput(unsigned char key, int x, int y)
  194. {
  195.         if ( key == 27 )        //este ESCAPE, ies
  196.                 exit(0);
  197.  
  198.         if ( key == 'm' || key == 'M' ) //cu "m" schimb cine incepe jocul
  199.         {
  200.                 char *dumi;
  201.                 int val;
  202.  
  203.                 if ( freopen(CFG_FILE,"r",stdin) != NULL )
  204.                 {
  205.                    scanf("%s%d",dumi,&val);
  206.                    fclose(stdin);
  207.         }
  208.        
  209.                 if ( freopen(CFG_FILE,"w",stdout) != NULL )
  210.                 {
  211.                    printf("%s %d\n",dumi,val^1);
  212.                    fclose(stdout);
  213.         }
  214.         else
  215.            printf("FISIERUL %s NU S-A PUTUT SCRIE\n",CFG_FILE);
  216.         }
  217.  
  218.         //cu "y" reinitializez jocul
  219.         if ( key == 'y' || key == 'Y' )
  220.                 init(), glutPostRedisplay(), startGame();
  221. }
  222.  
  223. void printString(char *txt)
  224. {
  225.         int i;
  226.  
  227.         glRasterPos2f(3.0,0.5);
  228.  
  229.         for ( i = 0; i < strlen(txt); ++i )
  230.         {
  231.                 //fontul definit de GLUT este ciudat ... nu am gasit nicicum o modalitate de a-i schimba culoarea
  232.                 glColor3f(0.0,1.0,0.0);
  233.                 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, txt[i]);
  234.         }
  235.  
  236.         glutSwapBuffers();     
  237. }
  238.  
  239. void mouseInput(int button, int state, int x, int y)
  240. {
  241.         int winnerc;
  242.         pozitie tmp;
  243.  
  244.         if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && !flagStopPlay )
  245.         {
  246.                 x = x / ( CurrWindowWidth  / 5 );
  247.                 y = ( CurrWindowHeight - y ) / ( CurrWindowHeight / 5 );
  248.  
  249.                 if ( mat[y][x] == 0 && x < 4 && y < 4 && x > 0 && y > 0 )
  250.                 {
  251.                         drawMove(x,y,1);
  252.                         mat[ y ][ x ] = 1;      //trebuie inversate coordonatele !!!
  253.                         ++step;
  254.  
  255.                         winnerc = see_winner(mat);
  256.                         if ( winnerc == 1 )
  257.                         {
  258.                                 flagStopPlay = 1;
  259.                                 printString("Ai castigat!");
  260.                                 return ;
  261.                         }
  262.  
  263.                         if ( step == 9 )
  264.                         {
  265.                                 printString("Remiza");
  266.                                 flagStopPlay = 1;
  267.                                 return ;
  268.                         }
  269.  
  270.             tmp = choosePC(mat,2,1);
  271.  
  272.                         drawMove(tmp.y,tmp.x,2);        //se inverseaza coordonatele matricei in spatiul ortogonal
  273.                         mat[ tmp.x ][ tmp.y ] = 2;
  274.                         ++step;
  275.  
  276.                         winnerc = see_winner(mat);
  277.                         if ( winnerc == 2 )
  278.                         {
  279.                                 flagStopPlay = 1;
  280.                                 printString("Ai pierdut!");
  281.                                 return ;
  282.                         }
  283.  
  284.                         if ( step == 9 )
  285.                         {
  286.                                 printString("Remiza");
  287.                                 flagStopPlay = 1;
  288.                                 return ;
  289.                         }
  290.                 }
  291.         }
  292. }
  293.  
  294. void renderArena()
  295. {
  296.         int i,j;
  297.  
  298.         glColor3f(0.0,1.0,0.0);
  299.        
  300. //desenez tabla de joc pentru X si 0
  301.         glBegin(GL_LINE_LOOP);
  302.                 glVertex2f(2.0,2.0);
  303.                 glVertex2f(2.0,8.0);
  304.                 glVertex2f(8.0,8.0);
  305.                 glVertex2f(8.0,2.0);
  306.         glEnd();       
  307.         glBegin(GL_LINE_LOOP);
  308.                 glVertex2f(2.0,4.0);
  309.                 glVertex2f(8.0,4.0);
  310.         glEnd();
  311.         glBegin(GL_LINE_LOOP);
  312.                 glVertex2f(2.0,6.0);
  313.                 glVertex2f(8.0,6.0);
  314.         glEnd();
  315.        
  316.         glBegin(GL_LINE_LOOP);
  317.                 glVertex2f(4.0,2.0);
  318.                 glVertex2f(4.0,8.0);
  319.         glEnd();
  320.         glBegin(GL_LINE_LOOP);
  321.                 glVertex2f(6.0,2.0);
  322.                 glVertex2f(6.0,8.0);
  323.         glEnd();
  324. //gata
  325.  
  326.         glutSwapBuffers();     
  327.  
  328. //desenez si mutarile folosite, in caz ca in timpul jocului este nevoie de o redesenare a tablei de joc
  329.         for ( i = 1; i < 4; ++i )
  330.         for ( j = 1; j < 4; ++j )
  331.                 if ( mat[i][j] )
  332.                         drawMove(j,i,mat[i][j]);
  333. }
  334.  
  335. //desenez mutarea
  336. void drawMove(int x,int y,int side)
  337. {
  338.         int centerx,centery;
  339.  
  340.         //centrez coordonatele pozitiei mutarii din matricea jucatorilor in sistemul meu ortogonal
  341.         centerx = x * 2 + 1;   
  342.         centery = y * 2 + 1;
  343.  
  344.         //computerul este la mutare, el este "0"
  345.         if ( side == PCP )
  346.         {
  347.                 //ii desenez mutarea
  348.                 glBegin(GL_LINE_LOOP);
  349.                         glColor3f(1.0,0.0,1.0);
  350.                         glVertex2f(centerx-0.5,centery+0.5);
  351.                         glVertex2f(centerx+0.5,centery+0.5);
  352.                         glVertex2f(centerx+0.5,centery-0.5);
  353.                         glVertex2f(centerx-0.5,centery-0.5);
  354.                 glEnd();
  355.         }
  356.         else
  357.         //desenez mutarea jucatorului, "X"
  358.         if ( side == HUP )
  359.         {
  360.                 glBegin(GL_LINE_LOOP);
  361.                         glColor3f(1.0,1.0,0.0);
  362.                         glVertex2f(centerx-0.5,centery+0.5);
  363.                         glVertex2f(centerx+0.5,centery-0.5);
  364.                 glEnd();
  365.                 glBegin(GL_LINE_LOOP);
  366.                         glColor3f(1.0,1.0,0.0);
  367.                         glVertex2f(centerx-0.5,centery-0.5);
  368.                         glVertex2f(centerx+0.5,centery+0.5);
  369.                 glEnd();
  370.         }
  371.  
  372.         glutSwapBuffers();
  373. }
  374.  
  375. void initGame()
  376. {
  377.         char dumi[10];
  378.         int opt;
  379.         pozitie tmp;
  380.  
  381.         step = 0;
  382.  
  383.         //initializez matricea jucatorilor, nimeni nu a plasat nimic
  384.         memset(mat,0,sizeof(mat));
  385.  
  386.         srand(time(0));
  387.  
  388.         //ma uit in fisierul de configuratie
  389.        
  390.     if ( freopen(CFG_FILE,"r",stdin) != NULL )
  391.     {
  392.            scanf("%s%d",&dumi,&opt);
  393.        fclose(stdin);
  394.     }
  395.     else
  396.     {
  397.         printf("FISIERUL DE CONFIGURATIE NU EXISTA\n");
  398.         if ( freopen(CFG_FILE,"w",stdout) != NULL )
  399.         {
  400.              printf("%s %d\n",CFG_FILE,rand()%2);
  401.              printf("FISIERUL A FOST CREAT");
  402.              fclose(stdout);    
  403.         }
  404.         else
  405.             printf("FISIERUL NU A PUTUT FI CREAT\n");
  406.     }  
  407.         //daca am ca parametru sa inceapa computerul prima data atunci il las sa isi faca el prima mutare
  408.         if ( strcmp(dumi,"pcStart") == 0 && opt == 1 )
  409.         {
  410.                 tmp = choosePC(mat,2,1);
  411.                 drawMove(tmp.y,tmp.x,2);
  412.                 mat[tmp.x][tmp.y] = 2;
  413.  
  414.                 step = 1;
  415.         }       
  416. }
  417.  
  418. void startGame()
  419. {
  420.         initGame();
  421.         flagStopPlay = 0;
  422. }
  423.  
  424. int main(int argc,char** argv)
  425. {
  426.         //initializez GLUT
  427.         glutInit(&argc,argv);
  428.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  429.         glutInitWindowSize(300,300);
  430.         glutInitWindowPosition(350,350);
  431.        
  432.         //creez fereastra
  433.         glutCreateWindow("X si 0");
  434.        
  435.         //initializez toate starile
  436.         init();
  437.  
  438.         glutReshapeFunc(resizeScene);
  439.         glutKeyboardFunc(keyboardInput);
  440.         glutMouseFunc(mouseInput);
  441.         glutDisplayFunc(renderArena);
  442.  
  443.         startGame();
  444.  
  445.         //incep main loop-ul, fara el nu se va afisa nimic
  446.         glutMainLoop();
  447.  
  448.         return 0;
  449. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me