📄 qiban.java
字号:
package like.wuziqi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.MouseListener;
import java.util.EventListener;
import java.awt.event.MouseMotionListener;
//用于下子和记录信息的棋板
//棋盘大小为(404,404),棋子大小16*16
public class QiBan extends Panel implements MouseMotionListener
{
//跟主游戏界面的接口
Game game;
boolean ifRobot;
Robot robot = new Robot(this);
PeoplePlayer people = new PeoplePlayer(this);
//棋子是否为黑,棋子图片以及棋子边长
boolean ifBlack;
Image baiZiImage,heiZiImage;
//棋盘棋格的状态,图片及记录状态的二围数组
final int hei_state = -1;
final int bai_state = 1;
final int none_state = 0;
Image qiPanImage;
int [][] qiGes = new int[15][15];
//记录下子信息以及最后一颗子&当前鼠标
ZuoBiao [] qiZis = new ZuoBiao[225];
int lastQiZi;
ZuoBiao currMouse;
boolean hua = false;
//是否允许下本子棋,游戏是否结束了
boolean canPutBen;
boolean gameOver = false;
//屏外缓冲图
Image offscreen;
//游戏结束语
String str = "";
//创建一个指定主游戏接口的棋盘
public QiBan(Game game)
{
super();
//this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
this.game = game;
this.lastQiZi = -1;
this.setSize(403,403);
this.addMouseMotionListener(this);
//添加鼠标点击事件
this.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getButton()!=e.BUTTON1)
return;
if(!canPutBen)
{
System.out.println("a");
return;
}
int x = e.getX();
int y = e.getY();
if(x>403||y>403)
return;
if(!putDown(new ZuoBiao(((y-20)/13+1)/2,((x-20)/13+1)/2),ifBlack))
return;
canPutBen = false;
if(ifRobot)
robot.next();
else
people.next();
}
});
}
//开始游戏(指定黑白方)
public void start()
{System.out.println("start()");
this.gameOver = false;
this.canPutBen = ifBlack;
if(this.canPutBen)
for(int i=0;i<+this.lastQiZi;i++)
{
this.qiZis[i] = null;
}
this.lastQiZi = -1;
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
this.qiGes[i][j] = 0;
}
if(this.ifRobot&&!this.ifBlack)
this.robot.next();
if(!this.ifRobot&&!this.ifBlack)
this.people.next();
//更新画面
this.repaint();
}
//悔棋
public void back()
{
if(this.lastQiZi<1)
{
JOptionPane.showMessageDialog(game,"已无子可悔!!","悔棋",JOptionPane.ERROR_MESSAGE);
return;
}
qiGes[qiZis[lastQiZi].x][qiZis[lastQiZi].y] = none_state;
this.lastQiZi--;
qiGes[qiZis[lastQiZi].x][qiZis[lastQiZi].y] = none_state;
this.lastQiZi--;
repaint();
}
//黑白互换
public void change()
{
if(!this.gameOver)
{
this.ifBlack = !this.ifBlack;
}
}
public void update(Graphics g)
{//System.out.print("update(),");
updateGame();
this.updateOffscreen();
if(gameOver)
{
Graphics og = this.offscreen.getGraphics();
og.setColor(Color.blue);
og.setFont(new Font(null,Font.PLAIN,30));
og.drawString(str,95,100);
this.paint(this.getGraphics());
this.canPutBen = false;
this.game.endGame();
}
paint(g);
}
public void paint(Graphics g)
{//System.out.print("paint(),");
this.createImages();
g.drawImage(this.offscreen,0,0,this);
if(currMouse!=null)
{
if(!hua)
return;
Graphics2D gd = (Graphics2D)g;
gd.setColor(Color.red);
gd.setStroke(new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
int k = 5;
gd.draw(new Line2D.Float(6+currMouse.y*26,6+currMouse.x*26,6+currMouse.y*26+k,6+currMouse.x*26));
gd.draw(new Line2D.Float(6+currMouse.y*26,6+currMouse.x*26,6+currMouse.y*26,6+currMouse.x*26+k));
gd.draw(new Line2D.Float(6+currMouse.y*26+26-k,6+currMouse.x*26,6+currMouse.y*26+26,6+currMouse.x*26));
gd.draw(new Line2D.Float(6+currMouse.y*26+26,6+currMouse.x*26,6+currMouse.y*26+26,6+currMouse.x*26+k));
gd.draw(new Line2D.Float(6+currMouse.y*26,6+currMouse.x*26+26-k,6+currMouse.y*26,6+currMouse.x*26+26));
gd.draw(new Line2D.Float(6+currMouse.y*26+26-k,6+currMouse.x*26+26,6+currMouse.y*26+26,6+currMouse.x*26+26));
gd.draw(new Line2D.Float(6+currMouse.y*26+26,6+currMouse.x*26+26-k,6+currMouse.y*26+26,6+currMouse.x*26+26));
gd.draw(new Line2D.Float(6+currMouse.y*26,6+currMouse.x*26+26,6+currMouse.y*26+k,6+currMouse.x*26+26));
}
}
//更新游戏
public void updateGame()
{//System.out.print("updateGame(),");
robot.updateScore();
}
//在指定的位置下一颗指定颜色的棋
public boolean putDown(ZuoBiao z,boolean ifHei)
{System.out.print("putDown(),");
//不符合条件的棋子就直接return
if(this.gameOver) return false;
if(z.x<0||z.x>14||z.y<0||z.y>14) return false;
if(qiGes[z.x][z.y]!=this.none_state)
return false;
this.qiZis[++lastQiZi] = z;
//记录此棋格信息
if(ifHei)
qiGes[z.x][z.y] = this.hei_state;
else
qiGes[z.x][z.y] = this.bai_state;
//画下此棋子
this.update(this.getGraphics());
return true;
}
//更新缓冲图片,即画上新的棋子
public void updateOffscreen()
{//System.out.print("updateOffscreen(),");
Graphics og = offscreen.getGraphics();
og.drawImage(this.qiPanImage,0,0,this);
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
if(this.qiGes[i][j]==this.bai_state)
og.drawImage(baiZiImage,6+j*26,6+i*26,this);
if(this.qiGes[i][j]==this.hei_state)
og.drawImage(this.heiZiImage,6+j*26,6+i*26,this);
}
if(this.lastQiZi>-1)
{
ZuoBiao temp = qiZis[this.lastQiZi];
Graphics2D ogd = (Graphics2D)og;
ogd.setColor(Color.red);
ogd.setStroke(new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
ogd.draw(new Line2D.Float(6+26*temp.y+13,6+26*temp.x+8,6+26*temp.y+13,6+26*temp.x+18));
ogd.draw(new Line2D.Float(6+26*temp.y+8,6+26*temp.x+13,6+26*temp.y+18,6+26*temp.x+13));
}
}
//判断个图片是否加载成功,如果没有则重新加载
//并把棋盘画到缓冲图上去
public void createImages()
{
if(this.baiZiImage==null || this.baiZiImage.getWidth(this)<0)
this.baiZiImage = this.getToolkit().getImage("images\\baizi.jpg");
if(this.heiZiImage==null || this.heiZiImage.getWidth(this)<0)
this.heiZiImage = this.getToolkit().getImage("images\\heizi.jpg");
if(this.qiPanImage==null || this.qiPanImage.getWidth(this)<0)
this.qiPanImage = this.getToolkit().getImage("images\\qipan.jpg");
while(this.offscreen==null || this.offscreen.getWidth(this)!=this.getWidth()
||this.offscreen.getHeight(this)!=this.getHeight())
{
this.offscreen = this.createImage(this.getWidth(),this.getHeight());
offscreen.getGraphics().drawImage(this.qiPanImage,0,0,this);
}
}
//----------------------get set-----------------
public boolean getGameOver()
{
return this.gameOver;
}
public void setGameOver(boolean gameOver)
{
this.gameOver = gameOver;
this.repaint();
}
public void setStr(String str)
{
this.str = str;
}
public void setIfBlack(boolean ifBlack)
{
this.ifBlack = ifBlack;
}
public void mouseDragged(MouseEvent e)
{
// TODO: 在这添加你的代码
}
public void mouseMoved(MouseEvent e)
{
int x,y;
x = e.getX();
y = e.getY();
if(x>390||y>390)
return;
if(this.gameOver)
{
this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
return;
}
if(this.canPutBen)
{
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
if(qiGes[((y-20)/13+1)/2][((x-20)/13+1)/2]!=none_state)
return;
if(this.currMouse==null)
currMouse = new ZuoBiao(((y-20)/13+1)/2,((x-20)/13+1)/2);
else if(((y-20)/13+1)/2==currMouse.x&&((x-20)/13+1)/2==currMouse.y)
{
return;
}
else
{
currMouse.x = ((y-20)/13+1)/2;
currMouse.y = ((x-20)/13+1)/2;
hua = true;
paint(this.getGraphics());
}
}
else
this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -