Chess program finished!
My second VB.NET program was to play the game of Loser Chess. The object is to lose one’s pieces before your opponent does! I never considered attempting to develop a program to play proper chess as it would have been impossible to get the computer to make sensible moves. So instead, I decided to concentrate on the mechanism of playing chess and to use it to play a simplified version. ie Loser Chess.
Now I have to think of another project.
A code snippet
Public Sub PossibleMoves(sq As Integer)
'
' Determine all possible moves for the piece on the specified square
'
Select Case Piece(sq)
Case ChessPiece.pawn
Call PossibleMoves_pawn(sq)
Case ChessPiece.rook
Call PossibleMoves_horizontally(sq)
Call PossibleMoves_vertically(sq)
Case ChessPiece.knight
Call PossibleMoves_knight(sq)
Case ChessPiece.bishop
Call PossibleMoves_diagonally(sq)
Case ChessPiece.queen, ChessPiece.king
Call PossibleMoves_horizontally(sq)
Call PossibleMoves_vertically(sq)
Call PossibleMoves_diagonally(sq)
Case ChessPiece.none
MsgBox("OOPS PossibleMoves")
End Select
End Sub
Private Sub PossibleMoves_pawn(sq As Integer)
'
'Let's see where this pawn can move to...
'
Dim incr As Integer
incr = IIf(PieceOwner(sq) = ChessColour.white, 1, -1) ' Going up for white, down for black
' Move 1 or 2 squares forward?
If TryThisSquare(sq, incr, 0) = SquareContent.empty Then
Call AddToMoves(sq, incr, 0)
If (incr = 1 And RowNumber(sq) = 2) Or (incr = -1 And RowNumber(sq) = 7) Then 'Can move 2 spaces on first pawn rank
If TryThisSquare(sq, 2 * incr, 0) = SquareContent.empty Then Call AddToMoves(sq, 2 * incr, 0)
End If
End If
' Take enemy piece diagonally left or right?
If TryThisSquare(sq, incr, -1) = SquareContent.enemypiece Then Call AddToMoves(sq, incr, -1)
If TryThisSquare(sq, incr, 1) = SquareContent.enemypiece Then Call AddToMoves(sq, incr, 1)
End Sub






Have your say! (No need for any ID!)