【问题标题】:memory game random color记忆游戏随机颜色
【发布时间】:2023-03-18 16:12:01
【问题描述】:

大家好,我正在创建一个使用 4x4 2Darray 的记忆游戏。

我用一对从 0 到 7 的整数填充了 4x4,它们是随机分散的。我想为每一对分配一种颜色,因此当鼠标单击该方块时,分配的颜色将基于整数出现,您必须根据其对应的匹配颜色找到另一个整数。 我的这个 setColor 方法遇到了一些问题。我将包含我的整个代码,以防我在其他地方搞砸了,这就是原因。目前,如果我使用了两次分配的所有 8 种颜色一次单击每个方块,但某些颜色与另一个图块上相同整数的位置不匹配。此外,当我多次单击同一个图块时,它会在 2-3 种颜色之间变化,我不确定自己做错了什么。

我需要建议的部分是我分配的 setColor 方法及其背后的逻辑。

/*Sets the background of your memory board to black*/
public void init() 
{
    setSize(400,400);
    setBackground(Color.BLACK);
    buildBoard(4);

}   
/*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
    {

        print2DArray(board);
        buildBoard(4);
        firstRun = false;
    } 
    else // once our board is built we will display the game
    {
        displayGame(canvas);
        if (mouseClicked) // if the mouse has been clicked
        {
            displayHit(canvas);//find which box the user clicked
            mouseClicked = false;
        }
    }
}

/*
    DO NOT change this method
    determines 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 values 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 static void fillRect(Graphics g, int x, int y, int width, int length)
{
    g.setColor(Color.BLACK);
    width = 100;
    length = 100;
    x = (int)xMouse/100;
    y = (int)yMouse/100;        
}

public void buildBoard(int s)

{
    int a = 4;
    for (int row = 0; row < a; row++)
        for (int column = 0; column < a; column++)
        {
            board[row][column] = count++ % 8;
        }
    for(int row = 0; row < 4; row++)

        for(int column = 0; column < 4; column ++) 
        {
            int x = (int)Math.floor(Math.random()*4);
            int y = (int)Math.floor(Math.random()*4);
            temp = board[row][column];
            board[row][column] = board[x][y];
            board[x][y] = temp;


        }
}
public static void print2DArray(int[][] arr)
{
    for (int row = 0; row < arr.length; row++)
    {
        for (int col = 0; col < arr[row].length; col++)
        {
            System.out.print(arr[row][col] + " ");
        }
        System.out.println();
    }
}





public void displayGame(Graphics 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);
}

/*
    Pre: xMouse and yMouse have been initialized
    Post: A circle is displayed in the correct box on the screen
    Currently the circle is displayed at the mouse location
 */
public void displayHit(Graphics g)
{

    /*int xGuess = (int)xMouse/100;
    int yGuess = (int)yMouse/100;
    board[xGuess][yGuess] = guess1;
    int xGuess2 = (int)xMouse/100;
    int yGuess2 = (int)yMouse/100;
    board[xGuess2][yGuess2] = guess2;

    if (guess1 == guess2)
    {
        setColor(g);
        centerHit(xMouse, yMouse);
        g.fillOval(xMouse, yMouse, 40, 40);
    }
    else 
    g.fillRect(guess1, guess2, width, length);
    g.setColor(Color.BLACK);*/
    setColor(g);
    centerHit(xMouse, yMouse);
    g.fillOval(xMouse, yMouse, 40, 40);
}

public void setColor(Graphics g)
{

    centerClick(x1,y1);     
    //int x = xMouse;
    //int y = yMouse;
    colorIndex = board[row][column];
    board[row][column] = board[x1][y1];
    board[x1][y1] = colorIndex;


    switch(colorIndex)
    {
    case 0: g.setColor(Color.RED);
    break;
    case 1: g.setColor(Color.GREEN);
    break;
    case 2: g.setColor(Color.BLUE);
    break;
    case 3: g.setColor(Color.ORANGE);
    break;
    case 4: g.setColor(Color.CYAN);
    break;
    case 5: g.setColor(Color.MAGENTA);
    break;
    case 6: g.setColor(Color.PINK);
    break;
    case 7: g.setColor(Color.YELLOW);
    break;
    }

}
public void centerHit(int centerX, int centerY)
{
    {
        if ((xMouse > 0) && (xMouse <=100))
            xMouse = 33;
        else if ((xMouse > 100) && (xMouse <=200))
            xMouse = 133;
        else if ((xMouse > 200) && (xMouse <=300))
            xMouse = 233;
        else if ((xMouse > 300) && (xMouse <=400))
            xMouse = 333;
    }
    {
        if ((yMouse > 0) && (yMouse <=100))
            yMouse = 33;
        else if ((yMouse > 100) && (yMouse <=200))
            yMouse = 133;
        else if ((yMouse > 200) && (yMouse <=300))
            yMouse = 233;
        else if ((yMouse > 300) && (yMouse <=400))
            yMouse = 333;
    }
}
public void centerClick(int centerX, int centerY)
{
    {
        if ((xMouse > 0) && (xMouse <=100))
            x1 = 0;
        else if ((xMouse > 100) && (xMouse <=200))
            x1 = 1;
        else if ((xMouse > 200) && (xMouse <=300))
            x1 = 2;
        else if ((xMouse > 300) && (xMouse <=400))
            x1 = 3;
    }
    {
        if ((yMouse > 0) && (yMouse <=100))
            y1 = 0;
        else if ((yMouse > 100) && (yMouse <=200))
            y1 = 1;
        else if ((yMouse > 200) && (yMouse <=300))
            y1 = 2;
        else if ((yMouse > 300) && (yMouse <=400))
            y1 = 3;
    }
}

}

【问题讨论】:

  • 我不确定你想用三行代码做什么,这就是我的想法colorIndex = board[row][column];你正在阅读一些单元格的颜色,不确定哪个单元格因为我看不到column或@987654324 @值改变。 board[row][column] = board[x1][y1]; 在我看来就像你在改变原来的板,我不认为这是你想要的,board[x1][y1] = colorIndex; 将最后点击的单元格颜色分配给点击的单元格......嗯,请解释这三行清楚
  • @jambriz 板最初读取 0-7 并在最后两行重复 0-7。我混合了行和列以随机化周围的数字。这三行代码是我尝试使用鼠标点击器在 4x4 2D 阵列上选择一个块。然后使用 xindex 和 yindex 并在 board[row][column] 上找到该点对应的数组值。然后我将在刚刚通过 switch 语句单击的索引处设置该值,以根据它的整数值分配它的任何颜色

标签: java arrays memory colors


【解决方案1】:

要防止颜色改变,请更改此代码

    colorIndex = board[row][column];
    board[row][column] = board[x1][y1];
    board[x1][y1] = colorIndex;

    colorIndex = board[x1][y1];

颜色匹配问题是因为您要构建电路板两次:首先在您打印数组的 paint 方法的 firstRun 上,然后在电路板被覆盖的 init 方法上,因此您会看到不同的值

要解决这个问题,你应该在init 方法中只构建一次电路板,然后调用print2DArray 进行检查,所以

 buildBoard(4);
 print2DArray(board); //<-- add this line to verify colors

您可以省略firstRun 标志和所有相关代码。

评论或删除此代码:

/*if(firstRun) //for the first run we need to build our random board
    {
        print2DArray(board);
        buildBoard(4);


        firstRun = false;
    } 
    else // once our board is built we will display the game
    {*/ 

【讨论】:

  • 好的,这有助于解决我的一个问题。它可以防止每次点击多次更改颜色,但我的整数和分配的颜色仍然不匹配,我不知道为什么
  • 你能不能解释一下 board[x1][y1] = colorIndex vs colorIndex = board[x1][y1] = colorIndex;
  • 你如何检查它们是否匹配?他们似乎与我相匹配,无论如何尝试将colorIndex = board[x1][y1]; 更改为colorIndex = board[y1][x1]; 看看这是否是你想要的
  • 成对的颜色应该根据它们在 2Darray 上的位置来匹配,但它们不是并且知道为什么
  • = 运算符是具有从右到左关联性的赋值运算符,因此当您执行x=y=x 时,您首先将x 值分配给y,然后将yvalue 分配给@ 987654337@,所以最后x 保持不变,y 以与x 相同的值结束
猜你喜欢
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多