Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/freeglut.h>
- #include <math.h>
- #define PI 3.1415927
- //-------------- VARIAVEIS GLOBAIS -------------------------
- float _angle = 45;
- float i,j, selX=0, selY=0; //selX e selY = coordenadas do "cursor"
- int k=0;
- int board[8][8];
- int player = 1; //1 = vermelhas 2= azuis
- int pickInProgress = 0;
- float atualX, atualy;
- int pickingX, pickingY;
- GLuint _displayListId_blackArea; //The OpenGL id of the display list
- GLuint _displayListId_whiteArea; //The OpenGL id of the display list
- GLuint _displayListId_redArea;
- GLuint _displayListId_blueArea;
- GLuint _displayListId_clearArea;
- GLuint _displayListId_whitePiece;
- GLuint _displayListId_blackPiece;
- //--------------- PROTOTIPOS DE FUNCOES ---------------------
- void getCoord();
- void checkLimits();
- void drawPieces();
- void drawMatrix();
- void drawScene();
- void showBoard();
- int checkPecas();
- int movValido(int jogador, int xDestino, int yDestino);
- int destinoValido( int x, int y, int destX, int destY);
- void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B);
- void movePeca(int origemX, int origemY, int destX, int destY);
- void comePeca(int origemX, int origemY, int destX, int destY);
- //Compilar: gcc -o chess chess.c -lglut -lGLU -lGL -lm
- void handleResize(int w,int h)
- {
- glViewport(0,0,w,h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(65.0,(double)w/(double)h,1.0,200);
- //gluLookAt(0.0f,5.5f, 15.0f, 0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f);
- gluLookAt(0.0,13 ,10.0,0.0,0.0,0.0,0.0,0.0,-1.0);
- }
- void draw_BlackArea()
- {
- // glPushMatrix();
- //glTranslatef(1.5f,0.0f,0.0f);
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glTranslatef(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.3f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(1.5f,0.0f,0.0f);
- glVertex3f(1.5f,0.0f,-1.5f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(1.5f,0.3f,0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(0.0f,0.0f,-1.5f);
- glVertex3f(1.50f,0.0f,-1.5f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.0f,-1.5f);
- glVertex3f(0.0f,0.0f,-1.5f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glVertex3f(1.50f,0.3f,0.0f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glEnd();
- // glPopMatrix();
- }
- void draw_whiteArea()
- {
- //glPushMatrix();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glTranslatef(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.3f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(1.5f,0.0f,0.0f);
- glVertex3f(1.5f,0.0f,-1.5f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(1.5f,0.3f,0.0f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(0.0f,0.0f,-1.5f);
- glVertex3f(1.50f,0.0f,-1.5f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.05f,0.05f,0.05f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.0f,-1.5f);
- glVertex3f(0.0f,0.0f,-1.5f);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0f,1.0f,1.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glVertex3f(1.50f,0.3f,0.0f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glEnd();
- // glPopMatrix();
- }
- void draw_redArea()
- {
- glBegin(GL_QUADS);
- glColor3f(0.7f,0.0f,0.0f);
- glTranslatef(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.3f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glTranslatef(0.001+selX, 0.001+selY, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.7f,0.0f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glVertex3f(1.50f,0.3f,0.f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glTranslatef(0.001+selX, 0.001+selY, 0.0);
- glEnd();
- }
- void draw_clearArea()
- {
- glBegin(GL_QUADS);
- glColor3f(0.0f,0.7f,0.0f);
- glTranslatef(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.3f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glTranslatef(0.001+selX, 0.001+selY, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.f,0.7f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glVertex3f(1.50f,0.3f,0.f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glTranslatef(0.001+selX, 0.001+selY, 0.0);
- glEnd();
- }
- void draw_blueArea()
- {
- glBegin(GL_QUADS);
- glColor3f(0.0f,0.0f,0.7f);
- glTranslatef(0.0f,0.0f,0.0f);
- glVertex3f(0.0f,0.0f,0.0f);
- glVertex3f(1.50f,0.0f,0.0f);
- glVertex3f(1.5f,0.3f,0.0f);
- glVertex3f(0.0f,0.3f,0.0f);
- glTranslatef(0.002+selX, 0.002+selY, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.0f,0.0f,0.7f);
- glVertex3f(0.0f,0.3f,0.0f);
- glVertex3f(1.50f,0.3f,0.f);
- glVertex3f(1.5f,0.3f,-1.5f);
- glVertex3f(0.0f,0.3f,-1.5f);
- glTranslatef(0.002+selX, 0.002+selY, 0.0);
- glEnd();
- }
- void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B)
- {
- GLfloat x = 0.0;
- GLfloat y = 0.0;
- GLfloat angle = 0.0;
- GLfloat angle_stepsize = 0.1;
- //Tubo do cilindro
- glColor3ub(0.6*R,0.6*G,0.6*B);
- glBegin(GL_QUAD_STRIP);
- angle = 0.0;
- while( angle <= 2*PI ) {
- x = radius * cos(angle);
- y = radius * sin(angle);
- glVertex3f(x, y , 1);
- glVertex3f(x, y , 0.0);
- angle = angle + angle_stepsize;
- }
- glVertex3f(radius, 0.0, 1);
- glVertex3f(radius, 0.0, 0.0);
- glEnd();
- //Circulo do topo
- glColor3ub(R,G,B);
- glBegin(GL_POLYGON);
- angle = 0.0;
- while( angle <= 2*PI ) {
- x = radius * cos(angle);
- y = radius * sin(angle);
- glVertex3f(x, y , 1);
- angle = angle + angle_stepsize;
- }
- glVertex3f(radius, 0.0, height);
- glEnd();
- }
- void initRendering()
- {
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_COLOR_MATERIAL);
- glClearColor(0.0f,0.0f,0.2f,1.0f);
- _displayListId_blackArea = glGenLists(1); //Make room for the display list
- glNewList(_displayListId_blackArea, GL_COMPILE); //Begin the display list
- draw_BlackArea(); //Add commands for drawing a black area to the display list
- glEndList(); //End the display list
- //Set up a display list for drawing a cube
- _displayListId_whiteArea = glGenLists(2); //Make room for the display list
- glNewList(_displayListId_whiteArea, GL_COMPILE); //Begin the display list
- draw_whiteArea(); //Add commands for drawing a black to the display list
- glEndList(); //End the display list
- _displayListId_redArea = glGenLists(3); //Make room for the display list
- glNewList(_displayListId_redArea, GL_COMPILE); //Begin the display list
- draw_redArea(); //Add commands for drawing a black to the display list
- glEndList(); //End the display list
- _displayListId_clearArea = glGenLists(5); //Make room for the display list
- glNewList(_displayListId_clearArea, GL_COMPILE); //Begin the display list
- draw_clearArea(); //Add commands for drawing a black to the display list
- glEndList(); //End the display list
- _displayListId_blueArea = glGenLists(5); //Make room for the display list
- glNewList(_displayListId_blueArea, GL_COMPILE); //Begin the display list
- draw_blueArea(); //Add commands for drawing a black to the display list
- glEndList(); //End the display list
- _displayListId_whitePiece = glGenLists(4); //Make room for the display list
- glNewList(_displayListId_whitePiece, GL_COMPILE); //Begin the display list
- glTranslatef(0.75,0.0,-0.75);
- glRotatef(-90, 1.0, 0.0, 0.0);
- draw_cylinder(0.5, 0.0, 255, 1, 1); //Add commands for drawing a black to the display list
- glEndList(); //End the display list
- _displayListId_blackPiece = glGenLists(4); //Make room for the display list
- glNewList(_displayListId_blackPiece, GL_COMPILE); //Begin the display list
- glTranslatef(0.75,0.0,-0.75);
- glRotatef(-90, 1.0, 0.0, 0.0);
- draw_cylinder(0.5, 0.0, 29, 66, 214); //Add commands for drawing a black to the display list
- glEndList(); //End the display list
- }
- void showBoard(){ //cenas da matriz
- int a=0, i=0;
- for (a = 0; a < 8 ; a++){
- for(i = 0;i < 8; i++){
- printf(" %d ", board[a][i]);
- }
- printf(" \n");
- }
- }
- void drawPieces(){ //cenas da matriz
- int a=0, i=0; // a = linhas; i = colunas
- // 1 = brancas; 2 = pretas
- for( a = 0; a < 8; a++){ // cria a matriz "em branco"
- for(i=0; i < 8; i++){
- board[a][i]= 0;
- }
- }
- // ----------------- coloca as brancas na matriz
- for( a = 1; a < 3; a += 2){ //fila 2
- for(i=0; i < 8; i += 2){
- board[a][i]= 1;
- glPushMatrix();
- glCallList(_displayListId_whitePiece);
- glRotated(_angle, 1.0, 0.0, 0.0);
- glPopMatrix();
- glutSwapBuffers();
- glutDisplayFunc(drawScene);
- //glRotated(_angle, 1.0, 0.0, 0.0);
- //glPopMatrix();
- }
- }
- for( a = 0; a < 3; a += 2){ //fila 1 e 3
- for(i=1; i < 8; i += 2){
- board[a][i]= 1;
- }
- }
- // ----------------- coloca as pretas na matriz
- for( a = 5; a < 8; a += 2){ // 6 e 8 filas
- for(i=0; i < 8; i += 2){
- board[a][i]= 2;
- }
- }
- for( a = 6; a < 8; a += 2){ // 7 fila
- for(i=1; i < 8; i += 2){
- board[a][i]= 2;
- }
- }
- //chamar a funcao para fazer a primeira leitura da matriz aqui
- drawMatrix();
- }
- void drawMatrix(){ //nao faz nada
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.75,0.0,-0.75);
- glRotatef(-90, 1.0, 0.0, 0.0);
- draw_cylinder(0.5, 0.0, 255, 1, 1); //Add commands for drawing a black to the display list
- for(j=0.0;j>(-8*1.5);j-=1.5)
- {
- k++;
- for(i=0.0;i<(4*3.0);i+=3.0)
- {
- if(k%2==0) //Se for par
- {
- glPushMatrix();
- glTranslatef(i,0.0,j);
- glCallList(_displayListId_blackArea);
- glPopMatrix();
- }
- else //Se for impar
- {
- glPushMatrix();
- glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
- glCallList(_displayListId_blackArea);
- glPopMatrix();
- }
- }
- }
- for(float j=0.0;j>(-8*1.5);j-=1.5)
- {
- k++;
- for(i=0.0;i<(4*3.0);i+=3.0)
- {
- if(k%2!=0) //Se for impar
- {
- glPushMatrix();
- glTranslatef(i,0.0,j);
- glCallList(_displayListId_whiteArea);
- glPopMatrix();
- }
- else //Se for par
- {
- glPushMatrix();
- glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
- glCallList(_displayListId_whiteArea);
- glPopMatrix();
- }
- }
- }
- showBoard();
- /*
- int linha = 0, coluna = 0;
- for(linha = 0; linha < 8; linha ++){
- for(coluna = 0; coluna < 8; coluna ++){
- if(board[linha][coluna] == 1){
- printf(" encountrou uns \n");
- }
- }
- }*/
- }
- void drawScene(){
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(-90, 0.0f, 1.0f, 0.0f); //rodar automaticamente com o angulo: -_angle
- glTranslatef(-4*1.5, 0.0, 4*1.5);
- for(j=0.0;j>(-8*1.5);j-=1.5)
- {
- k++;
- for(i=0.0;i<(4*3.0);i+=3.0)
- {
- if(k%2==0) //Se for par
- {
- glPushMatrix();
- glTranslatef(i,0.0,j);
- glCallList(_displayListId_blackArea);
- glPopMatrix();
- }
- else //Se for impar
- {
- glPushMatrix();
- glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
- glCallList(_displayListId_blackArea);
- glPopMatrix();
- }
- }
- }
- for(float j=0.0;j>(-8*1.5);j-=1.5)
- {
- k++;
- for(i=0.0;i<(4*3.0);i+=3.0)
- {
- if(k%2!=0) //Se for impar
- {
- glPushMatrix();
- glTranslatef(i,0.0,j);
- glCallList(_displayListId_whiteArea);
- glPopMatrix();
- }
- else //Se for par
- {
- glPushMatrix();
- glTranslatef(i+1.5,0.0,j); //Espaçamento de 1.5 entre cada peça
- glCallList(_displayListId_whiteArea);
- glPopMatrix();
- }
- }
- }
- if(player == 1 && pickInProgress == 0){
- glPushMatrix();
- if(selX <0){
- selX = 0;
- }
- if(selX > 10.5){
- selX =10.5;
- }
- if(selY > 0){
- selY =0;
- }
- if(selY < -10.5){
- selY =-10.5;
- }
- glTranslated(0.001+selX, 0.001, 0.001+selY);
- glCallList(_displayListId_redArea);
- glPopMatrix();
- }
- if (player == 1 && pickInProgress == 1){
- glPushMatrix();
- if(selX <0){
- selX = 0;
- }
- if(selX > 10.5){
- selX =10.5;
- }
- if(selY > 0){
- selY =0;
- }
- if(selY < -10.5){
- selY =-10.5;
- }
- glColor3f(0.6f,0.3f,0.7f);
- glTranslated(0.001+selX, 0.001, 0.001+selY);
- glCallList(_displayListId_clearArea);
- glPopMatrix();
- }
- if (player == 2 && pickInProgress == 0){
- glPushMatrix();
- if(selX <0){
- selX = 0;
- }
- if(selX > 10.5){
- selX =10.5;
- }
- if(selY > 0){
- selY =0;
- }
- if(selY < -10.5){
- selY =-10.5;
- }
- glColor3f(0.6f,0.3f,0.7f);
- glTranslated(0.001+selX, 0.001, 0.001+selY);
- glCallList(_displayListId_blueArea);
- glPopMatrix();
- }
- if (player == 2 && pickInProgress == 1){
- glPushMatrix();
- if(selX <0){
- selX = 0;
- }
- if(selX > 10.5){
- selX =10.5;
- }
- if(selY > 0){
- selY =0;
- }
- if(selY < -10.5){
- selY =-10.5;
- }
- glTranslated(0.001+selX, 0.001, 0.001+selY);
- glCallList(_displayListId_clearArea);
- glPopMatrix();
- }
- //-------- pecas brancas
- int a = 0, i = 0;
- float offX = 0, offY = 0; //off y coloca nas co
- for (a = 0; a < 8 ; a++){
- for(i = 0;i < 8; i++){
- if(board[a][i] == 1 ){
- glPushMatrix();
- //glRotated(_angle, 1.0, 0.0, 0.0);
- glTranslated(offX, 0, offY);
- glCallList(_displayListId_whitePiece);
- glPopMatrix();
- }
- offY -= 1.5;
- }
- offY = 0;
- offX += 1.5;
- }
- //-------- ciclo para as pretas
- offX = 0;
- offY = 0;
- for (a = 0; a < 8 ; a++){
- for(i = 0;i < 8; i++){
- if(board[a][i] == 2 ){
- glPushMatrix();
- //glRotated(_angle, 1.0, 0.0, 0.0);
- glTranslated(offX, 0, offY);
- glCallList(_displayListId_blackPiece);
- glPopMatrix();
- }
- offY -= 1.5;
- }
- offY = 0;
- offX += 1.5;
- }
- glutSwapBuffers();
- glFlush();
- }
- int checkPecas(){
- int a=0, i=0;
- for (a = 0; a < 8 ; a++){
- for(i = 0;i < 8; i++){
- if(board[a][i] == 1 ){
- return 1;
- }
- if(board[a][i] == 2){
- return 2;
- }
- }
- }
- return 0;
- }
- void update(int value)
- {
- _angle += 1.0f;
- if (_angle > 360)
- {
- _angle -= 360;
- }
- glutPostRedisplay();
- glutTimerFunc(25, update, 0);
- }
- void handleKeypress(unsigned char key,int x,int y)
- {
- switch(key)
- {
- case('d'):
- glPushMatrix();
- //selX -= 1.5;
- selY -= 1.5;
- glCallList(_displayListId_redArea);
- glPopMatrix();
- //getCoord();
- glutSwapBuffers();
- glutDisplayFunc(drawScene);
- break;
- case('a'):
- glPushMatrix();
- //selX -= 1.5;
- selY += 1.5;
- glCallList(_displayListId_redArea);
- glPopMatrix();
- //getCoord();
- glutSwapBuffers();
- glutDisplayFunc(drawScene);
- break;
- case('w'):
- glPushMatrix();
- selX -= 1.5;
- //selY += 1.5;
- glCallList(_displayListId_redArea);
- glPopMatrix();
- //getCoord();
- glutSwapBuffers();
- glutDisplayFunc(drawScene);
- break;
- case('s'):
- glPushMatrix();
- selX += 1.5;
- //selY += 1.5;
- glCallList(_displayListId_redArea);
- glPopMatrix();
- getCoord();
- glutSwapBuffers();
- glutDisplayFunc(drawScene);
- break;
- case('q'):
- checkLimits();
- atualX = selX / 1.5;
- atualy = fabsf (selY/1.5);
- printf("%d -- Posicao de X Selecionada: %f \n Posicao de Y Selecionada: %f \n",player, atualX, atualy);
- if(pickInProgress == 0){
- if(movValido(player, (int) abs(atualX), (int) abs(atualy)) == 1){
- printf("Peca do tipo certa encontrada \n");
- pickInProgress = 1;
- pickingX = (int) abs(atualX); //variaveis temporarias por causa da selecao
- pickingY = (int) abs(atualy);
- //fazer aqui a mudanca de cor
- break;
- }
- }
- if(pickInProgress == 1){
- printf("escolha em curso para o jogador %d \n", player);
- if(destinoValido(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy)) == 1){
- printf("Destino valido!\n");
- movePeca(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy));
- pickInProgress = 0;
- }
- if(destinoValido(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy)) == 2){
- printf("Destino válido - comer Peca \n");
- comePeca(pickingX, pickingY,(int) abs(atualX), (int) abs(atualy));
- pickInProgress = 0;
- }else{
- printf("destino invalido i dk \n");
- pickInProgress = 0;
- }
- }
- printf("rip \n");
- break;
- case('e'):
- break;
- case 27:
- exit(0);
- }
- }
- void comePeca(int origemX, int origemY, int destX, int destY){
- int offX, offY;
- offX = destX-origemX;
- offY = destY-origemY;
- board[destX+offX][destY+offY] = board[origemX][origemY];
- board[origemX][origemY] = 0;
- board[destX][destY] = 0;
- if (player == 1){
- printf("esta no jogador %d \n", player);
- player = 2;
- return;
- }
- if (player == 2){
- printf("esta no jogador %d \n", player);
- player = 1;
- return;
- }
- }
- int movValido(int jogador, int xDestino, int yDestino){ //retorna 1 se se a peca selecionada é do jogador atual
- printf("X recebido = %d \n Y recebido = %d \n", xDestino, yDestino);
- if(board[xDestino][yDestino] == jogador){
- return 1;
- }
- return 0;
- }
- int destinoValido( int x, int y, int destX, int destY){
- int jogador = board[x][y];
- printf("--------\n ");
- printf("X origem: %d \n Y origem: %d\n", x, y);
- printf("X destino: %d \n Y destino: %d \n", destX, destY);
- printf("--------\n ");
- if(player == 1 ){
- if(destX == x + 1 && destY == y + 1){
- if(board[destX][destY] == 0){
- return 1;
- }
- if(board[destX][destY] == 2){
- if(board[destX+1][destY+1] == 0 && (destX+1)<8 && (destY+1)<8 ){
- return 2;
- }
- }
- }
- if(destX == x + 1 && destY == y - 1){
- if(board[destX][destY] == 0)
- return 1;
- if(board[destX][destY] == 2){
- if(board[destX+1][destY-1] == 0 && (destX+1)<8 && (destY-1)>=0){
- return 2;
- }
- }
- }
- }
- if(player == 2){
- if(destX == x - 1 && destY == y - 1){
- if(board[destX][destY] == 0)
- return 1;
- if(board[destX][destY] == 1){
- if(board[destX-1][destY-1] == 0 && (destX-1)>=0 && (destY-1)>=0 ){
- return 2;
- }
- }
- }
- if(destX == x - 1 && destY == y + 1){
- if(board[destX][destY] == 0)
- return 1;
- if(board[destX][destY] == 1){
- if(board[destX-1][destY+1] == 0 && (destX-1)>=0 && (destY+1)<8 ){
- return 2;
- }
- }
- }
- }
- return 0;
- }
- void movePeca(int origemX, int origemY, int destX, int destY){
- board[destX][destY] = board[origemX][origemY];
- board[origemX][origemY] = 0;
- if (player == 1){
- printf("esta no jogador %d \n", player);
- player = 2;
- return;
- }
- if (player == 2){
- printf("esta no jogador %d \n", player);
- player = 1;
- return;
- }
- }
- int comeSequencia(int origemX, int origemY, int destX, int destY){
- int counter = 0;
- int offX, offY;
- offX = destX-origemX;
- offY = destY-origemY;
- return 1;
- }
- void getCoord(){
- checkLimits();
- atualX = selX / 1.5;
- atualy = selY / 1.5;
- printf("Posicao de X: %f \n Posicao de Y: %f \n", atualX, fabsf(atualy));
- }
- void checkLimits(){ //limita as coordenadas
- if(selX > 10.5){
- selX = 10.5;
- }
- if(selX < 0){
- selX = 0;
- }
- if(selY < -10.5){
- selY = -10.5;
- }
- if(selY > 0){
- selY = 0;
- }
- }
- int main(int argc,char** argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
- glutInitWindowSize(800,800);
- glutCreateWindow("ChessBoard");
- initRendering();
- //glutFullScreen();
- glutDisplayFunc(drawScene);
- drawPieces();
- glutKeyboardFunc(handleKeypress);
- glutReshapeFunc(handleResize);
- glutTimerFunc(25, update, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement