This site requires JavaScript, please enable it in your browser!
Greenfoot back
JollyGreenGiant
JollyGreenGiant wrote ...

2020/4/26

How do i make a simple memory card game?

1
2
3
4
JollyGreenGiant JollyGreenGiant

2020/4/26

#
Hi all, I'm new to this. I want to know how I can create a game that has 20 cards in it and you have to flip them over to reveal the image underneath it. Once you have two matches they disappear and you have to repeat this process until you clear the whole deck, I would also like to include a reset button. Thanks
JollyGreenGiant JollyGreenGiant

2020/4/26

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo) import java.util.Random; import java.util.Scanner; /** * Write a description of class MemoryGame here. * * @author (your name) * @version (a version number or a date) */ public class MemoryGame extends World { private Card board; private String words = {"g0","g0","g1","g1", "g2", "g2", "g3", "g3", "g4", "g4", "g5", "g5", "g6", "g6", "g7", "g7", "g8", "g8", "g9", "g9",}; private Random r; private Scanner reader; MemoryGame() { r = new Random(); reader = new Scanner(System.in); board = new Card ; shuffle(); setCells(); printCells(); playGame(); } public void playGame() { choosePairOfCards(); } public void choosePairOfCards() { int cardChoice, row1, col1, row2, col2; System.out.println(); System.out.println("Enter the number on the card."); System.out.print("First Card Choice? >"); cardChoice = getInputAsInt(); row1 = cardChoice / 4; col1 = cardChoice % 4; board.SetShowingStatus(); System.out.print('\u000C'); printCells(); } public void setCells() { int a=0; for (int row=0; row<board.length; row++) { for(int col=0; col<board.length; col++) { { board = new Card(words, a); } } } } public void printCells() { Card aCard; for (int row=0; row<board.length; row++) { for (int col=0; col<board.length; col++) { aCard = board ; aCard.showCard(); } System.out.println(); } } public void shuffle() { for (int a=0; a<words.length; a++) { int pos = r.nextInt(words.length); String temp = words; words = words; words = temp; } } public int getInputAsInt() { String temp = reader.nextLine(); return Integer.parseInt(temp); } public String getInputAsString() { return reader.nextLine(); } }
JollyGreenGiant JollyGreenGiant

2020/4/26

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Card here. * * @author (your name) * @version (a version number or a date) */ public class Card extends Actor { boolean showing; String back; int front; public Card(String theBack, int theFront) { showing = false; back = theBack; front = theFront; } public void showCard() { if (showing) System.out.print(String.format("%10s",back)); else System.out.print(String.format("%10s","")); } public void SetShowingStatus() { if(showing) showing = false; else showing = true; } }
JollyGreenGiant JollyGreenGiant

2020/4/26

#
This is what i managed to put together so far.
danpost danpost

2020/4/26

#
JollyGreenGiant wrote...
I'm new to this.
I presume you mean you are new to greenfoot; but, you have used before, and are familiar with, java. Is this correct?
JollyGreenGiant JollyGreenGiant

2020/4/26

#
Im new to Greenfoot yes, and I know a little bit of Java.
JollyGreenGiant JollyGreenGiant

2020/4/26

#
This is what I need to do for my assesment for college. Here is the outline of what is required: The game is played by clicking on the squares to match ‘like’ images, each try is recorded by a counter. Aims • To successfully match 10 pairs of images • To get the number of tries for successful matches as low as possible. The table produced to store the images is 5 columns and 4 rows, thus displaying 20 images or 10 pairs counted in an array from 0 to 9. You will keep the images stored with the prefix ‘g’ and a number 0 to 9 and the file extension which happens to be .gif. The reason for this is that the code will use the numbers in the loop. The flip side of each image is the same and is called cover.gif
danpost danpost

2020/4/26

#
JollyGreenGiant wrote...
Im new to Greenfoot yes, and I know a little bit of Java.
Well, with greenfoot, you cannot use the Scanner class for standard input as greenfoot captures the input and supplies methods for retrieving it. Furthermore, greenfoot provides a canvas (world background) for your project (your initial world will be created upon compilation of your codes). Your world (MemoryGame) constructor will need several int arguments before it will compile Please refer to the World class API documentation. You should probably look over the other classes provided by greenfoot as well. The Greenfoot class will have the methods for standard input.
danpost danpost

2020/4/26

#
You will also want to review the Greenfoot tutorials as that will get you acquainted with using greenfoot.
JollyGreenGiant JollyGreenGiant

2020/4/26

#
Thank you for your help, I will take a look at the API documentation. I worked my way through the tutorials, so I understand some of the basics, but im still a novice at this.
JollyGreenGiant JollyGreenGiant

2020/4/27

#
Update: I have removed the scanner class. I also think that the user should interact with the application via mouse clicks (and not via keyboard input), but I'm unsure on how to do this. Is there any tutorials I could find that covers this memory game project in greenfoot?
danpost danpost

2020/4/27

#
JollyGreenGiant wrote...
Update: I have removed the scanner class. I also think that the user should interact with the application via mouse clicks (and not via keyboard input), but I'm unsure on how to do this. Is there any tutorials I could find that covers this memory game project in greenfoot?
Did you notice the mouse related methods in the Greenfoot class? Can you please re-post your revised Card class?
JollyGreenGiant JollyGreenGiant

2020/4/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Card here. * * @author (your name) * @version (a version number or a date) */ public class Card extends Actor { boolean showing; String back; int front; public Card(String theBack, int theFront) { showing = false; back = theBack; front = theFront; } public void showCard() { if (showing) System.out.print(String.format("%10s",back)); else System.out.print(String.format("%10s","")); } public void SetShowingStatus() { if(showing) showing = false; else showing = true; } }
JollyGreenGiant JollyGreenGiant

2020/4/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo) import java.util.Random; /** * Write a description of class MemoryGame here. * * @author (your name) * @version (a version number or a date) */ public class MemoryGame extends World { private Card board; private String words = {"g0","g0","g1","g1", "g2", "g2", "g3", "g3", "g4", "g4", "g5", "g5", "g6", "g6", "g7", "g7", "g8", "g8", "g9", "g9",}; private Random r; MemoryGame() { r = new Random(); board = new Card ; shuffle(); setCells(); printCells(); playGame(); } public void playGame() { choosePairOfCards(); } public void choosePairOfCards() { int cardChoice, row1, col1, row2, col2; System.out.println(); System.out.println("Enter the number on the card."); System.out.print("First Card Choice? >"); cardChoice = getInputAsInt(); row1 = cardChoice / 4; col1 = cardChoice % 4; board.SetShowingStatus(); System.out.print('\u000C'); printCells(); } public void setCells() { int a=0; for (int row=0; row<board.length; row++) { for(int col=0; col<board.length; col++) { { board = new Card(words, a); } } } } public void printCells() { Card aCard; for (int row=0; row<board.length; row++) { for (int col=0; col<board.length; col++) { aCard = board ; aCard.showCard(); } System.out.println(); } } public void shuffle() { for (int a=0; a<words.length; a++) { int pos = r.nextInt(words.length); String temp = words; words = words; words = temp; } } public int getInputAsInt() { String temp = reader.nextLine(); return Integer.parseInt(temp); } public String getInputAsString() { return reader.nextLine(); } }
danpost danpost

2020/4/27

#
Okay. In order for there to be something to click on, you will need to convert from using terminal output to using actor images. So in the Card class, instead of "String back", you will need "GreenfootImage back" and another field for the showing image. The showCard method will then use these. You will also need a "public void act()" method to detect a click on a Card instance.
There are more replies on the next page.
1
2
3
4