Click to See Complete Forum and Search --> : Java Concentration Game


abcross92
05-24-2010, 08:32 AM
I'm writing a concentration game, using netbeans, for a highschool java class and need some help. Currently I'm just using numbers as opposed to pictures or colors for the flip side of the cards. I put all the numbers in an arraylist, but don't know how to shuffle the list. Also what would be the best way to 'flip' the card and keep it flipped until a second card is flipped, compare the two cards, then flip them back over if they do not contain the same number.

Here's my code:


package concentrationjava;
import java.util.Random;
import java.util.ListIterator;
import java.util.ArrayList;

public class NewApplet4 extends javax.swing.JApplet {


public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}


private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
ScoreButton = new javax.swing.JButton();
DoneButton = new javax.swing.JButton();
Random generator = new Random();
int selection;
ArrayList<String> al = new ArrayList<String>();
ListIterator listIterator = al.listIterator();

al.add("1");
al.add("1");
al.add("2");
al.add("2");
al.add("3");
al.add("3");
al.add("4");
al.add("4");
al.add("5");
al.add("5");
al.add("6");
al.add("6");
al.add("7");
al.add("7");
al.add("8");
al.add("8");

ArrayList<String> al2 = new ArrayList<String>();
ListIterator listIterator2 = al2.listIterator();
for(int i = 0; i < al.size(); i++)
{
al2.add(ListIterator.next(generator.nextInt(al.size())));
ListIterator.remove();
}

/*
*Generated netbeans code
*
*
*/

private void jButton18ActionPerformed(java.awt.event.ActionEvent evt){
//Done button
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//First Button
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
//Second Button
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){
//Third Button
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt){
//Fourth Button
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt){
//Fifth Button
}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt){
//Sixth Button
}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt){
//Eigth Button
}

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// Seventh Button
}

private void ScoreButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Score Button
}

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// Ninth Button
}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
// Tenth button
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
// Eleventh Button
}

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// Twelvth Button
}

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
// Thirteenth Button
}

private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
// Fourteenth Button
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// Fifteenth Button
}

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// Sixteenth Button
}

private void DoneButtonActionPerformed(java.awt.event.ActionEvent evt){
// TODO add your handling code here:
}



private javax.swing.JButton DoneButton;
private javax.swing.JButton ScoreButton;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;

}

criterion9
05-24-2010, 12:35 PM
Collections.shuffle(Arrays.asList(myArrayOfStrings ));

Are you looking for a logic solution to your card flipping question? It might be best to write it out using pencil and paper to figure out the logic.

sohguanh
05-24-2010, 08:55 PM
I'm writing a concentration game, using netbeans, for a highschool java class and need some help. Currently I'm just using numbers as opposed to pictures or colors for the flip side of the cards. I put all the numbers in an arraylist, but don't know how to shuffle the list. Also what would be the best way to 'flip' the card and keep it flipped until a second card is flipped, compare the two cards, then flip them back over if they do not contain the same number.


Sorry for interrupting but if it is a new Java Swing program, the trend now is to use JavaFX and quite a number of Java Swing/2D programmers have migrated to the new language JavaFX scripts. It makes building GUI easier and JavaFX is still evolving from my personal experience.

Back to your question, I think you need to have a psuedo-code or algorithm draft out first what you want to do. If times allows, you can forgo GUI code and focus on plain command line code to get your game logic correct first. Then once satisfied, you move on to add in those GUI code.

Please segregate non-GUI code and GUI code into their respective Java classes. This is good design say next time you want to port your game to mobile devices, you only need to change the GUI code only.

PS Above is how I go about developing my own pet project. I uses Java Swing in 2001 for the first version, update them in 2010 to use Java Generics etc and then port to Android and now in the process to port to JavaFX.