This article describes the Java based on swing implementation of the code. Share to everyone for your reference.
Package Llkan;
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
/**
* Watching Games
* @author Administrator
* October 17, 2014
*/
public class Maingame implements ActionListener {
JFrame MainFrame; Main panel
Container Thiscontainer;
JPanel Centerpanel, Southpanel, Northpanel; Child panel
JButton diamondsbutton[][] = new jbutton[6][5];//game button array
JButton Exitbutton, Resetbutton, Newlybutton; Exit, Reload, restart button
JLabel fractionlable = new JLabel ("0"); Score Label
JButton Firstbutton, Secondbutton; Record two selected buttons on each page
int grid[][] = new int[8][7];//Store game button position
static Boolean pressinformation = false; Determine if a button is selected
int x0 = 0, y0 = 0, x = 0, y = 0, fristmsg = 0, secondmsg = 0, Validatelv; Position coordinates of the game button
int I, j, K, n;//elimination method control
public void init () {
MainFrame = new JFrame ("Watch the Game");
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, "North");
Centerpanel.setlayout (New GridLayout (6, 5));
for (int cols = 0; cols < 6; cols++) {
for (int rows = 0; rows < 5; rows++) {
Diamondsbutton[cols][rows] = new JButton (
String.valueof (grid[cols + 1][rows + 1]));
Diamondsbutton[cols][rows].addactionlistener (this);
Centerpanel.add (Diamondsbutton[cols][rows]);
}
}
Exitbutton = new JButton ("Exit");
Exitbutton.addactionlistener (this);
Resetbutton = new JButton ("Re-column");
Resetbutton.addactionlistener (this);
Newlybutton = new JButton ("one more innings");
Newlybutton.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);
}
public void Randombuild () {
int randoms, cols, rows;
for (int twins = 1; twins <= twins++) {
randoms = (int) (Math.random () * 25 + 1);
for (int alike = 1; alike <= 2; alike++) {
cols = (int) (Math.random () * 6 + 1);
rows = (int) (Math.random () * 5 + 1);
while (Grid[cols][rows]!= 0) {
cols = (int) (Math.random () * 6 + 1);
rows = (int) (Math.random () * 5 + 1);
}
This.grid[cols][rows] = randoms;
}
}
}
public void fraction () {
Fractionlable.settext (String.valueof (integer.parseint) (fractionlable
. GetText ()) + 100);
}
public void Reload () {
int save[] = new INT[30];
int n = 0, cols, rows;
int grid[][] = new INT[8][7];
for (int i = 0; I <= 6; i++) {
for (int j = 0; J <= 5; j +) {
if (This.grid[i][j]!= 0) {
Save[n] = This.grid[i][j];
n++;
}
}
}
n = n-1;
This.grid = grid;
while (n >= 0) {
cols = (int) (Math.random () * 6 + 1);
rows = (int) (Math.random () * 5 + 1);
while (Grid[cols][rows]!= 0) {
cols = (int) (Math.random () * 6 + 1);
rows = (int) (Math.random () * 5 + 1);
}
This.grid[cols][rows] = Save[n];
n--;
}
Mainframe.setvisible (FALSE);
Pressinformation = false; Here is the button click Information must be classified as the original
Init ();
for (int i = 0; i < 6; i++) {
for (int j = 0; J < 5; J + +) {
if (grid[i + 1][j + 1] = = 0)
Diamondsbutton[i][j].setvisible (FALSE);
}
}
}
public void Estimateeven (int placex, int placey, JButton BZ) {
if (pressinformation = = False) {
x = Placex;
y = Placey;
Secondmsg = Grid[x][y];
Secondbutton = BZ;
Pressinformation = true;
} else {
x0 = x;
y0 = y;
Fristmsg = secondmsg;
Firstbutton = Secondbutton;
x = Placex;
y = Placey;
Secondmsg = Grid[x][y];
Secondbutton = BZ;
if (fristmsg = = secondmsg && Secondbutton!= Firstbutton) {
Xiao ();
}
}
}
public void Xiao () {//the same situation can be eliminated. Careful analysis, all the notes
if (x0 = = x && (y0 = = y + 1 | | y0 = = y-1))
|| ((x0 = = x + 1 | | x0 = = x-1) && (y0 = = y))) {//Judge whether adjacent
Remove ();
} else {
for (j = 0; J < 7; J + +) {
if (grid[x0][j] = = 0) {//Determine which button is blank for the first button
if (Y > J) {//If the y-coordinate of the second button is greater than the y-coordinate of the empty button, the first button is on the left of the second button
for (i = y-1 i >= j; i--) {//judge the left side of the second button until there is no button in the middle of the first button
if (Grid[x][i]!= 0) {
k = 0;
Break
} else {
K = 1;
}//K=1 instructions passed the first validation
}
if (k = = 1) {
Linepassone ();
}
}
if (Y < J) {//If the y-coordinate of the second button is less than the y-coordinate of the empty button, the first button is on the right side of the second button
for (i = y + 1; I <= J; i++) {//judge the left side of the second button until there is no button in the middle of the first button
if (Grid[x][i]!= 0) {
k = 0;
Break
} else {
K = 1;
}
}
if (k = = 1) {
Linepassone ();
}
}
if (y = = j) {
Linepassone ();
}
}
if (k = = 2) {
if (x0 = = x) {
Remove ();
}
if (x0 < x) {
for (n = x0 n <= x-1; n++) {
if (Grid[n][j]!= 0) {
k = 0;
Break
}
if (grid[n][j] = = 0 && n = = x-1) {
Remove ();
}
}
}
if (x0 > x) {
for (n = x0; n >= x + 1; n--) {
if (Grid[n][j]!= 0) {
k = 0;
Break
}
if (grid[n][j] = = 0 && N = = x + 1) {
Remove ();
}
}
}
}
}
for (i = 0; i < 8; i++) {//Column
if (grid[i][y0] = = 0) {
if (x > i) {
for (j = x-1 J >= i; j--) {
if (Grid[j][y]!= 0) {
k = 0;
Break
} else {
K = 1;
}
}
if (k = = 1) {
Rowpassone ();
}
}
if (x < i) {
for (j = x + 1; j <= I; j + +) {
if (Grid[j][y]!= 0) {
k = 0;
Break
} else {
K = 1;
}
}
if (k = = 1) {
Rowpassone ();
}
}
if (x = = i) {
Rowpassone ();
}
}
if (k = = 2) {
if (y0 = = y) {
Remove ();
}
if (Y0 < y) {
for (n = y0 n <= y-1; n++) {
if (Grid[i][n]!= 0) {
k = 0;
Break
}
if (grid[i][n] = = 0 && n = = y-1) {
Remove ();
}
}
}
if (y0 > Y) {
for (n = y0; n >= y + 1; n--) {
if (Grid[i][n]!= 0) {
k = 0;
Break
}
if (grid[i][n] = = 0 && N = = y + 1) {
Remove ();
}
}
}
}
}
}
}
public void Linepassone () {
if (Y0 > J) {//First button peer blank button on left
for (i = y0-1 i >= j; i--) {//Determine if there is a button between the first button and the left blank button
if (Grid[x0][i]!= 0) {
k = 0;
Break
} else {
K = 2;
}//k=2 note passed second validation
}
}
if (Y0 < J) {//First button peer blank button between and the second button
for (i = y0 + 1; I <= J; i++) {
if (Grid[x0][i]!= 0) {
k = 0;
Break
} else {
K = 2;
}
}
}
}
public void Rowpassone () {
if (x0 > i) {
for (j = x0-1 J >= i; j--) {
if (Grid[j][y0]!= 0) {
k = 0;
Break
} else {
K = 2;
}
}
}
if (x0 < i) {
for (j = x0 + 1; j <= I; j + +) {
if (Grid[j][y0]!= 0) {
k = 0;
Break
} else {
K = 2;
}
}
}
}
public void Remove () {
firstbutton.setvisible (FALSE);
Secondbutton.setvisible (FALSE);
fraction ();
pressinformation = false;
k = 0;
Grid[x0][y0] = 0;
Grid[x][y] = 0;
}
public void actionperformed (ActionEvent e) {
if (e.getsource () = = Newlybutton) {
int grid[][] = new INT[8][7];
This.grid = grid;
Randombuild ();
Mainframe.setvisible (FALSE);
Pressinformation = false;
Init ();
}
if (e.getsource () = = Exitbutton)
System.exit (0);
if (e.getsource () = = Resetbutton)
Reload ();
for (int cols = 0; cols < 6; cols++) {
for (int rows = 0; rows < 5; rows++) {
if (e.getsource () = = Diamondsbutton[cols][rows])
Estimateeven (cols + 1, rows + 1, diamondsbutton[cols][rows]);
}
}
}
public static void Main (string[] args) {
Maingame mg = new Maingame ();
Mg.randombuild ();
Mg.init ();
}
}
I hope this article will help you with your Java programming.