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

2020/4/30

#
public class MemoryGame extends World 
{
    private Card [][] board;
    super(600, 400, 1);
        
    MemoryGame()
    {    
       board = new Card [5][4];
       shuffle();
       
    }
 
danpost danpost

2020/4/30

#
Insert before line 8, the following line:
super(5, 4, 100);
JollyGreenGiant JollyGreenGiant

2020/5/1

#
I did that so, what is the next thing I need to do?
danpost danpost

2020/5/1

#
JollyGreenGiant wrote...
I did that so, what is the next thing I need to do?
Does it compile at this point? If not, what is the terminal output?
JollyGreenGiant JollyGreenGiant

2020/5/1

#
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()
    {    
       super(5, 4, 100);
       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/5/1

#
Still having issues with the compiling.
JollyGreenGiant JollyGreenGiant

2020/5/1

#
choosePairOfCards();
                    board[row][col] = new Card(words[a], a);
 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/5/1

#
Having errors with these.
danpost danpost

2020/5/1

#
JollyGreenGiant wrote...
choosePairOfCards();
Remove that line. In fact, remove the method it was in as well. You will need an act method to control the game.
board[row][col] = new Card(words[a], a);
 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; 
}
These two are due to you removing the filename array (which is not needed. For now, comment out the problem line in the setCards method and remove the shuffle method. There is a shuffle method in java you can use; however it will not work on a 2-d array. I guess it may look bad, but I want to strip your world to bare minimum and start over:
import greenfoot.*;

public class MemoryGame extends World
{
    Card[] cards = new Card[20];
    
    public MemoryGame()
    {
        super(5, 4, 100);
        // load array
        // shuffle
        // deal
    }
}
This would be the main starting point. Loading the array takes a single loop with a single command inside the loop. So, that is not worth an extra method (also, because loading the array is only done at that one point):
for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);
Shuffling, likewise, is only done here and requires only one line of code:
java.util.Collections.shuffle(java.util.Arrays.asList(cards));
Dealing the card ... again, similar:
for (int i=0; i<cards.length; i++) addObject(cards[i], i/5, i%5);
Hopefully, it will compile and you will see your cards at this point.
JollyGreenGiant JollyGreenGiant

2020/5/1

#
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo)
     
    /**
     * Write a description of class MemoryGame here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class MemoryGame extends World
    {
    Card[] cards = new Card[20];
    java.util.Collections.shuffle(java.util.Arrays.asList(cards));     
        public MemoryGame()
        {
            super(5, 4, 100);
            // load array
            // shuffle
            // deal
    }
    
 
      
    public void setCells()
    {
        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 void printCells()
    {
        Card aCard;
        for (int i=0; i<cards.length; i++) addObject(cards[i], i/5, i%5);
        {
            for (int col=0; col<board[0].length; col++)
            {
                aCard = board [row][col];
                aCard.showCard();
            }
            System.out.println();
        }
    }
    
    public void shuffle()
    {
        for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+i, i);
        {
            int pos = r.nextInt(words.length);
            String temp = words[a];
            words[a] = words[pos];
            words [pos] = temp; 
        }
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/1

#
Like this?
danpost danpost

2020/5/1

#
JollyGreenGiant wrote...
Like this?
I did not say anything about adding in those other methods. The single lines of code given were each to replace a commented line in the base class.
JollyGreenGiant JollyGreenGiant

2020/5/1

#
Sorry I got confused.
JollyGreenGiant JollyGreenGiant

2020/5/1

#
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo)
     
    /**
     * Write a description of class MemoryGame here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class MemoryGame extends World
    {
    Card[] cards = new Card[20];     
    public MemoryGame()
    {
            super(5, 4, 100);
            // load array
            // shuffle
            // deal
            for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);
            java.util.Collections.shuffle(java.util.Arrays.asList(cards));
            for (int i=0; i<cards.length; i++) addObject(cards[i], i/5, i%5);
    }
    
}
danpost danpost

2020/5/1

#
Does it compile? If not, copy/paste terminal output and show related classes.
There are more replies on the next page.
1
2
3
4
5
6
7