📄 queensvsknights.java
字号:
import java.awt.*;
import java.*;
import java.applet.*;
public class QueensvsKnights extends Applet
{
static int count,queen,knight;
int turn=0;
int array[][] = new int[8][8];
int arrx,arry,orix,oriy,posx,posy,check,check1,empty;
int bj[] = {-2,-2,-1,1,2,2,1,-1};
int bi[] = {-1,1,2,2,1,-1,-2,-2};
String str1,str2,str3;
Font appFont;
Image imageQueen,imageBoard,imageKnight,imageX;
public void init()
{
Color c = Color.blue;
setBackground(c);
appFont= new Font("Verdana",Font.BOLD,14);
count = 0;
queen=0;
knight=0;
imageQueen = getImage(getCodeBase(), "queenShoji.gif");
imageBoard = getImage(getCodeBase(), "boardGrid.gif");
imageKnight = getImage(getCodeBase(),"knightshoji3d.gif");
imageX = getImage(getCodeBase(),"pointX.gif");
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
array[i][j] = 0;
}
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.drawImage(imageBoard,0,0,512,512,this);
if(turn == 1 )
{
str1 = String.valueOf(count);
str2 = String.valueOf(queen);
str3 = String.valueOf(knight);
g.setFont(appFont);
g.drawString("Mouse Clicks: " + str1,520,20);
g.drawString("Queens Used: " +str2,520,40);
g.drawString("Knights Used: "+str3,520,60);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(array[i][j] == 3)
{
//draws image of the queen
g.drawImage(imageQueen,i*64,j*64,64,64,this);
}
if(array[i][j] == 2)
{
//draws image of knight
g.drawImage(imageKnight,i*64,j*64,64,64,this);
}
if(array[i][j] == 1)
{
g.drawImage(imageX,i*64,j*64,64,64,this);
}
}
}
}
}
public boolean mouseDown(Event evt, int x, int y)
{
count++;
orix=x;
oriy=y;
arrx=(orix-0)/64;
arry=(oriy-0)/64;
if((evt.modifiers & Event.META_MASK) !=0) // 0 = right button & 1 = left button
{
//this is the queen function
check = checkKnight(arrx,arry); //check for the existance of a knight
empty = checkEmpty(arrx,arry); //check status of square
if(empty !=1 && check != 1)
{
turn = 1 ;
array[arrx][arry] = 3;
queen++;
queenmark(arrx,arry);//marking the queen
repaint();
return true;
}
else return false;
}
else
{
//this is the knight function
empty = checkEmpty(arrx,arry); //check the status of the square
if(empty != 1)
{
turn = 1 ;
array[arrx][arry] = 2;
knight++;
NewMarkKnight(arrx,arry);
repaint();
return true;
}
else return false;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
public void NewMarkKnight(int arrx, int arry)
{
int i,xi,yi;
for (i=0;i<8;i++)
{
xi = arrx + bi[i];
yi = arry + bj[i];
if (xi >= 0 && xi < 8 && yi >=0 && yi < 8)
array[xi][yi] = 1;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// marking for the queen
public void queenmark(int arrx,int arry)
{
if (arrx >= 0 && arrx <= 7 && arry >=0 && arry <= 7)
{
mark1(arrx,arry);
mark2(arrx,arry);
mark3(arrx,arry);
mark4(arrx,arry);
mark5(arrx,arry);
mark6(arrx,arry);
mark7(arrx,arry);
mark8(arrx,arry);
}
}
//marking functions, all eight of them
public void mark1(int row,int col)
{
int i,j;
i=row+1;
j=col;
while(i<=7 && j<=7)
{
array[i++][j]= 1;
}
}
public void mark2(int row,int col)
{
int i,j;
i=row;
j=col-1;
while(i<=7 && j>=0)
{
array[i][j--]= 1;
}
}
public void mark3(int row,int col)
{
int i,j;
i=row-1;
j=col;
while(i>=0 && j<=7)
{
array[i--][j]= 1;
}
}
public void mark4(int row,int col)
{
int i,j;
i=row;
j=col+1;
while(i<=7 && j<=7)
{
array[i][j++]= 1;
}
}
public void mark5(int row,int col)
{
int i,j;
i=row-1;
j=col-1;
while(i>=0 && j>=0)
{
array[i--][j--]= 1;
}
}
public void mark6(int row,int col)
{
int i,j;
i=row+1;
j=col+1;
while(i<=7 && j<=7)
{
array[i++][j++]= 1;
}
}
public void mark7(int row,int col)
{
int i,j;
i=row-1;
j=col+1;
while(i>=0 && j<=7)
{
array[i--][j++]= 1;
}
}
public void mark8(int row,int col)
{
int i,j;
i=row+1;
j=col-1;
while(i<=7 && j>=0)
{
array[i++][j--]= 1;
}
}
////////////////////
//checking for the knight
public int checkKnight(int x,int y)
{
if(checkKnight1(x,y) == true )return 1;
if(checkKnight2(x,y) == true )return 1;
if(checkKnight3(x,y) == true )return 1;
if(checkKnight4(x,y) == true )return 1;
if(checkKnight5(x,y) == true )return 1;
if(checkKnight6(x,y) == true )return 1;
if(checkKnight7(x,y) == true )return 1;
if(checkKnight8(x,y) == true )return 1;
return(0);
}
////////////////////////////////////////////////////////
// checking the existance of a knight, done by the queen.
public boolean checkKnight1(int x,int y)
{
while(true)
{
if(y < 0) return false;
else
{
if(array[x][y--] == 2)
return true;
}
}
}
public boolean checkKnight2(int x,int y)
{
while(true)
{
if(x > 7 || y < 0)return false;
else
{
if(array[x++][y--] == 2)
return true;
}
}
}
public boolean checkKnight3(int x,int y)
{
while(true)
{
if(x > 7)return false;
else
{
if(array[x++][y] == 2)
return true;
}
}
}
public boolean checkKnight4(int x,int y)
{
while(true)
{
if(x > 7 || y > 7)return false;
else
{
if(array[x++][y++] == 2)
return true;
}
}
}
public boolean checkKnight5(int x,int y)
{
while(true)
{
if(y > 7)return false;
else
{
if(array[x][y++] == 2)
return true;
}
}
}
public boolean checkKnight6(int x,int y)
{
while (true)
{
if (x < 0 || y > 7) return false;
else
{
if (array[x--][y++] == 2)
return true;
}
}
}
public boolean checkKnight7(int x,int y)
{
while (true)
{
if (x < 0) return false;
else
{
if(array[x--][y] == 2)
return true;
}
}
}
public boolean checkKnight8(int x,int y)
{
while (true)
{
if (x < 0 || y < 0) return false;
else
{
if(array[x--][y--] == 2)
return true;
}
}
}
public int checkEmpty(int column, int row)
{
if(array[column][row] != 0 ) return 1;
else return 0;
}
// the point of no return.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -