Question
I am making a Memory Card matching game in java. Currently I have code that will output a 4x4 display of an two-dimensional array that
I am making a Memory Card matching game in java. Currently I have code that will output a 4x4 display of an two-dimensional array that has 2 sets of the same cards (Ace-8). I can click on a card and it flips from the back of the card that is initially displayed to the image of the card. The images of the cards come from a file named \"card\" with all the cards numbered 1-54 and the back image of a card called \"b1fv\". MemCardGame inherits from \"Card\" which is shown after the MemCardGame code. Also, i am using a parrallel array \"cards\" to array \"btn\" to try to implement the matching. I have been trying to get the program to check if two cards that are clicked are matched and if not, after a 3 second timer the cards flip back over. If matched the cards stay flipped. Once all the cards are matched it says game won. Here is what i have so far. I need help completing to code. If you need more information let me know.
______________________________________________________________________________________ import java.util.ArrayList; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.paint.Color; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.image.Image; import javafx.scene.control.Button; import javafx.scene.image.ImageView; import javafx.event.ActionEvent; import javafx.event.EventHandler; import java.util.Collections; import java.util.List; import javafx.scene.layout.GridPane; import javax.swing.Timer; public class MemCardGame extends Application { Button[][] btn=new Button[4][4]; Image [] images = new Image[16]; Card [][] cards = new Card[4][4] //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
boolean clicked = false; Button bt; Button bt2; public void start( Stage stage ) { CardFlipper bh = new CardFlipper( ); GridPane root = new GridPane(); // Image images = new Image(\"card/\" + ((int)(Math.random() * (54)) + 1) +\".png\"); Image imageBack = new Image(\"card/b1fv.png\"); for ( int k = 0; k images[k] = new Image(\"card/\" + (k + 1) +\".png\"); for ( int k = 0; k images[k+8] = new Image(\"card/\" + (k+1) + \".png\");
for(int i =0; i { for(int j = 0; j4;> { btn[i][j]= new Button(\"\", new ImageView(imageBack) ); //bt2 = new Button(\"\", new ImageView(images) ); root.add(btn[i][j], j, i); //root.add(bt2, 1, 0); btn[i][j].setOnAction( bh ); } } Scene scene = new Scene( root, 450, 450 ); stage.setScene( scene ); stage.setTitle( \"Click a card\" ); //bt2.setOnAction( bh ); stage.show( ); } public static void main( String [] args ) { launch( args ); }
private class CardFlipper implements EventHandler { @Override public void handle( ActionEvent event ) { for(int i=0; i { for(int j=0; j { if (event.getSource( ) == btn[i][j] ) { btn[i][j].setGraphic(new ImageView(images[4*i+j]));
//btn[i][j].getGraphic().equals(btn[s][t].getGraphic())
//card[i][j].setMatched(true);
//card[s][t].setMatched(true); clicked = true; }} } } }//end CardFlipper class }//end outer class
---------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Card { private boolean isFaceUp; //true if face up, false otherwise private boolean isMatched; //true if matched to other card, false otherwise public Card() { isFaceUp = false; isMatched = false; } public Card setIsFaceUp(boolean ifup) { this.isFaceUp = ifup; return this; } public Card setIsMatched(boolean iMatch) { this.isMatched = iMatch; return this; } public boolean getIsFaceUp() { return this.isFaceUp; } public boolean getIsMatched() { return this.isMatched; } }//end card class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started