Of kings, knights, and musicals...

BlitzPlus Forums/BlitzPlus Programming/Of kings, knights, and musicals...

Tomas Khan(Posted 2008) [#1]
(Actually, this isn't about the musical Chess; it only came to mind because members of ABBA wrote the music.)

I've just designed graphics for a chess set, and naturally I want to make a chess game with them. This would be simple enough if the pieces could jump and capture other pieces of either color, but as only the Knight can do the former and no piece can do the latter, I need help.

I'm considering using a group of tiles, each with a separate x and y as well as "space x" and "space y" to indicate its position on the board.

I think I know how I can program in most of the movements -- even the Knight's. However, Pawn capturing movements are unusual, especially the en passant capture. Question 1: How could you program in Pawn capture moves? (For the normal capture, how do you determine whether a "man" (a piece or Pawn) is in that space?)

Question 2: How do you program in check? Either the King must be moved, or something must be moved to block the attack, but how can you force the player to do so?

Question 3: How do you prevent jumping in non-Knights? In other words, if there is another piece in the way, how do you prevent the piece from being moved past the other piece?

Also, about a couple of the rules that might make this more or less playable:
Gameplay Question 1: When a Pawn reaches the end of the board, does it always become a Queen (as I've always played it), or is the player entitled to choose what to make it (as I've heard it played)?

Gameplay Question 2: Should a Pawn be able to move two spaces on its first turn, and should Pawns be able to capture en passant?
These seem like they might be a little complicated to code, but they're probably worth it.

I guess that's probably enough to start. Sorry if I'm being a little too long-winded.

Thank you!
Tomas Khan


Sauer(Posted 2008) [#2]
As for the chess questions, you can choose any piece in place of the queen, but this is rare because the queen is the best piece, and having the pawn move double on first turn and en passant capture is crucial to game strategy so it should be implemented.

As for the rest of the stuff you're dealing with paths of the piece and checking what pieces are in the way. I would think the best way to do this would be an array, then create a list of x and y 'coordinates' per space per move then systematically check to see if each space is occupied, then follow a procedure according to what is there.

I imagine programming an effective chess AI with multiple strategies and the ability to adapt would be quite difficult, hope your up to the challenge. As far as the programming part though, that's what I would do.


Tomas Khan(Posted 2008) [#3]
Well, my immediate goal is to get a two-player game going. We don't know what we've done with our physical chessboards, and so this could be the next best thing. I knew it would be hard to make an AI, and in any case I wouldn't be making Deep Blue, but right now programming it to play against me is a secondary goal, though one I would have to accomplish if I ever released it.

I imagine that to let the player choose, you would add in a loop where you would show the possible pieces somewhere on the screen, preventing any interference or selection of other pieces?

I have an idea for moving double on the first move, but I'm not so sure with the en passant. How would you ensure that en passant would only be allowed if the enemy Pawn had just passed over the attacked square?

Meanwhile, about the paths... the King, Knight and Pawn will do fine, as their paths can't be blocked (well, except the double-moving Pawn, but I think I know how I could solve that). The Bishop, Rook, and Queen, however, could be a little harder.

Oh, and I forgot castling. Hmm.

Thank you!


David Boudreau(Posted 2008) [#4]
Yeah king paths can be blocked for castling, and line-of-check (can't legally castle through nor move into check). Standard computer chess UI for castling requires the player to move the king first; your program accepts or not the legal 2-space king move, i.e. accepting both the conditions of path (not done through a line of check), the castling rook and king have not moved before, and that there are no other pieces in the line of castling (in between the two pieces).

Pawns must be promoted on their 8th rank to any piece besides a king or another pawn.

Recognize draws would be good too: stalemates, and king vs. any of the following: king and bishop, king and knight. king and 2 bishops has a limit like 60 moves, same for king with knight and bishop. usually though the "repeat" rule comes into effect before then: can't have same position for 3 straight moves in a row, that is automatic condition for a draw as well.

Definitely go with an array(8*8) at the very least, and approach the structure conceptually (and be able to read ahead to the next move to determine each potentially legal move to prevent illegal moves) chess seems like a big huge battle during play but the field is not that big. It's relatively easy to program as it is turn-based, and each of the 64 squares is kind of like a "pixel" you'd want to"collision detect" in a faster-paced game.


David Boudreau(Posted 2008) [#5]
Question 1: How could you program in Pawn capture moves?

Various ways- this will be the fun of making the program-- and don't worry so much about whatever method you start with, because in a turn-based game like chess you could have it be very inefficient and still perfectly playable, compared to a fast-paced FPS action game.

Question 2: How do you program in check? Either the King must be moved, or something must be moved to block the attack, but how can you force the player to do so?

Is the king in any opposing piece's attacking squares? then he's in check. don't force the player to do anything except "not make illegal moves". You simply allow only moves that are legal. Just think it through logically.

When I first learned chess notation, I used the traditional method which was much worse than a much simpler, better notation: just write Square coordinateFrom, a dash, then SquareCoordinateTo. No need to even record what piece it was. Keep that in mind. It doesn't matter so much what kind of piece takes what kind of piece. Any piece from a given square has a finite number of possible squares for its next move.

oh and if you add any extra features, I highly encourage you to add a review type of branching system so players can review their games and go off in different nodes, then return to the main branchline-- that really needs to be encouraged more in chess players, reviewing games. Don't invest your time in a thinking game like chess only to forget or ignore the critical moves right after.


Question 3: How do you prevent jumping in non-Knights? In other words, if there is another piece in the way, how do you prevent the piece from being moved past the other piece?

It might be a function called AnotherPieceInTheWay() that returns true or false and called at the top of an if statement:

If AnotherPieceInTheWay() = True Then PreventMove = True
; prevent illegal move

and in the function, check each square in the array between move-from square and move-to square.

Gameplay Question 2: Should a Pawn be able to move two spaces on its first turn, and should Pawns be able to capture en passant?

Yes, and yes.

En Passant is not so tough to program in and check for. The point of the rule is to prevent passed pawns that got to be passed pawns only because they took advantage of the two-space move. So the rule is there to say "hold it, he just moved that pawn two spaces-- if he had moved it at the normal one-square speed per move, I would be able to capture it normally with my pawn on the 5th rank" so the ruleset allows the capture. (So in the position right before en passant could happen, it doesn't matter whether the pawn moves 1 or 2 spaces, it can still be captured from the opposing pawn on the 5th rank in either case, so just treat it the same in your code.)

It only comes into play when a pawn moves two spaces from his second rank (From white's perspective, if Black loses a pawn to this rule it always means it was an attempt to move it from the 7th rank to 5th rank).


Tomas Khan(Posted 2008) [#6]
All of the pieces are now moving -- even the Pawn's double first move! (Only they don't interact with each other yet. I'll be getting to that later.)


Ginger Tea(Posted 2008) [#7]
with regards to the game play question 2:
if you deleted the (musical) part from wiki you would have had tonnes of info on the game itself and im assuming you have read that page at some point

ommiting a set of rules from chess, even under the pretence of "i dont know how to code it" breaks the game for those that want to play chess with out the hassle of setting it up or finding another player (assuming your AI develops)
if they got the full rules of chess running on a spectrum 48 and perhaps a zx81 then it had damn well better work on a circa 2008/09 machine ;)

chess variants do exist but most follow the movement/caputure rules they just might lay them out differently

also i wanted to learn shogi (japanese chess) but with no idea on the player glyphs(?) i hadnt a clue which piece was which, but if i took the time to read and understand the rules, i could (when i used to have time) work out the game code and play from there (or cheat and play someone elses windows version)

the same (or perhaps modified) 8*8 array couild also be used in checkers othello and with a tweak go


Tomas Khan(Posted 2009) [#8]
Sorry for not responding sooner; it's just that I haven't been making any progress on this program. Perhaps checkers might be a useful jumping-off point; it has no particularly strange rules, and its mildly strange rules -- jumping over enemies and promotion -- probably wouldn't be that difficult to handle, certainly in comparison to en passant and castling.

In any case, I don't think I'll be making any progress on it for some time. Thank you, and any suggestions will be helpful when I come back to it, but as I said, I don't think I might be doing much on chess for a while. Maybe we should just buy another chessboard...


Ginger Tea(Posted 2009) [#9]
im sure coding chess is way easier than mastering playing chess
chess AI however ;)

the board is just a 2d array and would have different internal coordinates than you would be used to in regular chess notations
and as such piece moving would be written differently, but in a way just the same as regular notations

rooks would be in grid coordinates 0,0 0,7 7,0 7,7 assuming that the top left is 0,0 and bottom right is 7,7 and pawns spread out across 1,0 to 1,7 and 6,0 to 6,7
thats easier for me to find than kings bishop 5 all these other bits real chess players under stand

aslong as the pieces adhear to their real counterparts when moving its all a case of making sure that if the queen wants to move she has a clear line of sight between start and finish and cannot pass through a fellow team mate to get to the piece she is capturing


Tomas Khan(Posted 2009) [#10]
You know something, I think you just gave me an idea...

rooks would be in grid coordinates 0,0 0,7 7,0 7,7 assuming that the top left is 0,0 and bottom right is 7,7 and pawns spread out across 1,0 to 1,7 and 6,0 to 6,7

That's basically what I'm doing, only with types, not an actual array. I don't know if an array would be easier, but anyway. Combine that with what David Boudreau said earlier --
check each square in the array between move-from square and move-to square

And I realize that maybe I could use a piece's location and a tile's location and check tiles in between!

I still don't think I'll have time to code it for a while, but maybe when I do, it'll work better. Thank you!


Ross C(Posted 2009) [#11]
I think your best using an array:

Each piece has a number: 1 = white pawn, 2 = white knight... etc 9 = black pawn 10 = black knight.

Just fill your array with the appropriate numbers to indicate there starting positions. What i did for movements was something like another array, which held all the possible squares a piece can move too. There are many ways to do the piece moving thing, but the foundation should be the array, where you can check any square to see what piece occupies a square.


Tomas Khan(Posted 2009) [#12]
I think I might have some idea of where to go with this, but it would probably involve arrays, which I don't use much -- in fact, mainly, if not only, for rotations. I'm much more familiar with types, and I've been trying to use them for the tiles, but an array would probably simplify things.

First of all, can types and arrays be merged; if so, how; and is that a useful way to implement a group of tiles with characteristics of both?

Second, how do you store multiple related items (space x, space y, and whether a piece can be moved there) in an array? Do you put in a third dimension?

Third, when I create tiles and such, I generally use a For... Next loop (e.g., "For a = 1 To 8 [...] Next"). But can a For... Next loop be run backwards? (e.g., For b = (piece\spx - 1) To 0) If so, I think I would be able to Exit if there was a piece in the way.

Thank you!