GitHub
谁方便谁拍,谁重要拍谁。在这个砖头满天飞的时代,一个好的生态显得尤为重要。
红颜小头发,要的很简单。
也许成绝唱,只因鱼心火。
姚贝福娃的离去,除感叹人生无常外,活着做点有意义的事情,同样显得尤为重要。
数年前为学习人工智能,写了围棋程序,却发现其难度超出了我的想象。特将其放到 GitHub 上,希望有人斧正。注意,是斧正,而非小修小改。
调整重绘
窗口大小改变时,棋盘也要作相应的重绘。这个比较简单,我的方法是把 BoardBase 类中的 m_sbSteps 字段改成 public,在主窗口 MainWindows 中保存之。具体的操作参考了 StepBoard 类中的办法,复制粘贴,略作修改而已,代码如下:
class="code_img_closed" src="/Upload/Images/2015050121/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('28b5e09a-c52b-42c4-b58a-1a4e3d6381d2',event)" src="/Upload/Images/2015050121/2B1B950FA3DF188F.gif" alt="" />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