Step-by-Step wuziqi AI [1] Implementing the interface and position board

Source: Internet
Author: User

First, let's pass this one up. We have to think about how to clarify the 72 vectors of the situation evaluation and the comprehensive comparison and alpha-beta tailoring beyond the boundary, so as to avoid ambiguity. Because the two parts of the Code are the most important, most important, and longest part of the code. Only the source code updated in the next set contains over 600 lines. Of course, the logic is very clear, because many of them are repetitive logic content-hard-coding templates. The alpha-beta tailoring beyond the boundary is the most difficult part of the entire program. The difficulty is not only due to recursion, but more importantly, the idea of this tailoring. The rest is well understood and rarely encoded. Just like general extension and empty step pruning, ten lines and eight lines of code are implemented. Static search only slightly changes the code according to alpha-beta. Well, I don't have to talk about it.

Note: Starting from this article, we will first explain the implementation content, and then upload the complete source code of this set. The source code is all written in VB2010.

I. Board

1. Why do I use a board?

According to our practice, we can use an array board (224). The data type can be byte or integer, But we waste a lot of storage space. Of course, this is not the main problem, the main problem is that the speed is too slow. The reason is simple. Addressing is much slower than retrieving a certain digit from an integer.

2. How to implement a board

We need a 225-bit data type. This type of length does not exist, so we can use an integer array. Fortunately, VB. NET provides us with a bitarray class, and the internal implementation of this class actually uses an integer array. But the problem also arises: one digit can only be 0 or 1, and we want to represent three types of data: white, black, and empty. What should we do? In fact, it is a good solution. You can use 2 words to represent a location or two bitarrays. My program adopts the second method:

Private wBitArr As BitArray
Private bBitArr As BitArray

3. How to operate a chessboard

Directly run the Code:

 

'Board
Public Class mBitBoard
'Use bitarray instead of byte array to increase the read/write speed. (Bitarray uses an integer array. Each element is 32 bytes, so the addressing speed is much faster than the byte array)
'0 = white, 1 = Black, 2 = empty
Private wBitArr As BitArray
Private bBitArr As BitArray

'15*15.
Sub New ()
WBitArr = New BitArray (225)
BBitArr = New BitArray (225)
End Sub

'Set pawns'
Sub [Set] (index As Integer, value As Integer)
If value = 0 Then
WBitArr. Set (index, True)
ElseIf value = 1 Then
BBitArr. Set (index, True)
Else
BBitArr. Set (index, False)
WBitArr. Set (index, False)
End If
End Sub

'Retrieve pawns'
Function [Get] (index As Integer) As Integer
If wBitArr. Get (index) Then Return 0
If bBitArr. Get (index) Then Return 1
Return 2
End Function

End Class

 

Very clear. This class was modified a little later -- added a function. Of course, this is not the question we want to discuss now. Now that we have the display method of the board, let's implement the interface. In fact, the interface is too simple:

 

Ii. wuziqi Interface

1. Search for or draw chess boards and pawns by yourself

2. Obtain the Chess Piece Offset, chess piece size, and board size.

3. Draw pawns Based on the mBitBoard instance

 

I drew a panel on the form and named it pnlBoard. Then I wrote a function like this:

'Draw a pawn
Private Sub pnlBoard_Paint (sender As System. Object, e As System. Windows. Forms. PaintEventArgs) Handles pnlBoard. Paint
E. Graphics. DrawImage (My. Resources. _ 2064, Point. Empty)
For I As Integer = 0 To 224
If ucpcSquares. get (I) = 1 Then e. graphics. drawImage (bb, New Point (I Mod 15) * cs. height, (I \ 15) * cs. height) + co)
If ucpcSquares. get (I) = 0 Then e. graphics. drawImage (bw, New Point (I Mod 15) * cs. height, (I \ 15) * cs. height) + co)
Next
End Sub

First, draw the board image, and then draw the chess piece.

 

The following code is as follows:

 

'Subaccounts
Private Sub pnlBoard_MouseDown (sender As Object, e As System. Windows. Forms. MouseEventArgs) Handles pnlBoard. MouseDown
Dim sdr = CType (sender, Panel)
'Mouse coordinate to chessboard Coordinate
Dim p As Point = e. Location-co
P. X \ = cs. Width
P. Y \ = cs. Height
'Next white child
If e. Button = Windows. Forms. MouseButtons. Left Then
UcpcSquares. Set (p. Y * 15 + p. X, 0)
'Update display
PnlBoard_Paint (sdr, New PaintEventArgs (sdr. CreateGraphics, sdr. DisplayRectangle ))
End If
End Sub

It's easy, isn't it.

 

 

The complete code is as follows:

 

Public Class Form1
'Black and white pawns
Private bb As Bitmap
Private bw As Bitmap
'Pawn offset
Private co As Point = New Point (5, 5)
'Pawn size
Private cs As Size = New Size (25, 25)
'Board
Private ucpcSquares As mBitBoard

Private Sub Form1_Load (sender As System. Object, e As System. EventArgs) Handles MyBase. Load
'Initialize the pawn'
Dim resbmp As Bitmap = New Bitmap (My. Resources. _ 7041, cs. Width * 4, cs. Height)
Dim allbmp As Bitmap = New Bitmap (resbmp. Width \ 2, resbmp. Height)
'Retrieve the pawns' Image
For y As Integer = 0 To allbmp. Height-1
For x As Integer = 0 To allbmp. Width-1
If resbmp. GetPixel (x + allbmp. Width, y). ToArgb = Color. Black. ToArgb Then allbmp. SetPixel (x, y, resbmp. GetPixel (x, y ))
Next
Next
Bb = allbmp. Clone (New Rectangle (0, 0, allbmp. Width/2, allbmp. Height), allbmp. PixelFormat)
Bw = allbmp. Clone (New Rectangle (allbmp. Width/2, 0, allbmp. Width/2, allbmp. Height), allbmp. PixelFormat)
'====================================== You can set ======== ======================================
UcpcSquares = New mBitBoard ()
UcpcSquares. Set (7*15 + 7, 1)
End Sub

'Draw a pawn
Private Sub pnlBoard_Paint (sender As System. Object, e As System. Windows. Forms. PaintEventArgs) Handles pnlBoard. Paint
E. Graphics. DrawImage (My. Resources. _ 2064, Point. Empty)
For I As Integer = 0 To 224
If ucpcSquares. get (I) = 1 Then e. graphics. drawImage (bb, New Point (I Mod 15) * cs. height, (I \ 15) * cs. height) + co)
If ucpcSquares. get (I) = 0 Then e. graphics. drawImage (bw, New Point (I Mod 15) * cs. height, (I \ 15) * cs. height) + co)
Next
End Sub

'Subaccounts
Private Sub pnlBoard_MouseDown (sender As Object, e As System. Windows. Forms. MouseEventArgs) Handles pnlBoard. MouseDown
Dim sdr = CType (sender, Panel)
'Mouse coordinate to chessboard Coordinate
Dim p As Point = e. Location-co
P. X \ = cs. Width
P. Y \ = cs. Height
'Next white child
If e. Button = Windows. Forms. MouseButtons. Left Then
UcpcSquares. Set (p. Y * 15 + p. X, 0)
'Update display
PnlBoard_Paint (sdr, New PaintEventArgs (sdr. CreateGraphics, sdr. DisplayRectangle ))
End If
End Sub

End Class

 

Among them, resource no. 2064 is a chessboard, and 7041 is composed of four pictures: sunspots, whitelists, and their respective masks. I didn't use a mask. I used a loop. The principle of my laziness is to initialize the code to save trouble ......

 

 

Complete sample code:

/Files/zcsor/qingyue lianzhu 0.1.7z

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.