In this post we see how to create the game of Checkers in Excel using VBA macros. We use shapes for each piece on the checkers board and also to indicate moving options. This version of Excel Checkers allows to play against the computer, which follows a common algorithm to make the moves. Download the file below to play Checkers in Excel.
Checkers Game
Checkers, also known as
draughts in British English, is a group of strategy board games for two players.
Players can move a piece to an adjacent unoccupied square and must jump over an
opponent's piece when possible in order to capture it. A piece that reaches the
opposite side of the board becomes a king and can move both forward and
backwards. The player who loses all the pieces or cannot make a valid movement
loses the game.
The most popular form of
checkers is the American checkers (also called English draughts), which is
played on an 8×8 board. There are other versions of checkers played on 8x8,
10x10, and even 12x12. International checkers is played on a 10x10 board.
Checkers Board
This version of checkers in
Excel is played on a 8x8 board that spans 8 rows and 8 columns in range B2:I9 in
the active worksheet. Each cell in that range is formatted with either a dark
or light color, representing the classic look of the checkers grid. The code
below is used in this version of Excel Checkers to achieve that.
For Each cell In rng
If cell.Row / 2 <> Int(cell.Row / 2) And _
cell.Column / 2 = Int(cell.Column / 2) Or _
cell.Row / 2 = Int(cell.Row / 2) And _
cell.Column / 2 <> Int(cell.Column / 2) Then
cell.Interior.ColorIndex = 51 'very dark gray (almost black)
Else
cell.Interior.ColorIndex = 24 'light bluish gray
End If
Next cell
Additionally, borders could
be added to each cell. A thicker border around the range is added in this
version of Checkers in Excel to nicely delineate the board.
Game Pieces
Excel checkers game pieces are oval shapes added to fit each cell. The shapes are added to cells with dark interior color only (ColorIndex 51). The example code below adds an oval shape that fits the active cell leaving some spacing.
Set cell = ActiveCell
l = cell.Left + 4
t = cell.Top + 3
w = cell.Width - 8
h = cell.Height - 6
Set shp = Sheet1.Shapes.AddShape(msoShapeOval, l, t, w, h)
Each shape is given a name that includes the word “piece” and the cell address. The calling macro passes a variable (n) that determines whether to format the piece as player (blue) or computer (red). The shape line is hidden – it will be shown later when a piece is selected. Each piece is assigned a macro – blue pieces will call “SelectPiecePlayer1” (see later) and red pieces will call a macro that does nothing (or displays a message saying it cannot be selected, because it’s the opponent’s piece).
.Name = "piece" & cell.Address(False, False)
.Line.Visible = msoFalse
.Fill.ForeColor.RGB = RGB(0, 0, 255) 'blue
.OnAction = "SelectPiecePlayer1"
Else
.Fill.ForeColor.RGB = RGB(255, 0, 0) 'red
.OnAction = "NoSelect"
End If
End With
In addition to shapes, each
piece cell is given the value of 1 or 2 for player or opponent (computer)
respectively. This is used in the macros that check options (only player) and
move pieces (player and computer) – see later.
Selecting a piece
Clicking on a blue piece triggers a macro that selects/deselects that piece in Excel Checkers. The variable “PieceSelected” is used to store the range/cell object that has been selected (it is declared as pubic because is used across various modules). A separate function gets the cell where the shape is located. When selected, the line of the shape is made visible (white color).
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 255, 255) 'white
.Weight = 1
End With
Another macro checks where
that piece can move and highlights those cells adding an empty oval shape in
this version of Checkers in Excel. That routine checks whether the target cells
are empty using the property Offset. It also checks for empty cells where a
jump is possible.
The empty shapes (with white
line) are assigned a macro that will trigger the move of the piece from point A
to B in Excel Checkers (see next). They are also given a specific name that can
be recognized later to delete those empty shapes when deselecting a piece (the
name includes the word “option”).
Moving a piece
Click on an option shape triggers the macro that initiates the move of a piece. The moving macro accepts the initial and final locations – these are both cell objects (cell1 and cell2). The initial cell is taken from the global variable “PieceSelected”. A separate function gets the shape object in that cell. The destination cell is taken from the name of the option shape. Finally, the shape is moved to the destination as indicated below.
With shp
.Line.Visible = msoFalse
.Left = cell2.Left + 4
.Top = cell2.Top + 3
End With
Afterwards, there are a
number of checks that trigger various macros. See below a summary of the next
steps:
1.
Check if
the piece has reached the upper row in the checkers board (row 2 in this
example of Checkers in Excel) to make it king
2.
Check if
the move was a jump – in such case, delete the shape (see EatPiece procedure)
3.
Check if a
player has no pieces left to end the game (the game also ends if one of the
players cannot make a move)
Computer’s move
The computer’s move
triggers the same macros to move a piece explained above. All those macros are
in the module “M2_Pieces” in the Excel Checkers Game file available for
download below. But the computer follows an algorithm to decide which piece to
choose and where to move it. Here’s a summary of the main components of that
algorithm:
1.
Check if
it can jump over a piece (to eat it)
2.
Check if
it can reach the last row to become king
3.
Make a
normal move that does not leave a space behind
4.
Move a
king piece to chase the opponent
5.
Make any
move
Each of those steps have a
series of conditional statements along the property Offset to determined where
to make the next move. In this version of Checkers in Excel, that includes
checking whether a certain cell is free, or if there are any threads when
moving to a particular cell. If the computer cannot make any move, you win the
game.
No comments:
Post a Comment