[Computer Science]

Your browser can't run 1.0 Java applets.

This Java applet runs a simple game of memory. The game starts with 16 cards, face down. Click on the button above to start, then choose cards (by clicking on them with the mouse) to turn them over. You can only turn two at a time (it turns them over automatically when you click on a third). If you turn two that are the same, they are removed from the board. You continue flipping cards until you have removed all cards from the board.

Below is the code from the Java program that makes this game work.

import java.awt.*;

public class Flip extends Frame {
  static int nCARDS = 16;
  Button startButton; 
  String szButton_msg = "Click here to start.";
  boolean bInAnApplet = true;
  Card[] aCard = new Card[nCARDS];
  int nHitCount = 0;
  Label label;
  Card[] aChosen = new Card[2];

  public Flip () {
    Panel centrePanel = new Panel();
    Panel controlPanel = new Panel();
    int i;

    centrePanel.setLayout(new GridLayout(4,4));
    for (i = 0; i < nCARDS; i++) {
      centrePanel.add(aCard[i] = new Card());
    }
    add("Center", centrePanel);

    controlPanel.setLayout(new GridLayout(2,1));
    controlPanel.add(label = new Label("Count: " + this.nHitCount + " of " + nCARDS/2, Label.CENTER));
    controlPanel.add(startButton = new Button(szButton_msg));
    add("South", controlPanel);

    validate();
  }

  public boolean handleEvent(Event event){
    int i = 0;
    int nChosen = 0;
    boolean bFilled = true;

    if (event.id == Event.WINDOW_DESTROY) {
      if (bInAnApplet) {
        dispose();
      } 
      else {
        System.exit(0);
      }
    }
    if (event.target == startButton && event.id == Event.MOUSE_DOWN) {
      this.szButton_msg = "Restart";
      resetCards();
      dealCards();
      nHitCount = 0;
      label.setText("Count: " + this.nHitCount + " of " + nCARDS/2);
      startButton.setLabel(szButton_msg);
      repaint();
    } 
    if (event.target instanceof Card && event.id == Event.MOUSE_DOWN) {
      /* figure out which card was hit */
      for(i = 0;i < nCARDS && event.target != aCard[i]; i++) {}

      /* record it */
      if (aCard[i].bActive) {
        bFilled = true;
        for(nChosen = 0; nChosen < 2; nChosen++) {
          if (aChosen[nChosen] == null) {
            aChosen[nChosen] = aCard[i];
            bFilled = false;
          }
        }

        /* and check the match before resetting the cards */
        if (bFilled) {
          if (aChosen[0] != null && aChosen[1] != null) {
            if (aChosen[0].nCardType == aChosen[1].nCardType) {
              this.nHitCount++;
              aChosen[0].remove();
              aChosen[1].remove();
            }
          }  

          resetCards();
          aChosen[1] = null;
          aChosen[0] = aCard[i];
        }
        aCard[i].flip();
      }
      label.setText("Count: " + this.nHitCount + " of " + nCARDS/2);
      if (this.nHitCount == nCARDS/2) {
        label.setText("Congratulations!");
        for (i = 0; i < nCARDS; i++) {
          aCard[i].done();
        }
      }
    }
    return super.handleEvent(event);
  }
  public static void main(String args[]) {
    Flip window = new Flip();
    window.setTitle("Flip!");
    window.setResizable(false);
    window.pack();
    window.show();
  }
  public void dealCards() {
    int i = 0;

    switch((int)(Math.random() * 4)) {
     case 0:
       aCard[i].nCardType = 1;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 4;
       aCard[++i].nCardType = 4;
       break;
    case 1:
       aCard[i].nCardType = 2;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 4;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 4;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 1;
       break;
    case 2:
       aCard[i].nCardType = 4;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 4;
       break;
    case 3:
       aCard[i].nCardType = 5;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 4;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 4;
       break;
    default:
       aCard[i].nCardType = 1;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 2;
       aCard[++i].nCardType = 7;
       aCard[++i].nCardType = 4;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 1;
       aCard[++i].nCardType = 5;
       aCard[++i].nCardType = 3;
       aCard[++i].nCardType = 6;
       aCard[++i].nCardType = 8;
       aCard[++i].nCardType = 4;
       break;
    }
  } 
  public void resetCards() {
    int i = 0;
    
    aChosen[0] = null;
    aChosen[1] = null;
    for (i = 0; i < nCARDS; i++) {
      if (!aCard[i].bActive) {
        aCard[i].flip();
      }
    }
  }
}

/*
 * This class makes individual cards && gives them some possible actions *
                                                                         */
class Card extends Canvas {
  boolean bActive = true;
  int nCardType = 0;

  public void paint(Graphics g) {   
    if (this.bActive) {
      g.setColor(Color.blue);
      g.fillRect(0, 0, 40, 40);
    }
    else { 
      g.clearRect(0, 0, 40, 40);
      switch (this.nCardType) {
        case 0: g.setColor(getBackground()); break;
        case 1: g.setColor(Color.red); break;
        case 2: g.setColor(Color.orange); break;
        case 3: g.setColor(Color.yellow); break;
        case 4: g.setColor(Color.green); break;
        case 5: g.setColor(Color.blue); break;
        case 6: g.setColor(Color.magenta); break;
        case 7: g.setColor(Color.pink); break;
        case 8: g.setColor(Color.darkGray); break;
        default: 
          g.setColor(Color.yellow); 
          break;
      }
      g.fillOval(0, 0, 40, 40);
      if (nCardType == -1) {        /* Celebrate! */
         g.setColor(Color.black);
         g.drawOval(0, 0, 40, 40);
         g.setColor(Color.white);
         g.fillRect(13, 13, 5, 5);
         g.fillRect(23, 13, 5, 5);
         g.setColor(Color.blue);
         g.fillRect(15, 15, 2, 2);
         g.fillRect(25, 15, 2, 2);
         g.fillRect(20, 20, 2, 4);
         g.setColor(Color.red);
         g.fillArc(10, 17, 20, 20, 180, 180); 
      }
    }
  }
  public Dimension minimumSize() {
    return new Dimension(42,42);
  }
  public Dimension preferredSize() {
     return minimumSize();
  }
/* This would make the cards flip when clicked, but it's commented out   */
/* It'd be nice to have this here, but here it would activate without    */
/* checking all the things we need to check for the purposes of the game */
/*  public boolean handleEvent(Event event){
    if (event.id == Event.MOUSE_DOWN) {
      flip();
    }
    return super.handleEvent(event);
  } */ 

  public void flip() {
    if (this.nCardType > 0) {
      if (this.bActive == true) {
        this.bActive = false;
      }
      else {
        this.bActive = true;
      }
      repaint();
    }
  }

  public void remove() {
    this.bActive = false;
    this.nCardType = 0;
    repaint();
  }

  public void done() {
    this.nCardType = -1; /* -1 is our magic number where special stuff happens */
    repaint();
  }
}

[Back]


[Home][Canadian Scientists][Credits][Français]

Physics | Chemistry | Biology | Engineering | Computer Science | Understanding Science and Technology

Produced by Galactics.
Comments: galactics@spacesim.org.
Last updated on 14 August 1998.