X01.Weiqi. 8: Improved. x01.Weiqi. 8: Improved.

Source: Internet
Author: User

X01.Weiqi. 8: Improved. x01.Weiqi. 8: Improved.

All of the original code is deleted, and in-depth reconstruction is performed. There are two advantages:

1. About display

Based on the StoneSize attribute, the following sentence is added in the set: Width = Height = m_StoneSize * 19; to control the size of the Board. All objects are created in the Init () method, and the specific rendering is completed in the Redraw () method. This method separates the creation and re-painting, making it easier to adjust the size of the hour for re-painting. The code for these two methods is as follows:

1 void Init () 2 {3 // Line 4 for (int I = 0; I <19; I ++) {5 m_LinesH [I] = new Line (); 6 m_LinesH [I]. stroke = Brushes. black; 7 m_Canvas.Children.Add (m_LinesH [I]); 8 9 m_LinesV [I] = new Line (); 10 m_LinesV [I]. stroke = Brushes. black; 11 m_Canvas.Children.Add (m_LinesV [I]); 12} 13 14 // star 15 for (int j = 0; j <3; j ++) {16 for (int I = 0; I <3; I ++) {17 m_Stars [I, j] = new Ellipse (); 18 m_Stars [I, j]. fill = Brushes. black; 19 m_Canvas.Children.Add (m_Stars [I, j]); 20} 21} 22 23 for (int I = 0; I <19; I ++) {24 for (int j = 0; j <19; j ++) {25 m_Stones [I, j] = new Ellipse (); 26 m_Stones [I, j]. visibility = Visibility. hidden; 27 m_Canvas.Children.Add (m_Stones [I, j]); 28 29 m_Numbers [I, j] = new TextBlock (); 30 m_Numbers [I, j]. background = Brushes. transparent; 31 m_Numbers [I, j]. visibility = Visibility. hidden; 32 m_Canvas.Children.Add (m_Numbers [I, j]); 33 34 m_Steps [I, j] = new Step (); 35 m_Steps [I, j]. row = I; 36 m_Steps [I, j]. col = j; 37 m_EmptySteps.Add (m_Steps [I, j]); 38 m_AllSteps.Add (m_Steps [I, j]); 39} 40} 41 42 // current flag 43 m_CurrentRect.Visibility = System. windows. visibility. hidden; 44 m_CurrentRect.Fill = Brushes. red; 45 m_Canvas.Children.Add (m_CurrentRect); 46 47 for (int I = 0; I <19; I ++) {48 for (int j = 0; j <19; j ++) {49 Rectangle rect = new Rectangle (); 50 rect. visibility = System. windows. visibility. hidden; 51 m_EmptyRects [I, j] = rect; 52 m_Canvas.Children.Add (m_EmptyRects [I, j]); 53 54} 55} 56}Init () 1 public void Redraw () 2 {3 // draw Line 4 for (int I = 0; I <19; I ++) {5 Line l = m_LinesH [I]; 6 int y = I * StoneSize + StoneSize/2; 7 l. x1 = StoneSize/2; 8 l. y1 = y; 9 l. x2 = 19 * StoneSize-StoneSize/2; 10 l. y2 = y; 11 12 l = m_LinesV [I]; 13 int x = I * StoneSize + StoneSize/2; 14 l. x1 = x; 15 l. y1 = StoneSize/2; 16 l. x2 = x; 17 l. y2 = 19 * StoneSize-StoneSize/2; 18} 19 20 // STAR 21 for (int j = 0; j <3; j ++) {22 for (int I = 0; I <3; I ++) {23 Ellipse e = m_Stars [I, j]; 24 e. width = e. height = StoneSize/3; 25 double left = 4 * StoneSize + j * 6 * StoneSize-StoneSize/2-e. width/2; 26 double top = 4 * StoneSize + I * 6 * StoneSize-StoneSize/2-e. height/2; 27 Canvas. setLeft (e, left); 28 Canvas. setTop (e, top); 29} 30} 31 32 // Stones and Numbers 33 fo R (int I = 0; I <19; I ++) {34 for (int j = 0; j <19; j ++) {35 var stone = m_Stones [I, j]; 36 stone. width = stone. height = StoneSize; 37 Canvas. setLeft (stone, j * StoneSize); 38 Canvas. setTop (stone, I * StoneSize); 39 40 ShowNumber (I, j, m_Steps [I, j]. stepCount); 41} 42} 43 44 // point objective log 45 if (IsShowMesh) 46 for (int I = 0; I <19; I ++) {47 for (int j = 0; j <19; j ++) {48 var rect = m_EmptyR Ects [I, j]; 49 rect. width = rect. height = m_CurrentRect.Width; 50 double offset = (StoneSize-rect. width)/2.0; 51 Canvas. setLeft (rect, j * StoneSize + offset); 52 Canvas. setTop (rect, I * StoneSize + offset); 53} 54} 55} 56 57 public bool NextOne (int row, int col) 58 {59 if (m_Steps [row, col]. stoneColor! = StoneColor. empty) 60 return false; 61 if (m_BanOnce.Row = row & m_BanOnce.Col = col) {62 return false; 63} 64 m_BanOnce.Row = m_BanOnce.Col =-1; 65 66 DrawStep (row, col); 67 bool isBlack; 68 if (m_Steps [row, col]. stoneColor = StoneColor. black) {69 m_BlackSteps.Add (m_Steps [row, col]); 70 isBlack = true; 71} else {72 m_WhiteSteps.Add (m_Steps [row, col]); 73 isBlack = false; 74} 75 m_Empty Steps. Remove (m_Steps [row, col]); 76 77 UpdateBlackBlocks (); 78 UpdateWhiteBlocks (); 79 if (isBlack) {80 if (! UpdateDeadBlocks (m_WhiteBlocks) 81 UpdateDeadBlocks (m_BlackBlocks); 82} else {83 if (! UpdateDeadBlocks (m_BlackBlocks) 84 UpdateDeadBlocks (m_WhiteBlocks); 85} 86 87 MoveCurrentRect (); 88 89 m_StepCount ++; 90 91 StoneColor selfColor = isBlack? StoneColor. black: StoneColor. white; 92 bool isKillSelf = m_DeadBlocks.ContainsKey (m_StepCount-1) 93 & m_DeadBlocks [m_StepCount-1]. steps. count = 1 94 & m_DeadBlocks [m_StepCount-1]. steps [0]. stoneColor = selfColor; 95 if (isKillSelf) {96 m_DeadBlocks.Remove (m_StepCount-1); 97 BackOne (); 98 return false; 99} 100 101 return true; 102}Redraw ()

2. Introduction

Relying on the LinkSteps () method, the pick-up is no longer a mess on the left or right, but a collection method. You just need to check whether a piece of chess is angry. The Code is as follows:

1 // + 2 // ++ the pawn pieces connected to the step, including itself. 3 // + the color parameter determines whether it is all, same color, black, white, or empty. 4 List <Step> LinkSteps (Step step, StoneColor color = StoneColor. empty) 5 {6 List <Step> links = new List <Step> (); 7 for (int I =-1; I <2; I ++) {8 for (int j =-1; j <2; j ++) {9 if (I = j | I =-j) {10 continue; 11} 12 if (InRange (step. row + I, step. col + j) {13 links. add (m_Steps [step. row + I, step. col + j]); 14} 15} 16} 17 links. add (step); 18 if (color = StoneColor. all) {19 return links; 20 } Else {21 links. RemoveAll (l => l. StoneColor! = Color); 22 return links; 23} 24}LinkSteps ()

Of course, you cannot understand the code of the fight and regret. However, LinkSteps () is the basis of the set. From a collection point of view, we believe it is more feasible to study go than other methods.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.