Здравствуйте дорогие друзья
Для проверки работы функции перемещения исподьзуется
А также очень быстрая реализация на Cи - (облако)
MainActivity.java
public TextView myText,myTextView,myAlert;
public boolean testalert = false;
public long depthmov=0L;
public long startGlobalTime1;
public double resultGlobalTime1,resultGlobalTime2;
public long GlobalTime = 60L;
...
Изменено
потому приводим к виду
showAlert(sInstance," ", " ",null);
public static void showAlert(Context context,String title, String message, View view)
...
if (view==null)
{
builder.setMessage(message);
}
else
{
builder.setView(view);
}
...
public DrawView(Context context) {
super(context);
sInstance = context;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
if (desc == null)
{
desc = new Desc();
Start=System.currentTimeMillis ();
MascBit.OneBit();
MascBit.maskAttak();
Stop=System.currentTimeMillis ();
time_count = TimeCount(Stop-Start);
} else
{
time_count = "";
}
if (!desc.FenToLong(StrFen))
{
desc = MoveGenerator.newPos("");
showAlert(context, "ERROR", "Ошибка fen строки",null);
} else
{
//desc = MoveGenerator.newPos(StrFen);
desc = new Desc(StrFen);
// greatAlert();
// showAlertDesc(desc);
dsc = new Desc();
}
...
protected void onDraw(Canvas canvas) {
canvas.drawARGB(80, 102, 204, 255);
printAttak();
// доска
canvas.drawBitmap(bitmap3, 0, 0, paint);
//parsingStrFen(StrFen, canvas);
printDesc(canvas);
PrintLine(0);
...
protected void onDrawText(Canvas canvas)
canvas.drawText(""+testalert, 566, 172, paint);
canvas.drawText("", 566, 232, paint);
canvas.drawText("show Desc", 566, 292, paint);
canvas.drawText("", 566, 352, paint);
canvas.drawText("сброс", 566, 422, paint);
case 136:
case 126:
case 116:
case 106:
{
if (testalert)
{
testalert=false;
}
else
{
testalert=true;
}
invalidate();
return true;
}
case 152:
case 122:
case 132:
case 142:
{
startGlobalTime1 = System.nanoTime();
depthmov=0L;
desc = MoveGenerator.newPos("8/8/2KQ4/8/8/8/8/7k w");
InviolableKing(desc,0,7);
if (resultGlobalTime2>GlobalTime)
{
showAlert(sInstance,"Внимание!", "Превышено время работы кода",null);
} else
{
resultGlobalTime1 = (double)(System.nanoTime() - startGlobalTime1) / 1000000000;
time_count = resultGlobalTime1 + "сек";
showAlert(sInstance,"Число ходов", ""+depthmov,null);
}
invalidate();
return true;
}
if (test)
{
test = false;
}
long startTime1 = System.nanoTime();
if (desc.returnPiece(NumPol)<6)
{
number1=MoveGenerator.AttakPiece(desc,number2,0);
}
else
{
number1=MoveGenerator.AttakPiece(desc,number2,1);
}
validateMove();
double resultTime1 = (double)(System.nanoTime() - startTime1) / 1000000000;
time_count = resultTime1 + "сек";
} //(SwitchFromTo==0)
else
public void greatAlert ()
{
if (myAlert!=null){return;}
//отладка
myAlert=new TextView(sInstance);
myAlert.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/gc2004d.ttf"));
myAlert.setTextSize(30.0f);
myAlert.setTextColor(Color.BLACK);
myAlert.setBackgroundColor(Color.WHITE);//отладка
}
public void showAlertDesc(Desc d)
{
//отладка
try {
String sFen = FenToVision.DescToFen(d);
myAlert.setText(FenToVision.fenToVis(sFen));
showAlert(sInstance, "DESC", "",myAlert);
}//try
catch (Exception e)
{
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
String trace = sw.getBuffer().toString();
showAlert(sInstance,"ERROR", trace,null);
}//отладка
myAlert=null;
}
public void validateMove()
{
int nbit=Long.bitCount(number1);
long getbit;
long number=0L;
for (int i=0;i<nbit;i++)
{
getbit = Long.lowestOneBit(number1);
MoveGenerator.moveFromTo(desc,dsc,number2,getbit);
if (dsc.mov)
{
number|=getbit;
}
number1^=getbit;
if (testalert&dsc.mov)
{
greatAlert();
showAlertDesc(dsc);
}
}
number1=number;
}
//неприкосновенный король
public void InviolableKing(Desc d,int col,int depth)
{
if (resultGlobalTime2>GlobalTime)
{
return;
}
if (depth==0)
{
return;
}
resultGlobalTime2 = (double)(System.nanoTime() - startGlobalTime1) / 1000000000;
// поз короля или ферзя
long pos=0L;
//атаки короля или ферзя
long attak=0L;
//кол-во атак
int nattak=0;
//ход фигуры
long oneMove=0L;
//массивы лля фмгур
long[] WP = new long[2];
long[] BP = new long[2];
//кол фигур
int figure=0;
figure=Long.bitCount(d.returnPC(col));
for (int x=0;x<figure;x++)
{
switch (col)
{
case 0:
{
attak=MoveGenerator.AttakPiece(d,d.WPiece[x],0);
pos=Long.lowestOneBit(d.WPiece[x]);
break;
}
case 1:
{
attak=MoveGenerator.AttakPiece(d,d.BPiece[0],1);
pos=Long.lowestOneBit(d.BPiece[0]);
break;
}
}//switch
nattak=Long.bitCount(attak);
for (int i=0;i<nattak;i++)
{
oneMove=Long.lowestOneBit(attak);
for (int j = 0; j < 2; j++)
{
WP[j] = d.WPiece[j];
BP[j] = d.BPiece[j];
}
MoveGenerator.moveFromTo(d,dsc,pos,oneMove);
attak^=oneMove;
if (!dsc.mov)
{
for (int j = 0; j < 2; j++)
{
d.WPiece[j]=WP[j];
d.BPiece[j]=BP[j];
}
continue ;
}
InviolableKing(dsc,col^1,depth-1);
if (depth==1)
{
depthmov++;
//greatAlert();
//showAlertDesc(dsc);
}
for (int j = 0; j < 2; j++)
{
d.WPiece[j]=WP[j];
d.BPiece[j]=BP[j];
}
}//for
}//for
}
MoveGenerator.java
public static Desc moveFromTo(Desc dfrom,Desc dto,long from, long to)
{
int colfrom=-1;
int colto=-1;
long posKing=0L;
for (int i=0;i<6;i++)
{
dto.WPiece[i]=dfrom.WPiece[i];
dto.BPiece[i]=dfrom.BPiece[i];
}
//фигура на from
int f = dto.returnPiece(Long.numberOfLeadingZeros(from));
colfrom=dfrom.Color;
//фигура на to
int t = dto.returnPiece(Long.numberOfLeadingZeros(to));
colto=dto.Color;
dfrom.mov=false;
dto.mov=false;
if ((f==-1)|(from==to)|(colfrom==colto)){return dto;}
dto.removePiece(from);
dto.removePiece(to);
dto.addPiece(f,to);
switch (colto)
{
case 1:
{
posKing=dto.WPiece[0];
break ;
}
case 0:
{
posKing=dto.BPiece[0];
break ;
}
}
if ((posKing&dto.returnAllattak(colto))!=0)
{
for (int i=0;i<6;i++)
{
dto.WPiece[i]=dfrom.WPiece[i];
dto.BPiece[i]=dfrom.BPiece[i];
}
dto.mov=false;
return dto;
}
dto.mov=true;
dfrom.mov=true;
return dto;
}
Desc.java
public long returnMoveBKing()
{
long temp = returnAllattak(0);
MoveBlackKing=MoveGenerator.AttakPiece(this,BPiece[0],1);
MoveBlackKing^=(MoveBlackKing&temp);
return MoveBlackKing;
}
FenToVision.java
public static String DescToFen(Desc d)
{
String strN = "12345678";
String strF = "KQRBNPkqrbnp";
String FenStr = "";
int z=-1;
int k=0;
long Figure = 0L;
long Pos;
for (int i=0;i<6;i++)
{
Figure |= (d.WPiece[i]|d.BPiece[i]);
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
k++;
Pos=0x8000000000000000L>>>(8*i+j);
if ((Figure&Pos)!=0)
{
z=d.returnPiece(8*i+j);
if (z!=-1)
{
if (k>1)
{
k--;
FenStr+=strN.substring(k-1,k);
}
FenStr+=strF.substring(z,z+1);
k=0;
}
}
}//for
if (k>0)
{
FenStr+=strN.substring(k-1,k);
}
if (i<7)
{
FenStr+="/";
}
k=0;
}//for
return FenStr;
}
Итак после проверки для позиции 8/8/2KQ4/8/8/8/8/7k - w
на глубине 7 количество ходов у нас и у образцовой программы совпало.
значит мы идем правильной дорогой дорогие друзья.
До встречи.