`
IO_oI
  • 浏览: 22156 次
社区版块
存档分类
最新评论

连连看bug log

    博客分类:
  • Java
阅读更多

第一次出错
package kyodai;

import kyodai.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
//记录point在搜寻时的状态
class Dot {
static final int NO = 1, YES = 2, ALL = 3;
private Point p;
private int minCross = -1;
//被搜寻的状态
private int huntStatus = NO;
//相对前一个dot的方向
private int direction;
//记录每一个被创建的dot
private static ArrayList<Dot> list = new ArrayList<Dot>();
static int pathFlag = 0; //refactor
private Dot(Point p, int direction) {
this.p = p;
this.direction = direction;
list.add(this);
}
static Dot getDot(Point p, int direction) {
if(p==null)
return null;
return getDot((int)p.getX(), (int)p.getY(), direction);
}
static Dot getDot(int x, int y, int direction) {
Point p = new Point(x,y);
for( Dot d : list )
if(d.pointEquals(p))
return d;
return new Dot(p, direction);
}
void setStatus(int status) { this.huntStatus = status; }
void setMinCross(int minCross) { this.minCross = minCross; }
void setDirection(int direction) { this.direction = direction; }
int getStatus() { return huntStatus; }
int getMinCross() { return minCross; }
int getDirection() { return direction; }
Point getPoint() { return p; }
boolean pointEquals(Point p) {
if( p!=null && this.p.equals(p) )
return true;
return false;
}
static void reset() {
list = new ArrayList<Dot>();
}
public String toString() {
String s = null;
switch(huntStatus) {
case NO: s = "[NO] ";break;
case YES: s = "[YES] ";break;
case ALL: s = "[ALL] ";break;
}
switch(direction) {
case Path.UP: s += "[UP] ";break;
case Path.DOWN: s += "[DOWN] ";break;
case Path.LEFT: s += "[LEFT] ";break;
case Path.RIGHT: s += "[RIGHT] ";break;
case Path.NONE: s += "[NONE] ";break;
}
s += minCross+" ";
s += "("+p.getX()+","+p.getY()+")";
return s; 
}
}



package kyodai;
//ok
import java.awt.*;
import java.util.*;
import kyodai.*;

class Square {
private boolean exist = true;
private int id;
private Point p;
private Image img;
Square() {}
Square(int x, int y, int id) {
p = new Point(x,y);
this.id = id;
}
void setExist(boolean exist) { this.exist = exist; }
void setID(int id) { this.id = id; }
void setPoint(Point p) { this.p = p; }
void setImg(Image img) { this.img = img; }
boolean isExist() { return exist; }
int getID() { return id; }
Point getPoint() { return p; }
Image getImg() { return img; }
int getX() { return (int)p.getX(); }
int getY() { return (int)p.getY(); }
Square getMatch(Square[] squares) {
if(squares!=null)
for( Square sq : squares )
if( sq.getID()==id && !sq.pointEquals(p) )
return sq;
return null;
}
boolean isMatch(Square sq) {
if( sq!=null && sq.getID()==id && !sq.pointEquals(p) )
return true;
return false;
}
boolean pointEquals(Point p) {
if( p!=null && this.p.equals(p) )
return true;
return false;
}
static Square getSquare(Square[] squares, Point p) {
if(squares!=null && p!=null)
for(Square sq : squares)
if(sq.pointEquals(p))
return sq;
return null;
}
public String toString() {
return "["+id+"] ("+p.getX()+","+p.getY()+") ["+exist+"]";
}
}

public class Maps {
public static final int AREA = 160;
public static final int LENGTH = 16;
public static final int WIDTH = AREA/LENGTH;
private static class RandomPoint {
static final Random RAND = new Random(13); //bug
static Point[] points = new Point[Maps.AREA];
static int randomNum;
static {
reset();
}
static void reset() {
for(int y=0;y<WIDTH;y++)
for(int x=0;x<LENGTH;x++)
points[ LENGTH*y+x ] = new Point(x,y);
randomNum = AREA;
}
static Point get() {
if( randomNum != AREA )
points[randomNum] = null;
int flag = 0;
for( Point p : points )
if( p==null && ++flag==points.length ) return null;
do    
randomNum = RAND.nextInt(AREA);
while( points[randomNum] == null );
returnpoints[randomNum];
}
static Point[] random(Point[] points) {
Point[] randomPoints = new Point[points.length];
int randomNum;
for(int i=0;i<points.length;i++)
{
do 
randomNum = RAND.nextInt(points.length);
while( points[randomNum] == null );
randomPoints[i] = points[randomNum];
points[randomNum] = null;
}
return randomPoints;
}
}
static Square[] create() {
RandomPoint.reset();
Square[] squares = new Square[AREA]; //x*y=16*10
for(int i=0;i<squares.length;i++)
{
squares[i] = new Square();
squares[i].setID( i/2%ImgFactory.SUM );  
squares[i].setImg( ImgFactory.images[ squares[i].getID() ] );  
squares[i].setPoint( RandomPoint.get() );
}
return squares;
}
static void random(Square[] squares) {
if(squares==null)
return;
ArrayList<Point> pointList = new ArrayList<Point>();
for( Square sq : squares )
if(sq.isExist())
pointList.add(sq.getPoint());
Point[] newPoints = pointList.toArray(new Point[] {});
newPoints = RandomPoint.random(newPoints);
int j = 0;
for(int i=0;i<squares.length;i++)
if(squares[i].isExist())
squares[i].setPoint(newPoints[j++]);
}
static boolean isGetat(Square[] squares, Point p) {
return isGetat(squares, (int)p.getX(), (int)p.getY());
}
static boolean isGetat(Square[] squares, int x, int y) {
if( squares==null || x<-1 || y<-1 || x>LENGTH || y>WIDTH )
return false;
for( Square sq : squares )
if( x==(int)sq.getPoint().getX() && y==(int)sq.getPoint().getY() && sq.isExist() )
return false;
return true;
}
}




package kyodai;

import kyodai.*;
import java.awt.*;
import java.util.*;

public class Path extends Stack<Dot>{
public static final int NONE = 0, UP = 1,DOWN = 2,LEFT = 3,RIGHT = 4;
//bug : 有时不能消去
static Path explore(Square[] squares, Square prev, Square current) {
if( !(squares==null || prev == null || current == null) && prev.isMatch(current) ) {
LinkedList<Dot> q = new LinkedList<Dot>();
Path path = new Path();
q.add( Dot.getDot(prev.getPoint(), NONE) );
while(q.size()>0)
{
Dot dot = q.poll();
path.push(dot);
for(int i=UP;i<=RIGHT;i++)
{
int x = (int)dot.getPoint().getX();
int y = (int)dot.getPoint().getY();
Dot neighbor = null;
switch(i) {
case UP: neighbor = Dot.getDot(x,--y,UP);break;
case DOWN: neighbor = Dot.getDot(x,++y,DOWN);break;
case LEFT: neighbor = Dot.getDot(--x,y,LEFT);break;
case RIGHT: neighbor = Dot.getDot(++x,y,RIGHT);break;
}
if( neighbor.getStatus()==Dot.NO  && dot.getMinCross()<3 )
{
boolean flag = false;
if( dot.getMinCross() == 2 )
flag = true;
if( flag && neighbor.getDirection() == dot.getDirection() )
flag = false;
else dot.pathFlag++;
if(!flag)
{
if( Maps.isGetat(squares, neighbor.getPoint()) )
{
neighbor.setStatus( Dot.YES );
if( neighbor.getDirection() != dot.getDirection() )
neighbor.setMinCross( dot.getMinCross()+1 );
else neighbor.setMinCross( dot.getMinCross() );
q.add(neighbor);
}
else if( current.pointEquals( neighbor.getPoint() ) )
{
neighbor.setDirection(NONE);
Path temp = new Path();
Dot dotTemp = null;
temp.push(neighbor);
while(!path.empty())
{
dotTemp = path.pop();
if(dotTemp.pathFlag!=4)
temp.push(dotTemp);
}
path = temp;
Dot.reset();
return path;
}
else dot.pathFlag++;
}
}
else dot.pathFlag++;
if(dot.pathFlag==4)
path.pop();
}
dot.setStatus(Dot.ALL);
}
}
Dot.reset();
return null;
}
}

莫名bug:有时无法消去。。。
致命bug:无法储存消去路径……





第二次出错
package kyodai;

import java.awt.*;
import java.util.*;
import kyodai.*;

class Square {
private boolean exist = true;
private int id;
private Image img;
private int x = -1;
private int y = -1;
Square(int id) {
setID(id);
}
void setExist(boolean exist) { this.exist = exist; }
void setID(int id) { 
this.id = id; 
img = ImgFactory.images[id];
}
boolean isExist() { return exist; }
int getID() { return id; }
Image getImg() { return img; }
public String toString() {
return "["+id+"] ["+exist+"]";
}
}

public class Map {
private static final int XLEN = 16;
private static final int YLEN = 10;
private static Square[][] mapGrid;
static final Random SEED = new Random(); 
static Square[][] getMap() {
if(mapGrid!=null)
return mapGrid;
mapGrid = new Square[YLEN][XLEN];
for(int y=0;y<YLEN;y++)
for(int x=0;x<XLEN;x++)
if(mapGrid[y][x]==null)
{
int x2, y2;
do {
x2 = SEED.nextInt(XLEN);
y2 = SEED.nextInt(YLEN);
} while(mapGrid[y2][x2]!=null || !(x2==x&&y2==y));
int id = SEED.nextInt(ImgFactory.images.length);
boolean sameID = false; 
for(int i=0;i<YLEN;i++)
for(int j=0;j<XLEN;j++)
if(mapGrid[i][j]!=null && mapGrid[i][j].getID()==id)
{
mapGrid[y][x] = mapGrid[y2][x2] = mapGrid[i][j];
sameID = true;
}
if(!sameID)
mapGrid[y][x] = mapGrid[y2][x2] = new Square(id); 
}
return mapGrid;
}
static void shuffile() {
if(mapGrid==null)
return;
ArrayList<Integer> idList = new ArrayList<Integer>();
for(int y=0;y<YLEN;y++)
for(int x=0;x<XLEN;x++)
if(mapGrid[y][x].isExist())
idList.add(mapGrid[y][x].getID());
Integer[] randID = idList.toArray(new Integer[] {});
for(int i=0;i<randID.length;i++)
{
int rand = SEED.nextInt(randID.length);
Integer temp = randID[i];
randID[i] = randID[rand];
randID[rand] = temp;
}
int i = 0;
for(int y=0;y<YLEN;y++)
for(int x=0;x<XLEN;x++)
if(mapGrid[y][x].isExist())
mapGrid[y][x].setID(randID[i++]);
}
}




package kyodai;

import kyodai.*;
import java.awt.*;
import java.util.*;

public class Path extends Vector<Point>{
private static final int INFINITE = 9;
private static Path[] paths = new Path[] {
new Path(), new Path(), new Path()
};
static boolean xCourse(int x1, int y1, int x2, int y2, Path path) {
path.removeAllElements();
if(y1!=y2 || x1==x2)
return false;
Square[][] mapGrid = Map.getMap();
int direction = x1<x2? 1 : -1;
for(int x=x1+direction; x!=x2; x+=direction)
if(mapGrid[y1][x].isExist()) {
path.removeAllElements();
return false;
}
else path.add(new Point(x,y1));
path.add(new Point(x2,y1));
return true;
}
static boolean yCourse(int x1, int y1, int x2, int y2, Path path) {
path.removeAllElements();
if(x1!=x2 || y1==y2)
return false;
Square[][] mapGrid = Map.getMap();
int direction = y1<y2? 1 : -1;
for(int y=y1+direction; y!=y2; y+=direction)
if(mapGrid[y][x1].isExist()) {
path.removeAllElements();
return false;
}
else path.add(new Point(x1,y));
path.add(new Point(x1,y2));
return true;
}
static int explore(int x1, int y1, int x2, int y2) {
Square[][] mapGrid = Map.getMap();
if( mapGrid[y1][x1]==mapGrid[y2][x2] && mapGrid[y1][x1].isExist() ) {
if(xCourse(x1, y1, x2, y2, paths[0]) || yCourse(x1, y1, x2, y2, paths[0]))
return 0;
if( ( xCourse(x1, y1, x2, y1, paths[0]) && yCourse(x2, y1, x2, y2, paths[1]) )   
|| ( yCourse(x1, y1, x1, y2, paths[0]) && xCourse(x1, y2, x2, y2, paths[1]) ) )
return 1;
//vertical
for(int y=-1;y<=mapGrid.length;y++)
if( (y!=y1||y!=y2) 
&& yCourse(x1, y1, x1, y, paths[0]) 
&& xCourse(x1, y, x2, y, paths[1]) 
&& yCourse(x2, y, x2, y2, paths[2]) )
return 3;
//horizontal
for(int x=-1;x<=mapGrid[0].length;x++)
if( (x!=x1||x!=x2) 
&& xCourse(x, y1, x1, y1, paths[0]) 
&& yCourse(x, y1, x, y2, paths[1]) 
&& xCourse(x, y2, x2, y2, paths[2]) )
return 3;
}
return INFINITE;
}
}

致命bug:出现4个相同图片时,消掉任意两个,全部消掉了……

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics