?? heibaiqi.java
字號:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class HeiBaiQi extends Applet implements MouseListener
{
private static int BASE = 25;
private int gamegrid[][];
private boolean playing;
private boolean playersMove;
private boolean playerPlaysWhite;
private boolean lastMoveWasWhite;
private boolean allowPlay;
public void init()
{
gamegrid = new int[8][8];
resize(16*BASE, 16*BASE);
this.addMouseListener(this);
}
public void start()
{ //初始化棋盤(8*8),-1表示沒有放棋子,1為放黑子,0為放白子
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
gamegrid[x][y] = -1;
}
gamegrid[3][3] = 1;
gamegrid[3][4] = 0;
gamegrid[4][3] = 0;
gamegrid[4][4] = 1;
lastMoveWasWhite = false;
playing = false;
playersMove = true;
playerPlaysWhite = false;
allowPlay = true;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
Color black = new Color(0);
Color grey90 = new Color(0x191919);
Color grey80 = new Color(0x333333);
Color grey60 = new Color(0x666666);
Color grey40 = new Color(0x999999);
Color grey10 = new Color(0xe5e5e5);
Color white = new Color(0xffffff);
Color green = new Color(0x669966);
Color darkgreen = new Color(0x336600);
int radius = 20;
//設置棋盤顏色
g.setColor(darkgreen);
g.fillRect(0, 0, 16 * BASE, 16 * BASE);
//畫分隔線
g.setColor(green);
for(int i = 1; i < 8; i++)
{
g.drawLine(0, i * BASE * 2, 16 * BASE, i * BASE * 2);
g.drawLine(i * BASE * 2, 0, i * BASE * 2, 16 * BASE);
}
//畫棋子
for(int x = 0; x < 8; x++)
for(int y = 0; y < 8; y++)
{
int xpos = x * BASE * 2 + BASE;
int ypos = y * BASE * 2 + BASE;
//畫黑棋子 ,兩次填充,形成輪廓的效果
if(gamegrid[x][y] == 0)
{
g.setColor(grey80);
g.fillOval((xpos - radius) + 5, (ypos - radius) + 5,
radius * 2, radius * 2);
g.setColor(grey10);
g.fillOval(xpos - radius, ypos - radius, radius * 2,
radius * 2);
g.setColor(white);
g.fillOval(xpos - 19, ypos - 19, 36, 36);
g.setColor(grey40);
g.fillOval(xpos - 17, ypos - 17, 36, 36);
g.setColor(grey10);
g.fillOval(xpos - 18, ypos - 18, 36, 36);
}
else if(gamegrid[x][y] == 1)
{
g.setColor(grey80);
g.fillOval((xpos - radius) + 5, (ypos - radius) +
5, radius * 2, radius * 2);
g.setColor(grey90);
g.fillOval(xpos - radius, ypos - radius, radius *
2, radius * 2);
g.setColor(grey60);
g.fillOval(xpos - 19, ypos - 19, 36, 36);
g.setColor(black);
g.fillOval(xpos - 17, ypos - 17, 36, 36);
g.setColor(grey90);
g.fillOval(xpos - 18, ypos - 18, 36, 36);
}
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited (MouseEvent e) {}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{
if(!allowPlay)//如果輸了,就start()重新開始;
{
start();
repaint();
return;
}
int gridx =e.getX()/(BASE*2);
int gridy =e.getY()/(BASE*2);
if(playersMove)
{ //在開始時,玩家放在不同的地方,則以為選擇了不同顏色的棋子
if(!playing)
switch(gridx)
{
case 2:
if(gridy == 3)
{ playerPlaysWhite = true; playing = true; }
else if(gridy == 4)
{ playerPlaysWhite = false; playing = true; }
else playing = false;
break;
case 3:
if(gridy == 2)
{ playerPlaysWhite = true; playing = true;}
else if(gridy == 5)
{ playerPlaysWhite = false; playing = true; }
else playing = false;
break;
case 4:
if(gridy == 2)
{ playerPlaysWhite = false; playing = true;}
else if(gridy == 5)
{ playerPlaysWhite = true; playing = true; }
else playing = false;
break;
case 5:
if(gridy == 3)
{ playerPlaysWhite = false; playing = true;}
else if(gridy == 4)
{ playerPlaysWhite = true; playing = true; }
else playing = false;
break;
default:
playing = false;
}
if(moveisvalid(gridx, gridy, !playerPlaysWhite))
{
plantapiece(gridx, gridy, playerPlaysWhite);
showStatus("My move.");
playersMove = false;
lastMoveWasWhite = playerPlaysWhite;
nextMove();
}
else
{
showStatus("You can't move there. Try again.");
playersMove = true;
}
}
}
private void paintsquare(int x, int y)
{
Rectangle r = new Rectangle(x * BASE * 2, y * BASE * 2, BASE * 2,
BASE* 2);
Graphics g = getGraphics();
g.clipRect(r.x, r.y, r.width, r.height);//不這樣做的話會閃爍的。
//hangon();//此處加上該函數會明顯看出界面的變化情況
paint(g);
r = null;
}
//若x,y位置可以放置子,就返回true
private boolean moveisvalid(int x, int y, boolean white)
{
//valid表示x,y位置是否已經放子
boolean valid = true;
if(gamegrid[x][y] != -1)
valid = false;
//validway表示x,y位置是否符合能夠吃子的條件
boolean validway = false;
BreakUp: for(int p = -1; p < 2; p++)
for(int q = -1; q < 2; q++)
if(countindirection(x, y, p, q, !white) > 0)
{
validway = true;
break BreakUp;//中斷最外層的for循環!
}
if(validway && valid)
valid = true;
else
valid = false;
return valid;
}
//這個函數是向上下左右,左上,左下,右上,右下八個方向尋找相反顏色的旗子
private int countindirection(int x, int y, int deltax, int deltay,
boolean black)
{
int returnvalue = 0;
if( x+deltax<0 || x+deltax>7 || y+deltay<0 || y+deltay>7 )
return 0;
if(deltax == 0 && deltay == 0)
return 0;
boolean maybetrue = false;
if(gamegrid[x + deltax][y + deltay] == 1)
if(black)
maybetrue = true;
else
return 0;
if(gamegrid[x + deltax][y + deltay] == 0)
if(!black)
maybetrue = true;
else
return 0;
if(gamegrid[x + deltax][y + deltay] == -1)
return 0;
if(maybetrue)//如果在這個方向上確定相鄰的是對方的棋子!
{
boolean continueloop = true;
boolean itsopposite = false;
int nextx = x + deltax;
int nexty = y + deltay;
while(continueloop)
{
if(gamegrid[nextx][nexty] == 1)
if(black){ returnvalue++; }
else { itsopposite = true; continueloop = false; }
if(gamegrid[nextx][nexty] == 0)
if(!black){ returnvalue++; }
else { itsopposite = true; continueloop = false; }
if(gamegrid[nextx][nexty] == -1)
continueloop = false;
nextx += deltax;
nexty += deltay;
if(nextx < 0 || nextx > 7 || nexty < 0 || nexty > 7)
continueloop = false;
}
if(!itsopposite)
returnvalue = 0;
}
return returnvalue;
}
//該函數輸入置子位置和玩家執子顏色,將棋盤上的異色子吃掉
private void plantapiece(int x, int y, boolean white)
{
if(white)
gamegrid[x][y] = 0;
else
gamegrid[x][y] = 1;
paintsquare(x, y);
for(int deltax = -1; deltax < 2; deltax++)
{
for(int deltay = -1; deltay < 2; deltay++)
if(countindirection(x, y, deltax, deltay, white) > 0)
{
int nextx = x + deltax;
int nexty = y + deltay;
for(boolean continueloop = true; continueloop; )
{
if(gamegrid[nextx][nexty] == 1)
{
if(white)
{
gamegrid[nextx][nexty] = 0;
paintsquare(nextx, nexty);
}
else
{
continueloop = false;
}
}
else
if(gamegrid[nextx][nexty] == 0)
if(!white)
{
gamegrid[nextx][nexty] = 1;
paintsquare(nextx, nexty);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -