Concentration Game with Pictures
I am attempting to make a concentration game using images for my computer science class at school, and I am having a hard time. Here is what I have so far, so if anyone has any suggestions, they are appreciated.
Code:
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 NBARewind extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JButton[] gameButton = new JButton[16];
private ArrayList<ImageIcon> gameList = new ArrayList<ImageIcon>();
private int counter = 0;
private int[] buttonID = new int[2];
private ImageIcon[] buttonValue = new ImageIcon[16];
ImageIcon barkley = new ImageIcon("Barkley");
ImageIcon hill = new ImageIcon("Hill");
ImageIcon jordan = new ImageIcon("Jordan");
ImageIcon kemp = new ImageIcon("Kemp");
ImageIcon olajuwon = new ImageIcon("Olajuwon");
ImageIcon robinson = new ImageIcon("Robinson");
ImageIcon rodman = new ImageIcon("Rodman");
ImageIcon stockton = new ImageIcon("Stockton");
ImageIcon nba = new ImageIcon("NBA");
public NBARewind()
{
init();
panel();
setArrayList();
setTitle("NBA Rewind");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void setArrayList()
{
gameList.add(barkley);
gameList.add(hill);
gameList.add(jordan);
gameList.add(kemp);
gameList.add(olajuwon);
gameList.add(robinson);
gameList.add(rodman);
gameList.add(stockton);
gameList.add(barkley);
gameList.add(hill);
gameList.add(jordan);
gameList.add(kemp);
gameList.add(olajuwon);
gameList.add(robinson);
gameList.add(rodman);
gameList.add(stockton);
shuffle(gameList);
}
public void init()
{
for (int j = 0; j < gameButton.length; j++)
{
gameButton[j] = new JButton();
gameButton[j].addActionListener(this);
}
}
public void panel()
{
Panel gamePanel = new Panel();
gamePanel.setLayout(new GridLayout(4, 4));
for (int j = 0; j < gameButton.length; j++)
{
gamePanel.add(gameButton[j]);
}
add(gamePanel, BorderLayout.CENTER);
}
public boolean sameValues()
{
if (buttonValue[0] == buttonValue[1])
{
return true;
}
return false;
}
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < gameButton.length; i++)
{
if (gameButton[i] == e.getSource())
{
//gameButton[i].setIcon(gameList.get(i));
gameButton[i].setEnabled(false);
counter++;
if (counter == 3)
{
if (sameValues())
{
gameButton[buttonID[0]].setEnabled(false);
gameButton[buttonID[1]].setEnabled(false);
}
else
{
gameButton[buttonID[0]].setEnabled(true);
gameButton[buttonID[0]].setIcon(nba);
gameButton[buttonID[1]].setEnabled(true);
gameButton[buttonID[1]].setText("");
}
counter = 1;
}
if (counter == 1)
{
buttonID[0] = i;
buttonValue[0] = gameList.get(i);
}
if (counter == 2)
{
buttonID[1] = i;
buttonValue[1] = gameList.get(i);
}
}
}
}
public static void main(String[] args)
{
new NBARewind();
}
}