Java:用Frame实现五子棋小程序

代码功能和界面比较粗糙,还有更多的完善空间。

思路:生成窗口 -> 画棋盘 ->真值数组和颜色数组 ->搜索判赢

注:文件中的主类名为Board

import java.awt.*;
import java.awt.event.*;

//import javax.swing.Painter;

public class Board extends Frame implements MouseListener {
	int win=0;
	int color_=0;
	boolean loc[][]=new boolean[15][15];    //记录此位置是否有棋子
	int colors[][]=new int[15][15];        //记录此位置棋子颜色
	
	public void tte(){ for(int i=0;i<15;i++) { for(int j=0;j<15;j++) {    //清空颜色数组
		colors[i][j]=-1; } } }
	 
	public void paint(Graphics g) {        //画棋盘
		// final Graphics g2 = null;
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		for (int i = 0; i < 15; i++) {
			g2.drawLine(i * 30 + 100, 100, i * 30 + 100, 14 * 30 + 100);
			g2.drawLine(100, i * 30 + 100, 14 * 30 + 100, i * 30 + 100);
		}
		g2.setStroke(new BasicStroke(3.5f));        //加粗棋盘边线
		g2.drawLine(100, 100, 100, 14 * 30 + 100);
		g2.drawLine(30 * 14 + 100, 100, 30 * 14 + 100, 14 * 30 + 100);
		g2.drawLine(100, 100, 14 * 30 + 100, 100);
		g2.drawLine(100, 30 * 14 + 100, 14 * 30 + 100, 14 * 30 + 100);
		g2.fillOval(100+85,100+85,5,5);
		g2.fillOval(185,95+11*30,5,5);
		g2.fillOval(95+11*30,185,5,5);
		g2.fillOval(95+11*30,95+11*30,5,5);
	}

	public Board() {        //显示棋盘和实现关闭窗口指令和鼠标监听器
		setSize(800, 800);
		setBackground(Color.ORANGE);
		setTitle("五子棋");
		setVisible(true);
		//paint(null);
		addWindowListener(new closeWin());
		tte();
		addMouseListener(this);
	}

	public static void main(String[] args) {        //新建窗口
		
		new Board();
	}

	class closeWin extends WindowAdapter {        //关闭窗口
		@Override
		public void windowClosing(WindowEvent e) {
			Window w = e.getWindow();
			w.dispose();
		}
	}
	public void find(int xx,int yy,int color) {        //判赢
		if(1+find1(xx-1,yy-1,color)+find5(xx+1,yy+1,color)>=5) {
			if(color==1)System.out.println("黑子胜!");
			else System.out.println("白子胜!");
			win=1;
		}
		else if(fi_2(xx,yy,color)>=5) {
			if(color==1)System.out.println("黑子胜!");
			else System.out.println("白子胜!");
			win=1;
		}
		else if(1+find3(xx+1,yy-1,color)+find7(xx-1,yy+1,color)>=5) {
			if(color==1)System.out.println("黑子胜!");
			else System.out.println("白子胜!");
			win=1;
		}
		else if(fi_4(xx,yy,color)>=5) {
			if(color==1)System.out.println("黑子胜!");
			else System.out.println("白子胜!");
			win=1;
		}
		if(win==1) {        //一方赢时输出信息,然后重新生成一个窗口进行下一场
			for(int i=0;i<15;i++) {
				for(int j=0;j<15;j++) {
					colors[i][j]=-1;
					loc[i][j]=false;
				}
			}
			new Board();
		}
	}

    //find和fi_都为搜索函数
	public int fi_2(int xx,int yy,int color) {           //遍历竖向
		int k=0,max=-1;
		for(int i=0;i<14;i++) {
			if(colors[xx][i]==color) {
				k++;
				if(k>max) {
					max=k;
				}
			}
			else k=0;
		}
		//System.out.println("竖着:"+max);
		return max;
	}
	public int fi_4(int xx,int yy,int color) {        //遍历横向
		int k=0,max=-1;
		for(int i=0;i<14;i++) {
			if(colors[i][yy]==color) {
				k++;
				if(k>max) {
					max=k;
				}
			}
			else k=0;
		}
		//System.out.println("横着:"+max);
		return max;
	}
	public int find1(int xx,int yy,int color) {        //递归
		//System.out.println("Find1*");
		if(xx>14||yy>14||xx<0||yy<0) {
			return 0;
		}
		else {
			if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
			else {
				//System.out.println(1+find1(xx-1,yy-1,color));
				return 1+find1(xx-1,yy-1,color);
			}
		}
	}
	public int find3(int xx,int yy,int color) {        //递归
		//System.out.println("Find3");
		if(xx>14||yy>14||xx<0||yy<0) {
			return 0;
		}
		else {
			if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
			else return 1+find3(xx+1,yy-1,color);
		}
	}
	public int find5(int xx,int yy,int color) {        //递归
		if(xx>14||yy>14||xx<0||yy<0) {
			return 0;
		}
		else {
			if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
			else return 1+find5(xx+1,yy+1,color);
		}
	}
	public int find7(int xx,int yy,int color) {        //递归
		if(xx>14||yy>14||xx<0||yy<0) {
			return 0;
		}
		else {
			if(loc[xx][yy]==false||colors[xx][yy]!=color)return 0;
			else return 1+find7(xx-1,yy+1,color);
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {        //在棋盘上增加棋子
		// TODO 自动生成的方法存根
		
			Graphics g = this.getGraphics();
			//g.drawOval(e.getX(), e.getY(), 5, 5);
			int xx=e.getX();
			int yy=e.getY();
			xx=(xx-100+15)/30;
			yy=(yy-100+15)/30;
			if(xx>14||yy>14)return;
			else if(loc[xx][yy])return;
			else {
				if(color_%2==0) {
					g.setColor(Color.black);
					colors[xx][yy]=1;
				}
				else {
					g.setColor(Color.white);
					colors[xx][yy]=0;
				}
				//System.out.println(xx+","+yy);
				loc[xx][yy]=true;
				g.fillOval(xx*30-7+100,yy*30-7+100, 12, 12);
				color_++;
				find(xx,yy,colors[xx][yy]);
			}
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO 自动生成的方法存根
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO 自动生成的方法存根

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO 自动生成的方法存根

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淬炼之火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值