AccessibleSquares.java~1~
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- package gao;
- class AccessibleSquares {
- private static int horizontal[] = {2,1,-1,-2,-2,-1,1,2};
- private static int vertical [] = {-1,-2,-2,-1,1,2,2,1};
- private int xpos[] ;
- private int ypos[] ;
- private int accessibility [];
- private int ownxpos ,ownypos ;
- private int ownAccessibility ;
- private int arrayPos ;
- private int countAccessibility;
- public AccessibleSquares(int x , int y ){
- int testXPos;
- int testYPos;
- xpos = new int [ 8 ];
- ypos = new int [ 8 ];
- accessibility = new int [ 8 ];
- arrayPos = 0 ;
- ownxpos = x ;
- ownypos = y ;
- ownAccessibility = KnightsTour.access[ x ][ y ];
- for (int i = 0 ; i < horizontal.length ; i++ ){
- testXPos = x + horizontal[ i ];
- testYPos = y + vertical [ i ];
- if ( (testXPos >= 0 ) & ( testXPos < 8 ) &
- (testYPos >= 0 ) & ( testYPos < 8 ) ) {
- xpos [ arrayPos ] = testXPos ;
- ypos [ arrayPos ] = testYPos ;
- accessibility [ arrayPos ] = KnightsTour.access [testXPos][testYPos];
- //because accessibility [ arrayPos ] = 0 indicating the square has been occupied
- if (accessibility [ arrayPos ] > 0 )
- arrayPos ++ ;
- }//end of if
- }// end of for
- countAccessibility = arrayPos ;
- if (countAccessibility > 0 )
- {sortAll();}
- arrayPos = -1 ;
- }// end of constructor
- public boolean hasMoreAccessible(){
- // arrayPos + 1 point to the next accessible
- if ( (arrayPos + 1 ) < countAccessibility ){
- return true;
- }else {
- return false;
- }
- }//end of the hasMoreAccessible()
- public AccessibleSquares nextAccessible(){
- arrayPos ++ ;
- return this;
- }
- public AccessibleSquares accessibleAt( int pos){
- if ((pos >= 0) & (pos < countAccessibility ))
- arrayPos = pos ;
- return this;
- }
- public int getXpos(){
- return xpos[ arrayPos ];
- }
- public int getYpos(){
- return ypos[ arrayPos ];
- }
- public int getAccessibility(){
- return accessibility[ arrayPos ];
- }
- public int getTotalAccessible(){
- return countAccessibility;
- }
- //bubble sorting
- private void sortAll (){
- for ( int begin = 0 ; begin < countAccessibility - 1 ; begin ++ ){
- for ( int i = begin + 1; i < countAccessibility ; i ++ ){
- if ( accessibility [ begin ] > accessibility [ i ] ){
- swapAll( begin, i );
- }//end of if
- }// end of inner for
- }// end of outer for
- }// end of sortAll
- private void swapAll ( int i , int j ){
- int temp ;
- temp = xpos [ i ];
- xpos [ i ] = xpos [ j ];
- xpos [ j ] = temp;
- temp = ypos [ i ];
- ypos [ i ] = ypos [ j ];
- ypos [ j ] = temp;
- temp = accessibility [ i ];
- accessibility [ i ] = accessibility [ j ];
- accessibility [ j ] = temp;
- }
- public void domoving(){
- for ( int i = 0 ; i < countAccessibility ; i ++ ){
- KnightsTour.access[ xpos [i] ][ ypos[i] ] -- ;
- }
- KnightsTour.access[ ownxpos ][ ownypos ] = 0 ;
- }
- public void undomoving(){
- for ( int i = 0 ; i < countAccessibility ; i ++ ){
- KnightsTour.access[ xpos [i] ][ ypos[i] ] ++ ;
- }
- KnightsTour.access[ ownxpos ][ ownypos ] = ownAccessibility ;
- }
- }