java五子棋人机

本文是作者作为Java初学者,通过编写一个简单的五子棋人机对战程序来锻炼逻辑思维能力的初步尝试。目前程序尚不完善,计划未来继续优化。
摘要由CSDN通过智能技术生成

初学Java,做个简单的五子棋人机对战(比较烂)锻炼一下逻辑思维,,, 暂时保存,以后完善。。。`

package 五子棋;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.*;
public class 人机 extends JFrame implements MouseListener {

int x, y, X, Y, h, s, p, l, m, n, k, b = 10, c, max = 1, x1, y1;
int[][] chess = new int[25][25];// 1黑2白
int[][] bai = new int[25][25];// 判断白棋下的位置
boolean canplay = true;

public 人机() {
	this.setTitle("五子棋0.1");

	this.setSize(800, 800);
	this.setLocationRelativeTo(null);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setResizable(false);
	this.setVisible(true);
	this.repaint();
	this.addMouseListener(this);
}

public void paint(Graphics g) {
	// 创建一个图片缓冲区,先将图片放到缓冲区上
	BufferedImage buf = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);

	Graphics g1 = buf.createGraphics(); // 为该画布创建画笔
	g1.setColor(Color.gray);
	g1.fill3DRect(75, 75, 625, 625, true);// 画棋盘
	for (int i = 0; i <= 25; i++) {
		g1.setColor(Color.white);
		g1.drawLine(75, 75 + 25 * i, 700, 75 + 25 * i);
		g1.drawLine(75 + 25 * i, 75, 75 + 25 * i, 700);
	}
	for (int i = 0; i < 25; i++) {
		for (int j = 0; j < 25; j++) {
			if (chess[i][j] == 1) {
				X = i * 25 + 50;
				Y = j * 25 + 50;
				g1.setColor(Color.black);
				g1.fillOval(X, Y, 22, 22);
			}
			if (chess[i][j] == 2) {
				X = i * 25 + 50;
				Y = j * 25 + 50;
				g1.setColor(Color.white);
				g1.fillOval(X, Y, 22, 22);
			}
		}
	}
	g.drawImage(buf, 0, 0, this);

}

public void mousePressed(MouseEvent e) {
	x = e.getX();
	y = e.getY();
	int k = 0, t = 0;
	if (x >= 75 && x <= 700 && y >= 75 && y <= 700) {
		if (canplay) {
			x = (x - 50) / 25;
			y = (y - 50) / 25;
			if (chess[x][y] == 0) {
				chess[x][y] = 1;
				if 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值