记一次JAVA实训(开发连连看小游戏)(二)

上次写到生成界面(图片成对生成,并打乱)

连连看相同图片的消除有四种情况(直连消除、拐一次弯消除、拐两次弯消除、越界拐弯消除)

在相关的data包下定义Rule文件,用于定义规则,在view包下调用验证

GameRule.java

package com.icss.linkgame.data;

public class GameRule {
	/**
	 * 判断数组data中,制定的两个位置处的图形能否消除
	 * 
	 * @param data
	 *            保存游戏数据的数组
	 * @param row1
	 *            第一次点击的按钮所在的行
	 * @param col1
	 *            第一次点击的按钮所在的列
	 * @param row2
	 *            第二次点击的按钮所在的行 // * @param col2 第二次点击的按钮所在的列
	 * @return 如果能消,返回true;否则返回false
	 */
	public static boolean isConnect(int[] data, int row1, int col1, int row2,
			int col2) {
		int index1 = row1 * GameData.cols + col1;
		int index2 = row2 * GameData.cols + col2;

		if (index1 == index2) {
			return false;
		}

		if (data[index1] != data[index2]) {
			return false;
		}
		
		/*
		 * 直接连接
		 */
		if (connect0(data, row1, col1, row2, col2)) {
			return true;
		}
		
		/*
		 * 拐弯一次
		 */
		if (connect1(data, row1, col1, row2, col2)) {
			return true;
		}
		
		/*
		 * 拐弯两次
		 */
		if (connect2(data, row1, col1, row2, col2)) {
			return true;
		}
		
		/*
		 * 越界连接
		 */
		if (connectOut(data, row1, col1, row2, col2)) {
			return true;
		}
		

		return false;
	}

	private static boolean connectOut(int[] data, int row1, int col1, int row2,
			int col2) {
		// 上面,(-1,col1) : (-1,col2)
		if(connect0(data,row1,col1,-1,col1) && connect0(data,row2,col2,-1,col2)){
			return true;
		}
		// 下面,(rows,col1) : (rows:col2)
		if(connect0(data,row1,col1,GameData.rows,col1) && connect0(data,row2,col2,GameData.rows,col2)){
			return true;
		}
		// 左面,(row1,-1) : (row2,-1)
		if(connect0(data,row1,col1,row1,-1) && connect0(data,row2,col2,row2,-1)){
			return true;
		}
		// 右面,(row1,cols) : (row2,cols)
		if(connect0(data,row1,col1,row1,GameData.cols) && connect0(data,row2,col2,row2,GameData.cols)){
			return true;
		}

		
		
		return false;
	}

	private static boolean connect2(int[] data, int row1, int col1, int row2,
			int col2) {
		// 转弯点在同一行
		for(int row = 0; row<GameData.rows; ++row){
			//转弯点分别是(row,col1)和(row,col2)
			if(connect0(data,row1,col1,row,col1)
					&& connect0(data,row,col1,row,col2)
					&& connect0(data,row,col2,row2,col2)
					&& data[row*GameData.cols+col1]==0
					&& data[row*GameData.cols+col2]==0){
				return true;
			}
		}
		
		//转弯点在同一列
		for(int col = 0; col<GameData.cols; ++col){
			//转弯点分别是(row1,col)和(row2,col)
			if(connect0(data,row1,col1,row1,col)
					&& connect0(data,row1,col,row2,col)
					&& connect0(data,row2,col,row2,col2)
					&& data[row1*GameData.cols+col]==0
					&& data[row2*GameData.cols+col]==0){
				return true;
			}
		}
		
		return false;
	}

	private static boolean connect1(int[] data, int row1, int col1, int row2,
			int col2) {
		// 通过row1,col2转弯
		if (connect0(data, row1, col1, row1, col2)
				&& connect0(data, row1, col2, row2, col2)
				&& data[row1 * GameData.cols + col2] == 0) {
			return true;
		}

		// 通过row2,col1转弯
		if (connect0(data, row1, col1, row2, col1)
				&& connect0(data, row2, col1, row2, col2)
				&& data[row2 * GameData.cols + col1] == 0) {
			return true;
		}
		
		return false;
	}

	private static boolean connect0(int[] data, int row1, int col1, int row2,
			int col2) {
		if (row1 == row2) {
			if (col1 > col2) {
				int temp = col1;
				col1 = col2;
				col2 = temp;
			}
			int sum = 0;
			int row = row1;
			for (int col = col1 + 1; col < col2; ++col) {
				sum += data[row * GameData.cols + col];
			}
			if (sum == 0) {
				return true;
			}
		}

		if (col1 == col2) {
			if (row1 > row2) {
				int temp = row1;
				row1 = row2;
				row2 = temp;
			}

			int col = col1;
			int sum = 0;
			for (int row = row1 + 1; row < row2; ++row) {
				sum += data[row * GameData.cols + col];
			}
			if (sum == 0) {
				return true;
			}
		}
		return false;
	}
}


view包下页面,上次生成的图片为4x4 ,16张。若想生成更多的图片,需要对其做越界处理

/**
 * 成对生成数据,存入data[]
 */
private void init() {
// TODO 自动生成的方法存根
   for (int i = 0; i < data.length; ++i) {
	data[i] = i / 2 % 18 + 1;
   }
}

在view下编写

private void initButtons(final GameData gameData) {
		this.getContentPane().setLayout(
				new GridLayout(GameData.rows, GameData.cols));

		for (int i = 0; i < GameData.rows*GameData.cols; ++i) {
			JButton button = new JButton();
			String path = this.getClass().getResource("/image").getPath();
			ImageIcon icon = new ImageIcon(path + "/fruit_" + gameData.data[i]
					+ ".png");
			icon.setImage(icon.getImage().getScaledInstance(width/GameData.cols, height/GameData.rows, Image.SCALE_DEFAULT));
			button.setIcon(icon);
			// button.setBounds(i%4*100, i/4*100, 100, 100);
			button.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO 自动生成的方法存根
					// 获取点击的按钮是哪个
					JButton button = (JButton) e.getSource();
					if (button1 == null) { // 以前没点过
						button1 = button;
					} else { // 已经点过
						button2 = button;

						int row1 = button1.getY() / button1.getHeight();
						int col1 = button1.getX() / button1.getWidth();
						int row2 = button2.getY() / button2.getHeight();
						int col2 = button2.getX() / button2.getWidth();

						if (GameRule.isConnect(gameData.data, row1, col1, row2, col2)) {
							button1.setVisible(false);
							button2.setVisible(false);
							
							gameData.data[row1*GameData.cols+col1] = 0;
							gameData.data[row2*GameData.cols+col2] = 0;
						}
						button1 = null;
						button2 = null;
					}

				}

			});
			this.getContentPane().add(button);
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_36289732/article/details/80725324