import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
public class Matchgame extends JPanel implements ActionListener {
private final JPanel Brd = new JPanel(new BorderLayout(3, 3));
String Winner;
private JButton[][] BoardSquares = new JButton[4][10];// 10 X 4 grid
int board[][] = {
{ 2, 11, 3, 2, 6, 5, 7, 9, 8, 1 },// numbers to fill Jbutton with
{ 10, 6, 1, 3, 8, 12, 13, 14, 15, 4 },
{ 17, 9, 10, 12, 11, 20, 13, 16, 4, 5 },
{ 19, 7, 17, 16, 14, 15, 19, 20, 18, 18 } };
private JPanel Board;// Sets JPanel
int guess1, guess2; // number value of the button pressed
public static boolean attempt1, attempt2, player1, player2; // players and
// attempts
int A1i, A1j, A2i, A2j;// numbers to put in place of board[i] and board[j]
private int rows = 4;// number of rows
private int cols = 10;// number of columns
private int turn = 20;// player turn
public int counter = -1;// counter
public int P1score = 0;// player 1 score
public int P2score = 0;// player 2 score
Matchgame() {
initialize();
}
public final void initialize() {
// set up the main GUI
Board = new JPanel(new GridLayout(rows, cols));
Board.setBorder(new LineBorder(Color.BLACK));
// Board.add(Score);
Brd.add(Board);
// create the board squares
for (int i = 0; i < BoardSquares.length; i++) {
for (int j = 0; j < BoardSquares[i].length; j++) {
JButton b = new JButton();// adds buttons
BoardSquares[i][j] = b;
b.addActionListener(this);
}
}
for (int i = 0; i < BoardSquares.length; i++) {
for (int j = 0; j < BoardSquares[0].length; j++) {
switch (j) {
default:
Board.add(BoardSquares[i][j]);
}
}
}
}
public final JComponent getBoard() {
return Board;
}
public final JComponent getGui() {
return Brd;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
Matchgame cb = new Matchgame();
JFrame f = new JFrame("MATCH GAME");
f.add(cb.getGui());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.setSize(500, 400);
// ensures the minimum size is enforced.
// f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
public void sameValues() {
if (player1 = true) {
P1score++;
}
if (player2 = true) {
P2score++;
}
}
public void actionPerformed(ActionEvent arg0) {
if (turn % 2 == 0) {//if even then
player1 = true;
} else {
player2 = true;
}
++counter;
if (counter == 0) {
attempt1 = true;
} else if (counter == 1) {
attempt2 = true;
}
if (counter == 2) {
if (guess1 == guess2) {
System.out.println("test" + guess1 + "test" + guess2);
sameValues();
} else if (guess1 != guess2) {
System.out.println("test" + guess1 + "test" + guess2);
BoardSquares[A1i][A1j].setText("");
BoardSquares[A2i][A2j].setText("");
}
counter = -1;
turn--;
}
for (int i = 0; i < BoardSquares.length; i++) {
for (int j = 0; j < BoardSquares[i].length; j++) {
if ((counter == 0) && (counter < 4)) {
if (arg0.getSource() == BoardSquares[i][j]) {
BoardSquares[i][j].setText("" + board[i][j]);
if (attempt1 == true) {
guess1 = board[i][j];
A1i = i;
A1j = j;
} else if (attempt2 == true) {
guess2 = board[i][j];
A2i = i;
A2j = j;
}
}
}
}
}
if (turn == 0) {
EndGame();
}
}
public void EndGame() {
if ((turn == 0) && (P1score > P2score)) {
Winner = JOptionPane
.showInputDialog("Player 1 won, enter anything to restart game!");
P1score = 0;
P2score = 0;
}
if ((turn == 0) && (P2score > P1score)) {
Winner = JOptionPane
.showInputDialog("Player 2 won, enter anything to restart game!");
P1score = 0;
P2score = 0;
}
}
}