所以基本上我試圖使Set Card遊戲的想法是從4x3棋盤中選擇3張牌,以確定它們是否匹配或全部相反並顯示消息。我製作了這些卡片,並且它們都具有陰影,形狀,顏色和數量的價值。我遇到的問題是當我點擊卡時,我可以將其刪除。我想要做的是點擊一張2紅色虛線波形的卡片,並將其返回2,1,2,3並將其添加到每個卡片上。然後在選擇三張牌之後確定它們是否匹配。我甚至不知道如何去做這件事。我真正嘗試過的唯一一件事就是將我點擊的卡片保存到一個新的對象上。從使用鼠標監聽器選擇的對象中獲取信息
package assignment3;
import acm.program.GraphicsProgram;
import acm.graphics.*;
import java.awt.event.MouseEvent;
import java.util.Random;
public class SetGame extends GraphicsProgram
{
private int APP_WIDTH = 470;
private int APP_HEIGHT = 300;
GObject Card[][];
public void run()
{
setSize(APP_WIDTH,APP_HEIGHT);
for(int row = 0;row < 3; row++)
{
for(int col = 0; col < 4; col++)
{
Card[][] setCard = new Card[4][3];
setCard[col][row] = new Card(col*60, row*60, getRandomNum()+1,getRandomNum()+1,getRandomNum()+1,getRandomNum()+1);
add(setCard[col][row]);
}
println();
}
addMouseListeners();
}
public int getPigment(int num)
{
int pigment = num;
String[] color = new String[3];
color[0] = "red";
color[1] = "green";
color[2] = "purple";
return pigment;
}
//gets a random shape
public int getShape(int num)
{
int shape = num;
String[] Shape = new String[3];
Shape[0] = "circle";
Shape[1] = "diamond";
Shape[2] = "squiggle";
return shape;
}
public int getShade(int num)
{
int shade = num;
String[] Shade = new String[3];
Shade[0] = "solid";
Shade[1] = "dashed";
Shade[2] = "hollow";
return shade;
}
public int getRandomNum()
{
Random number = new Random();
int num = number.nextInt(3);
return num;
}
public void mouseClicked(MouseEvent e)
{
GObject whichCard = getElementAt(e.getX(), e.getY());
if(whichCard == null)return;
remove(whichCard);
}
}
這裏是對其他類
package assignment3;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
import acm.program.GraphicsProgram;
import acm.graphics.*;
//Written by Dan Mattwig
//This program is built to display properties for cards
public class Card extends GCompound
{
private int color;
private int shape;
private int shading;
private int number;
public Card(int X,int Y,int color,int shape, int shading, int number)
{
setLocation(X,Y);
this.color = color;
this.shape = shape;
this.shading = shading;
this.number = number;
int image = (color * 3) + (shape * 9) + (shading * 27) + (number - 39);
GImage card = new GImage("images/"+image+".gif");
add(card,X,Y);
}
}
我們只能猜測你的「收集卡片的價值」的意思;我知道你很清楚你想要什麼,但對於那些想要幫助你的人卻不清楚,但卻沒有任何工作環境。試着想象我們對你的程序或你想要做的事情一無所知,並以這種方式解釋它。告訴我們你試過的是什麼;我們更好地解釋人們嘗試編寫的東西,而不是拿出代碼去做某件事。 – arcy