VB2022 VB2019 VB6 VB Sample Code About Us

Reversi



This is the mini version of the reversi game. In this program, we use two sets of two-dimensional arrays (4x4)and declare them as Boolean , one represent the white pieces and the other one represent the black pieces. If a white piece or a black piece occupies a square, the variable becomes true , otherwise it is false.

By using this concept, the program can check how many white and black pieces appear on the reversi board and which positions they occupy. We use If...Then and Select Case.... End Select statements to check for conditions whether a white piece or a black piece can be place in a certain position so that the pieces trapped in between will change colour. We also added a procedure to display the number of white pieces and the number of black pieces at any one time and also who is the winner.

To design the interface, we need to insert an image control and then copy and paste the image repeatedly to form a 4x4 array of images. Each image is defined by its index. For example, Image1(0) is the first image Image1(1) is the second image, Image1(2) is the third image and so on. The positions of the images are shown in Figure 1 below:

Next we need to create two images, a white solid circle and a black solid circle, save them as jpeg files. Load them into two image controls on the form. You can use whatever name for the two images, we use image17 to denote the while solid circle and image18 to denote the black circle. The two images are very important as we will later use the dragdrop method to enable the user to drag and drop the images into the circles.

The Design Interface

Image1(12) Image1(13) Image1(14) Image1(15)
Image1(8) Image1(9) Image1(10) Image1(11)
Image1(4) Image1(5) Image1(6) Image1(7)
Image1(0) Image1(1) Image1(2) Image1(3)

Figure 1

The runtime Interface

Figure 2

The Code

Option Base 1
Dim white(4, 4) As Boolean
Dim black(4, 4) As Boolean
Dim i, j, k, w, b As Integer
Dim imgtag As String

'The following subroutine is to check for the number and positions

of the white and black pieces on the board. Image17 is a white button while

image18 is a black button.

Sub checkstatus()
k = 0
For j = 1 To 4
For i = 1 To 4

If Image1(k).Picture = Image17.Picture Then
white(i, j) = True

Else: white(i, j) = False
End If

If Image1(k).Picture = Image18.Picture Then
black(i, j) = True
Else

black(i, j) = False

End If
k = k + 1
Print n

Next i

Next j


End Sub

 


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy