이 기사의 예에서는 Swing 기반 Java 기반 Lianliankan 코드 구현을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요.
주요 기능 코드는 다음과 같습니다.
다음과 같이 코드 코드를 복사합니다.
패키지 llkan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Lianliankan 게임
* @author 관리자
*2014년 10월 17일
*/
공개 클래스 MainGame은 ActionListener를 구현합니다.
JFrame mainFrame; // 메인 패널
컨테이너 thisContainer;
JPanel centerPanel, southPanel, northPanel // 하위 패널;
JButton DiamondsButton[][] = new JButton[6][5];//게임 버튼 배열
JButtonexitButton,resetButton,newButton;/종료, 재설정 및 다시 시작 버튼;
JLabel FractionLable = new JLabel("0") // 분수 라벨
JButton firstButton, secondButton; // 선택한 버튼을 각각 두 번 기록합니다.
int Grid[][] = new int[8][7];//게임 버튼 위치 저장
static boolean pressInformation = false; // 버튼이 선택되었는지 확인합니다.
int x0 = 0, y0 = 0, x = 0, y = 0, firstMsg = 0, secondMsg = 0, verifyLV // 게임 버튼의 위치 좌표;
int i, j, k, n; // 제거 방법 제어
공개 무효 초기화() {
mainFrame = new JFrame("Lianliankan 게임");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
thisContainer.add(centerPanel, "Center");
thisContainer.add(southPanel, "South");
thisContainer.add(northPanel, "북쪽");
centerPanel.setLayout(new GridLayout(6, 5));
for (int cols = 0; cols < 6; cols++) {
for (int 행 = 0; 행 < 5; 행++) {
다이아몬드버튼[cols][행] = 새로운 JButton(
String.valueOf(grid[cols + 1][rows + 1]));
DiamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton = new JButton("종료");
exitButton.addActionListener(this);
ResetButton = new JButton("재설정");
ResetButton.addActionListener(this);
newButton = new JButton("게임 하나 더");
newButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
FractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280, 100, 500, 450);
mainFrame.setVisible(true);
}
공공 무효 RandomBuild() {
int 무작위, 열, 행;
for (int 쌍둥이 = 1; 쌍둥이 <= 15; 쌍둥이++) {
무작위 = (int) (Math.random() * 25 + 1);
for (int alike = 1; alike <= 2; alike++) {
cols = (int) (Math.random() * 6 + 1);
행 = (int) (Math.random() * 5 + 1);
while (그리드[열][행] != 0) {
cols = (int) (Math.random() * 6 + 1);
행 = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = 무작위;
}
}
}
공공 무효 분수() {
FractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText()) + 100));
}
공개 무효 다시 로드() {
int save[] = 새로운 int[30];
int n = 0, 열, 행;
int 그리드[][] = new int[8][7];
for (int i = 0; i <= 6; i++) {
for (int j = 0; j <= 5; j++) {
if (this.grid[i][j] != 0) {
저장[n] = this.grid[i][j];
n++;
}
}
}
n = n - 1;
this.grid = 그리드;
동안 (n >= 0) {
cols = (int) (Math.random() * 6 + 1);
행 = (int) (Math.random() * 5 + 1);
while (그리드[열][행] != 0) {
cols = (int) (Math.random() * 6 + 1);
행 = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = 저장[n];
N--;
}
mainFrame.setVisible(false);
pressInformation = false; // 여기서는 버튼 클릭 정보를 초기값으로 분류해야 합니다.
초기화();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (그리드[i + 1][j + 1] == 0)
DiamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX, int placeY, JButton bz) {
if (pressInformation == false) {
x = 장소X;
y = 장소Y;
secondMsg = 그리드[x][y];
두 번째버튼 = bz;
언론정보 = true;
} 또 다른 {
x0 = x;
y0 = y;
첫 번째Msg = 두 번째Msg;
첫 번째버튼 = 두 번째버튼;
x = 장소X;
y = 장소Y;
secondMsg = 그리드[x][y];
두 번째버튼 = bz;
if (fristMsg == secondMsg && secondButton != firstButton) {
샤오();
}
}
}
public void xiao() { // 같은 상황에서 제거할 수 있나요? 일일이 댓글 달지 말고 꼼꼼히 분석해보세요
if ((x0 == x && (y0 == y + 1 || y0 == y - 1))
|| ((x0 == x + 1 || x0 == x - 1) && (y0 == y))) { // 서로 인접해 있는지 확인
제거하다();
} 또 다른 {
for (j = 0; j < 7; j++) {
if (grid[x0][j] == 0) { // 첫 번째 버튼 옆의 어떤 버튼이 비어 있는지 확인
if (y > j) { // 두 번째 버튼의 Y 좌표가 빈 버튼의 Y 좌표보다 크다면 첫 번째 버튼이 두 번째 버튼의 왼쪽에 있다는 뜻입니다.
for (i = y - 1; i >= j; i--) { // 두 번째 버튼 왼쪽부터 첫 번째 버튼 중앙까지 버튼이 있는지 확인
if (그리드[x][i] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 1;
} // K=1은 첫 번째 검증이 통과되었음을 나타냅니다.
}
if (k == 1) {
linePassOne();
}
}
if (y < j) { // 두 번째 버튼의 Y 좌표가 빈 버튼의 Y 좌표보다 작으면 첫 번째 버튼이 두 번째 버튼의 오른쪽에 있다는 뜻입니다.
for (i = y + 1; i <= j; i++) { // 두 번째 버튼 왼쪽부터 첫 번째 버튼 중앙까지 버튼이 있는지 확인
if (그리드[x][i] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 1;
}
}
if (k == 1) {
linePassOne();
}
}
if (y == j) {
linePassOne();
}
}
if (k == 2) {
만약 (x0 == x) {
제거하다();
}
만약 (x0 < x) {
for (n = x0; n <= x - 1; n++) {
if (그리드[n][j] != 0) {
k = 0;
부서지다;
}
if (그리드[n][j] == 0 && n == x - 1) {
제거하다();
}
}
}
만약 (x0 > x) {
for (n = x0; n >= x + 1; n--) {
if (그리드[n][j] != 0) {
k = 0;
부서지다;
}
if (그리드[n][j] == 0 && n == x + 1) {
제거하다();
}
}
}
}
}
for (i = 0; i < 8; i++) { // 열
if (그리드[i][y0] == 0) {
만약 (x > i) {
for (j = x - 1; j >= i; j--) {
if (그리드[j][y] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
만약 (x < i) {
for (j = x + 1; j <= i; j++) {
if (그리드[j][y] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 1;
}
}
if (k == 1) {
rowPassOne();
}
}
if (x == i) {
rowPassOne();
}
}
if (k == 2) {
만약 (y0 == y) {
제거하다();
}
만약 (y0 < y) {
for (n = y0; n <= y - 1; n++) {
if (그리드[i][n] != 0) {
k = 0;
부서지다;
}
if (grid[i][n] == 0 && n == y - 1) {
제거하다();
}
}
}
만약 (y0 > y) {
for (n = y0; n >= y + 1; n--) {
if (그리드[i][n] != 0) {
k = 0;
부서지다;
}
if (그리드[i][n] == 0 && n == y + 1) {
제거하다();
}
}
}
}
}
}
}
공공 무효 linePassOne() {
if (y0 > j) { // 첫 번째 버튼은 빈 버튼과 함께 왼쪽에 있습니다.
for (i = y0 - 1; i >= j; i--) { // 첫 번째 버튼과 왼쪽의 빈 버튼 사이에 버튼이 있는지 확인
if (그리드[x0][i] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 2;
} // K=2는 두 번째 검증이 통과되었음을 나타냅니다.
}
}
if (y0 < j) { // 첫 번째 버튼은 빈 버튼과 두 번째 버튼 사이에 있습니다.
for (i = y0 + 1; i <= j; i++) {
if (그리드[x0][i] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 2;
}
}
}
}
공공 무효 rowPassOne() {
만약 (x0 > i) {
for (j = x0 - 1; j >= i; j--) {
if (그리드[j][y0] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 2;
}
}
}
만약 (x0 < i) {
for (j = x0 + 1; j <= i; j++) {
if (그리드[j][y0] != 0) {
k = 0;
부서지다;
} 또 다른 {
k = 2;
}
}
}
}
공개 무효 제거() {
firstButton.setVisible(false);
secondButton.setVisible(false);
분수();
언론정보 = 거짓;
k = 0;
그리드[x0][y0] = 0;
그리드[x][y] = 0;
}
공공 무효 actionPerformed(ActionEvent e) {
if (e.getSource() == newButton) {
int 그리드[][] = new int[8][7];
this.grid = 그리드;
무작위빌드();
mainFrame.setVisible(false);
언론정보 = 거짓;
초기화();
}
if (e.getSource() == 종료버튼)
시스템.exit(0);
if (e.getSource() == 재설정버튼)
다시 로드();
for (int cols = 0; cols < 6; cols++) {
for (int 행 = 0; 행 < 5; 행++) {
if (e.getSource() == 다이아몬드버튼[cols][행])
estimateEven(열 + 1, 행 + 1, 다이아몬드버튼[열][행]);
}
}
}
공개 정적 무효 메인(String[] args) {
MainGame mg = new MainGame();
mg.randomBuild();
mg.init();
}
}
이 글이 모든 사람의 Java 프로그래밍에 도움이 되기를 바랍니다.