2020年最強大腦第五期黑白迭代小遊戲實現(Java)

遊戲規則:

8×8的棋盤上初始所有格子均爲白色。每點擊一個方格,則該方格與其上下左右共5個方格變色(由白變黑或相反)。遊戲目標是要把自己的棋盤變爲與題目棋盤完全一致。

 

實現代碼:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

import static java.awt.Color.*;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.Random;

public class Game extends JFrame implements MouseListener {

	public static Random random = new Random();
	
	public static int[][][] items = new int[2][8][8];//存儲棋盤狀態
	
	public static JButton[][][] jButtons = new JButton[2][8][8];//棋盤格子對應的按鈕
	
	public static JButton nextJButton = new JButton("next");
	public static JButton resetJButton = new JButton("reset");

	public static void main(String[] args) {
		new Game("黑白迭代");
		init(0);
		init(1);
		create();
	}

	public Game() {
		this("未命名");
	}

	/*
	 * 繪製遊戲窗口
	 */
	public Game(String title) {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				jButtons[0][i][j] = new JButton();
				jButtons[1][i][j] = new JButton();
				jButtons[1][i][j].addMouseListener(this);
			}
		}
		
		JPanel jPanel1 = new JPanel(new GridLayout(8, 8));
		JPanel jPanel2 = new JPanel(new GridLayout(8, 8));
		
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				jPanel1.add(jButtons[0][i][j]);
				jPanel2.add(jButtons[1][i][j]);
			}
		}
		
		JPanel jPanel3 = new JPanel(new GridLayout(1, 2, 10, 10));
		
		jPanel3.add(jPanel1);
		jPanel3.add(jPanel2);
		
		JPanel jPanel4 = new JPanel();
		
		nextJButton.addMouseListener(this);
		resetJButton.addMouseListener(this);
		
		jPanel4.add(nextJButton);
		jPanel4.add(resetJButton);
		
		this.add(jPanel3, BorderLayout.CENTER);
		this.add(jPanel4, BorderLayout.SOUTH);
		
		this.setTitle(title);
		this.setSize(1200, 900);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	/*
	 * 初始化棋盤,棋盤狀態數組裏置0,代表格子的按鈕恢復爲白色
	 */
	public static void init(int num) {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				items[num][i][j] = 0;
				jButtons[num][i][j].setBackground(WHITE);
			}
		}
	}

	/*
	 * 隨機出題,在棋盤的左4列25%概率地生成黑色格子,再軸對稱到右邊四列
	 */
	public static void create() {
		init(0);
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 4; j++) {
				if (random.nextInt(4) == 1) {
					items[0][i][j] = 1;
					items[0][i][7 - j] = 1;
					jButtons[0][i][j].setBackground(BLACK);
					jButtons[0][i][7 - j].setBackground(BLACK);
				}
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		/*
		 * 按遊戲規則,點擊就先改變狀態數組,要考慮邊緣情況
		 */
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (e.getSource() == jButtons[1][i][j]) {
					items[1][i][j] = 1 - items[1][i][j];
					if (i != 0)
						items[1][i - 1][j] = 1 - items[1][i - 1][j];
					if (j != 0)
						items[1][i][j - 1] = 1 - items[1][i][j - 1];
					if (i != 7)
						items[1][i + 1][j] = 1 - items[1][i + 1][j];
					if (j != 7)
						items[1][i][j + 1] = 1 - items[1][i][j + 1];
				}
			}
		}
		/*
		 * 遍歷狀態數組,改變對應格子顏色
		 */
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				jButtons[1][i][j].setBackground(items[1][i][j] == 0 ? WHITE : BLACK);
			}
		}
		//next按鈕重置右邊棋盤並重新出題
		if (e.getSource() == nextJButton) {
			init(1);
			create();
		}
		//reset按鈕只重置右邊棋盤
		if (e.getSource() == resetJButton)
			init(1);
	}
	
	@Override
	public void mousePressed(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
}

效果圖:

黑白迭代

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章