五子棋小游戏

package 五子棋;

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

import javax.swing.JFrame;

public class test {
	public static void main(String[] args) {
		JFrame frame = new JFrame("五子棋小游戏");
		frame.setSize(600, 600);
		frame.setResizable(false);//大小不可变
		frame.setLocationRelativeTo(null);//默认居中
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//按下叉号默认关闭
		//frame.getContentPane().setBackground(new Color(240, 233, 217));//修改背景颜色
		chessBoard cb = new chessBoard();
		frame.setBackground(Color.LIGHT_GRAY);
		frame.add(cb);
		frame.setVisible(true);
		Graphics g = cb.getGraphics();
		cb.paint(g);
		cb.chessClick();
		//cb.chessChanged(g);
	}

}

package 五子棋;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class chessBoard extends JPanel {
//设置格子的大小
	private int size = 50;
//10*10的表格
	private int length = 500;
//设置鼠标的位置
	private int x;
	private int y;
//设置棋子落下的位置
	private int tx;
	private int ty;
//设置棋子的位置 二维数组
	private int lx;
	private int ly;
	int[][] location = new int[12][12];
//private int i = 1;
//是否是黑棋
	Boolean isBlack = true;

//绘制10*10表格
	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		// g.drawLine(50, 50, 50, length+50);
		for (int i = 0; i < 11; i++) {
			g.drawLine(50 + (size * i), 50, 50 + (size * i), length + 50);
		}
		for (int j = 0; j < 11; j++) {
			g.drawLine(50, 50 + (size * j), length + 50, 50 + (size * j));
		}

	}

	public void chessClick() {
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mouseClicked(e);
				x = e.getX();
				y = e.getY();
				tx = returnX();
				ty = returnY();
				lx = tx / size;
				ly = ty / size;
				/*
				 * System.out.println("TX:"+tx); System.out.println("TY:"+ty);
				 * System.out.println("LX:"+lx); System.out.println("LY:"+ly);
				 */
				chessChanged();
			}
		});
	}

	private int returnY() {
		if (((y - 50) % size) < 25) {
			return ((y - 50) / size) * size + 50;
		} else {
			return ((y - 50) / size + 1) * size + 50;
		}
	}

	private int returnX() {
		if (((x - 50) % size) < 25) {
			return ((x - 50) / size) * size + 50;
		} else {
			return ((x - 50) / size + 1) * size + 50;
		}
	}

	public void chessChanged() {
		Color white = Color.WHITE;
		Color black = Color.BLACK;
		Graphics g = getGraphics();
		if (isBlack) {
			location[lx][ly] = 1;
			g.setColor(black);
			g.fillOval(tx - 10, ty - 10, 20, 20);
			isBlack = false;
		} else {
			location[lx][ly] = 2;
			g.setColor(white);
			g.fillOval(tx - 10, ty - 10, 20, 20);
			isBlack = true;
		}
		// this.repaint();
		if (this.isWin()) {
			if (isBlack) {
				JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
			} else {
				JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
			}
		}
	}

	private boolean isWin() {
		Boolean flag = false;
		int color = location[lx][ly];
		int count = checkCount(1, 0, color);
		//System.out.println("count:" + count);
		if (count >= 5) {
			flag = true;
		} else {
			count = checkCount(0, 1, color);
			if (count >= 5) {
				flag = true;
			} else {
				count = checkCount(1, 1, color);
				if (count >= 5) {
					flag = true;
				} else {
					count = checkCount(1, -1, color);
					if (count >= 5) {
						flag = true;
					}
				}
			}
		}
		return flag;
	}

	private int checkCount(int xChange, int yChange, int color) {
		//System.out.println(color);// 1是黑色,2是白色
		int number = 1;
		int tempX = xChange;
		int tempY = yChange;
		/*
		 * int to = lx+xChange; int ti = ly+yChange;
		 * 
		 * System.out.println("lx+xChange:"+to+",ly+yChange:"+ti);
		 * System.out.println("=======");
		 */
		while (lx + xChange <= 11 && ly + yChange <= 11 && lx + xChange > 0 && ly + yChange > 0
				&& color == location[lx + xChange][ly + yChange]) {
			number++;
			if (xChange != 0)
				xChange++;
			if (yChange != 0) {
				if (yChange > 0)
					yChange++;
				else {
					yChange--;
				}
			}
		}
		xChange = tempX;
		yChange = tempY;
		while (lx - xChange <= 11 && ly - yChange <= 11 && lx - xChange > 0 && ly - yChange > 0
				&& color == location[lx - xChange][ly - yChange]) {
			number++;
			if (xChange != 0)
				xChange++;
			if (yChange != 0) {
				if (yChange > 0)
					yChange++;
				else {
					yChange--;
				}
			}
		}
		return number;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值