|
{:soso_e113:}
本来打算就看看的,结果好奇之下就做了一个连连看,算法十分简陋,请算法厉害的帮忙改进。指出错误。
在此感谢那些为我免费纠错的朋友~
此版本为0.01版本,含有大量位置bug.
但不影响演示。
[pre lang="processing" line="1"]
PImage []img;
int [][]map;
int []testMap;
int []testMap2;
Point []CanConnectA;
Point []CanConnectB;
Point []CanConnectC;
Point []CanConnectW;
CPoint point;
int buttonMouse=0;
int x1, y1, x2, y2;
String temp;
boolean GameWin;
class Point
{
int x;
int y;
}
class CPoint
{
int cx;//Screen coordinates
int cy;//Screen coordinates
int x;//Logical coordinates
int y;//Logical coordinates
CPoint()
{
cx=0;
cy=0;
x=0;
y=0;
}
void ScreenToLogical(int cx, int cy)
{
if (cx<600&&cy<400)
{
x=cx/50;
y=cy/50;
}
}
}
////////////////////////////////////////////////////////////setup
void setup()
{
size(640, 400);
background(0);
textFont(createFont("MicrosoftYaHei-80", 10));
point =new CPoint();
CanConnectA=new Point[100];
CanConnectB=new Point[100];
CanConnectC=new Point[100];
CanConnectW=new Point[100];
for (int i=0;i<100;i++)
{
CanConnectA=new Point();
CanConnectB=new Point();
CanConnectC=new Point();
CanConnectW=new Point();
}
Arrary_Null(CanConnectA);
Arrary_Null(CanConnectB);
Arrary_Null(CanConnectC);
Arrary_Null(CanConnectW);
x1=0;
y1=0;
x2=0;
y2=0;
img=new PImage[12];
for (int i=1;i<12;i++)
{
temp=i+".png";
img=loadImage(temp);
}
InitGame();
}
void draw()
{
DrawMap();
}
void InitGame()
{
while (true)
{
InitMap();
if (TestMapIsOk())
{
break;
}
}
}
void GameMain()
{
if (IsAWiner())
{
GameOver();
}
if (buttonMouse==1)
{
x1=point.x;
y1=point.y;
println(x1+","+y1+":"+map[y1][x1]);
}
if (buttonMouse==2)
{
x2=point.x;
y2=point.y;
println(x2+","+y2+":"+map[y2][x2]);
if (map[y1][x1]==map[y2][x2]&&!((x1==x2)&&(y1==y2))&&(IsCanConnect(x1, y1, x2, y2)))
{
DrawALine(x1, y1, x2, y2);
println(map[y1][x1]+":"+map[y2][x2]);
map[y1][x1]=0;
map[y2][x2]=0;
x1=0;
y1=0;
x2=0;
y2=0;
DrawMap();
background(0);
buttonMouse=0;
}
else
{
x1=0;
y1=0;
x2=0;
y2=0;
background(0);
buttonMouse=0;
}
}
}
void InitMap()
{
map = new int[8][12];
testMap=new int[12];
int temp=0;
boolean bottonRandom=false;
for (int i=0;i<8;i++)
{
for (int j=0;j<12;j++)
{
if (!bottonRandom)
{
temp=(int)random(1, 11);
}
else
{
temp++;
if (temp>11)
{
temp=1;
}
}
testMap[temp]++;
if (testMap[temp]>8)
{
map[j]=temp;
}
else
{
map[j]=(int)random(1, 11);
}
}
}
}
void DrawMap()
{
for (int i=0;i<8;i++)
{
for (int j=0;j<12;j++)
{
if (map[j]==0)
{
continue;
}
image(img[map[j]], 0+j*50, 0+i*50, 50, 50);
}
}
}
boolean TestMapIsOk()
{
testMap2=new int[12];
for (int i=0;i<8;i++)
{
for (int j=0;j<12;j++)
{
testMap2[map[j]]++;
}
}
for (int i=1;i<12;i++)
{
if (((testMap2)%2)!=0)
{
return false;
}
}
return true;
}
boolean IsCanConnect(int x1, int y1, int x2, int y2)
{
if (map[y1][x1]==map[y2][x2]&&map[y1][x1]!=0)
{
println("come in iscan");
FindAllCanConnect(x1, y1, CanConnectA);
for (int i=0;i<CanConnectA.length;i++)
{
FindAllCanConnect(CanConnectA.x, CanConnectA.y, CanConnectB);
}
for (int i=0;i<CanConnectB.length;i++)
{
FindAllCanConnect(CanConnectB.x, CanConnectB.y, CanConnectC);
}
for (int i=0;i<CanConnectW.length;i++)
{
if (CanConnectW.x==-1&&CanConnectW.y==-1)
{
break;
}
println("CanConnectWx:"+CanConnectW.x+"CanConnectWy:"+CanConnectW.y);
if (CanConnectW.x==x2&&CanConnectW.y==y2)
{
return true;
}
}
Arrary_Null(CanConnectA);
Arrary_Null(CanConnectB);
Arrary_Null(CanConnectC);
Arrary_Null(CanConnectW);
}
return false;
}
void DrawALine(int x1, int y1, int x2, int y2)
{
}
void mousePressed()
{
if (mouseButton==LEFT)
{
point.ScreenToLogical(mouseX, mouseY);
buttonMouse++;
if (buttonMouse<3&&buttonMouse>0)
{
GameMain();
fill(0, 0, 255);
rect(point.x*50, point.y*50, 50, 50);
DrawMap();
}
}
}
void FindAllCanConnect(int x, int y, Point []CanConnect)
{
if (x<0||y<0)
{
// println("x:"+x+"y:"+y);
}
else
{
println("come in FindAll");
for (int i=y-1;i>0;i--)//shang
{
if (map[x]==0)
{
Arrary_Add(x, i, CanConnect);
}
else
if (map[x]==map[y2][x2])
{
println("come in FindAll shang");
Arrary_Add(x, i, CanConnectW);
break;
}
else
{
break;
}
}
for (int i=y+1;i<8;i++)//xia
{
if (map[x]==0)
{
Arrary_Add(x, i, CanConnect);
}
else
if (map[x]==map[y2][x2])
{
println("come in FindAll xia");
Arrary_Add(x, i, CanConnectW);
break;
}
else
{
break;
}
}
for (int i=x-1;i>0;i--)//zuo
{
if (map[y]==0)
{
Arrary_Add(i, y, CanConnect);
}
else
if (map[y]==map[y2][x2])
{
println("come in FindAll zuo");
Arrary_Add(i, y, CanConnectW);
break;
}
else
{
break;
}
}
for (int i=x+1;i<12;i++)//you
{
if (map[y]==0)
{
Arrary_Add(i, y, CanConnect);
}
else
if (map[y]==map[y2][x2])
{
println("come in FindAll you"+map[y]+"--"+map[y2][x2]);
Arrary_Add(i, y, CanConnectW);
break;
}
else
{
break;
}
}
}
}
void Arrary_Add(int x, int y, Point []CanConnect)
{
int end=0;
for (int i=0;i<CanConnect.length;i++)
{
if (CanConnect.x==-1&&CanConnect.y==-1)
{
end=i;
break;
}
}
CanConnect[end].x=x;
CanConnect[end].y=y;
println("end:"+end+"CanConnect[end]"+CanConnect[end].x+" "+CanConnect[end].y);
}
void Arrary_Null(Point []CanConnect)
{
for (int i=0;i<CanConnect.length;i++)
{
CanConnect.x=-1;
CanConnect.y=-1;
}
}
boolean IsAWiner()
{
for (int i=0;i<8;i++)
{
for (int j=0;j<12;j++)
{
if (map[j]!=0)
{
return false;
}
}
}
return true;
}
void GameOver()
{
fill(200, 0, 100);
textSize(80);
text("You Win!", 160, 160);
fill(100, 200, 100);
rect(160, 200, 150, 80);
rect(330, 200, 150, 80);
fill(0, 0, 100);
textSize(60);
text("Again", 160, 260);
text(" Exit", 330, 260);
if (point.x>160&&point.x<310&&point.y>200&&point.y<280)
{
InitGame();
println("again");
}
if (point.x>330&&point.x<480&&point.y>200&&point.y<280)
{
println("exit");
exit();
}
}
[/code] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|