Posted by Binary_Fire on Tue 13 May 21:19
report spam | download | new post
- //X si 0 versiunea veche
- #include<gl/gl.h>
- #include<gl/glut.h>
- #include<iostream>
- int arena[3][3];
- int winer=0,term=0;
- static float coord_x,coord_y;
- static int m_w,m_h;
- //declar font-ul folosit
- GLubyte space[] =
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- GLubyte letters[][13] = {
- {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18},
- {0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe},
- {0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e},
- {0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc},
- {0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff},
- {0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff},
- {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e},
- {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
- {0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e},
- {0x00, 0x00, 0x7c, 0xee, 0xc6, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06},
- {0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 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c3},
- {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3},
- {0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff}
- };
- GLuint fontOffset;
- void makeRasterFont(void)
- {
- GLuint i, j;
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- fontOffset = glGenLists (128);
- for (i = 0,j = 'A'; i < 26; i++,j++) {
- glNewList(fontOffset + j, GL_COMPILE);
- glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);
- glEndList();
- }
- glNewList(fontOffset + ' ', GL_COMPILE);
- glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
- glEndList();
- }
- void init(void)
- {
- glShadeModel (GL_FLAT);
- makeRasterFont();
- }
- void printString(char *s)
- {
- glPushAttrib (GL_LIST_BIT);
- glListBase(fontOffset);
- glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
- glPopAttrib ();
- }
- //terminarea functiei de afisare a textului
- int castigator() //verific daca exista un castigator
- {int i,j,i2,j2,linie,egal=1;
- for (i=0;i<=2;i++)
- for (j=0;j<=2;j++)
- {if (arena[i][j]==1)
- {linie=1;
- for (i2=0;i2<=2;i2++)
- if (arena[i2][j]!=1) linie=0;
- if (linie==1) winer=1;
- else {linie=1;
- for (j2=0;j2<=2;j2++)
- if (arena[i][j2]!=1) linie=0;
- if (linie==1) winer=1;
- else {linie=1;
- if (i==j)
- {j2=0;
- for (i2=0;i2<=2;i2++)
- {if (arena[i2][j2]!=1) linie=0;
- j2++;}}
- else if (i==(j+2) || j==(i+2))
- {j2=2;
- for (i2=0;i2<=2;i2++)
- {if (arena[i2][j2]!=1) linie=0;
- j2--;}}
- else linie=0;
- if (linie==1) winer=1;
- }
- }
- }
- else if (arena[i][j]==2)
- {linie=1;
- for (i2=0;i2<=2;i2++)
- if (arena[i2][j]!=2) linie=0;
- if (linie==1) winer=2;
- else {linie=1;
- for (j2=0;j2<=2;j2++)
- if (arena[i][j2]!=2) linie=0;
- if (linie==1) winer=2;
- else {linie=1;
- if (i==j)
- {j2=0;
- for (i2=0;i2<=2;i2++)
- {if (arena[i2][j2]!=2) linie=0;
- j2++;}}
- else if (i==(j+2) || j==(i+2))
- {j2=2;
- for (i2=0;i2<=2;i2++)
- {if (arena[i2][j2]!=2) linie=0;
- j2--;}}
- else linie=0;
- if (linie==1) winer=2;
- }
- }
- }
- if (arena[i][j]==0 || winer!=0) egal=0;
- }
- if (egal) winer=3;
- return winer;
- }
- void display_arena(void)
- {int i,j;
- float c_x,c_y,c_z,c_t;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glRasterPos2f(0,9.5);
- printString("PRESS ESCAPE TO EXIT");
- glColor3f(0.0,1.0,0.0);
- glBegin(GL_LINE_LOOP);
- glVertex2i(2,2);
- glVertex2i(2,8);
- glVertex2i(8,8);
- glVertex2i(8,2);
- glEnd();
- glBegin(GL_LINES);
- glVertex2i(2,4);
- glVertex2i(8,4);
- glVertex2i(2,6);
- glVertex2i(8,6);
- glVertex2i(4,8);
- glVertex2i(4,2);
- glVertex2i(6,8);
- glVertex2i(6,2);
- glEnd();
- glBegin(GL_LINES);
- for (i=2;i>=0;i--)
- for (j=0;j<=2;j++)
- {if (j==0) c_x=2.5;
- else if (j==1) c_x=4.5;
- else c_x=6.5;
- if (i==0) c_y=2.5;
- else if (i==1) c_y=4.5;
- else c_y=6.5;
- c_z=c_x+1;
- c_t=c_y+1;
- if (arena[i][j]==1) {glColor3f(1.0,0.0,0.0); //desenez mutarea user-ului
- glVertex2f(c_x,c_y);
- glVertex2f(c_z,c_t);
- glVertex2f(c_x,c_t);
- glVertex2f(c_z,c_y);
- }
- else
- if (arena[i][j]==2) {glColor3f(1.0,1.0,1.0); //desenez mutarea pc-ului
- glVertex2f(c_x,c_y);
- glVertex2f(c_x,c_t);
- glVertex2f(c_x,c_t);
- glVertex2f(c_z,c_t);
- glVertex2f(c_z,c_t);
- glVertex2f(c_z,c_y);
- glVertex2f(c_z,c_y);
- glVertex2f(c_x,c_y);
- }
- }
- glEnd();
- glColor3f(0.6,1.0,1.0);
- if (winer==1) {glRasterPos2i(4,1);
- printString("YOU WON");
- glRasterPos2f(1.3,0.5);
- printString("PRESS Y FOR ANOTHER GAME");
- term=1;}
- else if (winer==2) {glRasterPos2i(4,1);
- printString("YOU LOSE");
- glRasterPos2f(1.3,0.5);
- printString("PRESS Y FOR ANOTHER GAME");
- term=1;}
- else if (winer==3) {glRasterPos2i(4,1);
- printString("DRAW");
- glRasterPos2f(1.3,0.5);
- printString("PRESS Y FOR ANOTHER GAME");
- term=1;}
- glFlush();
- }
- void resize(int w, int h)
- {glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- m_w=w;
- m_h=h;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int cautare_mutare(int seeker,int i,int j)
- {int nr_el,loc_liber,i2,j2,mutare=0;
- nr_el=0;loc_liber=-1;
- for (i2=0;i2<=2;i2++)
- if (arena[i2][j]==seeker) nr_el++;
- else if(arena[i2][j]==0) loc_liber=i2;
- if (nr_el==2 && loc_liber!=-1 && mutare==0) {arena[loc_liber][j]=2;mutare=1;}
- if (mutare==0)
- {nr_el=0;loc_liber=-1;
- for (j2=0;j2<=2;j2++)
- if (arena[i][j2]==seeker) nr_el++;
- else if(arena[i][j2]==0) loc_liber=j2;
- }
- if (nr_el==2 && loc_liber!=-1 && mutare==0) {arena[i][loc_liber]=2;mutare=1;}
- if (mutare==0 && i==j)
- {nr_el=0;loc_liber=-1;j2=0;
- for (i2=0;i2<=2;i2++)
- {if (arena[i2][j2]==seeker) nr_el++;
- else if (arena[i2][j2]==0) loc_liber=i2;
- j2++;}
- if (nr_el==2 && loc_liber!=-1 && mutare==0) {arena[loc_liber][loc_liber]=2;
- mutare=1;}
- }
- if (mutare==0 && (i==j+2 || j==i+2))
- {nr_el=0;loc_liber=-1;j2=2;
- for (i2=0;i2<=2;i2++)
- {if (arena[i2][j2]==seeker) nr_el++;
- else if (arena[i2][j2]==0) loc_liber=i2;
- j2--;}
- if (nr_el==2 && loc_liber!=-1 && mutare==0)
- {if (loc_liber==0) arena[loc_liber][2]=2;
- else if (loc_liber==1) arena[loc_liber][1]=2;
- else arena[loc_liber][0]=2;
- mutare=1;}
- }
- return mutare;
- }
- void pc_move() //muta pc-ul
- {int i,j,mutare=0,i_lib,j_lib;
- for (i=0;i<=2;i++)
- for (j=0;j<=2;j++)
- if (arena[i][j]==2 && mutare==0) mutare=cautare_mutare(2,i,j);
- if (mutare==0)
- for (i=0;i<=2;i++)
- for (j=0;j<=2;j++)
- if (arena[i][j]==1 && mutare==0) mutare=cautare_mutare(1,i,j);
- else if(arena[i][j]==0) {i_lib=i;j_lib=j;}
- if (mutare==0) arena[i_lib][j_lib]=2;
- }
- void keyboard(unsigned char key,int x,int y)
- {int i,j;
- if (key==27) exit(0);
- else if (key=='y' && winer!=0)
- {for (i=0;i<=2;i++)
- for (j=0;j<=2;j++)
- arena[i][j]=0;
- winer=0;term=0;
- glutPostRedisplay();}
- }
- void mouse (int button,int state,int x,int y)
- {int i,j;
- if (button==GLUT_LEFT_BUTTON)
- if (state==GLUT_DOWN ) //voi transforma coord x si y (a pointerului)
- {coord_x=((float)x*100/m_w)*10/100; //in sist. ortogonal definit de mine (10x10)
- coord_y=10-((float)y*100/m_h)*10/100; //folosesc float pentru a avea o precizie mare
- if (coord_x>2 && coord_x<8 && coord_y>2 && coord_y<8)
- {if (coord_x<4) j=0;
- else if (coord_x>4 && coord_x<6) j=1;
- else j=2;
- if (coord_y<4) i=0;
- else if (coord_y>4 && coord_y<6) i=1;
- else i=2;
- if (arena[i][j]==0) {arena[i][j]=1;castigator();
- if (winer==0) pc_move();
- castigator();}
- if (winer==0 || term==0) glutPostRedisplay();
- }
- }
- }
- int main(int argc,char** argv)
- {int i,j;
- for (i=0;i<=2;i++)
- for (j=0;j<=2;j++)
- arena[i][j]=0;
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(300,300);
- glutInitWindowPosition(350,350);
- glutCreateWindow("Tic-Tac-Toe");
- init();
- glutReshapeFunc(resize);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glutDisplayFunc(display_arena); //desenez terenul
- glutMainLoop();
- return 0;
- }
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.