JollyGreenGiant wrote...
<< Code Omitted >>
Im having errors with these two lines of code.
for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);public Card(String fname, int val)
{
value= val;
front = new GreenfootImage(fname+".gif");
setImage(back);
}public void act()
{
mouseClicking();
}
private void mouseClicking()
{
if (Greenfoot.mouseClicked(null)) // any click
{
Actor clickedOn = Greenfoot.getMouseInfo().getActor();
if (clickedOn == null || ! (clickedOn instanceof Card)) return;
Card c = (Card)clickedOn;if (c.isShowing()) return;
public boolean isShowing()
{
return showing();
}
if (firstPicked == null) // first pick
{
firstPicked = c;
c.flipCard();
}
else // second pick
{
// future post
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo)
/**
* Write a description of class MemoryGame here.
*
* Andy McCallum
* 01/05/2020
*/
public class MemoryGame extends World
{
Card[] cards = new Card[20];
private static Card firstPicked;
public static int matchTries;
public static int matchCount;
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);
}
public void act()
{
mouseClicking();
}
private void mouseClicking()
{
if(Greenfoot.mouseClicked(null)) // any click
{
Actor clickedOn = Greenfoot.getMouseInfo().getActor();
if (clickedOn == null || ! (clickedOn instanceof Card)) return;
Card c = (Card)clickedOn;
if (c.isShowing()) return;
if (firstPicked == null) // first pick
{
firstPicked = c;
c.flipCard();
}
else // second pick
{
// future post
}
}
}
}
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 GreenfootImage front;
private boolean isShowing;
private int value;
public void flipCard()
{
isShowing = ! isShowing;
setImage(isShowing ? front : back);
}
public int getValue()
{
return value;
}
public Card(String fname, int val)
{
value= val;
front = new GreenfootImage(fname+".gif");
setImage(back);
}
public boolean isShowing()
{
return isShowing();
}
}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 GreenfootImage front;
private boolean isShowing;
private int value;
private GreenfootImage card1 = new GreenfootImage ("g0.gif");
private GreenfootImage card2 = new GreenfootImage ("g1.gif");
private GreenfootImage card3 = new GreenfootImage ("g2.gif");
private GreenfootImage card4 = new GreenfootImage ("g3.gif");
private GreenfootImage card5 = new GreenfootImage ("g4.gif");
private GreenfootImage card6 = new GreenfootImage ("g5.gif");
private GreenfootImage card7 = new GreenfootImage ("g6.gif");
private GreenfootImage card8 = new GreenfootImage ("g7.gif");
private GreenfootImage card9 = new GreenfootImage ("g8.gif");
private GreenfootImage card10 = new GreenfootImage ("g9.gif");
public void flipCard()
{
isShowing = ! isShowing;
setImage(isShowing ? front : back);
}
public int getValue()
{
return value;
}
public Card(String fname, int val)
{
value= val;
front = new GreenfootImage(fname+".gif");
setImage(back);
}
public boolean isShowing()
{
return isShowing();
}
}