PDA

View Full Version : VBA chess game, userform



Atesz
12-28-2013, 04:32 AM
Hello everyone,
We are working on a chess game in VBA as a homework. The basic idea to make it is to create a userform, and every field (a1, a2 etc.) of the chessboard will be a commandbutton and the figures too. If a figure is on a field, their position are the same. And that is what I can't find with code. I would like to make firstly to find the position for the figure, that is why I look for the field.

Private Sub bparaszt7_Click()
For i = 1 To 8
For j = 1 To 8
if "tomb(i)"&"j".top = bparaszt7.Top then
MsgBox tomb(i) & j
end if
next
next
End Sub
This is for a pawn (called paraszt, and it is the 7th black). But it is only good for that one, I don't know how to replace its name to the actual, to make it universal. Tomb() is filled with the characters, a, b, c, d, e, f, g, h. I'd like the button for the click to go through all the other field commandbuttons, named a1, a2, etc., and when the top and left(it is not included in the code yet) are the same with the pawn's position, it should write its position.
I hope you understand it.

mikerickson
12-28-2013, 07:22 AM
This is a perfect situtation to use custom objects by using class modules.
I would have two classes, clsPiece and clsSquare.

clsPiece would have properties like PostitionColumn, PositionRow, Type (pawn or knight..), Alive. And a method like MoveTo(pRow, pColumn).
Each clsPiece would also have a myCommandButton property.

Bob Phillips
12-28-2013, 08:10 AM
Also, why would you need to find its position, you know the start position of every piece, and you should be tracking every move, so that you know where it moves to.

How will the pieces be moved?

Atesz
12-28-2013, 08:56 AM
My plan is, when a player clicks on any figure, it then checks the free way, and highlights where it can move to. I used the caption of the field commandbuttons to mark is there anything or not, by setting 0 to empty, 1 to white 2 to black. The caption is invisible. So when it located its position, it can check the other fields as well, can it move or not. But I don't know how I can replace the name of commandbuttons with variables to solve it.

Moves are easy, when the way is highlighted, just change the position to the clicked field. Moves are tighted, they can only move 54 to left, right, up, and down.

mikerickson
12-28-2013, 09:51 AM
An invisible caption sounds like it would be hard to tell a Knight from a Bishop. Have you looked at the .Tag property? Delimited strings are a great way to store data.

Atesz
12-28-2013, 09:56 AM
The essence is not the caption or .tag, but thanks for the idea, I will try it :) I would like to know how can I change the commandbutton name with variables to locate them :/

Bob Phillips
12-28-2013, 10:50 AM
Generally I am confused by how you are trying to do it, although I tend to think that you are over-complicating it, but ...


But I don't know how I can replace the name of commandbuttons with variables to solve it.

you can use something like


If Me.Controls("button_name").Caption = 1 Then
'etc.
End If

Atesz
12-28-2013, 01:08 PM
Firstly I'd like to locate the button, I'd like to search which field button has the same place as the figure button. And in the first post I wrote the way I would like to do it, but VBA can't understand it. For example: I'd like to check every .top and .left property, and compare it with the clicked button, and if the 2 are the same, i have the position. Example: go through a1, a2, a3, a4 etc to h8, but i can't replace the charachters with variables to make it easier, like tomb(i) would be a, b, c, d, e, f, g or h, and a for i = 1 to 8 to the numbers. Is it possible to do?

Bob Phillips
12-28-2013, 02:23 PM
AS I said, you shouldn't need to locate it if you just track it as it moves. Other than that I have no insights to offer as I do not understand enough of what you are tryting to do.

Atesz
12-28-2013, 02:32 PM
But I have to locate it, to make its possible ways. After clicking I want the button to highlight the field where it can move to.

sassora
12-28-2013, 04:33 PM
I think the point is that if you have the initial location of each piece then you can update these each time a piece is moved. Positions for each piece would be known and there's no need to search for individual pieces.

Of course you could make life harder for no apparent reason!

Paul_Hossler
12-28-2013, 09:58 PM
Firstly I'd like to locate the button, I'd like to search which field button has the same place as the figure button. And in the first post I wrote the way I would like to do it, but VBA can't understand it. For example: I'd like to check every .top and .left property, and compare it with the clicked button, and if the 2 are the same, i have the position. Example: go through a1, a2, a3, a4 etc to h8, but i can't replace the charachters with variables to make it easier, like tomb(i) would be a, b, c, d, e, f, g or h, and a for i = 1 to 8 to the numbers. Is it possible to do?

You could create a 8x8 grid of command buttons on the UF to represent the board, and on top of the top 2 and bottom 2 rows add 32 additional command buttons to represent the pieces

Click a piece CB could execute to code to change the background color on possible moves.

Clicking one of the changed pieces would move the piece CB to the coordinates of the clicked available square

You can create the 8x8 with code, as well as the pieces. Set the board's 64 to light or dark, and the 32 pieces to black or white with a "K" or "Q" etc.

Did you look at the UI for MS's Chess Titans game? You might get some ideas

BTW, I hope you're not planning to play the computer -- That would be a challenge

Paul

Atesz
12-29-2013, 08:11 AM
I created the whole board with the pieces, and now I see the point that I don't have to locate it, because I know the basic place of them.

I used the Tag property to store there position. I filled a 2 dimensional array with the positions, a1, a2, a3 etc to h8.
I would like to make the clicked button to highlight its possible way, and to make the future easier, I'd like to replace the field names(a1, a2 etc) to my array.

Private Sub wparaszt1_Click()
If wparaszt1.Tag = betuk(1, 2) Then
betuk(1, 3).BackColor = RGB(100, 100, 100)
betuk(1, 4).BackColor = RGB(100, 100, 100)
Else
betuk(1, 3).BackColor = RGB(100, 100, 100)
End If
But in this way it doesn't work. How should I do it?
I defined the array as string

Dim betuk(8, 8) As String

Bob Phillips
12-29-2013, 10:49 AM
Glad you now understand that basic principle. When you say it doesn't work, in what way? AN error? The cells do not colour?> Or what?

Why don't you post the workbook, we have worked in the dark for too long.

Atesz
12-29-2013, 12:25 PM
True
It says
compile error:
invalid qualifier

And it highlihts "betuk" in the code. This is the name of my array.

Paul_Hossler
12-29-2013, 01:39 PM
Not much to work with, but as a guess ...




If wparaszt1.Tag = betuk(1, 2).Tag Then


Paul

Stabilo_Boss
02-11-2017, 03:46 PM
Check out 2 player hotseat Excel VBA Chess v1.0:

excelvbachess.wikidot.com

Features:

- 8x8 board with piece images moved by VB.
- Movement system based on colour: yellow for currently selected piece, green for possible moves, orange for impossible moves, red for King in check.
- Check and Checkmate system.
- Castling.
- En passant.
- Pawn promotion.
- The absolute pin.

SamT
02-11-2017, 05:13 PM
Where were you? Three years ago?

Paul_Hossler
02-12-2017, 07:33 AM
Check out 2 player hotseat Excel VBA Chess v1.0:

excelvbachess.wikidot.com

Features:

- 8x8 board with piece images moved by VB.
- Movement system based on colour: yellow for currently selected piece, green for possible moves, orange for impossible moves, red for King in check.
- Check and Checkmate system.
- Castling.
- En passant.
- Pawn promotion.
- The absolute pin.

Thanks for the input as your first post here, but after more than three years, it's probably not an issue anymore

It's usually a good practice to not reply to old posts (like this one) since it pushes it to the top, or the problem has long gone away / been forgotten



Similarly, it's not good practice to use an old post to address a new problem (even if similar) by just tagging on a new question. Posters get more attention / interest by starting their own with a descriptive title (e.g. not 'HELP!!!!' or 'VBA Problem')

Stabilo_Boss
02-13-2017, 12:43 PM
Better late than never?!

Point taken though should probably have been posted elsewhere...