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;
GreenfootImage back;
int front;
private static firstClicked;
protected GreenfootImage[] images;
protected int imagesIndex;
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","["+front+"]"));
}
public void SetShowingStatus()
{
if(showing)
showing = false;
else
showing = true;
}
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;
}
}
}
}
}
