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
Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.
Hi
ReplyDeleteQuite Interesting post!!! Thanks for posting such a useful post. I wish to read your upcoming blog to enhance my skill set in programming languages of java, keep blogging.
Regards,
SAP training in chennai
Helo Asad
ReplyDeleteI really enjoyed while reading your article, the information you have mentioned in this post was damn good. Keep sharing your blog with updated and useful information.
Regards,
SAP Training in Chennai|SAP Course in Chennai|sap training in Chennai|SAP MM Training In Chennai
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
tiktok jeton hilesi
antalya saç ekimi
ınstagram takipçi satın al
instagram takipçi satın al
metin2 pvp serverlar
instagram takipçi satın al
SMM PANEL
ReplyDeletesmm panel
iş ilanları
İnstagram Takipçi Satın Al
Hırdavatçı Burada
beyazesyateknikservisi.com.tr
servis
Jeton hile
maltepe alarko carrier klima servisi
ReplyDeletekadıköy alarko carrier klima servisi
maltepe daikin klima servisi
kadıköy daikin klima servisi
kartal toshiba klima servisi
ümraniye toshiba klima servisi
ümraniye beko klima servisi
beykoz vestel klima servisi
üsküdar vestel klima servisi