My friend sent me a code to help me work on my own memory game, but i have trouble understanding the code, and my friend are currently out of the country, so I really need someone to help and explain the code to me.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
import java.util.*;
/**
* Write a description of class GuessingGame here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GuessingGame extends World
{
// Total number of cells must be even
private static int numCellsX = 6;
private static int numCellsY = 6;
private static int screenX = numCellsX+5;
private static int screenY = numCellsY+2;
private static int cellSize = 60;
private Tile prevTileFlipped;
private int numTilesToUnflip;
/**
* Constructor for objects of class GuessingGame.
*
*/
public GuessingGame()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(screenX, screenY, cellSize);
GreenfootImage gfi = new GreenfootImage(screenX*cellSize, screenY*cellSize);
gfi.setColor(Color.white);
gfi.fill();
gfi.setColor(Color.black);
int end = numCellsY*cellSize-1;
for (int i=1; i <= numCellsX; i++)
gfi.drawLine(i*cellSize, 0, i*cellSize, end);
end = numCellsX*cellSize-1;
for (int i=1; i <= numCellsY; i++)
gfi.drawLine(0, i*cellSize, end, i*cellSize);
setBackground(gfi);
for (int i=0; i < numCellsX; i++)
for (int j=0; j < numCellsY; j++)
addObject(new Tile(), i, j);
reset();
prevTileFlipped = null;
numTilesToUnflip = numCellsX*numCellsY;
}
public void setPrevTileFlipped(Tile tile) {
prevTileFlipped = tile;
}
public Tile getPrevTileFlipped() {
return prevTileFlipped;
}
public void reset() {
java.util.List list = getObjects(null);
int id = 0;
for (int i=list.size(); i > 0; id++) {
for (int j=0; j < 2; j++) {
Tile tile = (Tile)(list.remove(Greenfoot.getRandomNumber(i--)));
tile.setID(id);
tile.setFlipped(false);
tile.showImage();
}
}
}
public void foundTilePair() {
numTilesToUnflip -= 2;
if (numTilesToUnflip == 0) {
addObject(new Bee(),0,5);
Greenfoot.playSound("applause.wav");
}
}
public int getScreenX() {
return screenX;
}
public int getScreenY() {
return screenY;
}
}
