x01.Weiqi.7: 調整重繪,x01.weiqi.7
GitHub
誰方便誰拍,誰重要拍誰。在這個磚頭滿天飛的時代,一個好的生態顯得尤為重要。
紅顏小頭發,要的很簡單。
也許成絕唱,只因魚心火。
姚貝福娃的離去,除感歎人生無常外,活著做點有意義的事情,同樣顯得尤為重要。
數年前為學習人工智能,寫了圍棋程序,卻發現其難度超出了我的想象。特將其放到 GitHub 上,希望有人斧正。注意,是斧正,而非小修小改。
調整重繪
窗口大小改變時,棋盤也要作相應的重繪。這個比較簡單,我的方法是把 BoardBase 類中的 m_sbSteps 字段改成 public,在主窗口 MainWindows 中保存之。具體的操作參考了 StepBoard 類中的辦法,復制粘貼,略作修改而已,代碼如下:

![]()
1 // For Size Change and Show Number
2
3 List<StepContent> m_steps = new List<StepContent>();
4 public void FillSteps()
5 {
6 m_steps.Clear();
7 string s = m_sbSteps.ToString();
8 if (s.Length < 1) return;
9 string[] steps = s.Substring(0, s.Length - 1).Split(',');
10 StepContent step = new StepContent();
11 for (int i = 0; i < steps.Length; i++)
12 {
13 if (i % 3 == 0)
14 {
15 step = new StepContent();
16 step.Col = Convert.ToInt32(steps[i]);
17 }
18 else if (i % 3 == 1)
19 {
20 step.Row = Convert.ToInt32(steps[i]);
21 }
22 else if (i % 3 == 2)
23 {
24 step.Count = Convert.ToInt32(steps[i]);
25 m_steps.Add(step);
26 }
27 }
28 }
29 public void RenderChess()
30 {
31 m_sbSteps.Clear();
32 foreach (var item in m_steps)
33 {
34 NextOne();
35 }
36 }
37
38 int m_count = 1;
39 public void NextOne()
40 {
41 if (m_count > m_steps.Count)
42 {
43 return;
44 }
45
46 foreach (var item in m_steps)
47 {
48 if (item.Count == m_count)
49 {
50 int col = item.Col;
51 int row = item.Row;
52
53 if (Steps[col, row].Color != ChessColor.Empty)
54 {
55 return;
56 }
57
58 if (NotInPos.X == col && NotInPos.Y == row)
59 {
60 return;
61 }
62 else
63 {
64 // If Pos(struct) is property, must use new.
65 NotInPos = new Pos(-1, -1);
66 }
67
68 DrawChess(col, row);
69
70 Eat(col, row);
71 }
72 }
73 m_count++;
74 }
75
76 public bool IsShowNumber { get; set; }
for size change
顯示步數
為了顯示步數,在 Chess 類中加了幾個屬性,然後在 BoardBase 的 DrawChess 方法中也作了相應的調整。代碼如下:

![]()
1 public string NumberText
2 {
3 get { return m_TxtNumber.Text; }
4 set { m_TxtNumber.Text = value; }
5 }
6
7 public double NumberFontSize
8 {
9 get { return m_TxtNumber.FontSize; }
10 set { m_TxtNumber.FontSize = value; }
11 }
12
13 public System.Windows.Thickness NumberPadding
14 {
15 get { return m_TxtNumber.Padding; }
16 set { m_TxtNumber.Padding = value; }
17 }
Chess properties

![]()
1 if (Steps[col, row].Chess == null)
2 {
3 Chess chess = new Chess(brush, ChessSize);
4 if (IsShowNumber)
5 {
6 double size = (double)ChessSize;
7 chess.NumberFontSize = (m_StepCount + 1) > 99
8 ? size / 2 : (m_StepCount + 1) > 9
9 ? size / 1.6 : size / 1.2;
10 chess.NumberPadding = (m_StepCount + 1) > 99
11 ? new Thickness(size/28, size / 5.5, 0, 0) : (m_StepCount + 1) > 9
12 ? new Thickness(size / 8, size / 8, 0, 0) : new Thickness(size / 4, 0, 0, 0);
13 chess.NumberText = (m_StepCount + 1).ToString();
14 chess.NumberBrush = (m_StepCount % 2 == 1) ? Brushes.Black : Brushes.White;
15 }
16 chess.SetShow(IsShowNumber);
17 Canvas.SetLeft(chess, left);
18 Canvas.SetTop(chess, top);
19 m_Canvas.Children.Add(chess);
20 Steps[col, row].Chess = chess;
21 }
in the DrawChess()
效果圖

代碼下載鏈接:https://github.com/chinax01/x01.Weiqi