【骐程】五子棋

19 篇文章 0 订阅
10 篇文章 0 订阅

【骐程】五子棋

附上所有代码:
(如最下)

效果
在这里插入图片描述

在这里插入图片描述

package wuziqi;

import javax.swing.JOptionPane;

public class Win {
	
	private int[][] chessArray;
	
	public Win(int[][] chessArray) {
		this.chessArray = chessArray;
	}
	public void isWin(int x,int y) {
		if(sp(x,y)>=5){
			if(chessArray[x][y] == 1) {
				System.out.println("黑棋赢了");
				JOptionPane.showInputDialog(null, "黑棋赢了",JOptionPane.ERROR_MESSAGE);
			}else if(chessArray[x][y] == -1) {
				System.out.println("白棋赢了");
				JOptionPane.showInputDialog(null, "白棋赢了",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	public int sp(int x,int y) {
		int count = 0;
		//当前位置向左向右去找
		if(chessArray[x-1][y] == chessArray[x][y] || chessArray[x+1][y] == chessArray[x][y]) {
			//向右
			for(int i=x+1;i<=chessArray.length;i++) {
				if(chessArray[i][y] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}
			//向左
			for(int i=x;i>=0;i--) {
				if(chessArray[i][y] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}
		}else if(chessArray[x][y-1] == chessArray[x][y] || chessArray[x][y+1] == chessArray[x][y]) {
			//向下
					for(int i=y+1;i<=chessArray.length;i++) {
						if(chessArray[x][i] == chessArray[x][y]) {
							count++;
						}else {
							break;
						}
					}
					
					//向上
					for(int i=y;i>=0;i--) {
						if(chessArray[x][i] == chessArray[x][y]) {
							count++;
						}else {
							break;
						}
					}
		}else if(chessArray[x-1][y-1] == chessArray[x][y] || chessArray[x+1][y+1] == chessArray[x][y]) {
			//向左上
			for(int i=x,j=y;i>=0&&j>=0;i--,j--) {
				if(chessArray[i][j] == chessArray[x][y]) {
					count++;
					//System.out.println("i="+i);
				}else {
					break;
				}
			}
			
			//向右下
			for(int i=x+1,j=y+1;i<=chessArray.length&&j<=chessArray.length;i++,j++) {
				if(chessArray[i][j] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}

		}else if(chessArray[x-1][y+1] == chessArray[x][y] || chessArray[x+1][y-1] == chessArray[x][y]) {
			//向右上
			for(int i=x,j=y;i<=chessArray.length&&j>=0;i++,j--) {
				//System.out.println("测试");
				if(chessArray[i][j] == chessArray[x][y]) {
					System.out.println("测试");
					count++;
					System.out.println("i="+i+"  j="+j);
				}else {
					break;
				}
			}
			//向左下
			for(int i=x-1,j=y+1;i>=0&&j<=chessArray.length;i--,j++) {
				if(chessArray[i][j] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}
		}	
								
		return count;
	}
}

思路:

1画图工具-画棋盘(paint,防止硬编码)(接口常量)—加监听器,重新定义一个类,画棋子-防止重回–判断输赢

具体实现

需要有一个画图工具,需要在画图工具上画出棋盘,这里需要画图的棋盘不停的显示,需要一直画在上面,所以需要在paint方法中一直实现,

在当前窗体直接使用paint方法,就不需要再保存画的棋盘,在当前这个类直接继承jframe

public void paint(Graphics g) {
        super.paint(g);
        System.out.println("绘制棋盘");

画线画棋盘(避免硬编码,就使用接口的常量),

public interface config {
    
    public static final int X0 = 62;
    public static final int Y0 = 100;
    public static final int LINE = 15;
    public static final int SIZE = 50;
    public static final int CHESS = 40;
    
}

新定义类不让画出的棋子消失:
public class GameMouse extends MouseAdapter implements config

画在交点上,并且有颜色

if(((x1-X0)%SIZE)<CHESS/2) {
     xx=(x1-X0)/SIZE;
 }else {
     xx=(x1-X0)/SIZE+1;
 }
 if(((y1-Y0)%SIZE)<CHESS/2) {
     yy = (y1-Y0)/SIZE;
 }else {
     yy = (y1-Y0)/SIZE+1;
 }
 
 if(chessArray[xx][yy]==0) {
 if(count%2==0) {
     gr.setColor(Color.BLACK);
     chessArray[xx][yy] = 1;
 }else {
     gr.setColor(Color.WHITE);
     chessArray[xx][yy] = -1;
 }

判断输赢:

public void isWin(int x,int y) {
        if(sp(x,y)>=5){
            if(chessArray[x][y] == 1) {
                System.out.println("黑棋赢了");
                JOptionPane.showInputDialog(null, "黑棋赢了",JOptionPane.ERROR_MESSAGE);
            }else if(chessArray[x][y] == -1) {
                System.out.println("白棋赢了");
                JOptionPane.showInputDialog(null, "白棋赢了",JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    public int sp(int x,int y) {
        int count = 0;

代码
chessUI类:

package wuziqi;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class ChessUI extends JFrame implements config{
	public static void main(String[] args) {
		ChessUI chessUI = new ChessUI();
		chessUI.DrawUI();
	}
	private int[][] chessArray;
	
	public void DrawUI() {
		//JFrame jf = new JFrame();
		this.setSize(820,850);
		this.setTitle("五子棋2.0");
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
		
		//边框布局管理器
		BorderLayout bla = new BorderLayout();
		this.setLayout(bla);
		//导入图片
		ImageIcon image = new ImageIcon("F:\\图片\\010906.jpg");
		
		//加入标签,放入图片
		JLabel jlb = new JLabel(image);
		Dimension dim  = new Dimension(800,800);
		jlb.setPreferredSize(dim);
		this.add(jlb);
		//菜单栏
		JMenuBar jmu = new JMenuBar();
		this.add(jmu,BorderLayout.NORTH);
		//主菜单选项
		JMenu jm = new JMenu("对战选项");
		jmu.add(jm);
		JMenu jm1 = new JMenu("对战模式");
		jmu.add(jm1);
		//子菜单
		JMenuItem jItem = new JMenuItem("重开");
		jm.add(jItem);
		JMenuItem jItem1 = new JMenuItem("悔棋");
		jm.add(jItem1);
		JMenuItem jItem2 = new JMenuItem("认输");
		jm.add(jItem2);
		
		JMenuItem jItem3 = new JMenuItem("人机对战");
		jm1.add(jItem3);
		JMenuItem jItem4 = new JMenuItem("人人对战");
		jm1.add(jItem4);
		
		this.setVisible(true);
		Graphics g = this.getGraphics();
		
		GameMouse mouse = new GameMouse();
		
		this.addMouseListener(mouse);
		mouse.gr=g;
		
		chessArray = mouse.getChessArray();
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		System.out.println("绘制棋盘");
		for(int i=0;i<LINE;i++) {
			g.drawLine(X0, Y0+i*SIZE,X0+(LINE-1)*SIZE,Y0+i*SIZE);
			g.drawLine(X0+i*SIZE, Y0, X0+i*SIZE, Y0+(LINE-1)*SIZE);
		}
		
		for(int i = 0;i< chessArray.length;i++){
			for(int j = 0;j< chessArray.length;j++) {
				if(chessArray[i][j] == 1) {
					g.setColor(Color.BLACK);
					g.fillOval(i*SIZE+X0-CHESS/2, j*SIZE+Y0-CHESS/2, CHESS, CHESS);
				}else if(chessArray[i][j] == -1){
					g.setColor(Color.WHITE);
					g.fillOval(i*SIZE+X0-CHESS/2, j*SIZE+Y0-CHESS/2, CHESS, CHESS);
				}

			}
		}
	}
}

接口常量:

public interface config {
	
	public static final int X0 = 62;
	public static final int Y0 = 100;
	public static final int LINE = 15;
	public static final int SIZE = 50;
	public static final int CHESS = 40;
	
}

Gamemouse:类

package wuziqi;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameMouse extends MouseAdapter  implements config{
	 
	public Graphics gr;
	public int x1,y1,xx,yy;
	private int count;
	private int[][] chessArray;
	private Win win ;
	
	public GameMouse(){
		chessArray = new int[LINE][LINE];
		win = new Win(chessArray);
	}
	
	public int[][] getChessArray() {
		return chessArray;
	}
	
    public void mouseClicked(MouseEvent e) {
    	
    	x1=e.getX();
    	y1=e.getY();
    	
    	if(((x1-X0)%SIZE)<CHESS/2) {
    		xx=(x1-X0)/SIZE;
    	}else {
    		xx=(x1-X0)/SIZE+1;
    	}
    	if(((y1-Y0)%SIZE)<CHESS/2) {
    		yy = (y1-Y0)/SIZE;
    	}else {
    		yy = (y1-Y0)/SIZE+1;
    	}
    	
    	if(chessArray[xx][yy]==0) {
    	if(count%2==0) {
    		gr.setColor(Color.BLACK);
    		chessArray[xx][yy] = 1;
    	}else {
    		gr.setColor(Color.WHITE);
    		chessArray[xx][yy] = -1;
    	}
    	win.isWin(xx, yy);
    	gr.fillOval(xx*SIZE+X0-CHESS/2, yy*SIZE+Y0-CHESS/2, CHESS, CHESS);
    	count++;
    	}
    }
}

判断输赢:

package wuziqi;

import javax.swing.JOptionPane;

public class Win {
	
	private int[][] chessArray;
	
	public Win(int[][] chessArray) {
		this.chessArray = chessArray;
	}
	public void isWin(int x,int y) {
		if(sp(x,y)>=5){
			if(chessArray[x][y] == 1) {
				System.out.println("黑棋赢了");
				JOptionPane.showInputDialog(null, "黑棋赢了",JOptionPane.ERROR_MESSAGE);
			}else if(chessArray[x][y] == -1) {
				System.out.println("白棋赢了");
				JOptionPane.showInputDialog(null, "白棋赢了",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	public int sp(int x,int y) {
		int count = 0;
		//当前位置向左向右去找
		if(chessArray[x-1][y] == chessArray[x][y] || chessArray[x+1][y] == chessArray[x][y]) {
			//向右
			for(int i=x+1;i<=chessArray.length;i++) {
				if(chessArray[i][y] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}
			//向左
			for(int i=x;i>=0;i--) {
				if(chessArray[i][y] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}
		}else if(chessArray[x][y-1] == chessArray[x][y] || chessArray[x][y+1] == chessArray[x][y]) {
			//向下
					for(int i=y+1;i<=chessArray.length;i++) {
						if(chessArray[x][i] == chessArray[x][y]) {
							count++;
						}else {
							break;
						}
					}
					
					//向上
					for(int i=y;i>=0;i--) {
						if(chessArray[x][i] == chessArray[x][y]) {
							count++;
						}else {
							break;
						}
					}
		}else if(chessArray[x-1][y-1] == chessArray[x][y] || chessArray[x+1][y+1] == chessArray[x][y]) {
			//向左上
			for(int i=x,j=y;i>=0&&j>=0;i--,j--) {
				if(chessArray[i][j] == chessArray[x][y]) {
					count++;
					//System.out.println("i="+i);
				}else {
					break;
				}
			}
			
			//向右下
			for(int i=x+1,j=y+1;i<=chessArray.length&&j<=chessArray.length;i++,j++) {
				if(chessArray[i][j] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}

		}else if(chessArray[x-1][y+1] == chessArray[x][y] || chessArray[x+1][y-1] == chessArray[x][y]) {
			//向右上
			for(int i=x,j=y;i<=chessArray.length&&j>=0;i++,j--) {
				//System.out.println("测试");
				if(chessArray[i][j] == chessArray[x][y]) {
					System.out.println("测试");
					count++;
					System.out.println("i="+i+"  j="+j);
				}else {
					break;
				}
			}
			//向左下
			for(int i=x-1,j=y+1;i>=0&&j<=chessArray.length;i--,j++) {
				if(chessArray[i][j] == chessArray[x][y]) {
					count++;
				}else {
					break;
				}
			}
		}	
								
		return count;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值