Найти в Дзене

Пишем шахматную программу (Битовые операции в java)

Предыдущая публикация

Здравствуйте дорогие друзья.

В шахматных боях без правил разрешается использовать не только фигуры, но так же доску, часы, стул и стол.

Побитовые операции

JAVA.LANG.LONG КЛАСС В JAVA

import android.view.MotionEvent;

import android.graphics.Color;
import java.lang.Long;
...

public Canvas canvasBmp3;

public long number1=0;
public long number2=0x8000000000000000L;
public Bitmap imgnumber,imgnumber0,imgnumber1;
public Bitmap button_up, button_down,button_click;
public Boolean bol_but = false;
public Long ObjectLong;
public int BitSwth = 0;
...
  • public void onBackPressed()

...

bol = true;
bol_but = false;
number1=0;
...

...

public void LongtoBin(long lg, Canvas canvas)
{
int h = 60; // размер клетки поля
// координаты поля a8
int dx = 26;
int dy = 22;
long k = 1;
for (int j = 7; j >= 0; j--)
{
for (int i = 7; i >= 0; i--)
{
if ((lg & k) == 0)
{
canvas.drawBitmap(imgnumber0, dx + h * i, dy + h * j, paint);
} else
{
canvas.drawBitmap(imgnumber1, dx + h * i, dy + h * j, paint);
}
k<<=1;
}
}
}
  • public DrawView(Context context)

...

initPiece();

paint.setColor(Color.BLACK);
paint.setTextSize(35.0f);
imgnumber = BitmapFactory.decodeResource(getResources(), R.drawable.number);
imgnumber1 = Bitmap.createBitmap(imgnumber, 0, 0, 60, 60);
imgnumber0 = Bitmap.createBitmap(imgnumber, 60, 0, 60, 60);
button_click = BitmapFactory.decodeResource(getResources(), R.drawable.on_off);
button_up = Bitmap.createBitmap(button_click, 0, 20, 160, 60);
button_down = Bitmap.createBitmap(button_click, 0, 80, 160, 60);
number2>>>=63;
number2^=1;
  • protected void onDraw(Canvas canvas) {
int h = 60; // размер клетки поля
// координаты поля a8
int dx = 26;
int dy = 22;
canvas.drawARGB(80, 102, 204, 255);
// доска
canvas.drawBitmap(bitmap3, 0, 0, paint);
parsingStrFen(StrFen, canvas);
switch (BitSwth) {
case 1:{
number1>>=1;
break;
}
case 2:{
number1>>>=1;
break;
}
case 3:{
number1<<=1;
break;
}
}
ObjectLong = new Long(number1);
switch (BitSwth) {
case 4:{
number1 = Long.rotateLeft(number1,8);
break;
}
case 5:{
number1 = Long.rotateLeft(number1,1);
//number1 = Long.rotateRight(number1,8);
break;
}
case 6:{
number1 = Long.reverseBytes(number1);
break;
}
case 7:{
number1 = 0;
break;
}
}
BitSwth = 0;
canvasBmp3.drawBitmap(bitmap1, 0, 0, null);
for (int i = 0; i < 64; i++)
{
if ((number1&(0x8000000000000000L>>>i))!=0)
{
canvasBmp3.drawBitmap(bitmap4, dx + (i % 8)*h, dy + (i / 8)*h, null);
}
}
if (bol_but)
{
LongtoBin(number1,canvas);
} canvas.drawBitmap(button_up, 566, 442, paint);
canvas.drawText("long = " + number1, 0, 600, paint);
number2 = Long.lowestOneBit(number1);
canvas.drawText("Поз-я младшего бита (LSB) = " + number2, 0, 650, paint);
number2 = Long.highestOneBit(number1);
canvas.drawText("Поз-я старшего бита (MSB) = " + number2, 0, 700, paint);
number2 = Long.highestOneBit(number1);
canvas.drawText("Кол-во уст. бит = " + Long.bitCount(number1), 0, 750, paint);
canvas.drawText("Кол-во 0 до старшего бита (MSB)= " + Long.numberOfLeadingZeros(number1), 0, 800, paint);
canvas.drawText("Кол-во 0 до младшего бита (LSB)= " + Long.numberOfTrailingZeros(number1), 0, 850, paint);
canvas.drawText("Знак числа = " + Long.signum(number1), 0, 900, paint);
canvas.drawText("long: хранит целое число ", 0, 1000, paint);
canvas.drawText("от -9223372036854775808", 0, 1050, paint);
canvas.drawText("до 9223372036854775807", 0, 1100, paint);
/*paint.setColor(Color.YELLOW);
canvas.drawRect(530, 20, 700, 430, paint);*/
paint.setColor(Color.BLUE);
canvas.drawText(">>1", 566, 52, paint);
canvas.drawText(">>>1", 566, 112, paint);
canvas.drawText("<<1", 566, 172, paint);
canvas.drawText("up", 566, 232, paint);
canvas.drawText("left", 566, 292, paint);
canvas.drawText("reverse", 566, 352, paint);
canvas.drawText("сброс", 566, 422, paint);
paint.setColor(Color.BLACK);
if (ObjectLong != null) {
ObjectLong = null;
}

}

  • public boolean onTouchEvent

...

XPol = (touchX - dx) / h;

YPol = (touchY - dy) / h;

if (XPol>7) {XPol=XPol*10;}
NumPol = XPol + 8 * YPol;
BitSwth = 0;
switch (NumPol)
{
case 90:{
BitSwth = 1;
invalidate();
return true;
}
case 98:{
BitSwth = 2;
invalidate();
return true;
}
case 106:{
BitSwth = 3;
invalidate();
return true;
}
case 114:{
BitSwth = 4;
invalidate();
return true;
}
case 122:{
BitSwth = 5;
invalidate();
return true;
}
case 130:{
BitSwth = 6;
invalidate();
return true;
}
case 138:{
BitSwth = 7;
invalidate();
return true;
}
case 156:
case 166:
case 146:{
Bitmap bmp = button_up;
button_up = button_down;
button_down = bmp;
bol_but = !bol_but;
invalidate();
return true;
}
}

...

NumPol = XPol + 8 * YPol;

ShowAlert("Поле " + NumPol);

canvasBmp3.drawBitmap(bitmap1, 0, 0, null); canvasBmp3.drawBitmap(bitmap4, dx + h * XPol, dy + h * YPol, null);

number2=0x8000000000000000L>>>NumPol;
number1 ^= number2;

Кнопки (облако)

Биты (облако)

продолжение следует ...

До встречи