Categories of Go Spectrum Program (including slits, substrings search, robbery, banners, etc.)

zhaozj2021-02-12  175

?

/ ************************************************** *********************************************** /? * Function:? Chessboard class? * Description:? Used to describe and calculate the status, gas, substring? * ??? 气 子, etc. ? * Author:? Chen Rong * Time:?? 2004-6-7 2:?? 24 * * History:? * ??? 2004-6-7 2:24: to achieve a substring search string of gas judgment. Use the ArrayList class, after the efficiency? * ??????? If you want to optimize, you can rewrite the adjoubus group. ? ********************************************************** ********************************************* /

Using system.collections; using system.drawing;

Namespace chenrong.weiqi {? ///

/// Board's summary description. ? ///

PUBLIC CLASS BOARD

? {

?? public static readonly int black = -1;

?? public static readonly int EMPTY = 0;

?? public static readonly int white = 1;

?? // Chess pieces array ?? private int [] stones; ??? // The last step kills the number of chess pieces ?? public int lastkillcount = 0;

?? // The offset array of left and right is used to determine the case of four points around a certain location.? Private int [,] offsets = new int = 0}, {{-1, 0}, {1, 0 }, {0, -1}, {0, 1}};

?? // constructor ?? public board () ?? {??? this.stones = new int rt [19, 19]; ??? this.setfornewgame (); ??}

?? // Copy the chessboard ?? public board (Board board) ?? {??? this.stones = board.stones; ??? this.lastkillcount; ??}

?? // Initialization ?? public void setfornewgame () ?? {??? for (int i = 0; i <19; i ) ??? {???? for (int J = 0; J <19; J ) ???? {????? stones [i, j] = board.empty; ????} ???} ??? this.lastkillcount = 0; ??}

?? // Get the status of a location on the board ?? Public int getPointContent (int Row, int co) {??? Return this.stones [Row, Col]; ??}

?? // Is it effective ?? Note: It needs to be improved, it is necessary to judge that it is not committed to suicide, and cannot be suiculated, and it is not possible to carry out rules restrictions. Currently don't consider? Public Bool IsValidMove (int color, int in, int co) ?? {??? if (this.stones [Row, Col]! = board.empty) ??????? Return false; ??? Else ??? {???? // Copy a checkerboard, As a backup ???? board board = new board (this); ???? // on the copy of the chessboard drill: trial off a child ???????? board.stones [Row, color; ???? arraylist chuanself = board.findchuan (row, col); ???? // Will not commit suicide, return ???? if (Board.countchchuangas (chuanself)> 0) ?????? { ????? Return True; ????} ???? // If the fall is suicide, it will judge whether it will kill the place, if you can't, this hand is not effective ???? // Do not consider the complex judgment of robbery) ???? Else ???? {????? // Restore, try off the next child, see if you can mention the child ?????????????????????????? COL] = board.empty; ????? Board.makemove (color, row, col); ????? if (Board.lastkillCount> 0) ?????? Return True; ????? Else ????? {?????? // must use the following sentence to restore it to empty state, it will be wrong, don't know why? ? ?????? this.stones [Row, Col] = Board.empty; ??????} ???????} ??? ??}

?? // Take a step and put the dead ?? public void makemove (int color, int) ?? {?????? this.stones [Row, col] = color;

??? arraylist stonesdead = new arraylist (); ?? // dead child collection ??? arraylist stonesvisited = new arraylist ();? // Access point collection

??? // Start judgment and put the dead ??? Int newx, newy; ??? for (int i = 0; i <4; i ) ??? {???? newx = row offsets [ I, 0]; ???? Newy = col offsets [i, 1]; ????

???? f (newx> = 0 && newx <19 && newy> = 0 && newy <19) ???? {????? f (this.stones [newx, newy] == (- color) ) ????? {??????? // If the current location has been found in the exploration process of a front direction, it is not necessary to judge. ?????? // Because the chess pieces in different directions may be the same string ?????? IF (! Stonesvisited.contains) ?????? {? ?????? // Look for strings with this location ??????? arraylist chuan = this.findchuan (newx, newy); ??????? // consume string of gas ???? ??? int chuangas = this.countchuangas (chuan); ??????? // added to the list of access ??????? stonesvisited.addrange (chuan); ??????? IF (chuanas == 0) ??????? {???????? stonesdead.addrange (chuan); ???????} ??????} ?????} ????} ???}

??? // Take the dead ??? for (int i = 1; i <= stonesdead.count; i ) ??? {???? Point P = (Point) stonesdead [i - 1];? ??? this.stones [px, py] = board.empty; ???}

??? // update the number of measures ??? this.lastkillcount = stonesdead.count; ??}

?? // 气 气 ?? public int countgas (int  int COL) ?? {??? int GAS = 0; ??? Int newx, newy; ??? for (int i = 0; i <4 ; i ) ??? {???? newx = row this.offsets [i, 0]; ???? newy = col this.offsets [i, 1];

???? f (newx> = 0 && newx <19 && newy> = 0 && newy <19) ???? {????? f (this.stones [newx, newy] == Board.empty) ?????? GAS ; ????} ???} ??? Return GAS; ??}

?? ///

?? // Chess string search ?? /// look for the chess pieces containing this location ?? ///

?? ///

?? ///?? Guangsheng preferred traversal algorithm

?? ///

?? ///

Start point X coordinate

?? ///

The starting point Y coordinate

?? ///

Searched chess pieces (no null found)

?

?? Public ArrayList Findchuan (int Row, Int Col)

?? {

??? arraylist chuan = NULL; ??????

??? Int color = this.stones [Row, Col]; // Current color

??? if (this.stones [Row, Color) ??? {???? chuan = new arraylist (); ???? // joined the current point ???? chuan.add (New Point (Row, COL)); ???? // Define two cursors ??????????????? Int end = 0; ???????? Int findcount;? // The number of neighbors found, the judgment condition for the end of the cycle ???? do ???? {????? findcount = 0; ?????? // BeGin to the end of some points indicating that there is no exploration Those points around ????? for (INT I = Begin; I <= End; i ) ????? {?????? // Explore the left and right four directions ????? ? for (int J = 0; j <4; j ) ?????? {??????? INT newx = ((Point) chuan [i]). x offsets [j, 0]; ??????? int newy = ((Point) chuan [i]). Y offsets [j, 1]; ?????????????? // If this point is on the board Inside, and the color is the same, and there is no existing string, add a string ??????? IF (newx> = 0 && newx <19 && newy> = 0 && newy <19 ?????? ? && this.stones [newx, newy] == color ???????? &&! chuan.contains (new point (new x, newy))) ??????? {?????? ?? chuan.add (new point (newx, newy)); ???????? // Looking for the neighbor counter plus 1 ???????? Findcount = 1; ????? ??} ??????} ?????} ????? // set the beginning and end of the next loop to list the cursor ????? begin = end 1; ???? • End = end findcount; ????} ???? // If all points in this round is not neighbors, it means that the string search is over, jump out of the loop ???? While (FindCount> 0) ; ???} ??? // Test: Return to the number of strings in the search ???? R r r et CHUAN.COUNT;

??? RETURN CHUAN; ????}

?? // Consultant ?? public int count chuan ?? {??? if (chuan == null) ???? Return 0; ??? int GAS = 0; ??? for INT i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-7656.html

New Post(0)