O exemplo deste artigo descreve a implementação do código Lianliankan baseado em java baseado em swing. Compartilhe com todos para sua referência.
Os principais códigos de função são os seguintes:
Copie o código do código da seguinte forma:
pacote llkan;
importar javax.swing.*;
importar java.awt.*;
importar java.awt.event.*;
/**
* Jogo Lianliankan
* @autor Administrador
*17 de outubro de 2014
*/
classe pública MainGame implementa ActionListener {
JFrame mainFrame; // Painel principal
Contêiner thisContainer;
JPanel centerPanel, southPanel, northPanel; // subpainel;
JButton diamantesButton[][] = new JButton[6][5];//Array de botões do jogo
JButton exitButton, resetButton, newButton; //Botões de saída, reinicialização e reinicialização
JLabel fracçãoLable = new JLabel("0"); // Rótulo da fração
JButton firstButton, secondButton; // Registra os botões selecionados duas vezes respectivamente;
int grid[][] = new int[8][7];//Salva a posição do botão do jogo
static boolean pressInformation = false; // Determina se um botão está selecionado
int x0 = 0, y0 = 0, x = 0, y = 0, firstMsg = 0, secondMsg = 0, validLV;
int i, j, k, n; // controle do método de eliminação
public void inicialização() {
mainFrame = novo JFrame("Jogo Lianliankan");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
SouthPanel = novo JPanel();
nortePanel = new JPanel();
thisContainer.add(centerPanel, "Centro");
thisContainer.add(southPanel, "Sul");
thisContainer.add(northPanel, "Norte");
centerPanel.setLayout(novo GridLayout(6, 5));
for (int colunas = 0; colunas < 6; colunas++) {
for (int linhas = 0; linhas < 5; linhas++) {
diamantesButton[cols][linhas] = new JButton(
String.valueOf(grid[cols + 1][linhas + 1]));
diamantesButton[cols][linhas].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton = new JButton("Sair");
exitButton.addActionListener (este);
resetButton = new JButton("Redefinir");
resetButton.addActionListener (este);
newButton = new JButton("Mais um jogo");
newButton.addActionListener (este);
SouthPanel.add(exitButton);
SouthPanel.add(resetButton);
SouthPanel.add(recentementeButton);
fraçãoLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText())));
nortePanel.add(fractionLable);
mainFrame.setBounds(280, 100, 500, 450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int aleatórios, colunas, linhas;
for (int gêmeos = 1; gêmeos <= 15; gêmeos++) {
aleatórios = (int) (Math.random() * 25 + 1);
for (int iguais = 1; iguais <= 2; iguais++) {
colunas = (int) (Math.random() * 6 + 1);
linhas = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {
colunas = (int) (Math.random() * 6 + 1);
linhas = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = aleatórios;
}
}
}
fração nula pública() {
fraçãoLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText()) + 100));
}
public void recarregar() {
int salvar[] = novo int[30];
int n = 0, colunas, linhas;
grade 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) {
salvar[n] = this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid = grade;
enquanto (n >= 0) {
colunas = (int) (Math.random() * 6 + 1);
linhas = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {
colunas = (int) (Math.random() * 6 + 1);
linhas = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = salvar[n];
n--;
}
mainFrame.setVisible(falso);
pressInformation = false; // As informações do clique do botão devem ser classificadas como iniciais aqui
iniciar();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
if (grade[i + 1][j + 1] == 0)
diamantesButton[i][j].setVisible(false);
}
}
}
public void estimativaEven(int lugarX, int lugarY, JButton bz) {
if (pressInformation == falso) {
x = lugarX;
y = lugarY;
segundaMsg = grade[x][y];
segundoButton = bz;
pressInformation = true;
} outro {
x0 = x;
y0 = y;
primeiraMsg = segundaMsg;
primeiroButton = segundoButton;
x = lugarX;
y = lugarY;
segundaMsg = grade[x][y];
segundoButton = bz;
if (fristMsg == segundaMsg && segundoButton!=primeiroButton) {
xiao();
}
}
}
public void xiao() { // Pode ser eliminado na mesma situação? Analise com cuidado sem comentar um por um
se ((x0 == x && (y0 == y + 1 || y0 == y - 1))
|| ((x0 == x + 1 || x0 == x - 1) && (y0 == y))) { // Determine se eles são adjacentes
remover();
} outro {
para (j = 0; j < 7; j++) {
if (grid[x0][j] == 0) { // Determina qual botão próximo ao primeiro botão está vazio
if (y > j) { // Se a coordenada Y do segundo botão for maior que a coordenada Y do botão vazio, significa que o primeiro botão está à esquerda do segundo botão
for (i = y - 1; i >= j; i--) { // Determina se existem botões do lado esquerdo do segundo botão até o meio do primeiro botão
if (grade[x][i] != 0) {
k = 0;
quebrar;
} outro {
k = 1;
} // K=1 indica que a primeira verificação foi aprovada
}
se (k == 1) {
linhaPassOne();
}
}
if (y < j) { // Se a coordenada Y do segundo botão for menor que a coordenada Y do botão vazio, significa que o primeiro botão está à direita do segundo botão
for (i = y + 1; i <= j; i++) { // Determina se existem botões do lado esquerdo do segundo botão até o meio do primeiro botão
if (grade[x][i] != 0) {
k = 0;
quebrar;
} outro {
k = 1;
}
}
se (k == 1) {
linhaPassOne();
}
}
se (y == j) {
linhaPassOne();
}
}
se (k == 2) {
se (x0 == x) {
remover();
}
se (x0 < x) {
para (n = x0; n <= x - 1; n++) {
if (grade[n][j] != 0) {
k = 0;
quebrar;
}
if (grade[n][j] == 0 && n == x - 1) {
remover();
}
}
}
se (x0 > x) {
para (n = x0; n >= x + 1; n--) {
if (grade[n][j] != 0) {
k = 0;
quebrar;
}
if (grade[n][j] == 0 && n == x + 1) {
remover();
}
}
}
}
}
for (i = 0; i < 8; i++) { // coluna
if (grade[i][y0] == 0) {
se (x > eu) {
para (j = x - 1; j >= i; j--) {
if (grade[j][y] != 0) {
k = 0;
quebrar;
} outro {
k = 1;
}
}
se (k == 1) {
rowPassOne();
}
}
se (x <eu) {
para (j = x + 1; j <= i; j++) {
if (grade[j][y] != 0) {
k = 0;
quebrar;
} outro {
k = 1;
}
}
se (k == 1) {
rowPassOne();
}
}
se (x == eu) {
rowPassOne();
}
}
se (k == 2) {
se (y0 == y) {
remover();
}
se (y0 <y) {
para (n = y0; n <= y - 1; n++) {
if (grade[i][n] != 0) {
k = 0;
quebrar;
}
if (grade[i][n] == 0 && n == y - 1) {
remover();
}
}
}
se (y0 > y) {
para (n = y0; n >= y + 1; n--) {
if (grade[i][n] != 0) {
k = 0;
quebrar;
}
if (grade[i][n] == 0 && n == y + 1) {
remover();
}
}
}
}
}
}
}
public void linePassOne() {
if (y0 > j) { // O primeiro botão está à esquerda com o botão vazio
for (i = y0 - 1; i >= j; i--) { // Determina se existe um botão entre o primeiro botão e o botão vazio à esquerda
if (grade[x0][i] != 0) {
k = 0;
quebrar;
} outro {
k = 2;
} // K=2 indica que a segunda verificação foi aprovada
}
}
if (y0 < j) { // O primeiro botão está entre o botão vazio e o segundo botão
para (i = y0 + 1; i <= j; i++) {
if (grade[x0][i] != 0) {
k = 0;
quebrar;
} outro {
k = 2;
}
}
}
}
public void rowPassOne() {
se (x0 > eu) {
para (j = x0 - 1; j >= i; j--) {
if (grade[j][y0] != 0) {
k = 0;
quebrar;
} outro {
k = 2;
}
}
}
se (x0 <eu) {
para (j = x0 + 1; j <= i; j++) {
if (grade[j][y0] != 0) {
k = 0;
quebrar;
} outro {
k = 2;
}
}
}
}
public void remover() {
primeiroButton.setVisible(falso);
segundoButton.setVisible(falso);
fração();
pressInformation = falso;
k = 0;
grade[x0][y0] = 0;
grade[x][y] = 0;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newButton) {
grade int[][] = new int[8][7];
this.grid = grade;
randomBuild();
mainFrame.setVisible(falso);
pressInformation = falso;
iniciar();
}
if (e.getSource() == exitButton)
Sistema.exit(0);
if (e.getSource() == resetButton)
recarregar();
for (int colunas = 0; colunas < 6; colunas++) {
for (int linhas = 0; linhas < 5; linhas++) {
if (e.getSource() == diamantesButton[cols][linhas])
estimaEven(cols + 1, rows + 1, diamantesButton[cols][rows]);
}
}
}
public static void main(String[] args) {
Jogo Principal mg = new Jogo Principal();
mg.randomBuild();
mg.init();
}
}
Espero que este artigo seja útil para a programação Java de todos.