我有一个网格是可点击的,但我不知道如何进行一套特定的规则。
编辑:我以一种更容易理解的方式重写了规则。非常类似于生命的游戏。
设置
跨/列21个单元格
10个单元格向下/行
4个基底细胞垂直排列在板的中心。
轮廓单元将包围基单元格。
其他细胞开始时都是不活动的。
碱基细胞2
中间有固定的和活跃的蓝色细胞,不能移除。
Active -> 1
单击时,不活动的白细胞变为黑色。
if
边缘接触到基本单元格的边缘。
或
边缘接触到另一个活动单元格的边缘。
(左、右、上或下-不是对角线)。
保持不活跃
非活动1 ->
单击时,活动的黑色单元格返回为白色。
大纲3
一系列的黄色细胞,这些细胞会不断更新以包围活跃细胞的邻域。
如果有人能帮我实现这一点,我将感谢评论,以帮助我了解这一过程。
以下是我的当前代码:
int boxsize = 100;
int cols, rows;
color[][] colors;
int saved_i = -1;
int saved_j = -1;
void setup() {
size(1300, 600);
cols = width/boxsize;
rows = height/boxsize;
colors = new color[cols][rows];
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
colors[i][j] = color(255);
}
}
}
void draw() {
background(255);
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
fill(colors[i][j]);
rect(i*boxsize, j*boxsize, boxsize, boxsize);
}
}
}
void mousePressed() {
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
int x = i*boxsize;
int y = j*boxsize;
if (mouseX > x && mouseX < (x + boxsize) && mouseY > y && mouseY < (y + boxsize)) {
if ( saved_i == -1 || saved_i == i || saved_j == j ) {
colors[i][j] = color(0);
if (j>0) colors[i][j-1]=color(255, 255, 0);
if (j>0) colors[i+1][j-1]=color(255, 255, 0);
if (j<rows-1) colors[i][j+1]=color(255, 255, 0);
if (j<rows-1) colors[i+1][j+1]=color(255, 255, 0);
if (i>0) colors[i-1][j]=color(255, 255, 0);
if (i>0) colors[i-1][j-1]=color(255, 255, 0);
if (i>0) colors[i-1][j+1]=color(255, 255, 0);
if (i<cols-1) colors[i+1][j]=color(255, 255, 0);
saved_i = i;
saved_j = j;
}
}
}
}
}发布于 2015-08-25 11:57:19
你的问题很广泛,所以我会大致回答。你需要弄清楚四件事:
colors,但您可能不想这样做。在我看来,你有三个逻辑选项:- Use a 2D array of enum values. The enum would have states for BASE, ACTIVE, INACTIVE, and OUTLINE. This is probably the correct way to go.
- Use a 2D array of ints. 0 for base, 1 for active, 2 for inactive, 3 for outline. Using an enum is probably better, but this is probably easier for a novice to understand.
- Use a 2D array of objects. Create a class that represents a cell, and the object would store its state (in either an enum or an int). You would use this approach if you wanted other logic inside each cell, or maybe if you wanted each cell to keep track of its own neighbors.
mouseX和mouseY,并返回在该位置的数组中的位置。像这样分解你的问题,如果你被困在一个特定的步骤上,就发布一个新的问题。这是很难帮助一般的“我如何做这个”类型的问题。帮助回答更具体的问题要容易得多,比如“我尝试了X,预期的Y,但得到了Z,我做错了什么?”
祝好运!
https://stackoverflow.com/questions/32193002
复制相似问题