原来的代码全部删除,进行了深层次重构,得其意而忘其言。得意之处有二:
1.关于显示
以 StoneSize 属性为依托,在 set 中加了一句:Width = Height = m_StoneSize * 19;以此来控制棋盘大小。所有的对象在 Init() 方法中创建,而具体的渲染在 Redraw() 方法中完成。这种创建与重绘相分开的办法,使调整大小时进行重绘更简单易行。这两个方法的代码如下:
void Init()
{
// 线
for (int i = ; i < ; i++) {
m_LinesH[i] = new Line();
m_LinesH[i].Stroke = Brushes.Black;
m_Canvas.Children.Add(m_LinesH[i]); m_LinesV[i] = new Line();
m_LinesV[i].Stroke = Brushes.Black;
m_Canvas.Children.Add(m_LinesV[i]);
} // 星
for (int j = ; j < ; j++) {
for (int i = ; i < ; i++) {
m_Stars[i, j] = new Ellipse();
m_Stars[i, j].Fill = Brushes.Black;
m_Canvas.Children.Add(m_Stars[i, j]);
}
} for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
m_Stones[i, j] = new Ellipse();
m_Stones[i, j].Visibility = Visibility.Hidden;
m_Canvas.Children.Add(m_Stones[i, j]); m_Numbers[i, j] = new TextBlock();
m_Numbers[i, j].Background = Brushes.Transparent;
m_Numbers[i, j].Visibility = Visibility.Hidden;
m_Canvas.Children.Add(m_Numbers[i, j]); m_Steps[i, j] = new Step();
m_Steps[i, j].Row = i;
m_Steps[i, j].Col = j;
m_EmptySteps.Add(m_Steps[i, j]);
m_AllSteps.Add(m_Steps[i, j]);
}
} // 当前标志
m_CurrentRect.Visibility = System.Windows.Visibility.Hidden;
m_CurrentRect.Fill = Brushes.Red;
m_Canvas.Children.Add(m_CurrentRect); for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
Rectangle rect = new Rectangle();
rect.Visibility = System.Windows.Visibility.Hidden;
m_EmptyRects[i, j] = rect;
m_Canvas.Children.Add(m_EmptyRects[i,j]); }
}
}
Init()
public void Redraw()
{
// 画线
for (int i = ; i < ; i++) {
Line l = m_LinesH[i];
int y = i * StoneSize + StoneSize / ;
l.X1 = StoneSize / ;
l.Y1 = y;
l.X2 = * StoneSize - StoneSize / ;
l.Y2 = y; l = m_LinesV[i];
int x = i * StoneSize + StoneSize / ;
l.X1 = x;
l.Y1 = StoneSize / ;
l.X2 = x;
l.Y2 = * StoneSize - StoneSize / ;
} // 画星
for (int j = ; j < ; j++) {
for (int i = ; i < ; i++) {
Ellipse e = m_Stars[i, j];
e.Width = e.Height = StoneSize / ;
double left = * StoneSize + j * * StoneSize - StoneSize / - e.Width / ;
double top = * StoneSize + i * * StoneSize - StoneSize / - e.Height / ;
Canvas.SetLeft(e, left);
Canvas.SetTop(e, top);
}
} // Stones and Numbers
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
var stone = m_Stones[i, j];
stone.Width = stone.Height = StoneSize;
Canvas.SetLeft(stone, j * StoneSize);
Canvas.SetTop(stone, i * StoneSize); ShowNumber(i, j, m_Steps[i, j].StepCount);
}
} // 点目标志
if (IsShowMesh)
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
var rect = m_EmptyRects[i, j];
rect.Width = rect.Height = m_CurrentRect.Width;
double offset = (StoneSize - rect.Width) / 2.0;
Canvas.SetLeft(rect, j * StoneSize + offset);
Canvas.SetTop(rect, i * StoneSize + offset);
}
}
} public bool NextOne(int row, int col)
{
if (m_Steps[row, col].StoneColor != StoneColor.Empty)
return false;
if (m_BanOnce.Row == row && m_BanOnce.Col == col) {
return false;
}
m_BanOnce.Row = m_BanOnce.Col = -; DrawStep(row, col);
bool isBlack;
if (m_Steps[row, col].StoneColor == StoneColor.Black) {
m_BlackSteps.Add(m_Steps[row, col]);
isBlack = true;
} else {
m_WhiteSteps.Add(m_Steps[row, col]);
isBlack = false;
}
m_EmptySteps.Remove(m_Steps[row, col]); UpdateBlackBlocks();
UpdateWhiteBlocks();
if (isBlack) {
if (!UpdateDeadBlocks(m_WhiteBlocks))
UpdateDeadBlocks(m_BlackBlocks);
} else {
if (!UpdateDeadBlocks(m_BlackBlocks))
UpdateDeadBlocks(m_WhiteBlocks);
} MoveCurrentRect(); m_StepCount++; StoneColor selfColor = isBlack ? StoneColor.Black : StoneColor.White;
bool isKillSelf = m_DeadBlocks.ContainsKey(m_StepCount - )
&& m_DeadBlocks[m_StepCount - ].Steps.Count ==
&& m_DeadBlocks[m_StepCount - ].Steps[].StoneColor == selfColor;
if (isKillSelf) {
m_DeadBlocks.Remove(m_StepCount - );
BackOne();
return false;
} return true;
}
Redraw()
2.关于提子
以 LinkSteps()方法为依托,提子不再是上下左右一通乱吃了,而是采用集合的办法,只需看看一块棋有没有气即可。其代码如下:
// +
// + + + 与 step 相连的棋子,包含自身
// + 根据 color 参数决定是所有,同色,黑色,白色,还是空色。
List<Step> LinkSteps(Step step, StoneColor color = StoneColor.Empty)
{
List<Step> links = new List<Step>();
for (int i = -; i < ; i++) {
for (int j = -; j < ; j++) {
if (i == j || i == -j) {
continue;
}
if (InRange(step.Row + i, step.Col + j)) {
links.Add(m_Steps[step.Row + i, step.Col + j]);
}
}
}
links.Add(step);
if (color == StoneColor.All) {
return links;
} else {
links.RemoveAll(l => l.StoneColor != color);
return links;
}
}
LinkSteps()
当然,关于劫争,关于悔棋,非深入代码,不能明白。但 LinkSteps()是构成集合的基础。从集合的观点,研究围棋,相信比其他方法更为可行。