java五子棋人机

大憨熊 提交于 2020-01-28 17:05:53

初学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 (check() <= 5) {
					this.repaint();
					if (this.win()) {
						JOptionPane.showMessageDialog(this, "黑  方  赢");
						this.canplay = false;
					}
					k = 1;
					while (chess[x][y] != 0 && canplay) {

						if (h >= 3) {
							x += k;

							if (chess[x][y] == 2) {
								while (chess[x][y] != 0 && canplay) {
									k = -1;
									x += k;
								}
							}
						} else if (s >= 3) {
							y += k;

							if (chess[x][y] == 2) {
								while (chess[x][y] != 0 && canplay) {
									k = -1;
									y += k;
								}
							}
						} else if (p >= 3) {
							x += k;
							y -= k;

							if (chess[x][y] == 2) {
								while (chess[x][y] != 0 && canplay) {
									k = -1;
									x += k;
									y -= k;
								}
							}
						} else if (l >= 3) {
							x += k;
							y += k;

							if (chess[x][y] == 2) {
								while (chess[x][y] != 0 && canplay) {
									k = -1;
									x += k;
									y += k;
								}
							}
						} else {
							for (int i = 1; i < 25; i++) {
								for (int j = 1; j < 25; j++) {
									if (chess[i][j] == 0)

									{
										b = 1;
										c = 1;
										if (i + c < 25) {
											while (chess[i + c][j] == 2) {
												c++;
												b = b * 10;
												if (i + c >= 25)
													break;
											}
										}
										bai[i][j] += b;
										if (i + c < 25) {
											if (chess[i + c][j] == 1)
												bai[i][j] -= 10;
										}
										b = 1;
										c = 1;
										if (i - c >= 0) {
											while (chess[i - c][j] == 2) {
												c++;
												b = b * 10;
												if (i - c < 0)
													break;
											}
										}
										bai[i][j] += b;
										if (i - c >= 0) {
											if (chess[i - c][j] == 1)
												bai[i][j] -= 10;
										}
										b = 1;
										c = 1;
										if (j + c < 25) {
											while (chess[i][j + c] == 2) {
												c++;

												b = b * 10;
												if (j + c >= 25)
													break;
											}
										}
										bai[i][j] += b;
										if (j + c < 25) {
											if (chess[i][j + c] == 1)
												bai[i][j] -= 10;
										}
										b = 1;
										c = 1;
										if (j - c >= 0) {
											while (chess[i][j - c] == 2) {
												c++;
												b = b * 10;
												if (j - c < 0)
													break;
											}
										}
										bai[i][j] += b;
										if (j - c >= 0) {
											if (chess[i][j - c] == 1)
												bai[i][j] -= 10;
										}
										b = 1;
										c = 1;
										if (i + c < 25 && j + c < 25) {
											while (chess[i + c][j + c] == 2) {
												c++;
												b = b * 10;
												if (j + c >= 25 || i + c >= 25)
													break;
											}
										}
										bai[i][j] += b;
										if (i + c < 25 && j + c < 25) {
											if (chess[i + c][j + c] == 1)
												bai[i][j] -= 10;
										}
										b = 1;
										c = 1;
										if (i - c >= 0 && j - c >= 0) {
											while (chess[i - c][j - c] == 2) {
												c++;
												b = b * 10;
												if (j - c < 0 || i - c < 0)
													break;
											}
										}
										bai[i][j] += b;
										if (i - c >= 0 && j - c >= 0) {
											if (chess[i - c][j - c] == 1)
												bai[i][j] -= 10;
										}
										b = 1;
										c = 1;
										if (i - c >= 0 && j + c < 25) {
											while (chess[i - c][j + c] == 2) {
												c++;
												b = b * 10;
												if (i - c < 0 || j + c >= 25)
													break;
											}
										}
										bai[i][j] += b;
										if (i - c >= 0 && j + c < 25) {
											if (chess[i - c][j + c] == 1)
												bai[i][j] -= 10;
										}

										b = 1;
										c = 1;
										if (i + c < 25 && j - c >= 0) {
											while (chess[i + c][j - c] == 2) {
												c++;
												b = b * 10;
												if (i + c >= 25 || j - c <= 0)
													break;
											}
										}
										bai[i][j] += b;
										if (i + c < 25 && j - c >= 0) {
											if (chess[i + c][j - c] == 1)
												bai[i][j] -= 10;
										}

										if (bai[i][j] >= max) {
											max = bai[i][j];

											if (max >= 11) {
												x = i;

												y = j;
											}
										}
									}

								}
							}

						}
						if (max <= 10)
							x += 1;

					}
					if (chess[x][y] == 0) {
						chess[x][y] = 2;
						this.repaint();
						if (this.win()) {
							JOptionPane.showMessageDialog(this, "白  方  赢");
							this.canplay = false;
						}
					}
				} else
					chess[x][y] = 0;
			}
		}
	}

}

public boolean win() {
	boolean la = false;
	int po;
	po = check();
	if (po == 5)
		la = true;
	else
		la = false;
	return la;
}

public int check() {
	int po = 0, a;
	a = chess[x][y];
	m = x;
	n = y;
	h = 0;
	s = 0;
	p = 0;
	l = 0;
	while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
		x++;
		po++;
		h++;
	}
	x = m;
	po = po - 1;
	h = h - 1;
	while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
		x--;
		po++;
		h++;
	}
	if (po > 5) {
		x = m;
		h = 0;
		JOptionPane.showMessageDialog(this, "禁手");

	}
	x = m;
	if (po < 5) {
		po = 0;
		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
			y++;
			po++;
			s++;
		}
		y = n;
		po = po - 1;
		s = s - 1;
		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
			y--;
			po++;
			s++;
		}
		if (po > 5) {
			y = n;
			s = 0;
			JOptionPane.showMessageDialog(this, "禁手");

		}
		y = n;

	}
	if (po < 5) {
		po = 0;
		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
			x++;
			y++;
			po++;
			l++;
		}
		x = m;
		y = n;
		po = po - 1;
		l = l - 1;
		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
			x--;
			y--;
			po++;
			l++;
		}
		if (po > 5) {
			x = m;
			y = n;
			l = 0;
			JOptionPane.showMessageDialog(this, "禁手");

		}
		x = m;
		y = n;
	}
	if (po < 5) {
		po = 0;
		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
			x++;
			y--;
			po++;
			p++;
		}
		x = m;
		y = n;
		po = po - 1;
		p = p - 1;
		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
			x--;
			y++;
			po++;
			p++;
		}
		if (po > 5) {
			x = m;
			y = n;
			p = 0;
			JOptionPane.showMessageDialog(this, "禁手");

		}
		x = m;
		y = n;
	}
	return po;
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public static void main(String[] args) {
	new 人机();
}

}
`
游戏简陋,内容复杂。。。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!