Programming logical Questions and logic
building conceptual code | Mini Games Project in Java
This is the simple but interesting
game in Java for beginners to understand the programming logic. The logic
behind the program is to match the same numbers in a row or column. The Simple
Java program is a mini project for beginners as well to work on GUI.
Source Code
package Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;
public class Game extends JFrame implements ActionListener {
/**
*
*/
private static final long sVUID = 1L;
private JButton exitPressed,
replayPressed;
private JButton[]
allButtons = new JButton[16];
private ArrayList<Integer> list = new ArrayList<Integer>();
private int counter = 0;
private int[] id = new int[2];
private int[] value = new int[2];
public Game()
{
initComponents();
initPanel();
setArrayListText();
setTitle("Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void initComponents() {
for
(int i = 0; i < allButtons.length; i++) {
allButtons[i] = new JButton();
allButtons[i].setFont(new Font("Serif", Font.BOLD, 28));
allButtons[i].addActionListener(this);
}
exitPressed = new JButton("Exit");
exitPressed.addActionListener(this);
replayPressed = new JButton("Replay");
replayPressed.addActionListener(this);
}
public void initPanel() {
Panel gamePnl =
new Panel();
gamePnl.setLayout(new GridLayout(4, 4));
for
(int i = 0; i < allButtons.length; i++) {
gamePnl.add(allButtons[i]);
}
Panel buttonPnl =
new Panel();
buttonPnl.add(replayPressed);
buttonPnl.add(exitPressed);
buttonPnl.setLayout(new GridLayout(1, 0));
add(gamePnl, BorderLayout.CENTER);
add(buttonPnl,
BorderLayout.SOUTH);
}
public void setArrayListText() {
for
(int i = 0; i < 2; i++) {
for (int ii = 1; ii < (allButtons.length / 2) + 1; ii++) {
list.add(ii);
}
}
shuffle(list);
int
newLine = 0;
for
(int a = 0; a < list.size(); a++) {
newLine++;
System.out.print(" " + list.get(a));
if (newLine == 4) {
System.out.println();
newLine = 0;
}
}
}
public boolean sameValues() {
if
(value[0] == value[1]) {
return true;
}
return false;
}
@Override
public void actionPerformed(ActionEvent e) {
if
(exitPressed == e.getSource()) {
System.exit(0);
}
if
(replayPressed == e.getSource()) {
this.dispose();
new Game();
}
for
(int i = 0; i < allButtons.length; i++) {
if (allButtons[i] == e.getSource()) {
allButtons[i].setText("" + list.get(i));
allButtons[i].setEnabled(false);
counter++;
if (counter == 3) {
if
(sameValues()) {
allButtons[id[0]].setEnabled(false);
allButtons[id[1]].setEnabled(false);
} else {
allButtons[id[0]].setEnabled(true);
allButtons[id[0]].setText("");
allButtons[id[1]].setEnabled(true);
allButtons[id[1]].setText("");
}
counter = 1;
}
if (counter == 1) {
id[0] = i;
value[0] = list.get(i);
}
if (counter == 2) {
id[1] = i;
value[1] = list.get(i);
}
}
}
}
public static void main(String[] args) {
new
Game();
}
}
Output of the Program
No comments:
Post a Comment