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
5
6
JollyGreenGiant JollyGreenGiant

2020/4/29

#
My deadline is for next Thursday.
danpost danpost

2020/4/29

#
JollyGreenGiant wrote...
My deadline is for next Thursday.
What are you having issues with at the moment? (explain and show current codes)
JollyGreenGiant JollyGreenGiant

2020/4/29

#
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 [4][4];
       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[row1][col1].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[0].length; col++)
            {
                {
                    board[row][col] = new Card(words[a], a);
                }
            }
        }
    }
    
    public void printCells()
    {
        Card aCard;
        for (int row=0; row<board.length; row++)
        {
            for (int col=0; col<board[0].length; col++)
            {
                aCard = board [row][col];
                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[a];
            words[a] = words[pos];
            words [pos] = temp; 
        }
    }
    
    public int getInputAsInt()
    {
        String temp = reader.nextLine();
        return Integer.parseInt(temp);
    }
    
    public String getInputAsString()
    {
        return reader.nextLine();
    }
    
}
JollyGreenGiant JollyGreenGiant

2020/4/29

#
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 { private static GreenfootImage back = new GreenfootImage("cover.gif"); private static Card firstPicked; public static int matchTries; public static int matchCount; private GreenfootImage front; private boolean showing; private int value; public Card(String fname, int val) { value = val; front = new GreenfootImage(fname+".gif"); setImage(back); } 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; } private void flipCard() { showing = ! showing; setImage(showing ? front : back); } public void firstClicked() { this.flip(); Greenfoot.delay(40); if (firstClicked == null) { firstClicked = this; } else { String image1 = firstClicked.getImage().toString().substring(17);; image1 = image1.substring(0, image1.indexOf(" ")); String image2 = this.getImage().toString().substring(17);; image2 = image2.substring(0, image2.indexOf(" ")); if (image1.equals(image2)) { Greenfoot.playSound("fanfare.wav"); // example code addObject(new Correct(), getX(), getY()+100); // or wherever } else { Greenfoot.playSound("wrong.wav"); // example code firstClicked.flip(); this.flip(); } firstClicked = null; } } private void flip() { imageIndex = (imageIndex+1)%2; setImage(images); } public void ifCorrect() { if((card1Flipped == true) && (card2Flipped == true)) { Greenfoot.delay(30); displayCorrect(); getWorld().removeObjects(getWorld().getObjects(Card2.class)); getImage().setTransparency(0); } else if((card3Flipped == true) && (card4Flipped == true)) { Greenfoot.delay(30); displayCorrect(); getWorld().removeObjects(getWorld().getObjects(Card3.class)); getWorld().removeObjects(getWorld().getObjects(Card4.class)); } else { flipBack(); } if (secondCard == null) { Cards card = pickedCard(); if (firstCard == null) { firstCard = card; } else { secondCard = card; if (firstCardImage().equals(secondCardImage())) { correctSequence = true; initializeCorrectSequence(); } else { wrongSequence = true; initializeWrongSequence(); } } } else { if (wrongSequence) { performWrongSequenceStep(); if (lastWrongsequenceStep()) { wrongSequence = false; firstCard = null; secondCard = null; } } else if (correctSequence) { performCorrectSequenceStep(); if (lastCorrectSequenceStep()) { correctSequence = false; firstCard = null; secondCard = null; } } } } }
JollyGreenGiant JollyGreenGiant

2020/4/29

#
I think the card class is fine, but i don't know what to write in the world class.
JollyGreenGiant JollyGreenGiant

2020/4/29

#
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
{
    private static GreenfootImage back = new GreenfootImage("cover.gif");
 
    private static Card firstPicked;
    public static int matchTries;
    public static int matchCount; 
 
    private GreenfootImage front;
    private boolean showing;
    private int value;
    
    
    public Card(String fname, int val)
    {
        value = val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
    }
    
     public void showCard()
    {
         if (showing)
           System.out.print(String.format("%10s",back));
        else
            System.out.print(String.format("%10s","["+front+"]"));
    }
    
    public void SetShowingStatus()
    {
        if(showing)
            showing = false;
        else
            showing = true;
    }
    
    private void flipCard()
    {
     showing = ! showing;
     setImage(showing ? front : back);
    }
    
    public void firstClicked()
    {
    this.flip();
    Greenfoot.delay(40);
    if (firstClicked == null)
    {
        firstClicked = this;
    }
    else
    {
        String image1 = firstClicked.getImage().toString().substring(17);;
        image1 = image1.substring(0, image1.indexOf("   "));
        String image2 = this.getImage().toString().substring(17);;
        image2 = image2.substring(0, image2.indexOf("   "));
        if (image1.equals(image2))
        {
            Greenfoot.playSound("fanfare.wav"); // example code
            addObject(new Correct(), getX(), getY()+100); // or wherever
        }
        else
        {
            Greenfoot.playSound("wrong.wav"); // example code
            firstClicked.flip();
            this.flip();
        }
        firstClicked = null;
    }
   }
 

    private void flip()
   {
    imageIndex = (imageIndex+1)%2;
    setImage(images[imageIndex]);
   }
   
    public void ifCorrect()
   {
    if((card1Flipped == true) && (card2Flipped == true))
    {
        Greenfoot.delay(30);
        displayCorrect();
        getWorld().removeObjects(getWorld().getObjects(Card2.class));
        getImage().setTransparency(0);
    }
     else if((card3Flipped == true) && (card4Flipped == true))
    {
        Greenfoot.delay(30);
        displayCorrect();
        getWorld().removeObjects(getWorld().getObjects(Card3.class));
        getWorld().removeObjects(getWorld().getObjects(Card4.class));
 
    }
    else
    {
        flipBack();
    }
    
    if (secondCard == null)
    {
    Cards card = pickedCard();
    if (firstCard == null)
    {
        firstCard = card;
    }
    else
    {
        secondCard = card;
        if (firstCardImage().equals(secondCardImage()))
        {    
            correctSequence = true;
            initializeCorrectSequence();
        }
        else
        {
            wrongSequence = true;
            initializeWrongSequence();
        }
    }
    }
    else
    {
    if (wrongSequence)
    {
        performWrongSequenceStep();
        if (lastWrongsequenceStep())
        {
            wrongSequence = false;
            firstCard = null;
            secondCard = null;
        }
    }
    else if (correctSequence)
    {
        performCorrectSequenceStep();
        if (lastCorrectSequenceStep())
        {
            correctSequence = false;
            firstCard = null;
            secondCard = null;
        }
    }
   }
 }
}
danpost danpost

2020/4/29

#
I really think you should strip your world class to bare minimum before anything else. You can keep the Card array and the shuffle method. The board array should be 5x4, not 4x4. The setCells method will need to change what it does inside the double loop. You need a super constructor call at the beginning of your world class constructor and you will need an act method to control the game. The Card class also needs trimming. In fact, the only things I would provide a card is its value and its two images, plus a boolean for its flipped state with a method that flips the card. I would then run the game completely from the world class.
JollyGreenGiant JollyGreenGiant

2020/4/29

#
Okay I'll try that thanks.
JollyGreenGiant JollyGreenGiant

2020/4/30

#
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
{
    private static GreenfootImage back = new GreenfootImage("cover.gif");
 
    private static Card firstPicked;
    public static int matchTries;
    public static int matchCount; 
 
    private GreenfootImage front;
    private boolean showing;
    private int value;
    
    
    public Card(String fname, int val)
    {
        value = val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
    }
        
        
    private void flipCard()
    {
     showing = ! showing;
     setImage(showing ? front : back);
    }
    
    
}
JollyGreenGiant JollyGreenGiant

2020/4/30

#
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 [5][4];
       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[row1][col1].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[0].length; col++)
            {
                {
                    board[row][col] = new Card(words[a], a);
                }
            }
        }
    }
    
    public void printCells()
    {
        Card aCard;
        for (int row=0; row<board.length; row++)
        {
            for (int col=0; col<board[0].length; col++)
            {
                aCard = board [row][col];
                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[a];
            words[a] = words[pos];
            words [pos] = temp; 
        }
    }
    
    public int getInputAsInt()
    {
        String temp = reader.nextLine();
        return Integer.parseInt(temp);
    }
    
    public String getInputAsString()
    {
        return reader.nextLine();
    }
    
}
JollyGreenGiant JollyGreenGiant

2020/4/30

#
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; MemoryGame() { board = new Card ; shuffle(); } public void playGame() { choosePairOfCards(); } 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; } } }
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;
   
    
    
    MemoryGame()
    {    
       board = new Card [5][4];
       shuffle();
       
    }
 
    public void playGame()
    {
        choosePairOfCards();
    }
      
    public void setCells()
    {
        int a=0;
        for (int row=0; row<board.length; row++)
        {
            for(int col=0; col<board[0].length; col++)
            {
                {
                    board[row][col] = new Card(words[a], a);
                }
            }
        }
    }
    
    public void printCells()
    {
        Card aCard;
        for (int row=0; row<board.length; row++)
        {
            for (int col=0; col<board[0].length; col++)
            {
                aCard = board [row][col];
                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[a];
            words[a] = words[pos];
            words [pos] = temp; 
        }
    }
}
JollyGreenGiant JollyGreenGiant

2020/4/30

#
This is what i have so far.
JollyGreenGiant JollyGreenGiant

2020/4/30

#
Could you show me which lines i need to change, and also what other codes i need to add thanks.
JollyGreenGiant JollyGreenGiant

2020/4/30

#
I think I'm gonna need quite a bit of help.
JollyGreenGiant JollyGreenGiant

2020/4/30

#
For the world class method i kept the card array and the shuffle method. I also changed the board to 5x4,. The setcells method I'm unsure on what i need to change inside the double loop. You also mentioned that i need a super constructor call at the beginning of your world class constructor and an act method to control the game. At the moment i want to focus on the world class then once that is fixed I'll move on to the card class.
There are more replies on the next page.
1
2
3
4
5
6