Memory Game Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • irm2216
    New Member
    • Apr 2008
    • 1

    Memory Game Program

    I'm trying to make a memory game where you try to link up the two corresponding colored circles, but if they are the wrong colors, they go back to being a blank canvas. The graphics methods do not link up, and I'm not sure why. Anyone mind taking a look at my code and seeing what the problem is?

    [CODE import javax.swing.*;
    import java.awt.*;
    import java.lang.Math;

    public class Memory extends java.applet.App let
    {
    private int xMouse;
    private int yMouse;
    private boolean mouseClicked = false;
    private boolean firstRun = true;
    private static final int WIDTH = 100;
    //add the rest of your instance variables here
    int[][] board = new int[4][4];


    /*Sets the background of your memory board to black*/
    public void init()
    {
    setBackground(C olor.BLACK);
    }
    /*This is main in java applets
    You may need to add (not change) a couple things in this method
    */
    public void paint(Graphics canvas)
    {
    if(firstRun) //for the first run we need to build our random board
    {
    buildBoard(4);
    firstRun = false;
    }
    else // once our board is built we will display the game
    {
    displayGame(can vas);
    if (mouseClicked) // if the mouse has been clicked
    {
    displayHit(canv as, x, y);//find which box the user clicked
    mouseClicked = false;
    }
    }
    }

    /*
    DO NOT change this method
    determins if the mouse has been pressed
    sets x and y Mouse to the location of the mouse arrow
    redraws the image
    */
    public boolean mouseDown(Event e, int x, int y )
    {
    mouseClicked = true;
    xMouse = x;
    yMouse = y;
    repaint();
    return true;
    }

    /*DO NOT change this method
    redraws the scene
    */
    public void update ( Graphics g )
    {
    paint(g);
    }

    /*
    pre: none
    post: build an array that holds the memory vales for a board of size x size
    the board will hold two of each int from 0 to size randomly placed in the array
    */
    public void buildBoard(int s)
    {
    int[] board1D = new int[16];
    for(int i=0; i<16; i++)
    {
    board1D[i] = i % 8;
    shuffle(board1D );

    }
    int index=0;
    for(int i=0; i < board.length; i++)
    {
    for(int j=0; j < board[i].length; j++)
    {

    board[i][j] = board1D[index];
    index = index + 1;
    }
    }
    }

    public void shuffle(int[] index)
    {
    int j;
    int k = index.length;
    for (int i=0; i < 15; i++)
    {
    j = ((int)(((double )(k - index.length))* Math.random())) + i;
    int temp = index[i];
    index[i] = index[j];
    index[j] = temp;
    }
    printArray(inde x);
    }

    public static void printArray(int[] board)
    {
    System.out.prin t("Random Order.");
    for(int i=0; i < board.length; i++)
    System.out.prin t(" " + board[i]);
    System.out.prin tln();
    }


    public static void displayGame(Gra phics canvas)
    {
    canvas.setColor (Color.WHITE);

    for(int i =0; i < 400; i+= WIDTH)
    for(int j = 0; j < 400; j+= WIDTH)
    canvas.drawRect (i, j, WIDTH, WIDTH);

    canvas.drawNumb ers(canvas, WIDTH);
    }


    /*
    Pre: xMouse and yMouse have been initialized
    Post: A circle is displayed in the correct box on the screen
    Currenlty the circle is displayed at the mouse location
    */
    public void displayHit(Grap hics g, int x, int y)
    {
    canvas.ballColo r(g, board[x][y]);
    g.setColor(Colo r.WHITE);
    g.fillOval(xMou se, yMouse, 40, 40);
    }

    public void drawNumbers(Gra phics canvas, int w)
    {
    int w2 = w / 3; // where to display number
    for(int i = 0; i < board.length; i++)
    for(int j = 0; j < board[i].length; j++)
    {
    canvas.setNumbe rColor(canvas, i, j);
    canvas.drawStri ng(Integer.toSt ring(board[i][j]), w*i + w2, w*j + (w/2));
    }
    }

    public void setNumberColor( Graphics canvas, int i, int j)
    {
    int r = 255;
    int g = 255;
    int b = 255;
    Color c = new Color(r-20*i, g-20*j, b-i*j);
    canvas.setColor (c);
    }


    public void setBallColor(Gr aphics g, int i)
    {
    switch(i)
    {
    case 0:
    g.setColor(Colo r.GREEN); break;
    case 1:
    g.setColor(Colo r.YELLOW); break;
    case 2:
    g.setColor(Colo r.LIGHT_GRAY); break;
    case 3:
    g.setColor(Colo r.MAGENTA); break;
    case 4:
    g.setColor(Colo r.BLUE); break;
    case 5:
    g.setColor(Colo r.RED); break;
    case 6:
    g.setColor(Colo r.ORANGE); break;
    case 7:
    g.setColor(Colo r.CYAN); break;
    default:
    g.setColor(Colo r.WHITE); break;
    }
    }
    }[/CODE]
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Better to use codetags when posting codes...

    Just reminding, :)
    sukatoa

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      The OP did, he just missed a ] at the top.

      As to the actual question, the only thing I see after a quick scan (Graphics isn't really my forte) is that anything in the java.lang package is always imported, so you don't need to import java.lang.Math.

      Comment

      Working...