Memory card game without classes

Hello everyone,

I’m making a memory card game in Processing. This is for a homework assignment. I’ve come pretty far with all the information online, but I’m stuck at a few problems.

My game has a few requirements to meet. I have to have a scoreboard, 12 sets of memory cards (so 24 cards) plus a “death card” which means if you click that one, you will lose a point. It needs to be in a 5x5 grid. It’s singleplayer. We cannot use classes.

In my code I have laid out all my functions, so it would be clearer to me which bit I can program first. Now, I have created a shuffleCards function. This does what it’s supposed to do. But now I have to make sure the cards only show up twice each. That’s where I am stuck. I have no idea how to make that. I thought about using modulo? I’m not sure.

Along with that, if that is fixed, I have to check if the cards actually match, and if they do, the card will dissapear off screen, and a point will be added. I have it mapped out on paper, but I need help realizing this as code.

Can someone help me with this? I would be forever grateful.

String cardImages[] = {"img1.png", "img2.png", "img3.png",  "img4.png", "img5.png", "img6.png", "img7.png", "img8.png", "img9.png", "img10.png", "img11.png", "img12.png"};
int[][] cards = new int[25][3];
boolean gameStarted = false; 
boolean cardFlipped = false;
boolean cardClicked = false; 
boolean gameOver = false;
int screenWidth = 1000;
int screenHeight = 1000;
int cardWidth = 175;
int cardHeight = 175;
int marge = 15;
int cardsOnXPos = 5;
int cardsOnYPos = 5;
int show = 0;
PImage cardImage;
void setup(){
  size(1500,1500);
  background(0,100,0);
  setupBoard();
  shuffleCards();
  drawImageOnScreen();
}
void draw(){
  //drawCardsOnScreen();
}
void mousePressed(){
  mouseClickedOnCard();
}
void startGame(){
  background(0,100,0);
}

void flipCards(){
}
void shuffleCards(){
  for (int i = 0; i < cardImages.length; i++){
    int index = (int)(random(cardImages.length));
    
    String temp = cardImages[i];
    cardImages[i] = cardImages[index];
    cardImages[index] = temp;
   
  }
}
void mouseClickedOnCard(){
  for (int i = 0; i < cards.length; i++){
    if((mouseX >= cards[i][0] && mouseX <= cards[i][0] + cardWidth) && (mouseY >= cards[i][1] && mouseY <= cards[i][1] + cardHeight)){
      println("Clicked on " + i);
    }
  }
}

void cardMatched(){
}
void cardOffScreen(){
}

void drawCard(int cardX, int cardY){
  fill(0);
  rect(cardX, cardY, cardWidth, cardHeight, 0);
}
void drawImage(int cardX, int cardY){
  
  cardImage = loadImage(cardImages[(int)random(12)]);
  image(cardImage, cardX, cardY);
}
void drawImageOnScreen(){
  for( int i = 0; i < cards.length; i++){ 
    drawImage(cards[i][0], cards[i][1]);
  }
}
void drawCardsOnScreen(){
  for( int i = 0; i < cards.length; i++){ 
    drawCard(cards[i][0], cards[i][1]);
  }
}
void setupBoard(){
  for( int i = 0; i < cards.length; i++){
    int horizontalCardIndex = i % cardsOnXPos;
    int verticalCardIndex = i / cardsOnYPos;
    
    int cardX = marge + (cardWidth + marge) * horizontalCardIndex;
    int cardY = marge + (cardHeight + marge) * verticalCardIndex;
    
    cards[i][0]= cardX;
    cards[i][1]= cardY;
    cards[i][2]= 0;
  }
}
void startScreen(){
}

void gameOver(){
  fill(255);
  textSize(50);
  text("YOU WIN", 200, 800);
}
void scoreBoard(){
}
1 Like

Hello im doing the same Project.

All i can see is that while you are loading the images is that you do it with random. So it can have the same value multiple times.

void drawImage(int cardX, int cardY){
  
  cardImage = loadImage(cardImages[(int)random(12)]);
  image(cardImage, cardX, cardY);
}

So what i would do is use a for loop for the method drawImage.

The first thing that comes to mind is looping over the array and check for 3 or more cards of the same type, changing the cards to another random number, but this could be potentially very slow. However, you could use a random number generator that never repeats, and do this for the 12 possible card types in game, then do this for the other 12, so that each card repeats twice. One will be left, which can just be sorted out with processing’s rng. You would make an array with the 12 cards, meaning that there are only 12 values to be sorted. You could also make an arraylist containing the remaining numbers to pick, and remove the number chosen from this arraylist every time a number is picked. Pick a random number with processing’s rng from 0 to the amount of numbers remaining, and choose the number that it corresponds to in the “remaining numbers” arraylist. Then, remove that number from the arraylist. You would repeat this process until there are 0 numbers left. Here is a function that does this:

public int[] nonRepeatingRNG(int min, int max) {
 int[] list = new int[max];
 ArrayList<Integer> remaining = new ArrayList<Integer>();
 for(int i = 0; i < max; i++) {
   remaining.add(i, i);
 }
 for(int i = 0; i < list.length; i++) {
  int rand = round(random(min, remaining.size() - 1)); 
  int chosen = remaining.get(rand);
  remaining.remove(rand);
  list[i] = chosen;
 }
 return list;
}