?
Solved

about chess

Posted on 2004-08-21
6
Medium Priority
?
576 Views
Last Modified: 2010-04-07
hi. i am trying chess in visual basic 6.0 can you please provide me some helpful links in this regards, i mean some tutorials, algorithms, tips etc. to make my work little easier.
Shall be highly thankful.
Thanks.
0
Comment
Question by:astudent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:Venabili
ID: 11858471
OK....

A six-part series about programming computers to play chess. Every part have subparts so try not to miss them:)
http://www.gamedev.net/reference/programming/features/chess1/ - Getting started
http://www.gamedev.net/reference/programming/features/chess2/ - Data Structures
http://www.gamedev.net/reference/programming/features/chess3/ - MOve Generation
http://www.gamedev.net/reference/programming/features/chess4/ - Basic Search
http://www.gamedev.net/reference/programming/features/chess5/ - Advanced Search
http://www.gamedev.net/reference/programming/features/chess6/ - Evaluation Functions

http://www.chessbrain.net/beowulf/theory.html - Chess Programming Theory
http://www.aihorizon.com/essays/chessai/intro_print.htm - Introduction to Chess Programming and AI
http://chess.verhelst.org/ - A good site for Chess Programming
http://www.xs4all.nl/~verhelst/chess/programming.html  - A large collection of links
http://www.cs.biu.ac.il/~davoudo/tutorials.html - Chess Programming Tutorials - links to a lot of such in one place
http://www.seanet.com/~brucemo/topics/topics.htm - Good collection of links on the topic

Venabili
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11858479
There is a VB Chess Programming group in MSN:
http://groups.msn.com/VBChessProgramming/
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11859340
Venabili,

Did you look at the MSN group?  There is nothing but months and months worth of pornography spam in it.



astudent,

What is your goal with the program?

Single Player?  Two Players?

Computer Controlled Players?

Play across a Network? the Internet?

Load/Save games in progress?

Drag and Drop pieces with mouse?  Enter moves via Keyboard.

etc...

Where are you at currently in your project?

Idle_Mind
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Venabili
ID: 11867790
Idle_Mind,

Yep I did. It used to be a great place a while ago and anyway the members can still be reached.
0
 

Author Comment

by:astudent
ID: 11878714
Hi  Idle_Mind !! thank for showing concern.
as i am still in learning stage so i am working on two players (both humans), stand alone machine (No network, or Internet support initially), picking up the pieces with mouse (double click rather than drag and drop) and with keyboard arrow keys.
can you please provide me some hints or tips.
thanks.

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 11985554
astudent,

Below is a down and dirty framework for a Chess game (I only spent about 2 hours on it tops).  The project consists of the default Form and a Class (Select Project --> Add Class Module).  Place on the form one picturebox and a one label.  They will both be adjusted and moved by the code at runtime.

A piece is selected by single clicking on it, which turns its border yellow.  Clicking it again will unselect it.  When a piece is selected, you can move the mouse around the board to see if the piece can move there.  The tile border will turn green if it is a valid move, otherwise it will turn red.

I only programmed in the logic for the Pawns.  The rest is left up to you.

Regards,

Idle_Mind

' --------------------------------------------------------
' Class1
' --------------------------------------------------------
Option Explicit

Public pieceName As String
Public pieceColor As String
Public pieceX As Single
Public pieceY As Single

Public Function canMoveTo(ByVal xGrid As Byte, ByVal yGrid As Byte, ByRef board As Variant) As Boolean
    canMoveTo = False
    Select Case pieceName
        Case "Pawn"
            Select Case pieceColor
                Case "Black"
                    If yGrid = pieceY + 1 Then
                        If (xGrid = pieceX) And (board(xGrid, yGrid) Is Nothing) Then
                            canMoveTo = True
                        ElseIf (xGrid = pieceX - 1) Or (xGrid = pieceX + 1) Then
                            If Not (board(xGrid, yGrid) Is Nothing) Then
                                If board(xGrid, yGrid).pieceColor = "White" Then
                                    canMoveTo = True
                                End If
                            End If
                        End If
                    ElseIf (xGrid = pieceX) And (yGrid = pieceY + 2) Then
                        If pieceY = 2 Then
                            If (board(xGrid, pieceY + 1) Is Nothing) And (board(xGrid, pieceY + 2) Is Nothing) Then
                                canMoveTo = True
                            End If
                        End If
                    End If
                   
                Case "White"
                    If yGrid = pieceY - 1 Then
                        If (xGrid = pieceX) And (board(xGrid, yGrid) Is Nothing) Then
                            canMoveTo = True
                        ElseIf (xGrid = pieceX - 1) Or (xGrid = pieceX + 1) Then
                            If Not (board(xGrid, yGrid) Is Nothing) Then
                                If board(xGrid, yGrid).pieceColor = "Black" Then
                                    canMoveTo = True
                                End If
                            End If
                        End If
                    ElseIf (xGrid = pieceX) And (yGrid = pieceY - 2) Then
                        If pieceY = 7 Then
                            If (board(xGrid, pieceY - 1) Is Nothing) And (board(xGrid, pieceY - 2) Is Nothing) Then
                                canMoveTo = True
                            End If
                        End If
                    End If
                   
            End Select
           
        Case "Rook"
            Select Case pieceColor
                Case "Black"
               
                Case "White"
               
            End Select
           
        Case "Knight"
            Select Case pieceColor
                Case "Black"
               
                Case "White"
               
            End Select
           
        Case "Bishop"
            Select Case pieceColor
                Case "Black"
               
                Case "White"
               
            End Select
           
        Case "Queen"
            Select Case pieceColor
                Case "Black"
               
                Case "White"
               
            End Select
           
        Case "King"
            Select Case pieceColor
                Case "Black"
               
                Case "White"
               
            End Select
           
    End Select
End Function


' --------------------------------------------------------
' Form1
' --------------------------------------------------------
Option Base 1
Option Explicit

Private pieces(8, 8) As Class1
Private curPiece As Class1
Private curPlayer As String

Private Sub Form_Load()
    Call newGame
    curPlayer = "White"
    Label1.Move 1440 / 4, 1440 / 12, 1440 * 2, 1440 * 0.5
    Label1.Caption = curPlayer & "s Turn"
    Picture1.Appearance = 0 ' flat
    Picture1.BorderStyle = 0 ' none
    Me.ScaleMode = vbTwips
    Picture1.ScaleMode = vbTwips
    Me.Width = 1440 * 5
    Me.Height = 1440 * 5
    Picture1.Move 1440 / 4, 1440 / 4, 1440 * 4, 1440 * 4
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim xStep As Single
    Dim yStep As Single
    Dim xGrid As Single
    Dim yGrid As Single
    Dim KingTaken As Boolean
    xStep = Picture1.Width / 8
    yStep = Picture1.Height / 8
    xGrid = (X \ xStep) + 1 ' Integer Division
    yGrid = (Y \ yStep) + 1 ' Integer Division
    If xGrid > 8 Or yGrid > 8 Then
        Exit Sub
    End If
       
    If curPiece Is Nothing Then
        ' no piece is selected so select piece cursor is over
        If Not (pieces(xGrid, yGrid) Is Nothing) Then
            If pieces(xGrid, yGrid).pieceColor = curPlayer Then
                Set curPiece = pieces(xGrid, yGrid)
                Picture1_Paint
            End If
        End If
    Else
        If curPiece Is pieces(xGrid, yGrid) Then
            ' piece selected again so toggle it off
            Set curPiece = Nothing
            Picture1_Paint
        Else
            If curPiece.canMoveTo(xGrid, yGrid, pieces) Then
                If Not (pieces(xGrid, yGrid) Is Nothing) Then
                    If pieces(xGrid, yGrid).pieceName = "King" Then
                        KingTaken = True
                    Else
                        KingTaken = False
                    End If
                End If
                ' remove curPiece from where it is currently
                Set pieces(curPiece.pieceX, curPiece.pieceY) = Nothing
               
                ' move curPiece to new location
                curPiece.pieceX = xGrid
                curPiece.pieceY = yGrid
               
                ' set new location to curPiece
                Set pieces(xGrid, yGrid) = curPiece
               
                ' unselect curPiece
                Set curPiece = Nothing

                ' redraw the board
                Picture1_Paint
               
                If KingTaken Then
                    MsgBox curPlayer & " Wins!", vbInformation, "Game Over"
                Else
                    ' swap players
                    If curPlayer = "White" Then
                        curPlayer = "Black"
                    Else
                        curPlayer = "White"
                    End If
                    Label1.Caption = curPlayer & "s Turn"
                End If
            End If
        End If
    End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim xStep As Single
    Dim yStep As Single
    Dim xGrid As Single
    Dim yGrid As Single
    Static xGridLast As Single
    Static yGridLast As Single
    Dim inverseColor As Long
    Dim overpiece As Class1
   
    inverseColor = QBColor(8)
    xStep = Picture1.Width / 8
    yStep = Picture1.Height / 8
    xGrid = (X \ xStep) + 1 ' Integer Division
    yGrid = (Y \ yStep) + 1 ' Integer Division
    If xGrid > 8 Or yGrid > 8 Then
        Exit Sub
    End If
    Set overpiece = pieces(xGrid, yGrid)
   
    If xGrid <> xGridLast Or yGrid <> yGridLast Then
        Picture1_Paint
        If Not (overpiece Is curPiece) Then
            If Not (curPiece Is Nothing) Then
                If curPiece.canMoveTo(xGrid, yGrid, pieces) Then
                    Picture1.Line ((xGrid - 1) * xStep, (yGrid - 1) * yStep)-(xGrid * xStep, yGrid * yStep), vbGreen, B
                Else
                    Picture1.Line ((xGrid - 1) * xStep, (yGrid - 1) * yStep)-(xGrid * xStep, yGrid * yStep), vbRed, B
                End If
            End If
        End If
        xGridLast = xGrid
        yGridLast = yGrid
    End If
End Sub

Private Sub Picture1_Paint()
    Dim X As Single
    Dim Y As Single
    Dim xStep As Single
    Dim yStep As Single
    Dim boardColor As Long
    Dim lightColor As Long
    Dim darkColor As Long
    Dim piece As Class1
    Dim pieceWidth As Single
    Dim pieceHeight As Single
    Dim pieceColor As Long
   
    xStep = Picture1.Width / 8
    yStep = Picture1.Height / 8
    lightColor = QBColor(7)
    darkColor = QBColor(8)
   
    For X = 1 To 8
        For Y = 1 To 8
       
            If ((X - 1) Mod 2) = 0 Then ' even column
                If ((Y - 1) Mod 2) = 0 Then ' even row
                    boardColor = lightColor
                Else ' odd row
                    boardColor = darkColor
                End If
            Else ' odd column
                If ((Y - 1) Mod 2) = 0 Then ' even row
                    boardColor = darkColor
                Else ' odd row
                    boardColor = lightColor
                End If
            End If
   
            Picture1.Line ((X - 1) * xStep, (Y - 1) * yStep)-(X * xStep, Y * yStep), boardColor, BF
           
            If Not (pieces(X, Y) Is Nothing) Then
                Set piece = pieces(X, Y)
                Select Case piece.pieceColor
                    Case "White"
                        pieceColor = QBColor(15)
                    Case "Black"
                        pieceColor = QBColor(0)
                End Select
                Picture1.ForeColor = pieceColor
                pieceWidth = Picture1.TextWidth(piece.pieceName)
                pieceHeight = Picture1.TextHeight(piece.pieceName)
                Picture1.CurrentX = ((piece.pieceX - 1) * xStep) + (xStep / 2) - (pieceWidth / 2)
                Picture1.CurrentY = ((piece.pieceY - 1) * yStep) + (yStep / 2) - (pieceHeight / 2)
                Picture1.Print piece.pieceName
            End If
        Next Y
    Next X
    If Not (curPiece Is Nothing) Then
        Picture1.Line ((curPiece.pieceX - 1) * xStep, (curPiece.pieceY - 1) * yStep)-(curPiece.pieceX * xStep, curPiece.pieceY * yStep), vbYellow, B
    End If
End Sub

Private Sub newGame()
    Dim piece As Class1
    Dim X As Integer
    Dim Y As Integer

    Set curPiece = Nothing

    For X = 1 To 8
        For Y = 1 To 8
            Set pieces(X, Y) = Nothing
        Next Y
    Next X

    ' Pawns
    For X = 1 To 8
        Set piece = New Class1
        piece.pieceColor = "Black"
        piece.pieceName = "Pawn"
        piece.pieceX = X
        piece.pieceY = 2
        Set pieces(piece.pieceX, piece.pieceY) = piece
       
        Set piece = New Class1
        piece.pieceColor = "White"
        piece.pieceName = "Pawn"
        piece.pieceX = X
        piece.pieceY = 7
        Set pieces(piece.pieceX, piece.pieceY) = piece
    Next X
   
    ' Black
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Rook"
    piece.pieceX = 1
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Knight"
    piece.pieceX = 2
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Bishop"
    piece.pieceX = 3
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Queen"
    piece.pieceX = 4
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "King"
    piece.pieceX = 5
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Bishop"
    piece.pieceX = 6
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Knight"
    piece.pieceX = 7
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "Black"
    piece.pieceName = "Rook"
    piece.pieceX = 8
    piece.pieceY = 1
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    ' White
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Rook"
    piece.pieceX = 1
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Knight"
    piece.pieceX = 2
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Bishop"
    piece.pieceX = 3
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Queen"
    piece.pieceX = 4
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "King"
    piece.pieceX = 5
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Bishop"
    piece.pieceX = 6
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Knight"
    piece.pieceX = 7
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
   
    Set piece = New Class1
    piece.pieceColor = "White"
    piece.pieceName = "Rook"
    piece.pieceX = 8
    piece.pieceY = 8
    Set pieces(piece.pieceX, piece.pieceY) = piece
End Sub
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question