JAVA程序设计(12.3)---- 监听器0基础应用:五子棋

1.制作五子棋游戏软件

由于老师已经基本做完了。重做的时候快了非常多……可是还是感觉思维非常混乱…… 哪边先哪边后,哪个方法在哪边好之类的问题 太纠结了……

先是窗体 内部类:鼠标适配器  窗体的构造器  画图

package com.lovo.homework2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

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

/**
 * 类 : 我的五子棋窗体
 * 
 * @author Abe
 */
public class MyFrameRenju extends JFrame {
	private MyboardRenju board = new MyboardRenju();
	private boolean isBlack = true;
	private Image offImage = new BufferedImage(800, 800,
			BufferedImage.TYPE_INT_RGB);// 双缓冲
	private boolean isGameOver = false;
	
	/**
	 * 内部类:鼠标适配器
	 * 
	 * @author Abe
	 */
	public class MyMouseAdapter extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) { // 重写点击鼠标的方法
			if (!isGameOver) {
				int x = e.getX();
				int y = e.getY();
				if (x > 25 && x < 775 && y > 25 && y < 775) {
					int i = (x - 25) / 50;
					int j = (y - 25) / 50;
					if (board.move(i, j, isBlack)) {
						repaint();
						if (board.win(i, j, isBlack)) {
							JOptionPane.showMessageDialog(null,
									isBlack ?

"黑方胜!

" : "白方胜"); isGameOver = true; } isBlack = !isBlack; } } } } } /** * 构造器 */ public MyFrameRenju() { this.setTitle("五子棋"); this.setSize(800, 800); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setLayout(null); this.getContentPane().setBackground(new Color(180, 125, 12)); MyMouseAdapter l = new MyMouseAdapter(); this.addMouseListener(l); } /** * 重写方法画出全部 newG均为双缓冲 去掉闪屏须要 */ @Override public void paint(Graphics g) { Graphics newG = offImage.getGraphics(); super.paint(newG); board.draw(newG); g.drawImage(offImage, 0, 0, 800, 800, null); } public static void main(String[] args) { new MyFrameRenju().setVisible(true); } }


然后是位置面板,绘制棋子。走棋。推断胜负

package com.lovo.homework2;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;

import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;

/**
 * 类 : 五子棋棋盘
 * 
 * @author Abe
 *
 */
public class MyboardRenju {
	private int[][] p = new int[15][15]; // 给每一个交点赋值

	public void draw(Graphics g) {
		g.setColor(Color.BLACK);
		Graphics2D g2d = (Graphics2D) g; // 强转g为2D型 赋值给g2d
		g2d.setStroke(new BasicStroke(5));
		g.drawRect(50, 50, 700, 700);
		g2d.setStroke(new BasicStroke(1));
		g.fillOval(392, 392, 16, 16); // 画天元 星
		g.fillOval(195, 195, 10, 10);
		g.fillOval(195, 595, 10, 10);
		g.fillOval(595, 195, 10, 10);
		g.fillOval(595, 595, 10, 10);

		for (int i = 0; i < 750; i += 50) { // 画横纵坐标线
			g.drawLine(50, 100 + i, 750, 100 + i);
			g.drawLine(100 + i, 50, 100 + i, 750);
		}
		for (int i = 0; i < p.length; i++) { // 画出棋盘上的棋子
			for (int j = 0; j < p.length; j++) {
				if (p[i][j] != 0) {
					g.setColor(p[i][j] == 1 ? Color.black : Color.WHITE);
					g.fillOval(25 + i * 50, 25 + j * 50, 50, 50);
				}
			}
		}
	}

	/**
	 * 走棋
	 */
	public boolean move(int i, int j, boolean isBlack) {
		if (p[i][j] == 0) {
			p[i][j] = isBlack ?

1 : 2; return true; } return false; } /** * 方法:推断胜负 */ public boolean win(int i, int j, boolean isBlack) { int currentColor = isBlack ? 1 : 2; if (countH(i, j, currentColor) >= 5 || countV(i, j, currentColor) >= 5 || countX1(i, j, currentColor) >= 5 || countX2(i, j, currentColor) >= 5) { return true; } return false; } private int countH(int i, int j, int currentColor) { int counter = 1; int tempi = i; while (--tempi >= 0 && p[tempi][j] == currentColor) { counter++; } tempi = i; while (++tempi <= p.length && p[tempi][j] == currentColor) { counter++; } return counter; } private int countV(int i, int j, int currentColor) { int counter = 1; int tempj = j; while (--tempj >= 0 && p[i][tempj] == currentColor) { counter++; } tempj = j; while (++tempj <= p.length && p[i][tempj] == currentColor) { counter++; } return counter; } private int countX1(int i, int j, int currentColor) { int counter = 1; int tempi = i; int tempj = j; while (--tempj >= 0 && --tempi >= 0 && p[tempi][tempj] == currentColor) { counter++; } tempi = i; tempj = j; while (++tempj <= p.length && ++tempi <= p.length && p[tempi][tempj] == currentColor) { counter++; } return counter; } private int countX2(int i, int j, int currentColor) { int counter = 1; int tempi = i; int tempj = j; while (--tempj >= 0 && ++tempi >= 0 && p[tempi][tempj] == currentColor) { counter++; } tempi = i; tempj = j; while (++tempj <= p.length && --tempi <= p.length && p[tempi][tempj] == currentColor) { counter++; } return counter; } }




posted on 2017-08-13 16:06  yjbjingcha  阅读(168)  评论(0编辑  收藏  举报

导航