将2D数组绘制到网格上

时间:2022-06-15 21:37:21

I am making a board game called "Go" based of the actual board game. Basically, there are 2 players (White and Black) and they take turns placing white and black stones on the intersections of the board game grid.

我正在根据实际的棋盘游戏制作一款名为“Go”的棋盘游戏。基本上,有2个玩家(白色和黑色),他们轮流在棋盘游戏网格的交叉点上放置白色和黑色宝石。

So far I have been able to draw the stone, alternate between players and change the value in my 2D array, where 0 = open spot, 1 = white, and 2 = black.

到目前为止,我已经能够绘制石头,在玩家之间交替并改变我的2D阵列中的值,其中0 =开放点,1 =白色,2 =黑色。

I want to draw the corresponding stone from the 2D array onto my board. I am able to draw the correct stone and make it follow my mouse but once I click on the location I want it to be placed at the location and stay there.

我想从2D阵列中绘制相应的石头到我的板上。我能够绘制正确的石头并使其跟随我的鼠标,但是一旦我点击我希望它放置在该位置的位置并留在那里。

This is what I use to make the stone follow my mouse

这就是我用石头跟着我的老鼠

if (turn == 1) {
    g2d.drawImage(s.getBlackStone(),
                  s.getBlackX() - Frame.frameLocation.x - 15,
                  s.getBlackY() - Frame.frameLocation.y - 35, null);
} else {
    g2d.drawImage(s.getWhiteStone(),
                  s.getWhiteX() - Frame.frameLocation.x - 15,
                  s.getWhiteY() - Frame.frameLocation.y - 35, null);
}

What I was thinking is I have a method that is called every time I press my mouse button and the method will get the grid location where my mouse is clicked and whos turn it is and draw the image. What are your guys thoughts on this?

我在想的是每次按下鼠标按钮时都会调用一个方法,该方法将获得点击鼠标的网格位置,然后转动它并绘制图像。你们对此有何看法?

1 个解决方案

#1


2  

I normally make a Stone class, which has variables like the status (black, white or empty), the number of liberties, and the location within the array. The 2D array is just an array of instances of the Stone class. If the status variable is not empty, then I paint a black or white stone on the cell corresponding to the location in the array.

我通常会创建一个Stone类,其中包含状态(黑色,白色或空白),*数量以及数组中的位置等变量。 2D数组只是Stone类的一个实例数组。如果状态变量不为空,那么我在对应于阵列中位置的单元格上绘制黑色或白色石头。

If you just use an array containing a number representing a stone, then you will run into problems when you need to deal with captures.

如果您只使用包含代表石头的数字的数组,那么当您需要处理捕获时,您将遇到问题。

In answer to your question, find the location of their click on the grid, update all of the affected Stones in the array, then repaint the array.

在回答您的问题时,找到他们点击网格的位置,更新阵列中所有受影响的石头,然后重新绘制阵列。

#1


2  

I normally make a Stone class, which has variables like the status (black, white or empty), the number of liberties, and the location within the array. The 2D array is just an array of instances of the Stone class. If the status variable is not empty, then I paint a black or white stone on the cell corresponding to the location in the array.

我通常会创建一个Stone类,其中包含状态(黑色,白色或空白),*数量以及数组中的位置等变量。 2D数组只是Stone类的一个实例数组。如果状态变量不为空,那么我在对应于阵列中位置的单元格上绘制黑色或白色石头。

If you just use an array containing a number representing a stone, then you will run into problems when you need to deal with captures.

如果您只使用包含代表石头的数字的数组,那么当您需要处理捕获时,您将遇到问题。

In answer to your question, find the location of their click on the grid, update all of the affected Stones in the array, then repaint the array.

在回答您的问题时,找到他们点击网格的位置,更新阵列中所有受影响的石头,然后重新绘制阵列。