java 五子棋源代码+注释

package cn.hcnu.fiveChess;


import java.awt.Color;
import java.awt.Graphics;




public class Chess {
private Point point;
private boolean isBlack;
public final static int SIZE=20;
public Chess(Point point, boolean isBlack) {
double minDistance=10000;
Point temp=null;
for (int i = 0; i < FiveChess.points.length; i++) {//把点画在分界线
for (int j = 0; j < FiveChess.points[i].length; j++) {
if (minDistance>getDistance(point, FiveChess.points[i][j])) {
minDistance=getDistance(point, FiveChess.points[i][j]);
temp=FiveChess.points[i][j];
}
}
}
if (temp!=null) {
point = temp;
}
this.point = point;
this.isBlack = isBlack;
}
public double getDistance(Point p1,Point p2){
return Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(), 2));
}
public void draw(Graphics g) {
if(isBlack){
g.setColor(Color.black);
}else{
g.setColor(Color.white);
}
g.fillOval(point.getX()-SIZE/2, point.getY()-SIZE/2, SIZE, SIZE);


}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public boolean isBlack() {
return isBlack;
}
public void setBlack(boolean isBlack) {
this.isBlack = isBlack;
}




}




package cn.hcnu.fiveChess;


import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;


public class FiveChess extends JFrame implements MouseListener, ActionListener{
private Chess chess[]=new Chess[0];
private JPanel game;
private Timer timer;//定时
private int t=1;
private JLabel lbPlaer1,lbPlaer2;
private boolean isBlackPlayer;//哪个玩家下的棋
private int countI=0;//记录points[i][j]是否可以下棋
private int countJ=0;
private boolean BlackWin,WhiteWin;//可以只设置一个blackWin,我这只是为了美观
static Point points[][]=new Point[20][20];
static{
for (int i = 0; i < points.length; i++) {
for (int j = 0; j < points[i].length; j++) {
points[i][j]=new Point(50+23*i,50+23*j);//设置点间隔

}
}
public FiveChess(){
this.setBounds(300, 100, 590, 600);//设置位置
this.setLayout(null);
setTitle("五子棋");
this.getContentPane().setBackground(Color.green);//设置背景颜色
//添加菜单栏
lbPlaer1=new JLabel("玩家1");
lbPlaer1.setFont(new Font("aaa", Font.BOLD, 20));
lbPlaer1.setBounds(520, 150, 80, 50);
this.getContentPane().add(lbPlaer1);
lbPlaer2=new JLabel("玩家2");
lbPlaer2.setForeground(Color.white);//设置字体颜色,
lbPlaer2.setFont(new Font("aaa", Font.BOLD, 20));
lbPlaer2.setBounds(10, 150, 80, 50);
this.getContentPane().add(lbPlaer2);
addMenu();
//主程序:
game=new GamesPanel();//画棋盘
timer=new Timer(500, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
t++;
if(isBlackPlayer&&t%2==0){
lbPlaer1.setVisible(false);
lbPlaer2.setVisible(true);
}else if(isBlackPlayer&&t%2!=0){
lbPlaer1.setVisible(true);
lbPlaer2.setVisible(true);
}else if(!isBlackPlayer&&t%2==0){
lbPlaer1.setVisible(true);
lbPlaer2.setVisible(false);
}else if(!isBlackPlayer&&t%2!=0){
lbPlaer1.setVisible(true);
lbPlaer2.setVisible(true);
}

}
});
timer.start();
this.getContentPane().add(game);
game.addMouseListener(this);//加监听
game.setBounds(20, 20, 500, 500);//设置棋盘位置
isBlackPlayer=true;//黑子先下
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}


private void addMenu() {//菜单项
JMenuBar bar=new JMenuBar();
this.setJMenuBar(bar);
bar.setBackground(Color.GREEN);
JMenu Jgame=new JMenu("游戏");
bar.add(Jgame);
JMenuItem NewMenu=new JMenuItem("开始游戏");
Jgame.add(NewMenu);
JMenuItem PauseMenu=new JMenuItem("暂停游戏");
Jgame.add(PauseMenu);
JMenuItem ExitMenu=new JMenuItem("退出游戏");
Jgame.add(ExitMenu);
JMenu Modegame=new JMenu("游戏模式");
bar.add(Modegame);
JMenuItem ppMenu=new JMenuItem("人人对战");
Modegame.add(ppMenu);
JMenuItem pcMenu=new JMenuItem("人机对战");
Modegame.add(pcMenu);
//添加监听
NewMenu.addActionListener(this) ;
NewMenu.setActionCommand("new");
PauseMenu.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
timer.stop();

}
});
ExitMenu.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);

}
});


}


// private void CreatMyCursor(int i) {//设置光标,
// Toolkit toolkit=Toolkit.getDefaultToolkit();
// Image img=toolkit.createImage("./images/black.jpg");
// Cursor c=toolkit.createCustomCursor(img, new java.awt.Point(0, 0), "1");
// this.setCursor(c);
// }
public static void main(String[] args) {
FiveChess f=new FiveChess();
}
public static FiveChess getFiveChess(FiveChess f){
return f;

}
class GamesPanel extends JPanel{
@Override
public void paint(Graphics g) {
g.setColor(Color.BLUE);
for (int i = 0; i < points.length; i++) {//棋盘
g.drawLine(points[0][i].getX(), points[0][i].getY(),
points[points.length - 1][i].getX(),
points[points.length - 1][i].getY());//划线
}
for (int i = 0; i < points.length; i++) {
g.drawLine(points[i][0].getX(), points[i][0].getY(),
points[i][points.length - 1].getX(),
points[i][points.length - 1].getY());//划线
}


if (chess!=null&&chess.length>0) {
for (int i = 0; i < chess.length; i++) {
//棋子
chess[i].draw(g);
}
}
}
}


private void addPerson(MouseEvent e) {//添加棋子
Point p=new Point(e.getX(),e.getY());
if (getLastPoint(p)||e.getX()<40||e.getY()<40||e.getX()>500||e.getY()>500) {


}else{
Chess[] temp=new Chess[chess.length+1];
for(int i=0;i<chess.length;i++){
temp[i]=chess[i];
}
temp[temp.length-1]=new Chess(p, isBlackPlayer);
chess=temp;
temp=null;
isBlackPlayer=!isBlackPlayer;
if (isBlackPlayer) {
lbPlaer1.setVisible(false);
lbPlaer2.setVisible(true);
}else{
lbPlaer2.setVisible(false);
lbPlaer1.setVisible(true);
}
t=1;
if(chess[chess.length-1].isBlack()){
points[countI][countJ].setBlackChess(1);
}else{
points[countI][countJ].setBlackChess(2);
}
points[countI][countJ].setValid(true);//设置不可用
}
repaint();//重画后再弹出对话框
if (ifWin()) {
int i;
if(BlackWin){
i=JOptionPane.showConfirmDialog(null, "黑棋胜出,是否继续", "游戏结束!", JOptionPane.YES_NO_OPTION);
}else{
i=JOptionPane.showConfirmDialog(null, "白棋胜出,是否继续", "游戏结束!", JOptionPane.YES_NO_OPTION);
}
if(i==JOptionPane.YES_OPTION){
this.dispose();
new FiveChess();
}
}
}
private void addComputer(MouseEvent e) {
Point p=new Point();
int maxX=0,maxY=0;
String str[]=new String[points.length];
for(int i=0;i<points.length;i++){
for(int j=0;j<points[i].length;j++){
str[i]+=points[i][j].getBlackChess();
if(str[i].indexOf("01111")>0){
p=new Point(str[i].indexOf("01111"), j);
}else if(str[i].indexOf("11110")>0){
p=new Point(str[i].indexOf("11110")+5, j);
}
}
}
//TODO
}
private boolean ifWin() {
String[] str=new String[points.length];
for(int i=0;i<points.length;i++){//判断竖是否赢
for(int j=0;j<points[i].length;j++){
str[i]=str[i]+points[i][j].getBlackChess();
}
if(str[i].indexOf("11111")>0){
BlackWin=true;
return true;
}else if(str[i].indexOf("22222",0)!=-1){
WhiteWin=true;
return true;
}
}
for(int i=0;i<points.length;i++){//判断横的是否赢
for(int j=0;j<points[i].length;j++){
str[i]=str[i]+points[j][i].getBlackChess();
}
if(str[i].indexOf("11111")>0){
BlackWin=true;
return true;
}else if(str[i].indexOf("22222",0)!=-1){
WhiteWin=true;
return true;
}
}
str=new String[2*points.length-1];//改变数组大小
for (int m = 0; m < str.length; m++) {//判断/方向的是否赢
for (int i = 0; i < points.length; i++) {
for (int j = 0; j < points.length; j++) {
if(i+j==m){
str[m]=str[m]+points[i][j].getBlackChess();
}
if(j>m||i>m){
break;
}
}
if(i>m){
break;
}
}
if(str[m].indexOf("11111")>0){
BlackWin=true;
return true;
}else if(str[m].indexOf("22222",0)>0){
WhiteWin=true;
return true;
}
}
str=new String[2*points.length-1];//重建数组大小,必须,否则会覆盖
for(int m=0;m<str.length;m++){
for(int i=0;i<points.length;i++){//y
for(int j=0;j<points.length;j++){//x
if (m>str.length/2) {
if (j - i == str.length / 2 -m) {
str[m]=str[m]+points[i][j].getBlackChess();
}
}else if(m<=str.length/2){
if (i -j == str.length / 2 -m) {
str[m]=str[m]+points[j][i].getBlackChess();//注意x,y置换
}
}
}
}
if(str[m].indexOf("11111")>0){
BlackWin=true;
return true;
}else if(str[m].indexOf("22222",0)>0){
WhiteWin=true;
return true;
}
}




return false;
}


public boolean getLastPoint(Point point){//获得最近的point[i][j] 看看是否已经有子
double minDistance=10000;
Point temp=null;
for (int i = 0; i < FiveChess.points.length; i++) {
for (int j = 0; j < FiveChess.points[i].length; j++) {
if (minDistance>getDistance(point, FiveChess.points[i][j])) {
minDistance=getDistance(point, FiveChess.points[i][j]);
temp=FiveChess.points[i][j];
countI=i;
countJ=j;
}
}
}
return points[countI][countJ].isValid();
}
private double getDistance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(), 2));
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub


}
@Override
public void mousePressed(MouseEvent e) {
addPerson(e);
addComputer(e);
repaint();
}


@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub


}
@Override
public void mouseEntered(MouseEvent e) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));


}
@Override
public void mouseExited(MouseEvent e) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));//退出棋盘是换种光标


}


@Override
public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("new")){
this.dispose();
new FiveChess();
}
}
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值