The implementation of the automatic sub-extraction function in the Go music program, C/C ++ source code

Source: Internet
Author: User

This article first address: http://blog.csdn.net/liigo/archive/2009/09/22/4582018.aspx

Reprinted please indicate the source: http://blog.csdn.net/liigo

Author: liigo, 2009/09/22

 

When playing Go, when a piece falls on the board, it will have an impact on the life and death of the other piece. If the other piece is no longer angry (dead ), it must be removed from the board ). This process is embodied in Go software. You need to use the program code to determine the life and death status of a piece or piece of piece, and then remove the dead piece from the board.

For further analysis, the falling chess pieces only threaten the four other chess pieces (or three or two, such as the ones at the corner) in the upper, lower, and right directions ), the opponent's chess pieces in these four directions may be an isolated chess piece, or a piece of chess piece (a piece of chess) connected to multiple chess pieces ). To determine whether a piece of chess is still alive, you need to check each piece of chess one by one: if there is no piece of chess next to a piece of chess, it means that this piece of chess has at least one breath, so it is sure it is not dead; if you have checked all the chess pieces in this game, you can never find one breath. You can confirm that the game is dead. It seems that this is a situation that requires recursive processing. When performing recursive processing on the entire chess set, remember to record the processed chess set. You cannot repeat the same chess set. Otherwise, loop recursion and dead recursion may occur. Once the life and death of a piece are determined, it is easy to remove it from the board, just to make some marks in the program.

Note: I (liigo) mentioned here that a piece of chess is "still alive". It is not equivalent to "already active" in go terms, but it only indicates that this piece of chess is "Not dead yet ", whether it will die in the future is not within the scope of the current process. Don't forget, our goal is "If a piece is dead, remove it from the Board". Since it is not dead (or not dead), why bother with it? (If you have to give it up in advance, instead, it violates the go rules ).

The following C/C ++ source code implements the automatic sub-extraction function mentioned above.

 

// Process the effect of the child that has just fallen on the life and death of the child in the surrounding area <br/> void processliving (INT row, int col) <br/>{< br/> stonecolor color = m_board [row-1] [col-1]; <br/> assert (color! = SC _blank); <br/> stonecolor othercolor = (color = SC _black? SC _white: SC _black); <br/> If (m_killedstones [m_stoneindex] = NULL) <br/> m_killedstones [m_stoneindex] = new bufferedmem (20 ); <br/> bufferedmem * pkilledstones = m_killedstones [m_stoneindex]; <br/> pkilledstones-> Empty (); <br/> // If the periphery is the child of the other party, check whether it is live or not, and remove it if it is dead <br/> bufferedmem stoneindexlist; <br/> If (row> 1 & m_board [row-1-1] [col-1] = othercolor & checkliving (Row-1, Col, color, & stoneindexlist) = false) <br/> processdeadstones (& stoneindexlist); <br/> stoneindexlist. empty (); <br/> If (row <19 & m_board [row + 1-1] [col-1] = othercolor & checkliving (row + 1, col, color, & stoneindexlist) = false) <br/> processdeadstones (& stoneindexlist); <br/> stoneindexlist. empty (); <br/> If (COL> 1 & m_board [row-1] [col-1-1] = othercolor & checkliving (row, col-1, color, & stoneindexlist) = false) <br/> processdeadstones (& stoneindexlist); <br/> stoneindexlist. empty (); <br/> If (COL <19 & m_board [row-1] [col + 1-1] = othercolor & checkliving (row, col + 1, color, & stoneindexlist) = false) <br/> processdeadstones (& stoneindexlist); <br/> invalidaterect (m_hwnd, null, true ); <br/>}

 

// Check whether the child of row/COL is alive. color indicates the color of the child of the other side. <br/> // 1 indicates that the system is alive (not dead), 0 indicates that the system is dead, and-1 indicates that the system is not dead. <br/> int checkliving (INT row, int Col, stonecolor color, bufferedmem * pstoneindexlist) <br/>{< br/> int Index = rowcoltoindex (row, col ); <br/> If (m_board [row-1] [col-1] = SC _blank) // if you are alive <br/> return 1; <br/> If (m_board [row-1] [col-1] = color) // This is the child of the other Party <br/> return-1; <br/> int * pindex = (int *) pstoneindexlist-> getdata (); <Br/> for (INT I = 0, n = pstoneindexlist-> getdatasize ()/sizeof (INT); I <n; I ++) <br/>{< br/> If (pindex [I] = index) <br/> return-1; // The child has been processed <br/>}< br/> pstoneindexlist-> appendmem (& Index, sizeof (INDEX )); <br/> // recursively check the child of the neighboring party, as long as the whole block is alive in one breath <br/> // The check is repeated here, optimization required <br/> If (row> 1 & m_board [row-1-1] [col-1]! = Color & checkliving (Row-1, Col, color, pstoneindexlist) = 1) <br/> return 1; <br/> If (row <19 & m_board [row + 1-1] [col-1]! = Color & checkliving (row + 1, Col, color, pstoneindexlist) = 1) <br/> return 1; <br/> If (COL> 1 & m_board [row-1] [col-1-1]! = Color & checkliving (row, col-1, color, pstoneindexlist) = 1) <br/> return 1; <br/> If (COL <19 & m_board [row-1] [col + 1-1]! = Color & checkliving (row, Col + 1, color, pstoneindexlist) = 1) <br/> return 1; <br/> return 0; <br/>}

 

Void processdeadstones (bufferedmem * deathstoneindexlist) <br/>{< br/> int * pindex = (int *) deathstoneindexlist-> getdata (); <br/> stonecolor * pboard = & m_board [0] [0]; <br/> for (INT I = 0, n = deathstoneindexlist-> getdatasize () /sizeof (INT); I <n; I ++) <br/> pboard [pindex [I] = SC _blank; <br/> // record the dead sub-records for use in the forward score <br/> If (deathstoneindexlist-> getdatasize ()> 0) <br/>{< br/> bufferedmem * pkilledstones = m_killedstones [m_stoneindex]; <br/> assert (pkilledstones ); <br/> pkilledstones-> appendmem (deathstoneindexlist-> getdata (), deathstoneindexlist-> getdatasize (); // duplicate values may exist here, but it does not matter <br/>}< br/>}

 

The above Code comes from the M8 go score software recently developed by liigo. This project is open-source on Google Code: http://code.google.com/p/m8weiqipu /.

If you have any mistakes or omissions, please criticize and correct them.

 

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.