⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coreaction.java

📁 扫雷的源代码,包结构采用经典的MVC模式,包括模型、控制、视图三部分
💻 JAVA
字号:
/*
 * Created on Mar 27, 2005
 *
 * TODO this class is control part of MVC structure. use to manage the main game
 * action
 */
package control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import view.Images;
import view.MyButton;
import view.UI;

import model.Location;
import model.Model;

/**
 * @author mqqqvpppm
 * 
 * TODO this class is control part of MVC structure. use to manage the main game
 * action
 */
public class CoreAction implements MouseListener, ActionListener {

	public CoreAction(Model model, UI ui) {
		super();
		MOUSE_RIGHT_PRESSED = false;
		MOUSE_LEFT_PRESSED = false;
		this.model = model;
		this.ui = ui;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 *      just for startButton;
	 */
	public void actionPerformed(ActionEvent e) {
		model.restart(model.getWidth(), model.getHeight(), model
				.getMineNumber(), model.getType());

	}

	public void mouseClicked(MouseEvent e) {
		//if( model.isGameOver() ) return;
	}

	public void mouseEntered(MouseEvent e) {
		//if( model.isGameOver() ) return;
		//if(MOUSE_LEFT_PRESSED && !MOUSE_RIGHT_PRESSED){
		//	MyButton button = (MyButton)e.getSource();
		//	if( !model.isClickedButton( button.getMyLocation() ) ){
		//		button.setIcon(Images.CLICKED);
		//	}
		//}
	}

	public void mouseExited(MouseEvent e) {
		//if( model.isGameOver() ) return;
		//if(MOUSE_LEFT_PRESSED && !MOUSE_RIGHT_PRESSED){
		//	MyButton button = (MyButton)e.getSource();
		//	if( !model.isClickedButton( button.getMyLocation() ) ){
		//		button.draw(Images.UNCLICKED);
		//	}
		//}
	}

	public void mousePressed(MouseEvent e) {
		if(model.isFreezed()) return;
		
		if (e.getButton() == MouseEvent.BUTTON2) {
			runOnBothPressed(e);
		} else if (e.getButton() == MouseEvent.BUTTON3) {//mouse right press
			MOUSE_RIGHT_PRESSED = true;
			if (MOUSE_LEFT_PRESSED) {//mouse left and right button are pressed
				// at the same time
				runOnBothPressed(e);
				return;
			}
			runOnRightPressed(e);
		} else if (e.getButton() == MouseEvent.BUTTON1) {//mouse left press
			MOUSE_LEFT_PRESSED = true;
			if (MOUSE_RIGHT_PRESSED) {//mouse left and right button are pressed
				// at the same time
				runOnBothPressed(e);
				return;
			}
			runOnLeftPressed(e);
		}
	}

	public void mouseReleased(MouseEvent e) {
		if(model.isFreezed()) return;
		
		
		if (e.getButton() == MouseEvent.BUTTON2) {
			this.runOnBothReleased(e);
			return;
		}

		if (MOUSE_LEFT_PRESSED && MOUSE_RIGHT_PRESSED) {
			runOnBothReleased(e);
			MOUSE_LEFT_PRESSED = false;
			MOUSE_RIGHT_PRESSED = false;
			return;
		}

		if (!MOUSE_LEFT_PRESSED && !MOUSE_RIGHT_PRESSED)
			return;

		if (e.getButton() == MouseEvent.BUTTON1) {
			MOUSE_LEFT_PRESSED = false;
			runOnLeftReleased(e);
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			MOUSE_RIGHT_PRESSED = false;
		}
	}

	private void runOnBothPressed(MouseEvent e) {
		MyButton button = (MyButton) e.getSource();
		Location location = button.getMyLocation();

		int x, y, m, n;
		if (model.isClickedButton(location) && model.isNumber(location)) {
			for (m = 0, x = location.x - 1; m != 3; x++, m++) {
				for (n = 0, y = location.y - 1; n != 3; y++, n++) {
					if (x < 0 || y < 0 || x > model.getWidth() - 1
							|| y > model.getHeight() - 1)
						continue;
					Location temp = new Location(x, y);
					if (model.isClickedButton(temp) || model.isMarkedMine(temp)
							|| model.isMark(temp))
						continue;
					ui.getButtons()[y][x].setIcon(Images.CLICKED);
				}
			}
		}
	}

	private void runOnLeftPressed(MouseEvent e) {
		
		MyButton button = (MyButton) e.getSource();
		Location location = button.getMyLocation();
		if (model.isClickedButton(location) || model.isMark(location)
				|| model.isMarkedMine(location))
			return;
		button.setIcon(Images.CLICKED);
		/*
		 * if( model.isGameOver() ) return;
		 * 
		 * MyButton button = (MyButton)e.getSource(); Location location =
		 * button.getMyLocation();
		 * 
		 * if( model.isClickedButton(location) || model.isMarkedMine(location) ){
		 * return; } button.draw(Images.CLICKED);
		 */
	}

	private void runOnRightPressed(MouseEvent e) {
		//if( model.isGameOver() ) return;
		MyButton button = (MyButton) e.getSource();
		Location location = button.getMyLocation();
		if (model.isClickedButton(location)) {
			return;
		}
		if (model.isMarkedMine(location)) {
			model.setMark(true, location);
			model.setMarkedMine(false, location);
		} else if (model.isMark(location)) {
			model.setMark(false, location);
		} else {
			model.setMarkedMine(true, location);
			model.setMark(false, location);
		}
	}

	private void runOnLeftReleased(MouseEvent e) {

		MyButton button = (MyButton) e.getSource();
		Location location = button.getMyLocation();
		if (model.isClickedButton(location) || model.isMarkedMine(location)) {
			return;
		}

		if (model.isNumber(location) || model.isMine(location)) {//show number
			//model.unlockACell(location);
			model.setClickedButton(location);
			//}else if(model.isMine(location)){//game over
			//model.setClickedButton(true,location);
			//	model.setClickedButton(location);
			//model.setGameOver(true);
		} else {//need to open a area
			model.unlockAll(location);
		}
	}

	private void runOnBothReleased(MouseEvent e) {
		MyButton button = (MyButton) e.getSource();
		Location location = button.getMyLocation();
		if(!model.isClickedButton(location)){
			button.setIcon(Images.UNCLICKED);
		}

		int x, y, m, n;
		if (model.isClickedButton(location) && model.isNumber(location)) {
			for (m = 0, x = location.x - 1; m != 3; x++, m++) {
				for (n = 0, y = location.y - 1; n != 3; y++, n++) {
					if (x < 0 || y < 0 || x > model.getWidth() - 1
							|| y > model.getHeight() - 1)
						continue;
					Location temp = new Location(x, y);
					if (model.isClickedButton(temp) || model.isMarkedMine(temp)
							|| model.isMark(temp))
						continue;
					ui.getButtons()[y][x].setIcon(Images.UNCLICKED);
				}
			}
			model.unlockOne(button.getMyLocation());
		}
//		if (model.isNumber(location)) {
//			model.unlockOne(button.getMyLocation());
//		}
	}

	private void runOnRightReleased(MouseEvent e) {

	}

	private boolean MOUSE_RIGHT_PRESSED;

	private boolean MOUSE_LEFT_PRESSED;

	private Model model;

	private UI ui;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -