Implementation and consideration of Go game program (4) -- the data structure of the Board

Source: Internet
Author: User

UCTAlgorithmAfter the introduction, I will focus on practices.

Because ofProgramming LanguageIn, I only use C ++ and objective-C (I may write some blog posts related to iOS development in the future). For performance considerations, selecting C ++ is inevitable. I remember when I answered the question, a teacher asked why C ++ was used instead of C ++ in consideration of performance? For this unknown question, I can only answer: the machine code has better performance. Later, I learned that Mr Chen zhixing's "talker" was written in a foreign exchange ......

In this article, let's talk about the data structure of the Board! (The whole process is not discussed here.ProgramBecause it is not well designed)

In general, the Go board is 19x19, but the computer go has a variety of sizes (no more than 19 paths are certain, the reason ......). Therefore, the program must support a variable-size board. There are two methods to consider:

1) a variable is used to indicate the size of the Board. The Board space is dynamically allocated when the program is running.

2) use the C ++ template to take the size of the horizontal disk as the template parameter of the Board class.

1st) You need to dynamically apply for pushing space when the program is running, and 2nd) You need to allocate stack space by the OS when the program is running, which is more efficient than 1st. Considering that the size of the Board does not change frequently in the program, the efficiency factor is very important, so we use the 2nd method! (I mistakenly used 1st in foolish go ......)

The following describes how to store board data:

A board is used to indicate all information about the current Board status. What information does a board have in reality?

1) the status of each intersection on the board (Sunday, white child, empty point ). 2) the current sub-party. 3) robbery.

In the horizontal disk category, the three pieces of information need to be expressed. You need to carefully consider how to represent the status of each intersection on the board?

What type of intersection status should it be?Boolean? However, there are three statuses. boolean indicates that it cannot be used ~ Integer?

An integer is indeed acceptable, or a program uses a short integer to indicate the intersection state. But from the perspective of saving the Board memory space, integer is not enough!

That's right, Boolean! But it takes two ~

My program foolish go draws on the GNU go (if you remember correctly) Practice-use two boards-one black board and one white board to represent the Board status, the essence is to use two cloth values to represent an intersection state. Calculate the size of the Board: 9*9 * 4bit = 324bit. If you add 8, it is 328bit.

What? A boolean value occupies 8 bits? Ms is ...... But you can write it by yourself.CodePackage, so that every Boolean value in the array occupies 1 bit! The read/write operations through bitwise operations are not time-consuming.

What? Too lazy to write! Well, I am too lazy to write it, but there is still a way -- the STL in C ++ has a bitset template class, And the array length is used as the template parameter ...... Don't worry, it is static memory allocation ~ (My foolish go uses the vector <bool>. Although the vector <bool> is a vector template that features bool, it is also packaged, but it dynamically allocates space ......) In addition to saving memory space, this solution also reduces the time complexity of the deep copy board.

There is a problem here: the Board is two-dimensional, and we use one-dimensional arrays, which requires conversion between one-dimensional array subscript and two-dimensional array. Using a function to do this is obviously necessary, but performance is a problem. What are the major tools for using C language, macro functions? Yes, but it is too ugly. The C ++ standard does not recommend this. inline functions are a good option to implement short functions that are frequently called.

The data structure of the checkerboard is discussed here. I plan to start the next article to talk about "raisins"-this is a difficult part of the conventional algorithm. The design of the algorithms will greatly affect the performance of the program.

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.