此代码将生成一个网格。 它将切换使用鼠标依次选择的2个相邻图块的颜色。 结果,相同的颜色块可以彼此相邻。 我想从网格中删除这些图块
#include<stdlib.h>
#include<glut.h>
#include<stdio.h>
#define maxx 11
#define maxy 11
#define dx 50
#define dy 50
typedef struct cell
{
GLfloat x0,y0,x1,y1;
int color;
}cellt;
cellt board[20][20];
GLfloat color[3][3]={{0,0,1},{0,1,0},{1,0,0}};
GLfloat x0=0,y0=0;
GLint i,j,stroke=0,pastx,pasty,nextx,nexty,swapthetwo=0;
void init()
{
int i=0,j=0;
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
glutPostRedisplay();
for(i=0;i<maxx;i++)
for(j=0;j<maxy;j++)
{
board[i][j].x0=x0+i*dx+1;
board[i][j].x1=x0+(i+1)*dx-1;
board[i][j].y0=y0+j*dy+1;
board[i][j].y1=y0+(j+1)*dy-1;
board[i][j].color=rand()%3;
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<maxx-1;i++)
{
for(j=0;j<maxy-1;j++)
{
glColor3fv(color[board[i][j].color]);
glBegin(GL_POLYGON);
glVertex2f(board[i][j].x0,board[i][j].y0);
glVertex2f(board[i][j].x0,board[i][j].y1);
glVertex2f(board[i][j].x1,board[i][j].y1);
glVertex2f(board[i][j].x1,board[i][j].y0);
glEnd();
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(board[i][j].x0,board[i][j].y0);
glVertex2f(board[i][j].x0,board[i][j].y1);
glVertex2f(board[i][j].x1,board[i][j].y1);
glVertex2f(board[i][j].x1,board[i][j].y0);
glEnd();
}
}
glFlush();
glutSwapBuffers();
}
void mouse(int btn,int status,int x,int y) //mouse interfacing
{
y=500-y;
if((stroke == 0) && (btn == GLUT_LEFT_BUTTON) && (status == GLUT_DOWN))
{
stroke=1;
pastx=x;
pasty=y;
printf("pastx=%d,%d\n",pastx/dx,pasty/dy);
}
if((stroke == 1) && (btn == GLUT_LEFT_BUTTON) && (status == GLUT_UP))
{
stroke=0;
nextx=x;
nexty=y;
printf("nextx=%d,%d\n",nextx/dx,nexty/dy);
if(abs(((pastx/dx)-(nextx/dx)) == 1) && ((pasty/dy) == (nexty/dy)))
swapthetwo=1;
if(abs(((pasty/dy)-(nexty/dy)) == 1) && ((pastx/dx) == (nextx/dx)))
swapthetwo=1;
}
}
void idle(void)
{
int temp;
if(swapthetwo)
{
swapthetwo=0;
temp=board[pastx/dx][pasty/dy].color;
board[pastx/dx][pasty/dy].color=board[nextx/dx][nexty/dy].color;
board[nextx/dx][nexty/dy].color=temp;
printf("swapped");
}
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("mesh");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMouseFunc(mouse);
init();
glutMainLoop();
return(0);
}
答案 0 :(得分:0)
我发布了另一个问题(关于同一个项目)here更具相关性。 通过侥幸,我在这个问题中看到了我的代码,你可能从我的博客中获取了这些代码。 您可能希望在我的回答后更新您的问题。您要问的问题是以下代码:
if(abs(((pastx/dx)-(nextx/dx)) == 1) && ((pasty/dy) == (nexty/dy)))
swapthetwo=1;
if(abs(((pasty/dy)-(nexty/dy)) == 1) && ((pastx/dx) == (nextx/dx)))
swapthetwo=1;
鼠标功能内部。在设置swap之前,先运行一个循环来检查该行或列是否具有相同的彩色图块。您应该在所有四个方向上循环两个图块。如果任何颜色是相同的交换,消失并向下移动。否则翻回来。 我已经定义了四个功能来消除这样的瓷砖:
void vanishup(int i,int j)
{
board[i][j].color=4;
board[i][j+1].color=4;
board[i][j+2].color=4;
whitecount+=3;
}
void vanishleft(int i,int j)
{
board[i][j].color=4;
board[i-1][j].color=4;
board[i-2][j].color=4;
whitecount+=3;
}
void vanishdown(int i,int j)
{
board[i][j].color=4;
board[i][j-1].color=4;
board[i][j-2].color=4;
whitecount+=3;
}
void vanishright(int i,int j)
{
board[i][j].color=4;
board[i+1][j].color=4;
board[i+2][j].color=4;
whitecount+=3;
}
然后在显示函数中,当条件合适时调用这些消失函数:
//detecting three colors in a row
if(!whitecount)
{
for(i=0;i<maxx-1;i++)
{
for(j=0;j<maxy-1;j++)
{
flag=0;
col=10;
for(k=0;k<3;k++)
{
if(k==0)
if(((j+k)<maxy-1))
col=board[i][j+k].color;
if(((j+k)<maxy-1))
{ if(board[i][j+k].color!=col)
{flag=1;
break;}
}
else flag=1;
}
if(!flag&&!vanishing)
vanishup(i,j);
//printf("vanish up\n");
flag=0;
col=10;
for(k=0;k<3;k++)
{
if(k==0)
if((j-k)>=0)
col=board[i][j-k].color;
if(((j-k)>=0))
{ if(board[i][j-k].color!=col)
{flag=1;
break;}
}
else flag=1;
}
if(!flag&&!vanishing)
vanishdown(i,j);
//printf("vanish down\n");
flag=0;
col=10;
for(k=0;k<3;k++)
{
if(k==0)
if(((i+k)<maxx-1))
col=board[i+k][j].color;
if(((i+k)<maxx-1))
{ if(board[i+k][j].color!=col)
{flag=1;
break;}
}
else flag=1;
}
if(!flag&&!vanishing)
vanishright(i,j);
//printf("vanish right\n");
flag=0;
col=10;
for(k=0;k<3;k++)
{
if(k==0)
if(((i-k)>=0)) col=board[i-k][j].color;
if(((i-k)>=0))
{ if(board[i-k][j].color!=col)
{flag=1;
break;}
} else flag=1;
}
if(!flag&&!vanishing)
vanishleft(i,j);
//printf("vanish left\n");
}
}
}
最后移动所有那些在其上方有一个白色块的块:
//move all blocks down if white spaces exist(some blocks vanished)
flag1=0;
for(i=0;i<maxx-1;i++)
{
for(j=0;j<maxy-1;j++)
{
if(board[i][j].color==4) //if any board color white
{
if((j+1)<(maxy-1)) // if any board position not above board
{ board[i][j].color=board[i][j+1].color;//move above color
board[i][j+1].color=4; // to below
}
else
{board[i][j].color=4;
whitecount--;
}
}
//else movecount--;
}
}
if(flag1) movecount=0;
cout<<movecount<<" "<<whitecount<<" ";
我必须同意所有的痛苦后仍然不满意,因为有几个错误。 1)如果任何三种颜色形成一条线甚至暂时它们都会向下移动,它们就会消失。 2)另一个问题是两条不同颜色的线条同时消失。