• 大小: 5KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: Java
  • 标签: 五子棋  

资源简介

一个五子棋的代码实现 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

资源截图

代码片段和文件信息

package wuziqi;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*
 * 创建一个棋盘面板。显示棋局,设置好鼠标事件处理。
 */
public class BoardPanel extends JPanel {
public static final int WIDTH=600;
public static final int HEIGHT=600;
private static int MARGIN=20;
private FiveInARowModel board;
private ComputerPlayer computerPlayer;
public BoardPanel(FiveInARowModel board){
this.board=board;
computerPlayer=new ComputerPlayer(board FiveInARowModel.BLACK);
this.setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Point p=e.getPoint();
int rowcol;
int dim=BoardPanel.this.board.getDimension();
int cellWidth=(getWidth()-2*MARGIN)/dim;
if(p.y>=MARGIN&&p.y p.x>=MARGIN&&p.x row=(p.y-MARGIN)/cellWidth;
col=(p.x-MARGIN)/cellWidth;
if(!BoardPanel.this.board.isEmpty(row col))
return;
try {
//user put a chessman
boolean win=BoardPanel.this.board.setChessman(row col FiveInARowModel.WHITE);
BoardPanel.this.drawChessman(row col);
if(win){//user wins
JOptionPane.showMessageDialog(BoardPanel.this “You win!“);
BoardPanel.this.board.clear();
BoardPanel.this.repaint();
}else if(BoardPanel.this.board.isFull()){//full
JOptionPane.showMessageDialog(BoardPanel.this “No empty cell!“);
BoardPanel.this.board.clear();
BoardPanel.this.repaint();
}else{
//computer put a chessman
win=computerPlayer.play();
int[]last=BoardPanel.this.board.getLastPut();
row=last[0];
col=last[1];
BoardPanel.this.drawChessman(row col);
if(win){//computer wins
JOptionPane.showMessageDialog(BoardPanel.this “Computer win!“);
BoardPanel.this.board.clear();
BoardPanel.this.repaint();
}else if(BoardPanel.this.board.isFull()){
JOptionPane.showMessageDialog(BoardPanel.this “No empty cell!“);
BoardPanel.this.board.clear();
BoardPanel.this.repaint();
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
public void paint(Graphics g) {
Graphics2D g2d=(Graphics2D) g;
//fill white back ground
g2d.setColor(Color.CYAN);
g2d.fillRect(0 0 WIDTH HEIGHT);
//write tip
g2d.setColor(Color.BLACK);
g2d.drawString(“Computer“ MARGIN MARGIN*2/3);
FontMetrics fm=g2d.getFontMetrics();
Rectangle2D rect=fm.getStringBounds(“Computer“ g2d);
int r=MARGIN-2;
g2d.drawOval(MARGIN+(int)rect.getWidth()+r 0 r

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-09-27 14:30  五子棋\
     文件        4555  2017-09-27 14:28  五子棋\BoardPanel.java
     文件        3331  2017-09-26 20:49  五子棋\ComputerPlayer.java
     文件        3209  2017-09-27 14:29  五子棋\FiveInARowModel.java
     文件         149  2017-09-26 20:25  五子棋\Main.java
     文件         488  2017-09-26 20:47  五子棋\Mainframe.java

评论

共有 条评论