Reversi Game Implementation in Visual Basic
This Reversi game demonstrates core programming concepts using Visual Basic 6 and VB.NET
VB Reversi Implementation
The implementation uses two-dimensional arrays to track game state and includes logic for validating moves and flipping pieces.
Core Game Logic
The game uses two 4×4 Boolean arrays:
- white(4,4) tracks positions of white pieces
- black(4,4) tracks positions of black pieces
When a piece is placed on the board, the corresponding array element is set to True. The game logic then checks adjacent positions to determine which pieces should be flipped.
Game Implementation Details
Board Representation
The game board is implemented using an array of 16 image controls arranged in a 4×4 grid. Each square is represented by an indexed image control (Image1(0) to Image1(15)).
Drag-and-Drop Mechanics
Players drag either the white or black piece image onto the board. The game validates each move to ensure it follows Reversi rules before updating the game state.
Game Logic
Using If...Then and Select Case statements, the program checks valid moves and flips trapped pieces between the newly placed piece and existing pieces of the same color.
Score Tracking
The game continuously counts and displays the number of white and black pieces. At game end, it declares the winner based on piece count.
Designing the Interface
To create the game interface:
- Insert an image control and duplicate it to create a 4×4 grid
- Create solid white and black circle images (saved as JPEG files)
- Implement drag-and-drop functionality using the
DragDropmethod
Board Layout Positions
| 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: Image control positions on the game board
Game Interface at Runtime
Figure 2: Reversi game in action with white and black pieces
Code Implementation
The following subroutine checks the status of each position on the board:
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
' Check board status and update piece positions
Sub checkstatus()
k = 0
For j = 1 To 4
For i = 1 To 4
' Check for white pieces
If Image1(k).Picture = Image17.Picture Then
white(i, j) = True
Else
white(i, j) = False
End If
' Check for black pieces
If Image1(k).Picture = Image18.Picture Then
black(i, j) = True
Else
black(i, j) = False
End If
k = k + 1
Next i
Next j
End Sub
How the CheckStatus Subroutine Works
This procedure:
- Iterates through each position on the 4×4 board
- Checks if the image at each position matches the white piece image (Image17)
- Updates the white array accordingly
- Checks if the image matches the black piece image (Image18)
- Updates the black array accordingly
Game Logic Implementation
The core game mechanics involve:
- Move validation: Ensuring moves follow Reversi rules
- Piece flipping: Identifying and flipping trapped pieces
- Turn management: Alternating between players
- Win detection: Determining when the game ends and who won
' Example of move validation logic
If ValidMove(row, col, currentPlayer) Then
PlacePiece(row, col, currentPlayer)
FlipPieces(row, col, currentPlayer)
currentPlayer = IIf(currentPlayer = WHITE, BLACK, WHITE)
Else
MsgBox "Invalid move! Please try again."
End If
Equivalent VB.NET Code
The following example shows how a simple Reversi game can be implemented in VB.NET using Windows Forms. This version uses an 8×8 grid of buttons, tracks the current player, validates moves, flips captured pieces, and updates the board after each turn.
Public Class Form1
Private board(7, 7) As Integer
Private buttons(7, 7) As Button
Private currentPlayer As Integer = 1 ' 1 = Black, 2 = White
Private directions As Integer(,) = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateBoard()
InitializeGame()
End Sub
Private Sub CreateBoard()
Dim size As Integer = 45
For r As Integer = 0 To 7
For c As Integer = 0 To 7
Dim btn As New Button()
btn.Width = size
btn.Height = size
btn.Left = 20 + c * size
btn.Top = 20 + r * size
btn.Tag = r & "," & c
btn.BackColor = Color.ForestGreen
btn.FlatStyle = FlatStyle.Flat
AddHandler btn.Click, AddressOf Cell_Click
Me.Controls.Add(btn)
buttons(r, c) = btn
Next
Next
End Sub
Private Sub InitializeGame()
For r As Integer = 0 To 7
For c As Integer = 0 To 7
board(r, c) = 0
Next
Next
board(3, 3) = 2
board(3, 4) = 1
board(4, 3) = 1
board(4, 4) = 2
currentPlayer = 1
DrawBoard()
UpdateStatus()
End Sub
Private Sub DrawBoard()
For r As Integer = 0 To 7
For c As Integer = 0 To 7
Select Case board(r, c)
Case 0
buttons(r, c).Text = ""
Case 1
buttons(r, c).Text = "●"
buttons(r, c).ForeColor = Color.Black
Case 2
buttons(r, c).Text = "●"
buttons(r, c).ForeColor = Color.White
End Select
Next
Next
End Sub
Private Sub Cell_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim parts() As String = btn.Tag.ToString().Split(","c)
Dim row As Integer = Convert.ToInt32(parts(0))
Dim col As Integer = Convert.ToInt32(parts(1))
If MakeMove(row, col, currentPlayer) Then
currentPlayer = If(currentPlayer = 1, 2, 1)
DrawBoard()
UpdateStatus()
If Not HasValidMove(currentPlayer) Then
currentPlayer = If(currentPlayer = 1, 2, 1)
If Not HasValidMove(currentPlayer) Then
ShowWinner()
Else
MessageBox.Show("No valid moves. Turn passes back.", "Reversi")
End If
End If
Else
MessageBox.Show("Invalid move.", "Reversi")
End If
End Sub
Private Function MakeMove(row As Integer, col As Integer, player As Integer) As Boolean
If board(row, col) <> 0 Then Return False
Dim opponent As Integer = If(player = 1, 2, 1)
Dim flipped As Boolean = False
For i As Integer = 0 To directions.GetLength(0) - 1
Dim dr As Integer = directions(i, 0)
Dim dc As Integer = directions(i, 1)
Dim r As Integer = row + dr
Dim c As Integer = col + dc
Dim path As New List(Of Point)
While r >= 0 AndAlso r <= 7 AndAlso c >= 0 AndAlso c <= 7 AndAlso board(r, c) = opponent
path.Add(New Point(r, c))
r += dr
c += dc
End While
If r >= 0 AndAlso r <= 7 AndAlso c >= 0 AndAlso c <= 7 AndAlso board(r, c) = player AndAlso path.Count > 0 Then
For Each p As Point In path
board(p.X, p.Y) = player
Next
flipped = True
End If
Next
If flipped Then
board(row, col) = player
End If
Return flipped
End Function
Private Function HasValidMove(player As Integer) As Boolean
For r As Integer = 0 To 7
For c As Integer = 0 To 7
Dim testBoard(7, 7) As Integer
Array.Copy(board, testBoard, board.Length)
If MakeMovePreview(r, c, player, testBoard) Then
Return True
End If
Next
Next
Return False
End Function
Private Function MakeMovePreview(row As Integer, col As Integer, player As Integer, tempBoard(,) As Integer) As Boolean
If tempBoard(row, col) <> 0 Then Return False
Dim opponent As Integer = If(player = 1, 2, 1)
Dim flipped As Boolean = False
For i As Integer = 0 To directions.GetLength(0) - 1
Dim dr As Integer = directions(i, 0)
Dim dc As Integer = directions(i, 1)
Dim r As Integer = row + dr
Dim c As Integer = col + dc
Dim count As Integer = 0
While r >= 0 AndAlso r <= 7 AndAlso c >= 0 AndAlso c <= 7 AndAlso tempBoard(r, c) = opponent
count += 1
r += dr
c += dc
End While
If r >= 0 AndAlso r <= 7 AndAlso c >= 0 AndAlso c <= 7 AndAlso tempBoard(r, c) = player AndAlso count > 0 Then
flipped = True
End If
Next
Return flipped
End Function
Private Sub UpdateStatus()
Dim blackCount As Integer = 0
Dim whiteCount As Integer = 0
For r As Integer = 0 To 7
For c As Integer = 0 To 7
If board(r, c) = 1 Then blackCount += 1
If board(r, c) = 2 Then whiteCount += 1
Next
Next
lblStatus.Text = "Current Player: " &
If(currentPlayer = 1, "Black", "White") &
" | Black: " & blackCount &
" | White: " & whiteCount
End Sub
Private Sub ShowWinner()
Dim blackCount As Integer = 0
Dim whiteCount As Integer = 0
For r As Integer = 0 To 7
For c As Integer = 0 To 7
If board(r, c) = 1 Then blackCount += 1
If board(r, c) = 2 Then whiteCount += 1
Next
Next
If blackCount > whiteCount Then
MessageBox.Show("Black wins!", "Game Over")
ElseIf whiteCount > blackCount Then
MessageBox.Show("White wins!", "Game Over")
Else
MessageBox.Show("It's a draw!", "Game Over")
End If
End Sub
End Class
In this VB.NET version, the board is stored in a two-dimensional array. Each cell is represented by a button, and the game checks all eight directions to determine whether opponent pieces should be flipped after a valid move.
Suggested VB.NET controls:
64 Buttons created dynamically for the board
1 Label for status: lblStatus
Optional buttons: btnNewGame, btnReset
VB.NET Reversi Simulation
This interactive demo shows how a simplified Reversi board works in a VB.NET-style application. Click a valid square to place a piece and flip the opponent’s pieces.